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