[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/project/release/ -> project-release-serve-history.php (source)

   1  <?php
   2  
   3  // $Id: project-release-serve-history.php,v 1.14 2010/01/16 19:59:13 dww Exp $
   4  
   5  /**
   6   * @file
   7   * Ultra-thin PHP wrapper to serve XML release history files to
   8   * the update.module ("update_status.module" in 5.x contrib).
   9   *
  10   * This script requires a local .htaccess file with the following:
  11   *
  12   * DirectoryIndex project-release-serve-history.php
  13   * <IfModule mod_rewrite.c>
  14   *   RewriteEngine on
  15   *   RewriteRule ^(.*)$ project-release-serve-history.php?q=$1 [L,QSA]
  16   * </IfModule>
  17   *
  18   * @author Derek Wright (http://drupal.org/user/46549)
  19   */
  20  
  21  /**
  22   * Required configuration: directory tree for the XML history files.
  23   */
  24  define('HISTORY_ROOT', '');
  25  
  26  /**
  27   * Required configuration: location of your Drupal installation for
  28   * bootstrapping and recording usage statistics.
  29   */
  30  define('DRUPAL_ROOT', '');
  31  
  32  /**
  33   * Required configuration: name of your site.
  34   *
  35   * Needed to find the right settings.php file to bootstrap Drupal with.
  36   */
  37  define('SITE_NAME', '');
  38  
  39  
  40  /**
  41   * Find and serve the proper history file.
  42   */
  43  
  44  // Set page headers for the XML response.
  45  header('Content-Type: text/xml; charset=utf-8');
  46  
  47  // Make sure we have the path arguments we need.
  48  $path = $_GET['q'];
  49  $args = explode('/', $path);
  50  if (empty($args[0])) {
  51    error('You must specify a project name to display the release history of.');
  52  }
  53  else {
  54    $project_name = $args[0];
  55  }
  56  if (empty($args[1])) {
  57    error('You must specify an API compatibility version as the final argument to the path.');
  58  }
  59  else {
  60    $api_version = $args[1];
  61  }
  62  
  63  // Sanitize the user-supplied input for use in filenames.
  64  $whitelist_regexp = '@[^a-zA-Z0-9_.-]@';
  65  $safe_project_name = preg_replace($whitelist_regexp, '#', $project_name);
  66  $safe_api_vers = preg_replace($whitelist_regexp, '#', $api_version);
  67  
  68  // Figure out the filename for the release history we want to serve.
  69  $project_dir = HISTORY_ROOT .'/'. $safe_project_name;
  70  $filename = $safe_project_name .'-'. $safe_api_vers .'.xml';
  71  $full_path = $project_dir .'/'. $filename;
  72  
  73  if (!is_file($full_path)) {
  74    if (!is_dir($project_dir)) {
  75      error(strtr('No release history was found for the requested project (@project).', array('@project' => _check_plain($project_name))));
  76    }
  77    error(strtr('No release history available for @project @version.', array('@project' => _check_plain($project_name), '@version' => _check_plain($api_version))));
  78    exit(1);
  79  }
  80  
  81  // Set the Last-Modified to the timestamp of the file.  Otherwise, disable all
  82  // caching since a) we continue to have problems with squid on d.o and b)
  83  // we're going to need this as soon as we start collecting stats.
  84  $stat = stat($full_path);
  85  $mtime = $stat[9];
  86  header('Last-Modified: '. gmdate('D, d M Y H:i:s', $mtime) .' GMT');
  87  header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
  88  header("Cache-Control: store, no-cache, must-revalidate");
  89  header("Cache-Control: post-check=0, pre-check=0", FALSE);
  90  
  91  // Serve the contents.
  92  $file = file_get_contents($full_path);
  93  // Old release xml files are missing the encoding. Prepend one if necessary.
  94  if (substr($file, 0, 5) != '<?xml') {
  95    echo '<?xml version="1.0" encoding="utf-8"?>' ."\n";
  96  }
  97  echo $file;
  98  
  99  // Record usage statistics.
 100  if (isset($_GET['site_key'])) {
 101    if (!chdir(DRUPAL_ROOT)) {
 102      exit(1);
 103    }
 104  
 105    // Setup variables for Drupal bootstrap
 106    $script_name = $argv[0];
 107    $_SERVER['HTTP_HOST'] = SITE_NAME;
 108    $_SERVER['REQUEST_URI'] = '/' . $script_name;
 109    $_SERVER['SCRIPT_NAME'] = '/' . $script_name;
 110    $_SERVER['PHP_SELF'] = '/' . $script_name;
 111    $_SERVER['SCRIPT_FILENAME'] = $_SERVER['PWD'] .'/'. $script_name;
 112    $_SERVER['PATH_TRANSLATED'] = $_SERVER['SCRIPT_FILENAME'];
 113  
 114    // Actually do the bootstrap.
 115    include_once  './includes/bootstrap.inc';
 116    drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
 117  
 118    // We can't call module_exists without bootstrapping to a higher level so
 119    // we'll settle for checking that the table exists.
 120    if (db_table_exists('project_usage_raw')) {
 121      $site_key = $_GET['site_key'];
 122      $project_version = isset($_GET['version']) ? $_GET['version'] : '';
 123      $ip_addr = ip_address();
 124  
 125      // Compute a GMT timestamp for begining of the day. getdate() is
 126      // affected by the server's timezone so we need to cancel it out.
 127      $now = time();
 128      $time_parts = getdate($now - date('Z', $now));
 129      $timestamp = gmmktime(0, 0, 0, $time_parts['mon'], $time_parts['mday'], $time_parts['year']);
 130  
 131      db_query("UPDATE {project_usage_raw} SET api_version = '%s', project_version = '%s', ip_addr = '%s' WHERE project_uri = '%s' AND timestamp = %d AND site_key = '%s'", $api_version, $project_version, $ip_addr, $project_name, $timestamp, $site_key);
 132      if (!db_affected_rows()) {
 133        db_query("INSERT INTO {project_usage_raw} (project_uri, timestamp, site_key, api_version, project_version, ip_addr) VALUES ('%s', %d, '%s', '%s', '%s', '%s')", $project_name, $timestamp, $site_key, $api_version, $project_version, $ip_addr);
 134      }
 135    }
 136  }
 137  
 138  
 139  /**
 140   * Copy of core's check_plain() function.
 141   */
 142  function _check_plain($text) {
 143    return htmlspecialchars($text, ENT_QUOTES);
 144  }
 145  
 146  /**
 147   * Generate an error and exit.
 148   */
 149  function error($text) {
 150    echo '<?xml version="1.0" encoding="utf-8"?>'. "\n";
 151    echo '<error>'. $text ."</error>\n";
 152    exit(1);
 153  }


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