-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.php
91 lines (90 loc) · 2.63 KB
/
sitemap.php
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
<?php
require('./include/config.php');
require('./include/init.php');
/**
* 生成站点地图
*/
class Site_map
{
/**
* URL 列表
*/
public array $url_list;
/**
* 输出格式:txt、xml
*/
public string $format;
/**
* 站点域名
*
* 如果站点位于二级目录,则填 `http://domain/path`,注意结尾没有斜杠
*/
public string $domain = 'http://iapp.apee.top';
public function __construct()
{
$this->url_list = [$this->domain];
$this->format = $_GET['format'] ?? 'txt';
$this->get_books_url();
$this->get_article_url();
}
/**
* 获取手册 URL 列表
*/
public function get_books_url()
{
$table = Config::$table['book'];
$sql = "SELECT `id` FROM `$table`";
$result = mysqli_query(Init::$conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$url = $this->domain . '/book_' . $id . '.html';
array_push($this->url_list, $url);
}
}
/**
* 获取手册 URL 列表
*/
public function get_article_url()
{
$table = Config::$table['article'];
$sql = "SELECT `id` FROM `$table`";
$result = mysqli_query(Init::$conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$url = $this->domain . '/article_' . $id . '.html';
array_push($this->url_list, $url);
}
}
}
$site_map = new Site_map();
if ($site_map->format == 'txt') {
header('Content-Type: text/plain; charset=utf-8');
foreach ($site_map->url_list as $url) {
echo $url . PHP_EOL;
}
} elseif ($site_map->format == 'xml') {
header('Content-Type: text/xml; charset=utf-8');
ini_set('date.timezone', 'Asia/Shanghai');
$lastmod = date('Y-m-d\TH:i:s+08:00');
echo '
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
foreach ($site_map->url_list as $url) {
if (preg_match('/book_(\d+).html/', $url)) {
$priority = 0.8;
} elseif (preg_match('/article_(\d+).html/', $url)) {
$priority = 0.64;
} elseif ($url == $site_map->domain) {
$priority = 1.0;
} else {
$priority = 0.3;
}
echo '
<url>
<loc>' . $url . '</loc>
<lastmod>' . $lastmod . '</lastmod>
<priority>' . $priority . '</priority>
</url>';
}
echo '
</urlset>';
}