3 /***************************************************************
6 * (c) 2005-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
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.
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.
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.
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
30 * Command Line Interface module dispatcher
34 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
36 * This script takes a "cliKey" as first argument and uses that to look up the path of the script to include in the end.
37 * See configuration of this feature in $TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys'].
38 * The point is to have only ONE script dealing with the environment initialization while the actual processing is all a developer should care for.
42 if (substr(php_sapi_name(), 0, 3) == 'cgi') {
43 // sanity check: ensure that we're running in a shell or cronjob (and NOT via HTTP)
44 $checkEnvVars = array('HTTP_USER_AGENT', 'HTTP_HOST', 'SERVER_NAME', 'REMOTE_ADDR', 'REMOTE_PORT', 'SERVER_PROTOCOL');
45 foreach ($checkEnvVars as $var) {
46 if (array_key_exists($var, $_SERVER)) {
47 echo 'SECURITY CHECK FAILD! This script cannot be used within your browser!' . chr(10);
48 echo 'If you are sure that we run in a shell or cronjob, please unset' . chr(10);
49 echo 'environment variable ' . $var . ' (usually using \'unset ' . $var . '\')' . chr(10);
50 echo 'before starting this script.' . chr(10);
56 // mimic CLI API in CGI API (you must use the -C/-no-chdir and the -q/--no-header switches!)
57 ini_set('html_errors', 0);
58 ini_set('implicit_flush', 1);
59 ini_set('max_execution_time', 0);
60 if (!ini_get('register_argc_argv')) {
61 $argv = $_SERVER['argv'];
62 $argc = $_SERVER['argc'];
64 define(STDIN, fopen('php://stdin', 'r'));
65 define(STDOUT, fopen('php://stdout', 'w'));
66 define(STDERR, fopen('php://stderr', 'w'));
67 } elseif (php_sapi_name() != 'cli') {
68 die('Not called from a command line interface (eg. a shell or scheduler).'.chr(10));
71 // Defining circumstances for CLI mode:
72 define('TYPO3_cliMode', TRUE);
74 // Get path to this script
75 $temp_PATH_thisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['_'] : $_SERVER['_']);
77 // Figure out if the path is relative
78 $relativePath = FALSE;
79 if (stristr(PHP_OS,'win') && !stristr(PHP_OS,'darwin')) {
81 if (!preg_match('/^([A-Z]:)?\\\/', $temp_PATH_thisScript)) {
86 if ($temp_PATH_thisScript{0} != '/') {
93 $workingDirectory = $_SERVER['PWD'] ? $_SERVER['PWD'] : getcwd();
94 if ($workingDirectory) {
95 $temp_PATH_thisScript =
96 $workingDirectory.'/'.preg_replace('/\.\//','',$temp_PATH_thisScript);
97 if (!@is_file($temp_PATH_thisScript)) {
98 die ('Relative path found, but an error occured during resolving of the absolute path: '.$temp_PATH_thisScript.chr(10));
101 die ('Relative path found, but resolving absolute path is not supported on this platform.'.chr(10));
105 // Define absolute path to this script
106 define('PATH_thisScript',$temp_PATH_thisScript);
108 if (!isset($_SERVER['argv'][1])) {
109 die ('The first argument must be a valid key.'.chr(10));
112 // First argument is a key that points to the script configuration
113 define('TYPO3_cliKey', $_SERVER['argv'][1]);
115 // Include init file:
116 require(dirname(PATH_thisScript).'/init.php');
118 // Make sure output is not buffered,
119 // so that command-line output and interaction can take place
120 t3lib_div::flushOutputBuffers();
122 if (defined('TYPO3_cliInclude')) {
123 include(TYPO3_cliInclude);
125 die('No include file configured for key "'.TYPO3_cliKey.'".'.chr(10));