[TYPO3-core] RFC: Bug / Feature #5838: Get cli_dispatch.phpsh to work on windows platforms

Martin Kutschker martin.kutschker-n0spam at no5pam-blackbox.net
Sun Feb 17 21:10:56 CET 2008


REMINDER

Problem:
It is not possible to run a cli script using cli_dispatch.phpsh, if the 
server is windows.

Solution:
Improve path handling.

Martin Kutschker schrieb:
> Martin Kutschker schrieb:
>> Andreas Otto schrieb:
>>> Hi Martin,
>>>
>>> Martin Kutschker wrote:
>>>> Please wait a bit, because I'll want to have a closer look a Dmitry's
>>>> getcwd() idea.
>>>
>>> OK.
>>
>> This code works on Windows even with relative paths. Note that 
>> $_SERVER['PWD'] is used because of getcwd() issues on some *nix systems.
>>
>> $relativePath = FALSE;
>> if (stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')) {
>>   if (!preg_match('/^([A-Z]:)?\\/', $temp_PATH_thisScript)) {
>>     $relativePath = TRUE;
>>   }
>> } else {
>>   if ($temp_PATH_thisScript{0} != '/') {
>>     $relativePath = TRUE;
>>   }
>> }
>>
>> if ($relativePath) {
>>   $wd = $_SERVER['PWD'] ? $_SERVER['PWD'] : getcwd();
>>   if ($wd) {
>>     $temp_PATH_thisScript =
>>       $wd.'/'.ereg_replace('\.\/','',$temp_PATH_thisScript);
>>     if (!@is_file($temp_PATH_thisScript)) {
>>       die ('relative path found and an error occured during resolving 
>> of the absolute path. $temp_PATH_thisScript');
>>     }
>>   } else {
>>     die ('relative path found, but resolving absolute path is not 
>> supported on this platform.');
>>   }
>> }
>> define('PATH_thisScript',$temp_PATH_thisScript);
> 
> Here is the change as diff. I have also added a copyright claim and the 
> SVN tag.
> 
> Masi
> 
> 
> ------------------------------------------------------------------------
> 
> Index: typo3/cli_dispatch.phpsh
> ===================================================================
> --- typo3/cli_dispatch.phpsh	(revision 3159)
> +++ typo3/cli_dispatch.phpsh	(working copy)
> @@ -1,12 +1,43 @@
>  #! /usr/bin/php -q
>  <?php
> +/***************************************************************
> +*  Copyright notice
> +*
> +*  (c) 2005-2008 Kasper Skaarhoj (kasperYYYY at typo3.com)
> +*  All rights reserved
> +*
> +*  This script is part of the TYPO3 project. The TYPO3 project is
> +*  free software; you can redistribute it and/or modify
> +*  it under the terms of the GNU General Public License as published by
> +*  the Free Software Foundation; either version 2 of the License, or
> +*  (at your option) any later version.
> +*
> +*  The GNU General Public License can be found at
> +*  http://www.gnu.org/copyleft/gpl.html.
> +*  A copy is found in the textfile GPL.txt and important notices to the license
> +*  from the author is found in LICENSE.txt distributed with these scripts.
> +*
> +*
> +*  This script is distributed in the hope that it will be useful,
> +*  but WITHOUT ANY WARRANTY; without even the implied warranty of
> +*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +*  GNU General Public License for more details.
> +*
> +*  This copyright notice MUST APPEAR in all copies of the script!
> +***************************************************************/
>  
> -// *****************************************
> -// CLI module dispatcher.
> -// This script can take a "cliKey" as first argument and uses that to look up the path of the script to include in the end.
> -// See configuration of this feature in $TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys']
> -// The point is to have only ONE script dealing with the environment initialization while the actual processing is all a developer should care for.
> -// *****************************************
> +/**
> + * Command Line Interface module dispatcher
> + *
> + * $Id: cli_dispatch.phpsh 2665 2007-11-05 19:38:07Z ingmars $
> + *
> + * @author      Kasper Skaarhoj <kasperYYYY at typo3.com>
> + *
> + * This script takes a "cliKey" as first argument and uses that to look up the path of the script to include in the end.
> + * See configuration of this feature in $TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys'].
> + * The point is to have only ONE script dealing with the environment initialization while the actual processing is all a developer should care for.
> + *
> + */
>  
>  if (PHP_SAPI!='cli')	{
>  	die('ERROR: Not called from a command line interface (eg. a shell or scheduler).'.chr(10));
> @@ -15,18 +46,39 @@
>  	// Defining circumstances for CLI mode:
>  define('TYPO3_cliMode', TRUE);
>  
> -	// Defining PATH_thisScript here: Must be the ABSOLUTE path of this script in the right context:
> -	// This will work as long as the script is called by it's absolute path!
> -$temp_PATH_thisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['_'] : $_SERVER['_']);
> +	// Get path to this script
> +$temp_PATH_thisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['
> +_'] : $_SERVER['_']);
>  
> -	// Alternatively, in some environments, we might be able to figure out the absolute path (with no "../" and "./" in) from environment variables...
> -if ($temp_PATH_thisScript{0}!='/')	{
> -	$temp_CURRENT_DIR = $_SERVER['PWD'].'/';
> -	$temp_PATH_thisScript = $temp_CURRENT_DIR.ereg_replace('\.\/','',$temp_PATH_thisScript);
> +	// Figure out if the path is relative
> +$relativePath = FALSE;
> +if (stristr(PHP_OS,'win') && !stristr(PHP_OS,'darwin')) {
> +		// Windows
> +	if (!preg_match('/^([A-Z]:)?\\/', $temp_PATH_thisScript)) {
> +		$relativePath = TRUE;
> +	}
> +} else {
> +		// *nix, et al
> +	if ($temp_PATH_thisScript{0} != '/') {
> +		$relativePath = TRUE;
> +	}
> +}
> +
> +	// Resolve path
> +if ($relativePath) {
> +	$workingDirectory = $_SERVER['PWD'] ? $_SERVER['PWD'] : getcwd();
> +	if ($workingDirectory) {
> +		$temp_PATH_thisScript =
> +			$workingDirectory.'/'.ereg_replace('\.\/','',$temp_PATH_thisScript);
>  	if (!@is_file($temp_PATH_thisScript))	{
> -		die(wordwrap('ERROR: '.$temp_PATH_thisScript.' was not a file. Maybe your environment does not support running this script with a relative path? Try to run the script with its absolute path and you should be fine.'.chr(10).chr(10)));
> +			die ('Relative path found, but an error occured during resolving of the absolute path: '.$temp_PATH_thisScript);
>  	}
> +	} else {
> +		die ('Relative path found, but resolving absolute path is not supported on this platform.');
> +	}
>  }
> +
> +	// Define absolute path to this script
>  define('PATH_thisScript',$temp_PATH_thisScript);
>  
>  	// First argument is a key that points to the script configuration
> @@ -38,7 +90,7 @@
>  if (defined('TYPO3_cliInclude'))	{
>  	include(TYPO3_cliInclude);
>  } else {
> -	echo 'ERROR: Nothing to include.'.chr(10);
> -	exit;
> +	die('No include file configured for key "'.TYPO3_cliKey.'".');
>  }
> -?>
> \ No newline at end of file
> +
> +?>
> 
> 
> ------------------------------------------------------------------------
> 
> Index: typo3/cli_dispatch.phpsh
> ===================================================================
> --- typo3/cli_dispatch.phpsh	(revision 3159)
> +++ typo3/cli_dispatch.phpsh	(working copy)
> @@ -1,32 +1,84 @@
>  #! /usr/bin/php -q
>  <?php
> +/***************************************************************
> +*  Copyright notice
> +*
> +*  (c) 2005-2008 Kasper Skaarhoj (kasperYYYY at typo3.com)
> +*  All rights reserved
> +*
> +*  This script is part of the TYPO3 project. The TYPO3 project is
> +*  free software; you can redistribute it and/or modify
> +*  it under the terms of the GNU General Public License as published by
> +*  the Free Software Foundation; either version 2 of the License, or
> +*  (at your option) any later version.
> +*
> +*  The GNU General Public License can be found at
> +*  http://www.gnu.org/copyleft/gpl.html.
> +*  A copy is found in the textfile GPL.txt and important notices to the license
> +*  from the author is found in LICENSE.txt distributed with these scripts.
> +*
> +*
> +*  This script is distributed in the hope that it will be useful,
> +*  but WITHOUT ANY WARRANTY; without even the implied warranty of
> +*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +*  GNU General Public License for more details.
> +*
> +*  This copyright notice MUST APPEAR in all copies of the script!
> +***************************************************************/
>  
> -// *****************************************
> -// CLI module dispatcher.
> -// This script can take a "cliKey" as first argument and uses that to look up the path of the script to include in the end.
> -// See configuration of this feature in $TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys']
> -// The point is to have only ONE script dealing with the environment initialization while the actual processing is all a developer should care for.
> -// *****************************************
> +/**
> + * Command Line Interface module dispatcher
> + *
> + * $Id: cli_dispatch.phpsh 2665 2007-11-05 19:38:07Z ingmars $
> + *
> + * @author      Kasper Skaarhoj <kasperYYYY at typo3.com>
> + *
> + * This script takes a "cliKey" as first argument and uses that to look up the path of the script to include in the end.
> + * See configuration of this feature in $TYPO3_CONF_VARS['SC_OPTIONS']['GLOBAL']['cliKeys'].
> + * The point is to have only ONE script dealing with the environment initialization while the actual processing is all a developer should care for.
> + *
> + */
>  
> -if (PHP_SAPI!='cli')	{
> +if (PHP_SAPI!='cli') {
>  	die('ERROR: Not called from a command line interface (eg. a shell or scheduler).'.chr(10));
>  }
>  
>  	// Defining circumstances for CLI mode:
>  define('TYPO3_cliMode', TRUE);
>  
> -	// Defining PATH_thisScript here: Must be the ABSOLUTE path of this script in the right context:
> -	// This will work as long as the script is called by it's absolute path!
> -$temp_PATH_thisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['_'] : $_SERVER['_']);
> +	// Get path to this script
> +$temp_PATH_thisScript = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : (isset($_ENV['_']) ? $_ENV['
> +_'] : $_SERVER['_']);
>  
> -	// Alternatively, in some environments, we might be able to figure out the absolute path (with no "../" and "./" in) from environment variables...
> -if ($temp_PATH_thisScript{0}!='/')	{
> -	$temp_CURRENT_DIR = $_SERVER['PWD'].'/';
> -	$temp_PATH_thisScript = $temp_CURRENT_DIR.ereg_replace('\.\/','',$temp_PATH_thisScript);
> -	if (!@is_file($temp_PATH_thisScript))	{
> -		die(wordwrap('ERROR: '.$temp_PATH_thisScript.' was not a file. Maybe your environment does not support running this script with a relative path? Try to run the script with its absolute path and you should be fine.'.chr(10).chr(10)));
> +	// Figure out if the path is relative
> +$relativePath = FALSE;
> +if (stristr(PHP_OS,'win') && !stristr(PHP_OS,'darwin')) {
> +		// Windows
> +	if (!preg_match('/^([A-Z]:)?\\/', $temp_PATH_thisScript)) {
> +		$relativePath = TRUE;
>  	}
> +} else {
> +		// *nix, et al
> +	if ($temp_PATH_thisScript{0} != '/') {
> +		$relativePath = TRUE;
> +	}
>  }
> +
> +	// Resolve path
> +if ($relativePath) {
> +	$workingDirectory = $_SERVER['PWD'] ? $_SERVER['PWD'] : getcwd();
> +	if ($workingDirectory) {
> +		$temp_PATH_thisScript =
> +			$workingDirectory.'/'.ereg_replace('\.\/','',$temp_PATH_thisScript);
> +		if (!@is_file($temp_PATH_thisScript)) {
> +			die ('Relative path found, but an error occured during resolving of the absolute path: '.$temp_PATH_thisScript);
> +		}
> +	} else {
> +		die ('Relative path found, but resolving absolute path is not supported on this platform.');
> +	}
> +}
> +
> +	// Define absolute path to this script
>  define('PATH_thisScript',$temp_PATH_thisScript);
>  
>  	// First argument is a key that points to the script configuration
> @@ -35,10 +87,10 @@
>  	// Include init file:
>  require(dirname(PATH_thisScript).'/init.php');
>  
> -if (defined('TYPO3_cliInclude'))	{
> +if (defined('TYPO3_cliInclude')) {
>  	include(TYPO3_cliInclude);
>  } else {
> -	echo 'ERROR: Nothing to include.'.chr(10);
> -	exit;
> +	die('No include file configured for key "'.TYPO3_cliKey.'".');
>  }
> -?>
> \ No newline at end of file
> +
> +?>


More information about the TYPO3-team-core mailing list