_sitemap.class.php
2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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", $update = 0){
array_push($this->tables_info,array('loc'=>$loc,'priority'=>$priority,'lastmod'=>$lastmod,'changefreq'=>$changefreq,'table'=>$table,'field'=>$field, 'where'=>$where, 'update'=>$update));
}
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=1;$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){
print $sql = "select ".implode(",",$row['field'])." from ".$row['table']." where ".$row['where'];
$res = $this->db->getAll($sql,array());
//if($row['update']==1){$this->updateTable($res,$row);}
$this->addData($res, $row);
}
}
function updateTable($res,$row){
foreach($res as $r){
$fields_values = array('sitemap'=>1);
$where = sprintf("id='%d'", $r[0]);
$a = $this->db->autoExecute($row['table'], $fields_values, DB_AUTOQUERY_UPDATE,$where);
if (PEAR::isError($a)) die($a->getMessage());
}
}
private function save($file,$xml){
$handle = fopen($file, "w");
fwrite($handle,$xml);
fclose($handle);
}
function saveXML($file){
$xml = '';
$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>';
$this->save($file,$xml);
$this->saveHeadFotter($file);
}
private function saveHeadFotter($file){
}
private function escapeLoc($loc){
$patterns = array();
$patterns[0] = '/&/';
$patterns[1] = '/\'/';
$patterns[2] = '/"/';
$patterns[3] = '/>/';
$patterns[4] = '/</';
$replacements = array();
$replacements[4] = '&';
$replacements[3] = ''';
$replacements[2] = '"';
$replacements[1] = '>';
$replacements[0] = '<';
return preg_replace($patterns, $replacements, $loc);
}
}
?>