PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file pearcmd.php

Documentation is available at pearcmd.php

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

Documentation generated on Mon, 11 Mar 2019 14:55:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.