commit | author | age
|
3e083b
|
1 |
<?php |
B |
2 |
|
|
3 |
/** |
|
4 |
* google sitemap 文件 |
|
5 |
*/ |
|
6 |
|
|
7 |
class sitemap |
|
8 |
{ |
|
9 |
var $head = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">\n"; |
|
10 |
var $footer = "</urlset>\n"; |
|
11 |
var $item; |
|
12 |
function item($item) |
|
13 |
{ |
|
14 |
$this->item .= "<url>\n"; |
|
15 |
foreach($item as $key => $val){ |
|
16 |
$this->item .=" <$key>".htmlentities($val, ENT_QUOTES)."</$key>\n"; |
|
17 |
} |
|
18 |
$this->item .= "</url>\n"; |
|
19 |
} |
|
20 |
function generate() |
|
21 |
{ |
|
22 |
$all = $this->head; |
|
23 |
$all .= $this->item; |
|
24 |
$all .= $this->footer; |
|
25 |
|
|
26 |
return $all; |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
define('IN_ECS', true); |
|
31 |
define('INIT_NO_USERS', true); |
|
32 |
define('INIT_NO_SMARTY', true); |
|
33 |
require(dirname(__FILE__) . '/includes/init.php'); |
|
34 |
if (file_exists(ROOT_PATH . DATA_DIR . '/sitemap.dat') && time() - filemtime(ROOT_PATH . DATA_DIR . '/sitemap.dat') < 86400) |
|
35 |
{ |
|
36 |
$out = file_get_contents(ROOT_PATH . DATA_DIR . '/sitemap.dat'); |
|
37 |
} |
|
38 |
else |
|
39 |
{ |
|
40 |
$site_url = rtrim($ecs->url(),'/'); |
|
41 |
$sitemap = new sitemap; |
|
42 |
$config = unserialize($_CFG['sitemap']); |
|
43 |
$item = array( |
|
44 |
'loc' => "$site_url/", |
|
45 |
'lastmod' => local_date('Y-m-d'), |
|
46 |
'changefreq' => $config['homepage_changefreq'], |
|
47 |
'priority' => $config['homepage_priority'], |
|
48 |
); |
|
49 |
$sitemap->item($item); |
|
50 |
/* 商品分类 */ |
|
51 |
$sql = "SELECT cat_id,cat_name FROM " .$ecs->table('category'). " ORDER BY parent_id"; |
|
52 |
$res = $db->query($sql); |
|
53 |
|
|
54 |
while ($row = $db->fetchRow($res)) |
|
55 |
{ |
|
56 |
$item = array( |
|
57 |
'loc' => "$site_url/" . build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']), |
|
58 |
'lastmod' => local_date('Y-m-d'), |
|
59 |
'changefreq' => $config['category_changefreq'], |
|
60 |
'priority' => $config['category_priority'], |
|
61 |
); |
|
62 |
$sitemap->item($item); |
|
63 |
} |
|
64 |
/* 文章分类 */ |
|
65 |
$sql = "SELECT cat_id,cat_name FROM " .$ecs->table('article_cat'). " WHERE cat_type=1"; |
|
66 |
$res = $db->query($sql); |
|
67 |
|
|
68 |
while ($row = $db->fetchRow($res)) |
|
69 |
{ |
|
70 |
$item = array( |
|
71 |
'loc' => "$site_url/" . build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']), |
|
72 |
'lastmod' => local_date('Y-m-d'), |
|
73 |
'changefreq' => $config['category_changefreq'], |
|
74 |
'priority' => $config['category_priority'], |
|
75 |
); |
|
76 |
$sitemap->item($item); |
|
77 |
} |
|
78 |
/* 商品 */ |
|
79 |
$sql = "SELECT goods_id, goods_name, last_update FROM " .$ecs->table('goods'). " WHERE is_delete = 0 LIMIT 300"; |
|
80 |
$res = $db->query($sql); |
|
81 |
|
|
82 |
while ($row = $db->fetchRow($res)) |
|
83 |
{ |
|
84 |
$item = array( |
|
85 |
'loc' => "$site_url/" . build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']), |
|
86 |
'lastmod' => local_date('Y-m-d', $row['last_update']), |
|
87 |
'changefreq' => $config['content_changefreq'], |
|
88 |
'priority' => $config['content_priority'], |
|
89 |
); |
|
90 |
$sitemap->item($item); |
|
91 |
} |
|
92 |
/* 文章 */ |
|
93 |
$sql = "SELECT article_id,title,file_url,open_type, add_time FROM " .$ecs->table('article'). " WHERE is_open=1"; |
|
94 |
$res = $db->query($sql); |
|
95 |
|
|
96 |
while ($row = $db->fetchRow($res)) |
|
97 |
{ |
|
98 |
$article_url=$row['open_type'] != 1 ? build_uri('article', array('aid'=>$row['article_id']), $row['title']) : trim($row['file_url']); |
|
99 |
$item = array( |
|
100 |
'loc' => "$site_url/" . $article_url, |
|
101 |
'lastmod' => local_date('Y-m-d', $row['add_time']), |
|
102 |
'changefreq' => $config['content_changefreq'], |
|
103 |
'priority' => $config['content_priority'], |
|
104 |
); |
|
105 |
$sitemap->item($item); |
|
106 |
} |
|
107 |
$out = $sitemap->generate(); |
|
108 |
file_put_contents(ROOT_PATH . DATA_DIR . '/sitemap.dat', $out); |
|
109 |
} |
|
110 |
if (function_exists('gzencode')) |
|
111 |
{ |
|
112 |
header('Content-type: application/x-gzip'); |
|
113 |
$out = gzencode($out, 9); |
|
114 |
} |
|
115 |
else |
|
116 |
{ |
|
117 |
header('Content-type: application/xml; charset=utf-8'); |
|
118 |
} |
|
119 |
die($out); |
|
120 |
?> |