sitemap.class.php 2.72 KB
<?php
class SiteMap{
 private $db = null;
 private $tables_info = array();
 private $data = array();

 function __construct($db){
  $this->db = $db;
 }
 
 function __destruct() {
  unset($this->tables_info);
  unset($this->data);
 }
 
 function addTable($loc,$priority,$lastmod,$changefreq,$table,$field, $where = "1=1"){
  array_push($this->tables_info,array('loc'=>$loc,'priority'=>$priority,'lastmod'=>$lastmod,'changefreq'=>$changefreq,'table'=>$table,'field'=>$field, 'where'=>$where));
 }
 
 function viewTablesData(){
  print_r($this->data);
 }
 
 private function addData($res,$url){
  foreach($res as $row){
   $this->addUrl($this->sprint_f($url['loc'],$row),$url['priority'],$url['lastmod'],$url['changefreq']);
  }
 }
 
 function sprint_f($str,$vars = array()){
   $eval = "\$r = sprintf(\$str,";
            for($i=0;$i<count($vars);$i++){$eval .= "\$vars[".$i."]";if($i<count($vars)-1)$eval .= ",";}
            $eval .= ");";
            eval($eval);
            return $r;
 }
 
 function addUrl($loc,$priority,$lastmod,$changefreq){
   array_push($this->data,array('loc'=>$loc,'priority'=>$priority,'lastmod'=>$lastmod,'changefreq'=>$changefreq));
 }
 
 function start(){
  foreach($this->tables_info as $row){
   $sql = "select ".implode(",",$row['field'])." from ".$row['table']." where ".$row['where'];
   $res = $this->db->getAll($sql,array());
   $this->addData($res, $row);
  }
 }
 
 private function save($file,$xml){
  $handle = fopen($file, "w");
  fwrite($handle,$xml);
  fclose($handle);
 }
 
 function saveXML($file){
 //print"<pre>";
 //print_r($this->data);
 //print"</pre>";
// print str_replace('®','A',"QUEST 4D gtx®'13");
 $xml = '<?xml version="1.0" encoding="UTF-8" ?>
 ';
  $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ';
  foreach($this->data as $row){
   $xml .= "<url>
   ";
   $xml .= sprintf('<loc>%s</loc>
   ', $this->escapeLoc($row['loc']));
   $xml .= sprintf("<lastmod>%s</lastmod>
   ", $row['lastmod']);
   $xml .= sprintf("<changefreq>%s</changefreq>
   ", $row['changefreq']);
   $xml .= sprintf("<priority>%s</priority>
   ", $row['priority']);
   $xml .= "</url>
   ";
  }
  $xml .= '</urlset>';
  //print $xml;
  $this->save($file,$xml);
 }
 
 private function escapeLoc($loc){
        $patterns = array();
        $patterns[0] = '/&/';
        $patterns[1] = '/\'/';
        $patterns[2] = '/"/';
        $patterns[3] = '/>/';
        $patterns[4] = '/</';
        $replacements = array();
        $replacements[4] = '&amp;';
        $replacements[3] = '&apos;';
        $replacements[2] = '&quot;';
        $replacements[1] = '&gt;';
        $replacements[0] = '&lt;';
		
        $loc = preg_replace($patterns, $replacements, $loc);
		$loc = iconv("WINDOWS-1251", "UTF-8", $loc);
		return $loc;
 }
 
 
}

?>