[FEATURE] Set exit status in CLI scripts
[Packages/TYPO3.CMS.git] / typo3 / cli_dispatch.phpsh
1 #! /usr/bin/env php
2 <?php
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2005-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Command Line Interface module dispatcher
31 *
32 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
33 *
34 * This script takes a "cliKey" as first argument and uses that to look up the path of the script to include in the end.
35 * See configuration of this feature in $TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys'].
36 * The point is to have only ONE script dealing with the environment initialization while the actual processing is all a developer should care for.
37 *
38 */
39
40 if (substr(php_sapi_name(), 0, 3) == 'cgi') {
41 // sanity check: ensure that we're running in a shell or cronjob (and NOT via HTTP)
42 $checkEnvVars = array('HTTP_USER_AGENT', 'HTTP_HOST', 'SERVER_NAME', 'REMOTE_ADDR', 'REMOTE_PORT', 'SERVER_PROTOCOL');
43 foreach ($checkEnvVars as $var) {
44 if (array_key_exists($var, $_SERVER)) {
45 echo 'SECURITY CHECK FAILED! This script cannot be used within your browser!' . chr(10);
46 echo 'If you are sure that we run in a shell or cronjob, please unset' . chr(10);
47 echo 'environment variable ' . $var . ' (usually using \'unset ' . $var . '\')' . chr(10);
48 echo 'before starting this script.' . chr(10);
49 exit;
50 }
51 }
52 unset($checkEnvVars);
53
54 // mimic CLI API in CGI API (you must use the -C/-no-chdir and the -q/--no-header switches!)
55 ini_set('html_errors', 0);
56 ini_set('implicit_flush', 1);
57 ini_set('max_execution_time', 0);
58 if (!ini_get('register_argc_argv')) {
59 $argv = $_SERVER['argv'];
60 $argc = $_SERVER['argc'];
61 }
62 define(STDIN, fopen('php://stdin', 'r'));
63 define(STDOUT, fopen('php://stdout', 'w'));
64 define(STDERR, fopen('php://stderr', 'w'));
65 } elseif (php_sapi_name() != 'cli') {
66 die('Not called from a command line interface (e.g. a shell or scheduler).' . chr(10));
67 }
68
69 // Defining circumstances for CLI mode:
70 define('TYPO3_cliMode', TRUE);
71
72 // Get path to this script
73 $temp_PATH_thisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['_'] : $_SERVER['_']);
74
75 // Figure out if the path is relative
76 $relativePath = FALSE;
77 if (stristr(PHP_OS,'win') && !stristr(PHP_OS,'darwin')) {
78 // Windows
79 if (!preg_match('/^([A-Z]:)?\\\/', $temp_PATH_thisScript)) {
80 $relativePath = TRUE;
81 }
82 } else {
83 // *nix, et al
84 if ($temp_PATH_thisScript{0} != '/') {
85 $relativePath = TRUE;
86 }
87 }
88
89 // Resolve path
90 if ($relativePath) {
91 $workingDirectory = $_SERVER['PWD'] ? $_SERVER['PWD'] : getcwd();
92 if ($workingDirectory) {
93 $temp_PATH_thisScript =
94 $workingDirectory.'/'.preg_replace('/\.\//','',$temp_PATH_thisScript);
95 if (!@is_file($temp_PATH_thisScript)) {
96 fwrite(STDERR, 'Relative path found, but an error occurred while resolving the absolute path: ' . $temp_PATH_thisScript . chr(10));
97 exit(1);
98 }
99 } else {
100 fwrite(STDERR, 'Relative path found, but resolving absolute path is not supported on this platform.' . chr(10));
101 exit(1);
102 }
103 }
104
105 // Define absolute path to this script
106 define('PATH_thisScript',$temp_PATH_thisScript);
107
108 if (!isset($_SERVER['argv'][1])) {
109 fwrite(STDERR, 'The first argument must be a valid key.' . chr(10));
110 exit(1);
111 }
112
113 // First argument is a key that points to the script configuration
114 define('TYPO3_cliKey', $_SERVER['argv'][1]);
115
116 // Include init file:
117 require(dirname(PATH_thisScript).'/init.php');
118
119 // Make sure output is not buffered,
120 // so that command-line output and interaction can take place
121 t3lib_div::flushOutputBuffers();
122
123 if (defined('TYPO3_cliInclude')) {
124 try {
125 include(TYPO3_cliInclude);
126 } catch (Exception $e) {
127 fwrite(STDERR, $e->getMessage() . LF);
128 exit(99);
129 }
130 } else {
131 fwrite(STDERR, 'No include file configured for key "'.TYPO3_cliKey.'".' . LF);
132 exit(1);
133 }
134
135 ?>