File: class.t3lib_exec.php

package info (click to toggle)
typo3-src 4.0.2%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 29,856 kB
  • ctags: 33,382
  • sloc: php: 134,523; xml: 6,976; sql: 1,084; sh: 168; makefile: 45
file content (408 lines) | stat: -rw-r--r-- 11,167 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2002-2006 Ren Fritz (r.fritz@colorcube.de)
*  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.
*
*  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!
***************************************************************/
/**
 * t3lib_exec finds executables (programs) on Unix and Windows without knowing where they are
 *
 * $Id: class.t3lib_exec.php 1421 2006-04-10 09:27:15Z mundaun $
 *
 * @author	Ren Fritz <r.fritz@colorcube.de>
 */
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 *
 *
 *   85: class t3lib_exec
 *   95:     function checkCommand($cmd, $handler='')
 *  166:     function getCommand($cmd, $handler='', $handlerOpt='')
 *  199:     function addPaths($paths)
 *  211:     function getPaths($addInvalid=false)
 *  237:     function _init()
 *  259:     function _initPaths($paths='')
 *  312:     function _getConfiguredApps()
 *  339:     function _getPaths()
 *  400:     function _fixPath($path)
 *
 * TOTAL FUNCTIONS: 9
 * (This index is automatically created/updated by the extension "extdeveval")
 *
 */





/**
 * returns exec command for a program
 * or false
 *
 * This class is meant to be used without instance:
 * $cmd = t3lib_exec::getCommand ('awstats','perl');
 *
 * The data of this class is hold in a global variable. Doing it this way the setup is cached.
 * That means if a program is found once it don't have to be searched again.
 *
 * user functions:
 *
 * addPaths() could be used to extend the search paths
 * getCommand() get a command string
 * checkCommand() returns true if a command is available
 *
 * Search paths that are included:
 * $TYPO3_CONF_VARS['GFX']['im_path_lzw'] or $TYPO3_CONF_VARS['GFX']['im_path']
 * $TYPO3_CONF_VARS['SYS']['binPath']
 * $GLOBALS['_SERVER']['PATH']
 * '/usr/bin/,/usr/local/bin/' on Unix
 *
 * binaries can be preconfigured with
 * $TYPO3_CONF_VARS['SYS']['binSetup']
 *
 * @author	Ren Fritz <r.fritz@colorcube.de>
 * @package TYPO3
 * @subpackage t3lib
 */
class t3lib_exec {

	/**
	 * Checks if a command is valid or not
	 * updates global vars
	 *
	 * @param	string		the command that should be executed. eg: "convert"
	 * @param	string		executer for the command. eg: "perl"
	 * @return	boolean		false if cmd is not found, or -1 if the handler is not found
	 */
	function checkCommand($cmd, $handler='')	{
		global $T3_VAR;

		if (!t3lib_exec::_init()) {
			return false;
		}


		if ($handler && !t3lib_exec::checkCommand($handler)) {
			return -1;
		}
			// already checked and valid
		if ($T3_VAR['t3lib_exec']['apps'][$cmd]['valid']) {
			return true;
		}
			// is set but was (above) not true
		if (isset($T3_VAR['t3lib_exec']['apps'][$cmd]['valid'])) {
			return false;
		}

		foreach($T3_VAR['t3lib_exec']['paths'] as $path => $validPath) {
				// ignore invalid (false) paths
			if ($validPath) {
				if (TYPO3_OS=='WIN') {
					if (@is_file($path.$cmd)) {
						$T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd;
						$T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path;
						$T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
						return true;
					}
					if (@is_file($path.$cmd.'.exe')) {
						$T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd.'.exe';
						$T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path;
						$T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
						return true;
					}
				} else { // UNIX
					if (@is_executable($path.$cmd)) {
						$T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd;
						$T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path;
						$T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
						return true;
					}
				}
			}
		}

			// try to get the executable with the command 'which'. It do the same like already done, but maybe on other paths??
		if (TYPO3_OS!='WIN') {
			$cmd = @exec ('which '.$cmd);

			if (@is_executable($cmd)) {
				$T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd;
				$T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = dirname($cmd).'/';
				$T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
				return true;
			}
		}

		return false;
	}


	/**
	 * Returns a command string for exec(), system()
	 *
	 * @param	string		the command that should be executed. eg: "convert"
	 * @param	string		handler (executor) for the command. eg: "perl"
	 * @param	string		options for the handler, like '-w' for "perl"
	 * @return	mixed		returns command string, or false if cmd is not found, or -1 if the handler is not found
	 */
	function getCommand($cmd, $handler='', $handlerOpt='')	{
		global $T3_VAR;

		if (!t3lib_exec::_init()) {
			return false;
		}

			// handler
		if ($handler) {
			$handler = t3lib_exec::getCommand($handler);

			if (!$handler) {
				return -1;
			}
			$handler .= ' '.$handlerOpt.' ';
		}

			// command
		if (!t3lib_exec::checkCommand($cmd)) {
			return false;
		}
		$cmd = $T3_VAR['t3lib_exec']['apps'][$cmd]['path'].$T3_VAR['t3lib_exec']['apps'][$cmd]['app'].' ';

		return trim($handler.$cmd);
	}


	/**
	 * Extend the preset paths. This way an extension can install an executable and provide the path to t3lib_exec.
	 *
	 * @param	string		comma seperated list of extra paths where a command should be searched. Relative paths (without leading "/") are prepend with site root path (PATH_site).
	 * @return	void
	 */
	function addPaths($paths)	{
		t3lib_exec::_initPaths($paths);
	}



	/**
	 * Returns an array of search paths
	 *
	 * @param	boolean		If set the array contains invalid path too. Then the key is the path and the value is empty
	 * @return	array		Array of search paths (empty if exec is disabled)
	 */
	function getPaths($addInvalid=false)	{
		global $T3_VAR;

		if (!t3lib_exec::_init()) {
			return array();
		}

		$paths = $T3_VAR['t3lib_exec']['paths'];
		if(!$addInvalid) {

			foreach($paths as $path => $validPath) {
				if(!$validPath) {
					unset($paths);
				}
			}
		}
		return $paths;
	}


	/**
	 * Initialization, internal
	 *
	 * @return	void
	 * @internal
	 */
	function _init()	{
		global $T3_VAR, $TYPO3_CONF_VARS;

		if ($TYPO3_CONF_VARS['BE']['disable_exec_function']) {
			return false;
		}
		if (!$T3_VAR['t3lib_exec']['init']) {
			t3lib_exec::_initPaths();
			$T3_VAR['t3lib_exec']['apps'] = t3lib_exec::_getConfiguredApps();;
			$T3_VAR['t3lib_exec']['init'] = true;
		}
		return true;
	}


	/**
	 * Init and extend the preset paths with own
	 *
	 * @param	string		Comma seperated list of extra paths where a command should be searched. Relative paths (without leading "/") are prepend with site root path (PATH_site).
	 * @return	void
	 * @internal
	 */
	function _initPaths($paths='')	{
		global $T3_VAR;

		$doCeck=false;

			// init global paths array if not already done
		if (!is_array($T3_VAR['t3lib_exec']['paths'])) {
			$T3_VAR['t3lib_exec']['paths'] = t3lib_exec::_getPaths();
			$doCeck=true;
		}
			// merge the submitted paths array to the global
		if ($paths) {
			$paths = t3lib_div::trimExplode(',',$paths,1);
			if (is_array($paths)) {
				foreach($paths as $path) {
						// make absolute path of relative
					if (!preg_match('#^/#',$path)) {
						$path = PATH_site.$path;
					}
					if (!isset($T3_VAR['t3lib_exec']['paths'][$path])) {
						if (@is_dir($path)) {
							$T3_VAR['t3lib_exec']['paths'][$path] = $path;
							$T3_VAR['t3lib_exec']['allPaths'].=','.$path;
							// $doCeck=true; just done
						} else {
							$T3_VAR['t3lib_exec']['paths'][$path] = false;
						}
					}
				}
			}
		}
			// check if new paths are invalid
		if ($doCeck) {
			$T3_VAR['t3lib_exec']['allPaths']='';
			foreach($T3_VAR['t3lib_exec']['paths'] as $path => $valid) {
					// ignore invalid (false) paths
				if ($valid AND !@is_dir($path)) {
					$T3_VAR['t3lib_exec']['paths'][$path] = false;
				}
				if ($path = $T3_VAR['t3lib_exec']['paths'][$path]) {
					$T3_VAR['t3lib_exec']['allPaths'].=','.$path;
				}
			}
		}
	}


	/**
	 * Processes and returns the paths from $TYPO3_CONF_VARS['SYS']['binSetup']
	 *
	 * @return	array		Array of commands and path
	 * @internal
	 */
	function _getConfiguredApps()	{
		global $TYPO3_CONF_VARS;

		$cmdArr = array();

		if ($TYPO3_CONF_VARS['SYS']['binSetup']) {
			$pathSetup = implode("\n", t3lib_div::trimExplode(',',$TYPO3_CONF_VARS['SYS']['binSetup'],1));
			$pathSetup = t3lib_div::trimExplode("\n",$pathSetup,1);
			foreach($pathSetup as $val) {
				list($cmd, $cmdPath) = t3lib_div::trimExplode('=',$val,1);

				$cmdArr[$cmd]['app'] = basename($cmdPath);
				$cmdArr[$cmd]['path'] = dirname($cmdPath).'/';
				$cmdArr[$cmd]['valid'] = true;
			}
		}

		return $cmdArr;
	}


	/**
	 * Set the search paths from different sources, internal
	 *
	 * @return	array		Array of absolute paths (keys and values are equal)
	 * @internal
	 */
	function _getPaths()	{
		global $T3_VAR, $TYPO3_CONF_VARS;

		$pathsArr = array();
		$sysPathArr = array();

			// image magick paths first
			// im_path_lzw take precedence over im_path
		if ($imPath = ($TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path'])) {
			$imPath = t3lib_exec::_fixPath($imPath);
			$pathsArr[$imPath] = $imPath;
		}

			// add configured paths
		if ($TYPO3_CONF_VARS['SYS']['binPath']) {
			$sysPath = t3lib_div::trimExplode(',',$TYPO3_CONF_VARS['SYS']['binPath'],1);
			foreach($sysPath as $val) {
				$val = t3lib_exec::_fixPath($val);
				$sysPathArr[$val]=$val;
			}
		}


			// add path from environment
// TODO: how does this work for WIN
		if ($GLOBALS['_SERVER']['PATH']) {
			$sep = (TYPO3_OS=='WIN') ? ';' : ':';
			$envPath = t3lib_div::trimExplode($sep,$GLOBALS['_SERVER']['PATH'],1);
			foreach($envPath as $val) {
				$val = t3lib_exec::_fixPath($val);
				$sysPathArr[$val]=$val;
			}
		}

		if (TYPO3_OS=='WIN') {
// TODO: add the most common paths for WIN
			$sysPathArr = array_merge($sysPathArr, array (
				'/usr/bin/' => '/usr/bin/',
				'/perl/bin/' => '/perl/bin/',
			));
		} else { // UNIX
			$sysPathArr = array_merge($sysPathArr, array (
				'/usr/bin/' => '/usr/bin/',
				'/usr/local/bin/' => '/usr/local/bin/',
			));
		}


		$pathsArr = array_merge($pathsArr, $sysPathArr);

		return $pathsArr;
	}


	/**
	 * Set a path to the right format
	 *
	 * @param	string		Input path
	 * @return	string		Output path
	 * @internal
	 */
	function _fixPath($path)	{
		return str_replace ('//','/',$path.'/');
	}
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']);
}
?>