source: trunk/library/PEAR/scripts/pearcmd.php @ 7730

Revision 7730, 14.1 KB checked in by marcieli, 11 years ago (diff)

Ticket #3236 - Correcoes de seguranca com base no segundo relatorio emitido.

Line 
1<?php
2/**
3 * PEAR, the PHP Extension and Application Repository
4 *
5 * Command line interface
6 *
7 * PHP versions 4 and 5
8 *
9 * @category   pear
10 * @package    PEAR
11 * @author     Stig Bakken <ssb@php.net>
12 * @author     Tomas V.V.Cox <cox@idecnet.com>
13 * @copyright  1997-2009 The Authors
14 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
15 * @version    CVS: $Id: pearcmd.php 313023 2011-07-06 19:17:11Z dufuz $
16 * @link       http://pear.php.net/package/PEAR
17 */
18
19ob_end_clean();
20if (!defined('PEAR_RUNTYPE')) {
21    // this is defined in peclcmd.php as 'pecl'
22    define('PEAR_RUNTYPE', 'pear');
23}
24define('PEAR_IGNORE_BACKTRACE', 1);
25/**
26 * @nodep Gtk
27 */
28if ('@include_path@' != '@'.'include_path'.'@') {
29    ini_set('include_path', '@include_path@');
30    $raw = false;
31} else {
32    // this is a raw, uninstalled pear, either a cvs checkout, or php distro
33    $raw = true;
34}
35@ini_set('allow_url_fopen', true);
36if (!ini_get('safe_mode')) {
37    @set_time_limit(0);
38}
39ob_implicit_flush(true);
40@ini_set('track_errors', true);
41@ini_set('html_errors', false);
42@ini_set('magic_quotes_runtime', false);
43$_PEAR_PHPDIR = '#$%^&*';
44set_error_handler('error_handler');
45
46$pear_package_version = "@pear_version@";
47
48require_once 'PEAR.php';
49require_once 'PEAR/Frontend.php';
50require_once 'PEAR/Config.php';
51require_once 'PEAR/Command.php';
52require_once 'Console/Getopt.php';
53
54
55PEAR_Command::setFrontendType('CLI');
56$all_commands = PEAR_Command::getCommands();
57
58// remove this next part when we stop supporting that crap-ass PHP 4.2
59if (!isset($_SERVER['argv']) && !isset($argv) && !isset($HTTP_SERVER_VARS['argv'])) {
60    echo 'ERROR: either use the CLI php executable, or set register_argc_argv=On in php.ini';
61    exit(1);
62}
63
64$argv = Console_Getopt::readPHPArgv();
65// fix CGI sapi oddity - the -- in pear.bat/pear is not removed
66if (php_sapi_name() != 'cli' && isset($argv[1]) && $argv[1] == '--') {
67    unset($argv[1]);
68    $argv = array_values($argv);
69}
70$progname = PEAR_RUNTYPE;
71array_shift($argv);
72$options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
73if (PEAR::isError($options)) {
74    usage($options);
75}
76
77$opts = $options[0];
78
79$fetype = 'CLI';
80if ($progname == 'gpear' || $progname == 'pear-gtk') {
81    $fetype = 'Gtk';
82} else {
83    foreach ($opts as $opt) {
84        if ($opt[0] == 'G') {
85            $fetype = 'Gtk';
86        }
87    }
88}
89//Check if Gtk and PHP >= 5.1.0
90if ($fetype == 'Gtk' && version_compare(phpversion(), '5.1.0', '>=')) {
91    $fetype = 'Gtk2';
92}
93
94$pear_user_config = '';
95$pear_system_config = '';
96$store_user_config = false;
97$store_system_config = false;
98$verbose = 1;
99
100foreach ($opts as $opt) {
101    switch ($opt[0]) {
102        case 'c':
103            $pear_user_config = $opt[1];
104            break;
105        case 'C':
106            $pear_system_config = $opt[1];
107            break;
108    }
109}
110
111PEAR_Command::setFrontendType($fetype);
112$ui = &PEAR_Command::getFrontendObject();
113$config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
114
115if (PEAR::isError($config)) {
116    $_file = '';
117    if ($pear_user_config !== false) {
118       $_file .= $pear_user_config;
119    }
120    if ($pear_system_config !== false) {
121       $_file .= '/' . $pear_system_config;
122    }
123    if ($_file == '/') {
124        $_file = 'The default config file';
125    }
126    $config->getMessage();
127    $ui->outputData("ERROR: $_file is not a valid config file or is corrupted.");
128    // We stop, we have no idea where we are :)
129    exit(1);
130}
131
132// this is used in the error handler to retrieve a relative path
133$_PEAR_PHPDIR = $config->get('php_dir');
134$ui->setConfig($config);
135PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
136if (ini_get('safe_mode')) {
137    $ui->outputData('WARNING: running in safe mode requires that all files created ' .
138        'be the same uid as the current script.  PHP reports this script is uid: ' .
139        @getmyuid() . ', and current user is: ' . @get_current_user());
140}
141
142$verbose = $config->get("verbose");
143$cmdopts = array();
144
145if ($raw) {
146    if (!$config->isDefinedLayer('user') && !$config->isDefinedLayer('system')) {
147        $found = false;
148        foreach ($opts as $opt) {
149            if ($opt[0] == 'd' || $opt[0] == 'D') {
150                $found = true; // the user knows what they are doing, and are setting config values
151            }
152        }
153        if (!$found) {
154            // no prior runs, try to install PEAR
155            if (strpos(dirname(__FILE__), 'scripts')) {
156                $packagexml = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'package2.xml';
157                $pearbase = dirname(dirname(__FILE__));
158            } else {
159                $packagexml = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package2.xml';
160                $pearbase = dirname(__FILE__);
161            }
162            if (file_exists($packagexml)) {
163                $options[1] = array(
164                    'install',
165                    $packagexml
166                );
167                $config->set('php_dir', $pearbase . DIRECTORY_SEPARATOR . 'php');
168                $config->set('data_dir', $pearbase . DIRECTORY_SEPARATOR . 'data');
169                $config->set('doc_dir', $pearbase . DIRECTORY_SEPARATOR . 'docs');
170                $config->set('test_dir', $pearbase . DIRECTORY_SEPARATOR . 'tests');
171                $config->set('ext_dir', $pearbase . DIRECTORY_SEPARATOR . 'extensions');
172                $config->set('bin_dir', $pearbase);
173                $config->mergeConfigFile($pearbase . 'pear.ini', false);
174                $config->store();
175                $config->set('auto_discover', 1);
176            }
177        }
178    }
179}
180foreach ($opts as $opt) {
181    $param = !empty($opt[1]) ? $opt[1] : true;
182    switch ($opt[0]) {
183        case 'd':
184            if ($param === true) {
185                die('Invalid usage of "-d" option, expected -d config_value=value, ' .
186                    'received "-d"' . "\n");
187            }
188            $possible = explode('=', $param);
189            if (count($possible) != 2) {
190                die('Invalid usage of "-d" option, expected -d config_value=value, received "' .
191                    $param . '"' . "\n");
192            }
193            list($key, $value) = explode('=', $param);
194            $config->set($key, $value, 'user');
195            break;
196        case 'D':
197            if ($param === true) {
198                die('Invalid usage of "-d" option, expected -d config_value=value, ' .
199                    'received "-d"' . "\n");
200            }
201            $possible = explode('=', $param);
202            if (count($possible) != 2) {
203                die('Invalid usage of "-d" option, expected -d config_value=value, received "' .
204                    $param . '"' . "\n");
205            }
206            list($key, $value) = explode('=', $param);
207            $config->set($key, $value, 'system');
208            break;
209        case 's':
210            $store_user_config = true;
211            break;
212        case 'S':
213            $store_system_config = true;
214            break;
215        case 'u':
216            $config->remove($param, 'user');
217            break;
218        case 'v':
219            $config->set('verbose', $config->get('verbose') + 1);
220            break;
221        case 'q':
222            $config->set('verbose', $config->get('verbose') - 1);
223            break;
224        case 'V':
225            usage(null, 'version');
226        case 'c':
227        case 'C':
228            break;
229        default:
230            // all non pear params goes to the command
231            $cmdopts[$opt[0]] = $param;
232            break;
233    }
234}
235
236if ($store_system_config) {
237    $config->store('system');
238}
239
240if ($store_user_config) {
241    $config->store('user');
242}
243
244$command = (isset($options[1][0])) ? $options[1][0] : null;
245if (empty($command) && ($store_user_config || $store_system_config)) {
246    exit;
247}
248
249if ($fetype == 'Gtk' || $fetype == 'Gtk2') {
250    if (!$config->validConfiguration()) {
251        PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
252            "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
253            'file to one of these locations, or use the -c and -s options to create one');
254    }
255    Gtk::main();
256} else do {
257    if ($command == 'help') {
258        usage(null, @$options[1][1]);
259    }
260
261    if (!$config->validConfiguration()) {
262        PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
263            "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
264            'file to one of these locations, or use the -c and -s options to create one');
265    }
266
267    PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
268    $cmd = PEAR_Command::factory($command, $config);
269    PEAR::popErrorHandling();
270    if (PEAR::isError($cmd)) {
271        usage(null, @$options[1][0]);
272    }
273
274    $short_args = $long_args = null;
275    PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
276    array_shift($options[1]);
277    $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
278
279    if (PEAR::isError($tmp)) {
280        break;
281    }
282
283    list($tmpopt, $params) = $tmp;
284    $opts = array();
285    foreach ($tmpopt as $foo => $tmp2) {
286        list($opt, $value) = $tmp2;
287        if ($value === null) {
288            $value = true; // options without args
289        }
290
291        if (strlen($opt) == 1) {
292            $cmdoptions = $cmd->getOptions($command);
293            foreach ($cmdoptions as $o => $d) {
294                if (isset($d['shortopt']) && $d['shortopt'] == $opt) {
295                    $opts[$o] = $value;
296                }
297            }
298        } else {
299            if (substr($opt, 0, 2) == '--') {
300                $opts[substr($opt, 2)] = $value;
301            }
302        }
303    }
304
305    $ok = $cmd->run($command, $opts, $params);
306    if ($ok === false) {
307        PEAR::raiseError("unknown command `$command'");
308    }
309
310    if (PEAR::isError($ok)) {
311        PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
312        PEAR::raiseError($ok);
313    }
314} while (false);
315
316// {{{ usage()
317
318function usage($error = null, $helpsubject = null)
319{
320    global $progname, $all_commands;
321    $stdout = fopen('php://stdout', 'w');
322    if (PEAR::isError($error)) {
323        fputs($stdout, $error->getMessage() . "\n");
324    } elseif ($error !== null) {
325        fputs($stdout, "$error\n");
326    }
327
328    if ($helpsubject != null) {
329        $put = cmdHelp($helpsubject);
330    } else {
331        $put = "Commands:\n";
332        $maxlen = max(array_map("strlen", $all_commands));
333        $formatstr = "%-{$maxlen}s  %s\n";
334        ksort($all_commands);
335        foreach ($all_commands as $cmd => $class) {
336            $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
337        }
338        $put .=
339            "Usage: $progname ['options'] command ['command-options'] <parameters>\n".
340            "Type \"$progname help options\" to list all options.\n".
341            "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
342            "Type \"$progname help <command>\" to get the help for the specified command.";
343    }
344    fputs($stdout, "$put\n");
345    fclose($stdout);
346
347    if ($error === null) {
348        exit(0);
349    }
350    exit(1);
351}
352
353function cmdHelp($command)
354{
355    global $progname, $all_commands, $config;
356    if ($command == "options") {
357        return
358        "Options:\n".
359        "     -v         increase verbosity level (default 1)\n".
360        "     -q         be quiet, decrease verbosity level\n".
361        "     -c file    find user configuration in `file'\n".
362        "     -C file    find system configuration in `file'\n".
363        "     -d foo=bar set user config variable `foo' to `bar'\n".
364        "     -D foo=bar set system config variable `foo' to `bar'\n".
365        "     -G         start in graphical (Gtk) mode\n".
366        "     -s         store user configuration\n".
367        "     -S         store system configuration\n".
368        "     -u foo     unset `foo' in the user configuration\n".
369        "     -h, -?     display help/usage (this message)\n".
370        "     -V         version information\n";
371    } elseif ($command == "shortcuts") {
372        $sc = PEAR_Command::getShortcuts();
373        $ret = "Shortcuts:\n";
374        foreach ($sc as $s => $c) {
375            $ret .= sprintf("     %-8s %s\n", $s, $c);
376        }
377        return $ret;
378
379    } elseif ($command == "version") {
380        return "PEAR Version: ".$GLOBALS['pear_package_version'].
381               "\nPHP Version: ".phpversion().
382               "\nZend Engine Version: ".zend_version().
383               "\nRunning on: ".php_uname();
384
385    } elseif ($help = PEAR_Command::getHelp($command)) {
386        if (is_string($help)) {
387            return "$progname $command ['options'] $help\n";
388        }
389
390        if ($help[1] === null) {
391            return "$progname $command $help[0]";
392        }
393
394        return "$progname $command ['options'] $help[0]\n$help[1]";
395    }
396
397    return "Command '$command' is not valid, try '$progname help'";
398}
399
400// }}}
401
402function error_handler($errno, $errmsg, $file, $line, $vars) {
403    if ((defined('E_STRICT') && $errno & E_STRICT) || (defined('E_DEPRECATED') &&
404          $errno & E_DEPRECATED) || !error_reporting()) {
405        if (defined('E_STRICT') && $errno & E_STRICT) {
406            return; // E_STRICT
407        }
408        if (defined('E_DEPRECATED') && $errno & E_DEPRECATED) {
409            return; // E_DEPRECATED
410        }
411        if ($GLOBALS['config']->get('verbose') < 4) {
412            return false; // @silenced error, show all if debug is high enough
413        }
414    }
415    $errortype = array (
416        E_ERROR   =>  "Error",
417        E_WARNING   =>  "Warning",
418        E_PARSE   =>  "Parsing Error",
419        E_NOTICE   =>  "Notice",
420        E_CORE_ERROR  =>  "Core Error",
421        E_CORE_WARNING  =>  "Core Warning",
422        E_COMPILE_ERROR  =>  "Compile Error",
423        E_COMPILE_WARNING =>  "Compile Warning",
424        E_USER_ERROR =>  "User Error",
425        E_USER_WARNING =>  "User Warning",
426        E_USER_NOTICE =>  "User Notice"
427    );
428    $prefix = $errortype[$errno];
429    global $_PEAR_PHPDIR;
430    if (stristr($file, $_PEAR_PHPDIR)) {
431        $file = substr($file, strlen($_PEAR_PHPDIR) + 1);
432    } else {
433        $file = basename($file);
434    }
435    print "\n$prefix: $errmsg in $file on line $line\n";
436    return false;
437}
438
439
440/*
441 * Local variables:
442 * tab-width: 4
443 * c-basic-offset: 4
444 * indent-tabs-mode: nil
445 * mode: php
446 * End:
447 */
448// vim600:syn=php
Note: See TracBrowser for help on using the repository browser.