#!/usr/bin/php " Example: {$script} "http://mysite.org/node" All arguments are long options. --help This page. --root Set the working directory for the script to the specified path, which has to be the root directory of your Drupal installation, f.e. /home/www/foo/drupal (assuming Drupal running on Unix). Use surrounding quotation marks on Windows. Current directory is not required. --verbose This option will display the options as they are set. However produces errors from setting the session. URI The URI to execute, f.e. http://default/foo/bar for executing the path '/foo/bar' in your site 'default'. Prefix 'http://' is required. Domain must exist below /sites. Enter '/cron.php' for executing cron.php (i.e. http://default/cron.php). URI has to be enclosured by quotation marks if there are ampersands in it (f.e. script.php?q=node&foo=bar). \n EOF; exit; } // define default settings $cmd = 'index.php'; $_SERVER['HTTP_HOST'] = 'default'; $_SERVER['PHP_SELF'] = '/index.php'; // toggle verbose mode if (in_array('--verbose', $_SERVER['argv'])) { $_verbose_mode = true; } else { $_verbose_mode = false; } // parse invocation arguments while ($param = array_shift($_SERVER['argv'])) { switch ($param) { case '--root': // change working directory $path = array_shift($_SERVER['argv']); if (is_dir($path)) { chdir($path); echo "cwd changed to: {$path}\n"; } else { echo "\nERROR: {$path} not found.\n\n"; } break; default: if (substr($param, 0, 2) == '--') { // ignore unknown options } else { // parse the URI // disregarding 'http://', it's only for usability preg_match('@^(?:http://)?([^/]+)(.+)@i', $param, $matches); // site name $_SERVER['HTTP_HOST'] = $matches[1]; if ($_verbose_mode) { echo "Site set to: {$_SERVER['HTTP_HOST']}\n"; } // custom script or file invocation if (preg_match('@^/[^\?]+@', $matches[2], $file)) { // alter invocation if URI contains a custom file // remove first slash, since we're already in cwd $cmd = $_SERVER['PHP_SELF'] = substr( $file[0], 1); if ($_verbose_mode) { echo "Script name set to: {$cmd}\n"; } } else if (file_exists( substr($matches[2], 1) )) { // test to invoke a real file // remove first slash, since we're already in cwd $cmd = $_SERVER['PHP_SELF'] = substr($matches[2], 1); if ($_verbose_mode) { echo "Script name set to: {$cmd}\n"; } } // path (clean urls) // parse only if it is not passed through GET if (!isset($_GET['q'])) { if (strpos( $matches[2], 'q=' )) { // try to match a path in a custom script preg_match('@q=([^\&]+)@', $matches[2], $path); if (substr( $path[1], 0 != '/')) { $path[1] = '/'. $path[1]; } $_REQUEST['q'] = $_GET['q'] = $path[1]; } else { // assume clean url path $_REQUEST['q'] = $_GET['q'] = $matches[2]; } if ($_verbose_mode) { echo "Path set to: {$_GET['q']}\n"; } } } } } if (file_exists( $cmd )) { include $cmd; } else { echo "\nERROR: {$cmd} not found.\n\n"; } exit();