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.35 2006/09/22 02:48:44 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.             list($key, $value) = explode('=', $param);
  187.             $config->set($key, $value, 'user');
  188.             break;
  189.         case 'D':
  190.             list($key, $value) = explode('=', $param);
  191.             $config->set($key, $value, 'system');
  192.             break;
  193.         case 's':
  194.             $store_user_config = true;
  195.             break;
  196.         case 'S':
  197.             $store_system_config = true;
  198.             break;
  199.         case 'u':
  200.             $config->remove($param, 'user');
  201.             break;
  202.         case 'v':
  203.             $config->set('verbose', $config->get('verbose') + 1);
  204.             break;
  205.         case 'q':
  206.             $config->set('verbose', $config->get('verbose') - 1);
  207.             break;
  208.         case 'V':
  209.             usage(null, 'version');
  210.         case 'c':
  211.         case 'C':
  212.             break;
  213.         default:
  214.             // all non pear params goes to the command
  215.             $cmdopts[$opt[0]] = $param;
  216.             break;
  217.     }
  218. }
  219.  
  220. if ($store_system_config) {
  221.     $config->store('system');
  222. }
  223.  
  224. if ($store_user_config) {
  225.     $config->store('user');
  226. }
  227.  
  228. $command = (isset($options[1][0])) ? $options[1][0] : null;
  229.  
  230. if (empty($command) && ($store_user_config || $store_system_config)) {
  231.     exit;
  232. }
  233.  
  234. if ($fetype == 'Gtk' || $fetype == 'Gtk2') {
  235.     if (!$config->validConfiguration()) {
  236.         PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  237.             "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  238.             'file to one of these locations, or use the -c and -s options to create one');
  239.     }
  240.     Gtk::main();
  241. } else do {
  242.     if ($command == 'help') {
  243.         usage(null, @$options[1][1]);
  244.     }
  245.     if (!$config->validConfiguration()) {
  246.         PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  247.             "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  248.             'file to one of these locations, or use the -c and -s options to create one');
  249.     }
  250.  
  251.     $cmd = PEAR_Command::factory($command, $config);
  252.     if (PEAR::isError($cmd)) {
  253.         usage(null, @$options[1][0]);
  254.     }
  255.  
  256.     $short_args = $long_args = null;
  257.     PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
  258.     if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  259.         array_shift($options[1]);
  260.         $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
  261.     } else {
  262.         $tmp = Console_Getopt::getopt($options[1], $short_args, $long_args);
  263.     }
  264.     if (PEAR::isError($tmp)) {
  265.         break;
  266.     }
  267.     list($tmpopt, $params) = $tmp;
  268.     $opts = array();
  269.     foreach ($tmpopt as $foo => $tmp2) {
  270.         list($opt, $value) = $tmp2;
  271.         if ($value === null) {
  272.             $value = true; // options without args
  273.         }
  274.         if (strlen($opt) == 1) {
  275.             $cmdoptions = $cmd->getOptions($command);
  276.             foreach ($cmdoptions as $o => $d) {
  277.                 if (@$d['shortopt'] == $opt) {
  278.                     $opts[$o] = $value;
  279.                 }
  280.             }
  281.         } else {
  282.             if (substr($opt, 0, 2) == '--') {
  283.                 $opts[substr($opt, 2)] = $value;
  284.             }
  285.         }
  286.     }
  287.     $ok = $cmd->run($command, $opts, $params);
  288.     if ($ok === false) {
  289.         PEAR::raiseError("unknown command `$command'");
  290.     }
  291.     if (PEAR::isError($ok)) {
  292.         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  293.         PEAR::raiseError($ok);
  294.     }
  295. } while (false);
  296.  
  297. // {{{ usage()
  298.  
  299. function usage($error = null, $helpsubject = null)
  300. {
  301.     global $progname, $all_commands;
  302.     $stderr = fopen('php://stderr', 'w');
  303.     if (PEAR::isError($error)) {
  304.         fputs($stderr, $error->getMessage() . "\n");
  305.     } elseif ($error !== null) {
  306.         fputs($stderr, "$error\n");
  307.     }
  308.     if ($helpsubject != null) {
  309.         $put = cmdHelp($helpsubject);
  310.     } else {
  311.         $put =
  312.             "Commands:\n";
  313.         $maxlen = max(array_map("strlen", $all_commands));
  314.         $formatstr = "%-{$maxlen}s  %s\n";
  315.         ksort($all_commands);
  316.         foreach ($all_commands as $cmd => $class) {
  317.             $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
  318.         }
  319.         $put .=
  320.             "Usage: $progname [options] command [command-options] <parameters>\n".
  321.             "Type \"$progname help options\" to list all options.\n".
  322.             "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
  323.             "Type \"$progname help <command>\" to get the help for the specified command.";
  324.     }
  325.     fputs($stderr, "$put\n");
  326.     fclose($stderr);
  327.     exit(1);
  328. }
  329.  
  330. function cmdHelp($command)
  331. {
  332.     global $progname, $all_commands, $config;
  333.     if ($command == "options") {
  334.         return
  335.         "Options:\n".
  336.         "     -v         increase verbosity level (default 1)\n".
  337.         "     -q         be quiet, decrease verbosity level\n".
  338.         "     -c file    find user configuration in `file'\n".
  339.         "     -C file    find system configuration in `file'\n".
  340.         "     -d foo=bar set user config variable `foo' to `bar'\n".
  341.         "     -D foo=bar set system config variable `foo' to `bar'\n".
  342.         "     -G         start in graphical (Gtk) mode\n".
  343.         "     -s         store user configuration\n".
  344.         "     -S         store system configuration\n".
  345.         "     -u foo     unset `foo' in the user configuration\n".
  346.         "     -h, -?     display help/usage (this message)\n".
  347.         "     -V         version information\n";
  348.     } elseif ($command == "shortcuts") {
  349.         $sc = PEAR_Command::getShortcuts();
  350.         $ret = "Shortcuts:\n";
  351.         foreach ($sc as $s => $c) {
  352.             $ret .= sprintf("     %-8s %s\n", $s, $c);
  353.         }
  354.         return $ret;
  355.  
  356.     } elseif ($command == "version") {
  357.         return "PEAR Version: ".$GLOBALS['pear_package_version'].
  358.                "\nPHP Version: ".phpversion().
  359.                "\nZend Engine Version: ".zend_version().
  360.                "\nRunning on: ".php_uname();
  361.  
  362.     } elseif ($help = PEAR_Command::getHelp($command)) {
  363.         if (is_string($help)) {
  364.             return "$progname $command [options] $help\n";
  365.         }
  366.         if ($help[1] === null) {
  367.             return "$progname $command $help[0]";
  368.         } else {
  369.             return "$progname $command [options] $help[0]\n$help[1]";
  370.         }
  371.     }
  372.     return "Command '$command' is not valid, try 'pear help'";
  373. }
  374.  
  375. // }}}
  376.  
  377. function error_handler($errno, $errmsg, $file, $line, $vars) {
  378.     if ((defined('E_STRICT') && $errno & E_STRICT) || !error_reporting()) {
  379.         if (defined('E_STRICT') && $errno & E_STRICT) {
  380.             return; // E_STRICT
  381.         }
  382.         if ($GLOBALS['config']->get('verbose') < 4) {
  383.             return; // @silenced error, show all if debug is high enough
  384.         }
  385.     }
  386.     $errortype = array (
  387.         E_ERROR   =>  "Error",
  388.         E_WARNING   =>  "Warning",
  389.         E_PARSE   =>  "Parsing Error",
  390.         E_NOTICE   =>  "Notice",
  391.         E_CORE_ERROR  =>  "Core Error",
  392.         E_CORE_WARNING  =>  "Core Warning",
  393.         E_COMPILE_ERROR  =>  "Compile Error",
  394.         E_COMPILE_WARNING =>  "Compile Warning",
  395.         E_USER_ERROR =>  "User Error",
  396.         E_USER_WARNING =>  "User Warning",
  397.         E_USER_NOTICE =>  "User Notice"
  398.     );
  399.     $prefix = $errortype[$errno];
  400.     global $_PEAR_PHPDIR;
  401.     if (stristr($file, $_PEAR_PHPDIR)) {
  402.         $file = substr($file, strlen($_PEAR_PHPDIR) + 1);
  403.     } else {
  404.         $file = basename($file);
  405.     }
  406.     print "\n$prefix: $errmsg in $file on line $line\n";
  407. }
  408.  
  409.  
  410. /*
  411.  * Local variables:
  412.  * tab-width: 4
  413.  * c-basic-offset: 4
  414.  * indent-tabs-mode: nil
  415.  * mode: php
  416.  * End:
  417.  */
  418. // vim600:syn=php
  419.  
  420. ?>

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