[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/xmlsitemap/ -> xmlsitemap.pages.inc (source)

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7