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