pastebin

Paste Search Dynamic
Recent pastes
xmlsitemap pages
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Page callbacks for the xmlsitemap module.
  6.  *
  7.  * @ingroup xmlsitemap
  8.  */
  9.  
  10. /**
  11.  * Get the sitemap chunk/page of the current request.
  12.  */
  13. function xmlsitemap_get_current_chunk(stdClass $sitemap) {
  14.   // Check if we should be displaing the index.
  15.   if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
  16.     if ($sitemap->chunks > 1) {
  17.       return 'index';
  18.     }
  19.     else {
  20.       return 1;
  21.     }
  22.   }
  23.   else {
  24.     return (int) $_GET['page'];
  25.   }
  26. }
  27.  
  28. /**
  29.  * Output a sitemap page.
  30.  *
  31.  * @see xmlsitemap_sitemap_load_by_context()
  32.  * @see xmlsitemap_get_current_chunk()
  33.  * @see xmlsitemap_sitemap_get_file()
  34.  * @see xmlsitemap_output_file()
  35.  */
  36. function xmlsitemap_output_chunk() {
  37.   $sitemap = xmlsitemap_sitemap_load_by_context();
  38.   if (!$sitemap) {
  39.     return MENU_NOT_FOUND;
  40.   }
  41.  
  42.   $chunk = xmlsitemap_get_current_chunk($sitemap);
  43.   $file = xmlsitemap_sitemap_get_file($sitemap, $chunk);
  44.  
  45.   // Provide debugging information if enabled.
  46.   if (variable_get('xmlsitemap_developer_mode', 0) && isset($_GET['debug'])) {
  47.     $output = array();
  48.     $context = xmlsitemap_get_current_context();
  49.     $output[] = "Current context: " . print_r($context, true);
  50.     $output[] = "Sitemap: " . print_r($sitemap, true);
  51.     $output[] = "Chunk: $chunk";
  52.     $output[] = "Cache file location: $file";
  53.     $output[] = "Cache file exists: " . (file_exists($file) ? 'Yes' : 'No');
  54.     return implode('<br />', $output);
  55.   }
  56.  
  57.   return xmlsitemap_output_file($file);
  58. }
  59.  
  60. /**
  61.  * Output the contents of a file to the browser and check caching headers.
  62.  */
  63. function xmlsitemap_output_file($file, array $headers = array()) {
  64.   if (!file_exists($file) || !is_readable($file)) {
  65.     return MENU_NOT_FOUND;
  66.   }
  67.  
  68.   $mtime = filemtime($file);
  69.   $last_modified = gmdate(DATE_RFC1123, $mtime);
  70.   $etag = '"' . md5($last_modified) . '"';
  71.  
  72.   // See if the client has provided the required HTTP headers.
  73.   $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
  74.   $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
  75.   if ($if_modified_since && $if_none_match && $if_none_match == $etag && $if_modified_since == $last_modified) {
  76.     header('HTTP/1.1 304 Not Modified');
  77.     // All 304 responses must send an etag if the 200 response for the same object contained an etag
  78.     header('Etag: ' . $etag);
  79.     exit;
  80.   }
  81.  
  82.   $headers += array(
  83.     'Content-type' => 'text/xml; charset=utf-8',
  84.     //'Content-length' => filesize($file),
  85.     'Last-modified' => $last_modified,
  86.     'Etag' => $etag,
  87.     'Expires' => gmdate(DATE_RFC1123, $mtime + variable_get('xmlsitemap_minimum_lifetime', 0)),
  88.     'Cache-Control' => 'must-revalidate',
  89.     'X-Robots-Tag' => 'noindex, follow',
  90.   );
  91.  
  92.   // Transfer the file as output.
  93.   xmlsitemap_file_transfer($file, $headers);
  94. }
  95.  
  96. /**
  97.  * Modified version of file_transfer() that invokes hook_exit()s afterwards.
  98.  *
  99.  * @see file_transfer()
  100.  */
  101. function xmlsitemap_file_transfer($uri, $headers) {
  102.   if (ob_get_level()) {
  103.     ob_end_clean();
  104.   }
  105.  
  106.   foreach ($headers as $name => $value) {
  107.     drupal_add_http_header($name, $value);
  108.   }
  109.   drupal_send_headers();
  110.  
  111.   // Attempt to increase time to transfer file.
  112.   drupal_set_time_limit(240);
  113.  
  114.   $scheme = variable_get('file_default_scheme', 'public');
  115.   // Transfer file in 16 KB chunks to save memory usage.
  116.   if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
  117.     while (!feof($fd)) {
  118.       print fread($fd, 1024*16);
  119.     }
  120.     fclose($fd);
  121.   }
  122.   else {
  123.     drupal_not_found();
  124.   }
  125.   drupal_exit();
  126. }
  127.  
  128. /**
  129.  * Output an XML transformation file for the sitemap XML.
  130.  */
  131. function xmlsitemap_output_xsl() {
  132.   // Read the XSL content from the file.
  133.   $module_path = drupal_get_path('module', 'xmlsitemap');
  134.   $xsl_content = file_get_contents($module_path . '/xsl/xmlsitemap.xsl');
  135.  
  136.   // Make sure the strings in the XSL content are translated properly.
  137.   $replacements = array(
  138.     'Sitemap file' => t('Sitemap file'),
  139.     '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')),
  140.     'Number of sitemaps in this index' => t('Number of sitemaps in this index'),
  141.     'Click on the table headers to change sorting.' => t('Click on the table headers to change sorting.'),
  142.     'Sitemap URL' => t('Sitemap URL'),
  143.     'Last modification date' => t('Last modification date'),
  144.     'Number of URLs in this sitemap' => t('Number of URLs in this sitemap'),
  145.     'URL location' => t('URL location'),
  146.     'Change frequency' => t('Change frequency'),
  147.     'Priority' => t('Priority'),
  148.     '[jquery]' => base_path() . 'misc/jquery.js',
  149.     '[jquery-tablesort]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
  150.     '[xsl-js]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.js',
  151.     '[xsl-css]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.css',
  152.   );
  153.   $xsl_content = strtr($xsl_content, $replacements);
  154.  
  155.   // Output the XSL content.
  156.   drupal_add_http_header('Content-type', 'application/xml; charset=utf-8');
  157.   drupal_add_http_header('X-Robots-Tag', 'noindex, follow');
  158.   print $xsl_content;
  159. }
Parsed in 0.098 seconds