cleanup and added a function to get the bin paths
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_exec.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2002-2005 René Fritz (r.fritz@colorcube.de)
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 /**
25 * t3lib_exec finds executables (programs) on Unix and Windows without knowing where they are
26 *
27 * $Id$
28 *
29 * @author René Fritz <r.fritz@colorcube.de>
30 */
31 /**
32 * [CLASS/FUNCTION INDEX of SCRIPT]
33 *
34 *
35 *
36 * 81: class t3lib_exec
37 * 91: function checkCommand($cmd, $handler='')
38 * 162: function getCommand($cmd, $handler='', $handlerOpt='')
39 * 191: function addPaths($paths)
40 * 201: function _getPaths()
41 * 269: function _init()
42 * 285: function _initPaths($paths='')
43 * 340: function _getOS()
44 * 351: function _fixPath($path)
45 *
46 * TOTAL FUNCTIONS: 8
47 * (This index is automatically created/updated by the extension "extdeveval")
48 *
49 */
50
51
52
53
54
55 /**
56 * returns exec command for a program
57 * or false
58 *
59 * This class is meant to be used without instance:
60 * $cmd = t3lib_exec::getCommand ('awstats','perl');
61 *
62 * The data of this class is hold in a global variable. Doing it this way the setup is cached.
63 * That means if a program is found once it don't have to be searched again.
64 *
65 * user functions:
66 *
67 * addPaths() could be used to extend the search paths
68 * getCommand() get a command string
69 * checkCommand() returns true if a command is available
70 *
71 * Search paths that are included:
72 * $TYPO3_CONF_VARS['GFX']['im_path_lzw'] or $TYPO3_CONF_VARS['GFX']['im_path']
73 * $TYPO3_CONF_VARS['SYS']['binPath']
74 * $GLOBALS['_SERVER']['PATH']
75 * '/usr/bin/,/usr/local/bin/' on Unix
76 *
77 * @author René Fritz <r.fritz@colorcube.de>
78 * @package TYPO3
79 * @subpackage t3lib
80 */
81 class t3lib_exec {
82
83 /**
84 * Checks if a command is valid or not
85 * updates global vars
86 *
87 * @param string the command that should be executed. eg: "convert"
88 * @param string executer for the command. eg: "perl"
89 * @return boolean false if cmd is not found, or -1 if the handler is not found
90 */
91 function checkCommand($cmd, $handler='') {
92 global $T3_VAR;
93
94 if (!t3lib_exec::_init()) {
95 return false;
96 }
97
98 $osType = t3lib_exec::_getOS();
99
100
101 if ($handler && !t3lib_exec::checkCommand($handler)) {
102 return -1;
103 }
104 // already checked and valid
105 if ($T3_VAR['t3lib_exec']['apps'][$cmd]['valid']) {
106 return true;
107 }
108 // is set but was (above) not true
109 if (isset($T3_VAR['t3lib_exec']['apps'][$cmd]['valid'])) {
110 return false;
111 }
112
113 foreach($T3_VAR['t3lib_exec']['paths'] as $path => $validPath) {
114 // ignore invalid (false) paths
115 if ($validPath) {
116 if ($osType=='WIN') {
117 if (@is_file($path.$cmd)) {
118 $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd;
119 $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path;
120 $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
121 return true;
122 }
123 if (@is_file($path.$cmd.'.exe')) {
124 $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd.'.exe';
125 $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path;
126 $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
127 return true;
128 }
129 } else { // UNIX
130 if (@is_executable($path.$cmd)) {
131 $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd;
132 $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = $path;
133 $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
134 return true;
135 }
136 }
137 }
138 }
139
140 // try to get the executable with the command 'which'. It do the same like already done, but maybe on other paths??
141 if ($osType=='UNIX') {
142 $cmd = @exec ('which '.$val['cmd']);
143
144 if (@is_executable($cmd)) {
145 $T3_VAR['t3lib_exec']['apps'][$cmd]['app'] = $cmd;
146 $T3_VAR['t3lib_exec']['apps'][$cmd]['path'] = dirname($cmd).'/';
147 $T3_VAR['t3lib_exec']['apps'][$cmd]['valid'] = true;
148 return true;
149 }
150 }
151
152 return false;
153 }
154
155
156 /**
157 * Returns a command string for exec(), system()
158 *
159 * @param string the command that should be executed. eg: "convert"
160 * @param string handler (executor) for the command. eg: "perl"
161 * @param string options for the handler, like '-w' for "perl"
162 * @return mixed returns command string, or false if cmd is not found, or -1 if the handler is not found
163 */
164 function getCommand($cmd, $handler='', $handlerOpt='') {
165 global $T3_VAR;
166
167 if (!t3lib_exec::_init()) {
168 return false;
169 }
170
171 // handler
172 if ($handler) {
173 $handler = t3lib_exec::getCommand($handler);
174
175 if (!$handler) {
176 return -1;
177 }
178 $handler .= ' '.$handlerOpt.' ';
179 }
180
181 // command
182 if (!t3lib_exec::checkCommand($cmd)) {
183 return false;
184 }
185 $cmd = $T3_VAR['t3lib_exec']['apps'][$cmd]['path'].$T3_VAR['t3lib_exec']['apps'][$cmd]['app'].' ';
186
187 return $handler.$cmd;
188 }
189
190
191 /**
192 * Extend the preset paths. This way an extension can install an executable and provide the path to t3lib_exec.
193 *
194 * @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).
195 * @return void
196 */
197 function addPaths($paths) {
198 t3lib_exec::_initPaths($paths);
199 }
200
201
202
203 /**
204 * Returns an array of search paths
205 *
206 * @param boolean If set the array contains invalid path too. Then the key is the path and the value is empty
207 * @return array Array of search paths (empty if exec is disabled)
208 */
209 function getPaths($addInvalid=false) {
210 global $T3_VAR;
211
212 if (!t3lib_exec::_init()) {
213 return array();
214 }
215
216 $paths = $T3_VAR['t3lib_exec']['paths'];
217 if(!$addInvalid) {
218
219 foreach($paths as $path => $validPath) {
220 if(!$validPath) {
221 unset($paths);
222 }
223 }
224 }
225 return $paths;
226 }
227
228
229 /**
230 * Set the search paths from different sources, internal
231 *
232 * @return array Array of absolute paths (keys and values are equal)
233 * @internal
234 */
235 function _getPaths() {
236 global $T3_VAR, $TYPO3_CONF_VARS;
237
238 $pathsArr = array();
239 $sysPathArr = array();
240 $osType = t3lib_exec::_getOS();
241
242 // image magick paths first
243 // im_path_lzw take precedence over im_path
244 if ($imPath = ($TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path'])) {
245 $imPath = t3lib_exec::_fixPath($imPath);
246 $pathsArr[$imPath] = $imPath;
247 }
248
249 // add configured paths
250 if ($TYPO3_CONF_VARS['SYS']['binPath']) {
251 $sysPath = t3lib_div::trimExplode(',',$TYPO3_CONF_VARS['SYS']['binPath'],1);
252 foreach($sysPath as $val) {
253 $val = t3lib_exec::_fixPath($val);
254 $sysPathArr[$val]=$val;
255 }
256 }
257
258
259 // add path from environment
260 #TODO: how does this work for WIN
261 if ($GLOBALS['_SERVER']['PATH']) {
262 $sep = ($osType=='WIN') ? ';' : ':';
263 $envPath = t3lib_div::trimExplode($sep,$GLOBALS['_SERVER']['PATH'],1);
264 foreach($envPath as $val) {
265 $val = t3lib_exec::_fixPath($val);
266 $sysPathArr[$val]=$val;
267 }
268 }
269
270 if ($osType=='WIN') {
271 #TODO: add the most common paths for WIN
272 $sysPathArr = array_merge($sysPathArr, array (
273 '/usr/bin/' => '/usr/bin/',
274 '/perl/bin/' => '/perl/bin/',
275 ));
276 } else { // UNIX
277 $sysPathArr = array_merge($sysPathArr, array (
278 '/usr/bin/' => '/usr/bin/',
279 '/usr/local/bin/' => '/usr/local/bin/',
280 ));
281 }
282
283
284 $pathsArr = array_merge($pathsArr, $sysPathArr);
285
286 return $pathsArr;
287 }
288
289
290 /**
291 * Initialization, internal
292 *
293 * @return void
294 * @internal
295 */
296 function _init() {
297 global $T3_VAR, $TYPO3_CONF_VARS;
298
299 if ($TYPO3_CONF_VARS['BE']['disable_exec_function']) {
300 return false;
301 }
302 if (!$T3_VAR['t3lib_exec']['init']) {
303 t3lib_exec::_initPaths();
304 $T3_VAR['t3lib_exec']['apps'] = array();
305 $T3_VAR['t3lib_exec']['init'] = true;
306 }
307 return true;
308 }
309
310
311 /**
312 * Init and extend the preset paths with own
313 *
314 * @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).
315 * @return void
316 * @internal
317 */
318 function _initPaths($paths='') {
319 global $T3_VAR;
320
321 $doCeck=false;
322
323 // init global paths array if not already done
324 if (!is_array($T3_VAR['t3lib_exec']['paths'])) {
325 $T3_VAR['t3lib_exec']['paths'] = t3lib_exec::_getPaths();
326 $doCeck=true;
327 }
328 // merge the submitted paths array to the global
329 if ($paths) {
330 $paths = t3lib_div::trimExplode(',',$paths,1);
331 if (is_array($paths)) {
332 foreach($paths as $path) {
333 // make absolute path of relative
334 if (!preg_match('#^/#',$path)) {
335 $path = PATH_site.$path;
336 }
337 if (!isset($T3_VAR['t3lib_exec']['paths'][$path])) {
338 if (@is_dir($path)) {
339 $T3_VAR['t3lib_exec']['paths'][$path] = $path;
340 $T3_VAR['t3lib_exec']['allPaths'].=','.$path;
341 // $doCeck=true; just done
342 } else {
343 $T3_VAR['t3lib_exec']['paths'][$path] = false;
344 }
345 }
346 }
347 }
348 }
349 // check if new paths are invalid
350 if ($doCeck) {
351 $T3_VAR['t3lib_exec']['allPaths']='';
352 foreach($T3_VAR['t3lib_exec']['paths'] as $path => $valid) {
353 // ignore invalid (false) paths
354 if ($valid AND !@is_dir($path)) {
355 $T3_VAR['t3lib_exec']['paths'][$path] = false;
356 }
357 if ($path = $T3_VAR['t3lib_exec']['paths'][$path]) {
358 $T3_VAR['t3lib_exec']['allPaths'].=','.$path;
359 }
360 }
361 }
362 }
363
364
365 /**
366 * Returns on which OS we're runing
367 *
368 * @return string the OS type: "UNIX" or "WIN"
369 * @internal
370 */
371 function _getOS() {
372 return stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'UNIX';
373 }
374
375
376 /**
377 * Set a path to the right format
378 *
379 * @param string Input path
380 * @return string Output path
381 * @internal
382 */
383 function _fixPath($path) {
384 return str_replace ('//','/',$path.'/');
385 }
386 }
387
388 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']) {
389 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_exec.php']);
390 }
391 ?>