xmlsitemap.pages.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // $Id: xmlsitemap.pages.inc,v 1.6 2010/01/24 05:54:40 davereid Exp $
  3. /**
  4. * @file
  5. * Page callbacks for the xmlsitemap module.
  6. *
  7. * @ingroup xmlsitemap
  8. */
  9. /**
  10. * Output a sitemap page.
  11. *
  12. * @see xmlsitemap_file_transfer()
  13. */
  14. function xmlsitemap_output_chunk($chunk = 0) {
  15. global $language;
  16. // @todo Remove this from the final version?
  17. if (isset($_GET['refresh']) && user_access('administer xmlsitemap')) {
  18. module_load_include('inc', 'xmlsitemap');
  19. xmlsitemap_generate($chunk, $language);
  20. }
  21. $file = xmlsitemap_get_chunk_file($chunk, $language->language);
  22. xmlsitemap_output_file($file);
  23. }
  24. /**
  25. * Output the contents of a file to the browser and check caching headers.
  26. */
  27. function xmlsitemap_output_file($file, array $headers = array()) {
  28. if (!file_exists($file) || !is_readable($file)) {
  29. return MENU_NOT_FOUND;
  30. }
  31. $mtime = filemtime($file);
  32. $last_modified = gmdate(DATE_RFC1123, $mtime);
  33. $etag = '"' . md5($last_modified) . '"';
  34. // See if the client has provided the required HTTP headers.
  35. $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
  36. $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
  37. if ($if_modified_since && $if_none_match && $if_none_match == $etag && $if_modified_since == $last_modified) {
  38. header('HTTP/1.1 304 Not Modified');
  39. // All 304 responses must send an etag if the 200 response for the same object contained an etag
  40. header('Etag: ' . $etag);
  41. exit;
  42. }
  43. $headers += array(
  44. 'Content-type' => 'text/xml; charset=utf-8',
  45. //'Content-length' => filesize($file),
  46. 'Last-modified' => $last_modified,
  47. 'Etag' => $etag,
  48. 'Expires' => gmdate(DATE_RFC1123, $mtime + variable_get('xmlsitemap_minimum_lifetime', 0)),
  49. 'Cache-Control' => 'must-revalidate',
  50. 'X-Robots-Tag' => 'noindex, follow',
  51. );
  52. // Transfer the file as output.
  53. xmlsitemap_file_transfer($file, $headers);
  54. }
  55. /**
  56. * Modified version of file_transfer() that invokes hook_exit()s afterwards.
  57. *
  58. * @see file_transfer()
  59. */
  60. function xmlsitemap_file_transfer($uri, $headers) {
  61. if (ob_get_level()) {
  62. ob_end_clean();
  63. }
  64. foreach ($headers as $name => $value) {
  65. drupal_add_http_header($name, $value);
  66. }
  67. drupal_send_headers();
  68. $scheme = $scheme = variable_get('file_default_scheme', 'public');
  69. // Transfer file in 1024 byte chunks to save memory usage.
  70. if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
  71. while (!feof($fd)) {
  72. print fread($fd, 1024);
  73. }
  74. fclose($fd);
  75. }
  76. else {
  77. drupal_not_found();
  78. }
  79. drupal_exit();
  80. }
  81. /**
  82. * Output an XML transformation file for the sitemap XML.
  83. */
  84. function xmlsitemap_output_xsl() {
  85. // Read the XSL content from the file.
  86. $module_path = drupal_get_path('module', 'xmlsitemap');
  87. $xsl_content = file_get_contents($module_path . '/xsl/xmlsitemap.xsl');
  88. // Make sure the strings in the XSL content are translated properly.
  89. $replacements = array(
  90. 'Sitemap file' => t('Sitemap file'),
  91. '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')),
  92. 'Number of sitemaps in this index' => t('Number of sitemaps in this index'),
  93. 'Click on the table headers to change sorting.' => t('Click on the table headers to change sorting.'),
  94. 'Sitemap URL' => t('Sitemap URL'),
  95. 'Last modification date' => t('Last modification date'),
  96. 'Number of URLs in this sitemap' => t('Number of URLs in this sitemap'),
  97. 'URL location' => t('URL location'),
  98. 'Change frequency' => t('Change frequency'),
  99. 'Priority' => t('Priority'),
  100. '[jquery]' => base_path() . 'misc/jquery.js',
  101. '[jquery-tablesort]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
  102. '[xsl-js]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.js',
  103. '[xsl-css]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.css',
  104. );
  105. $xsl_content = strtr($xsl_content, $replacements);
  106. // Output the XSL content.
  107. drupal_add_http_header('Content-type', 'application/xml; charset=utf-8');
  108. drupal_add_http_header('X-Robots-Tag', 'noindex, follow');
  109. echo $xsl_content;
  110. }