-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathxmlsitemap.pages.inc
162 lines (144 loc) · 5.33 KB
/
xmlsitemap.pages.inc
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* @file
* Page callbacks for the xmlsitemap module.
*
* @ingroup xmlsitemap
*/
/**
* Get the sitemap chunk/page of the current request.
*/
function xmlsitemap_get_current_chunk(stdClass $sitemap) {
// Check if we should be displaing the index.
if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
if ($sitemap->chunks > 1) {
return 'index';
}
else {
return 1;
}
}
else {
return (int) $_GET['page'];
}
}
/**
* Output a sitemap page.
*
* @see xmlsitemap_sitemap_load_by_context()
* @see xmlsitemap_get_current_chunk()
* @see xmlsitemap_sitemap_get_file()
* @see xmlsitemap_output_file()
*/
function xmlsitemap_output_chunk() {
$sitemap = xmlsitemap_sitemap_load_by_context();
if (!$sitemap) {
return MENU_NOT_FOUND;
}
$chunk = xmlsitemap_get_current_chunk($sitemap);
$file = xmlsitemap_sitemap_get_file($sitemap, $chunk);
// Provide debugging information if enabled.
if (config_get('xmlsitemap.settings', 'developer_mode') && isset($_GET['debug'])) {
$output = array();
$context = xmlsitemap_get_current_context();
$output[] = "Current context: " . print_r($context, TRUE);// phpcs:ignore
$output[] = "Sitemap: " . print_r($sitemap, TRUE);// phpcs:ignore
$output[] = "Chunk: $chunk";
$output[] = "Cache file location: $file";
$output[] = "Cache file exists: " . (file_exists($file) ? 'Yes' : 'No');
return implode('<br />', $output);
}
return xmlsitemap_output_file($file);
}
/**
* Output the contents of a file to the browser and check caching headers.
*/
function xmlsitemap_output_file($file, array $headers = array()) {
if (!file_exists($file) || !is_readable($file)) {
return MENU_NOT_FOUND;
}
$mtime = filemtime($file);
$last_modified = gmdate(DATE_RFC1123, $mtime);
$etag = '"' . md5($last_modified) . '"';
// See if the client has provided the required HTTP headers.
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
if ($if_modified_since && $if_none_match && $if_none_match == $etag && $if_modified_since == $last_modified) {
header('HTTP/1.1 304 Not Modified');
// All 304 responses must send an etag if the 200 response for the same
// object contained an etag.
header('Etag: ' . $etag);
exit;
}
$headers += array(
'Content-type' => 'text/xml; charset=utf-8',
'Last-modified' => $last_modified,
'Etag' => $etag,
'Expires' => gmdate(DATE_RFC1123, $mtime + config_get('xmlsitemap.settings', 'minimum_lifetime')),
'Cache-Control' => 'must-revalidate',
'X-Robots-Tag' => 'noindex, follow',
);
// Transfer the file as output.
xmlsitemap_file_transfer($file, $headers);
}
/**
* Modified version of file_transfer() that invokes hook_exit()s afterwards.
*
* @see file_transfer()
*/
function xmlsitemap_file_transfer($uri, $headers) {
if (ob_get_level()) {
ob_end_clean();
}
foreach ($headers as $name => $value) {
backdrop_add_http_header($name, $value);
}
backdrop_send_headers();
// Attempt to increase time to transfer file.
backdrop_set_time_limit(240);
$scheme = config_get('system.core', 'file_default_scheme');
// Transfer file in 16 KB chunks to save memory usage.
if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024 * 16);
}
fclose($fd);
// Disable session manipulation if PHP transferred a file.
backdrop_save_session(FALSE);
}
else {
backdrop_not_found();
}
backdrop_exit();
}
/**
* Output an XML transformation file for the sitemap XML.
*/
function xmlsitemap_output_xsl() {
// Read the XSL content from the file.
$module_path = backdrop_get_path('module', 'xmlsitemap');
$xsl_content = file_get_contents($module_path . '/xsl/xmlsitemap.xsl');
// Make sure the strings in the XSL content are translated properly.
$replacements = array(
'Sitemap file' => t('Sitemap file'),
'Generated by the <a href="http://backdropcms.org/project/xmlsitemap">XML sitemap module</a>.' => t('Generated by the <a href="@link-xmlsitemap">XML sitemap module</a>.', array('@link-xmlsitemap' => 'https://backdropcms.org/project/xmlsitemap')),
'Number of sitemaps in this index' => t('Number of sitemaps in this index'),
'Click on the table headers to change sorting.' => t('Click on the table headers to change sorting.'),
'Sitemap URL' => t('Sitemap URL'),
'Last modification date' => t('Last modification date'),
'Number of URLs in this sitemap' => t('Number of URLs in this sitemap'),
'URL location' => t('URL location'),
'Change frequency' => t('Change frequency'),
'Priority' => t('Priority'),
'[jquery]' => base_path() . 'core/misc/jquery.js',
'[jquery-tablesort]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
'[xsl-js]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.js',
'[xsl-css]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.css',
);
$xsl_content = strtr($xsl_content, $replacements);
// Output the XSL content.
backdrop_add_http_header('Content-type', 'application/xml; charset=utf-8');
backdrop_add_http_header('X-Robots-Tag', 'noindex, follow');
print $xsl_content;
backdrop_exit();
}