xmlsitemap.pages.inc 5.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
// $Id$

/**
 * @file
 * Page callbacks for the xmlsitemap module.
 *
 * @ingroup xmlsitemap
 */

11
12
13
/**
 * Get the sitemap chunk/page of the current request.
 */
14
function xmlsitemap_get_current_chunk(array $sitemap) {
15
  // Check if we should be displaing the index.
16
  if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
17
    if ($sitemap['chunks'] > 1) {
18
      return 'index';
19
20
    }
    else {
21
      return 1;
22
23
24
    }
  }
  else {
25
    return (int) $_GET['page'];
26
  }
27
}
28

29
30
31
/**
 * Output a sitemap page.
 *
32
 * @see xmlsitemap_sitemap_load_by_context()
33
 * @see xmlsitemap_get_current_chunk()
34
 * @see xmlsitemap_get_sitemap_file()
35
36
37
 * @see xmlsitemap_output_file()
 */
function xmlsitemap_output_chunk() {
38
39
40
41
42
43
44
  $sitemap = xmlsitemap_sitemap_load_by_context();
  if (!$sitemap) {
    return MENU_NOT_FOUND;
  }

  $chunk = xmlsitemap_get_current_chunk($sitemap);
  $file = xmlsitemap_get_sitemap_file($sitemap, $chunk);
45

46
  // Provide debugging information if enabled.
47
  if (variable_get('xmlsitemap_developer_mode', 0) || isset($_GET['debug'])) {
48
    $output = array();
49
    $output[] = "Sitemap: " . print_r($sitemap, TRUE);
50
51
52
53
54
55
    $output[] = "Chunk: $chunk";
    $output[] = "Cache file location: $file";
    $output[] = "Cache file exists: " . (file_exists($file) ? 'Yes' : 'No');
    return implode('<br />', $output);
  }

56
  return xmlsitemap_output_file($file);
57
58
59
60
61
62
63
}

/**
 * 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)) {
64
    return MENU_NOT_FOUND;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  }

  $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(
82
83
84
85
86
87
88
    'Content-type' => 'text/xml; charset=utf-8',
    //'Content-length' => filesize($file),
    'Last-modified' => $last_modified,
    'Etag' => $etag,
    'Expires' => gmdate(DATE_RFC1123, $mtime + variable_get('xmlsitemap_minimum_lifetime', 0)),
    'Cache-Control' => 'must-revalidate',
    'X-Robots-Tag' => 'noindex, follow',
89
90
91
92
93
94
95
96
97
98
99
  );

  // Transfer the file as output.
  xmlsitemap_file_transfer($file, $headers);
}

/**
 * Modified version of file_transfer() that invokes hook_exit()s afterwards.
 *
 * @see file_transfer()
 */
100
function xmlsitemap_file_transfer($uri, $headers) {
101
102
103
104
  if (ob_get_level()) {
    ob_end_clean();
  }

105
106
  foreach ($headers as $name => $value) {
    drupal_add_http_header($name, $value);
107
  }
108
  drupal_send_headers();
109

110
  $scheme = $scheme = variable_get('file_default_scheme', 'public');
111
  // Transfer file in 16 KB chunks to save memory usage.
112
113
  if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
    while (!feof($fd)) {
114
      print fread($fd, 1024*16);
115
    }
116
    fclose($fd);
117
118
119
120
  }
  else {
    drupal_not_found();
  }
121
  drupal_exit();
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
}

/**
 * Output an XML transformation file for the sitemap XML.
 */
function xmlsitemap_output_xsl() {
  // Read the XSL content from the file.
  $module_path = drupal_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://drupal.org/project/xmlsitemap">Drupal XML sitemap module</a>.' => t('Generated by the <a href="@link-xmlsitemap">Drupal XML sitemap module</a>.', array('@link-xmlsitemap' => 'http://drupal.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'),
144
145
    '[jquery]' => base_path() . 'misc/jquery.js',
    '[jquery-tablesort]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
146
    '[xsl-js]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.js',
147
    '[xsl-css]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.css',
148
149
150
151
  );
  $xsl_content = strtr($xsl_content, $replacements);

  // Output the XSL content.
152
153
  drupal_add_http_header('Content-type', 'application/xml; charset=utf-8');
  drupal_add_http_header('X-Robots-Tag', 'noindex, follow');
154
  print $xsl_content;
155
}