PHP 7
Drupal.sh Guest on 20th November 2020 05:22:37 AM
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /**
  5.  * @file
  6.  * Drupal shell execution script
  7.  *
  8.  * Check for your PHP interpreter - on Windows you'll probably have to
  9.  * replace line 1 with
  10.  *   #!c:/program files/php/php.exe
  11.  *
  12.  * @param path  Drupal's absolute root directory in local file system (optional).
  13.  * @param URI   A URI to execute, including HTTP protocol prefix.
  14.  */
  15.  
  16. $script = basename(array_shift($_SERVER['argv']));
  17.  
  18. if (in_array('--help', $_SERVER['argv']) || empty($_SERVER['argv'])) {
  19.   echo <<<EOF
  20.  
  21. Execute a Drupal page from the shell.
  22.  
  23. Usage:        {$script} [OPTIONS] "<URI>"
  24. Example:      {$script} "http://mysite.org/node"
  25.  
  26. All arguments are long options.
  27.  
  28.   --help      This page.
  29.  
  30.   --root      Set the working directory for the script to the specified path.
  31.               To execute Drupal this has to be the root directory of your
  32.               Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal
  33.               running on Unix). Current directory is not required.
  34.               Use surrounding quotation marks on Windows.
  35.  
  36.   --verbose   This option displays the options as they are set, but will
  37.               produce errors from setting the session.
  38.  
  39.   URI         The URI to execute, i.e. http://default/foo/bar for executing
  40.               the path '/foo/bar' in your site 'default'. URI has to be
  41.               enclosed by quotation marks if there are ampersands in it
  42.               (f.e. index.php?q=node&foo=bar). Prefix 'http://' is required,
  43.               and the domain must exist in Drupal's sites-directory.
  44.  
  45.               If the given path and file exists it will be executed directly,
  46.               i.e. if URI is set to http://default/bar/foo.php
  47.               and bar/foo.php exists, this script will be executed without
  48.               bootstrapping Drupal. To execute Drupal's update.php, specify
  49.               http://default/update.php as the URI.
  50.  
  51.  
  52. To run this script without --root argument invoke it from the root directory
  53. of your Drupal installation with
  54.  
  55.   ./scripts/{$script}
  56. \n
  57. EOF;
  58.   exit;
  59. }
  60.  
  61. // define default settings
  62. $cmd = 'index.php';
  63. $_SERVER['HTTP_HOST']       = 'default';
  64. $_SERVER['PHP_SELF']        = '/index.php';
  65. $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
  66. $_SERVER['SERVER_SOFTWARE'] = NULL;
  67. $_SERVER['REQUEST_METHOD']  = 'GET';
  68. $_SERVER['QUERY_STRING']    = '';
  69. $_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';
  70. $_SERVER['HTTP_USER_AGENT'] = 'console';
  71.  
  72. // toggle verbose mode
  73. if (in_array('--verbose', $_SERVER['argv'])) {
  74.   $_verbose_mode = TRUE;
  75. }
  76. else {
  77.   $_verbose_mode = FALSE;
  78. }
  79.  
  80. // parse invocation arguments
  81. while ($param = array_shift($_SERVER['argv'])) {
  82.   switch ($param) {
  83.     case '--root':
  84.       // change working directory
  85.       $path = array_shift($_SERVER['argv']);
  86.       if (is_dir($path)) {
  87.         chdir($path);
  88.         if ($_verbose_mode) {
  89.           echo "cwd changed to: {$path}\n";
  90.         }
  91.       }
  92.       else {
  93.         echo "\nERROR: {$path} not found.\n\n";
  94.       }
  95.       break;
  96.  
  97.     default:
  98.       if (substr($param, 0, 2) == '--') {
  99.         // ignore unknown options
  100.         break;
  101.       }
  102.       else {
  103.         // parse the URI
  104.         $path = parse_url($param);
  105.  
  106.         // set site name
  107.         if (isset($path['host'])) {
  108.           $_SERVER['HTTP_HOST'] = $path['host'];
  109.         }
  110.  
  111.         // set query string
  112.         if (isset($path['query'])) {
  113.           $_SERVER['QUERY_STRING'] = $path['query'];
  114.           parse_str($path['query'], $_GET);
  115.           $_REQUEST = $_GET;
  116.         }
  117.  
  118.         // set file to execute or Drupal path (clean URLs enabled)
  119.         if (isset($path['path']) && file_exists(substr($path['path'], 1))) {
  120.           $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = $path['path'];
  121.           $cmd = substr($path['path'], 1);
  122.         }
  123.         elseif (isset($path['path'])) {
  124.           $_SERVER['SCRIPT_NAME'] = '/' . $cmd;
  125.           $_SERVER['REQUEST_URI'] = $path['path'];
  126.         }
  127.  
  128.         // display setup in verbose mode
  129.         if ($_verbose_mode) {
  130.           echo "Hostname set to: {$_SERVER['HTTP_HOST']}\n";
  131.           echo "Script name set to: {$cmd}\n";
  132.           echo "Path set to: {$path['path']}\n";
  133.         }
  134.       }
  135.       break;
  136.   }
  137. }
  138.  
  139. if (file_exists($cmd)) {
  140.   include $cmd;
  141. }
  142. else {
  143.   echo "\nERROR: {$cmd} not found.\n\n";
  144. }
  145. exit();
  146. ?>

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.