All pastes #136595 Raw Edit

Something

public text v1 · immutable
#136595 ·published 2006-08-17 19:55 UTC
rendered paste body
<?php
define ('ROOT',"./");
define ('PUN_DEBUG',"1");
define ('SCREENSHOTS_FOLDER', "./sshots/");
require ROOT."functions.php";
include ROOT."include/common.php";
$version = "1.0";

$xml_file_location = "t_info.xml";
$tm_config["debug"]=false;
$tm_config['local_screenshots']=false;

//downloads file with http if remote location
if (substr($xml_file_location, 0, 4)=='http')
{
   get_file1($xml_file_location,"./","t_info.zip");
   $xml_file_location="./t_info.zip";
}
//unzip file if is zip
if (substr($xml_file_location, -3)=='zip')
{
   $xml_file_location= extract_here($xml_file_location);
}

//makes a SimpleXML object with xml string
if (file_exists($xml_file_location)) {
//   $xml = simplexml_load_file($xmlfile);
   $tmp = file_get_contents($xml_file_location);
   $tmp = str_replace('encoding="utf-8"', 'encoding="ISO-8859-15"', $tmp);
   $xml = new SimpleXMLElement($tmp);
} else {
   exit('Failure openning the file : '.$xml_file_location.'.');
}

//update categories
update_cats();
//update authors
update_authors();
//update styles
update_styles();

//makes an array with existent templates
$tms = array();
$res = $db->query("SELECT id_tm FROM tm_index LIMIT 0,20000");
while ($data = $db->fetch_assoc($res))
   $tms[] = $data['id_tm'];

//makes an array with existant keywords
$kws = array();
$res = $db->query("SELECT id_keyword, label_en FROM tm_keywords_index LIMIT 0,20000");
while ($data = $db->fetch_assoc($res))
   $kws[$data['label_en']] = $data['id_keyword'];


//loops on one template's xml data
foreach ($xml->template as $tm)
{

   //if templates doesn't exist in the database
   if (!in_array($tm->id, $tms)) //insert it
      $action = "INSERT INTO";
   else //update it
   {
      $action = "UPDATE";
      $condition = "WHERE `id_tm` = '".$tm->id."'";
   }
      
   //template insert sql query string
   $insert_tm = $action." `tm_index` SET 
   `id_tm`='".$tm->id."',
   `state`='".$tm->state."',
   `is_hosting`='".$tm->is_hosting."',
   `is_flash`='".$tm->is_flash."',
   `is_unique`='".$tm->is_unique."',
   `is_non_unique`='".$tm->is_non_unique."',
   `is_unique_corporate`='".$tm->is_unique_corporate."',
   `is_full_site`='".$tm->is_full_site."',
   `is_real_size`='".$tm->is_real_size."',
   `id_author`='".$tm->author->author_id."',
   `id_type`='".$tm->template_type->type_id."',
   `price`='".$tm->price."' ".$condition.";";
   //sql query
   $db->query($insert_tm) or error('Impossible to insert template<br />'.$insert_tm.mysql_error(), __FILE__, __LINE__, $db->error());
    
   //template details insert sql query string
   $insert_tm_details = $action." `tm_details` SET
   `id_tm`='".$db->insert_id()."',
   `exc_price`='".$tm->exc_price."',
   `downloads`='".$tm->downloads."',
   `inserted_date`='".$tm->inserted_date."',
   `software_required`='".$tm->is_non_unique."' ".$condition.";";
   //sql query
   $db->query($insert_tm_details) or error('Impossible to insert template details<br />'.$insert_tm_details.mysql_error(), __FILE__, __LINE__, $db->error());
    
   //makes an array with template's keywords
   $tm_kw = explode(" ", $tm->keywords);
   //loops on template's key array
   foreach ($tm_kw as $kw)
   {
      
      $kw = str_ireplace("\n", " ", trim($db->escape($kw)));
      if ($kw == "realtor\nhorizontalmenu")
         echo 'cla';
      //if keywords exists in database, inserts the template/keyword link
      if (array_key_exists($kw,$kws))
      {
         $sql = "INSERT INTO tm_keywords SET id_tm = ".$tm->id.", id_keyword = ".$kws[$kw];
         $db->query($sql) or die ($sql.mysql_error());
      }
      //or create the keyword_index and insert link
      else
      {
         $sql = "INSERT INTO tm_keywords_index SET label_en = '".$kw."'";
         $db->query($sql) or die ($sql.mysql_error());
         $kwid = $db->insert_id();
         $sql = "INSERT INTO tm_keywords SET id_tm = ".$tm->id.", id_keyword = '".$kwid."'";
         $db->query($sql) or die ($sql.mysql_error());
         //adds keyword to cached keywords
         $kws[$kw] = $kwid;
         //insert translation task here
      }
   }
    
   //if template does not exist
   if ($action == "INSERT INTO")
   {
      
      //adds style links
      foreach ($tm->styles->style as $style)
          tm_add_style($style->style_id, $tm->id);
      //adds category links
      foreach ($tm->categories->category as $cat)
          tm_add_cat($cat->category_id, $tm->id);
  
      // don't blame me blame because there is redundant information in xml please !
      //basically, we are making one array of all templates screenshots
      $screenshots = array();
      foreach ($tm->screenshots_list->screenshot as $screenshoot)
      {
         $rel_uri = trim(str_ireplace('http://images.templatemonster.com/screenshots/','',$screenshoot->uri));
         $screenshots[$rel_uri]['filemtime'] = mktimestamp($screenshoot->filemtime);
         $screenshots[$rel_uri]['id_page'] = 0;
         $screenshots[$rel_uri]['uri'] = $rel_uri;
      }
      foreach ($tm->pages->page as $page)
      {
         //get page id to use
         $sql = "SELECT id_page, label_en FROM tm_pages WHERE label_en='".$db->escape($page->name)."'";
         $res_pages = $db->query($sql) or error('Impossible to fetch pages'.$sql, __FILE__, __LINE__, $db->error());
         if ($db->num_rows($res_pages)<1)
         {
            $db->query("INSERT INTO tm_pages SET label_en='".$db->escape($page->name)."'") or die(mysql_error()."<br>"."INSERT INTO tm_pages SET label_en='".$db->escape($page->name)."'");
            $id_page = $db->insert_id();
         }
         else
         {
            $local_tmp = $db->fetch_assoc($res_pages);
            $id_page = $local_tmp['id_page'];
         }
         //got page id
         foreach($page->screenshots->scr as $scr)
         {
            $rel_uri = trim(str_ireplace('http://images.templatemonster.com/screenshots/','',$scr->uri));
            $screenshots[$rel_uri]['id_tm'] = $tm->id;
            $screenshots[$rel_uri]['id_page'] = $id_page;
            $screenshots[$rel_uri]['description'] = strtolower($scr->description);
            $screenshots[$rel_uri]['scr_type_id'] = $scr->scr_type_id;
            $screenshots[$rel_uri]['width'] = $scr->width;
            $screenshots[$rel_uri]['height'] = $scr->height;
            $screenshots[$rel_uri]['href'] = $scr->href;
            $screenshots[$rel_uri]['uri'] = $rel_uri;
         }
      }
      //Finally, we got these redundant information sorted, painfull isn't it ?
      
      //loops in screenshots array :
      foreach($screenshots as $sshot)
      {
         //makes screenshot insertion sql query script
         $order = "INSERT INTO `tm_screenshots`";
         $id_page = ($sshot['id_page']>0)?"`id_page` = '".$sshot['id_page']."', ":"";
         $sql = $order.' SET
         '.$id_page.'
         `id_tm` = "'.$tm->id.'",
         `scr_type_id` = "'.$sshot['scr_type_id'].'",
         `width` = "'.$sshot['width'].'",
         `height` = "'.$sshot['height'].'",
         `uri` = "'.$sshot['uri'].'",
         `filemtime` = "'.$sshot['filemtime'].'",
         `href` = "'.$sshot['href'].'",
         `description` = "'.$sshot['description'].'";';
         //queries for insertion
         $db->query($sql) or die ($sql."<br>".mysql_error());
         
         if ($tm_config['local_screenshots'])
         {
            //make folders and downloads screenshot
            $uri = array();
            $uri = explode("/", $sshot['uri']);
            mkdir(SCREENSHOTS_FOLDER.$uri[0]);
            get_file1("http://images.templatemonster.com/screenshots/".$sshot['uri'],SCREENSHOTS_FOLDER.$uri[0]."/",$uri[1]);
         }
      }
   }
    $i++;
}

echo $num_templates."<br />";
$db->end_transaction();