xmlsitemap.pages.inc 4.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
// $Id$

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

/**
 * Output a sitemap page.
 *
 * @see xmlsitemap_file_transfer()
 */
16
function xmlsitemap_output_chunk() {
17 18
  $context = module_invoke_all('xmlsitemap_context');
  drupal_alter('xmlsitemap_context', $context);
19 20

  // Check if we should be displaing the index.
21
  if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
22 23 24 25 26 27 28 29 30 31 32 33
    $index_file = xmlsitemap_get_file_from_context($context, 'index');
    if (file_exists($index_file)) {
      $chunk = 'index';
    }
    else {
      $chunk = 1;
    }
  }
  else {
    $chunk = (int) $_GET['page'];
  }

34 35
  // Fallback to non-context sitemap?

36
  $file = xmlsitemap_get_file_from_context($context, $chunk);
37
  return xmlsitemap_output_file($file);
38 39 40 41 42 43 44
}

/**
 * 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)) {
45
    return MENU_NOT_FOUND;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  }

  $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(
63 64 65 66 67 68 69
    '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',
70 71 72 73 74 75 76 77 78 79 80
  );

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

/**
 * Modified version of file_transfer() that invokes hook_exit()s afterwards.
 *
 * @see file_transfer()
 */
81
function xmlsitemap_file_transfer($uri, $headers) {
82 83 84 85
  if (ob_get_level()) {
    ob_end_clean();
  }

86 87
  foreach ($headers as $name => $value) {
    drupal_add_http_header($name, $value);
88
  }
89 90
  drupal_send_headers();
  $scheme = $scheme = variable_get('file_default_scheme', 'public');
91
  // Transfer file in 1024 byte chunks to save memory usage.
92 93 94
  if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) {
    while (!feof($fd)) {
      print fread($fd, 1024);
95
    }
96
    fclose($fd);
97 98 99 100
  }
  else {
    drupal_not_found();
  }
101
  drupal_exit();
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

/**
 * 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'),
124 125
    '[jquery]' => base_path() . 'misc/jquery.js',
    '[jquery-tablesort]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
126
    '[xsl-js]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.js',
127
    '[xsl-css]' => base_path() . $module_path . '/xsl/xmlsitemap.xsl.css',
128 129 130 131
  );
  $xsl_content = strtr($xsl_content, $replacements);

  // Output the XSL content.
132 133
  drupal_add_http_header('Content-type', 'application/xml; charset=utf-8');
  drupal_add_http_header('X-Robots-Tag', 'noindex, follow');
134
  print $xsl_content;
135
}