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.28 2005/09/28 22:45:56 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. $argv = Console_Getopt::readPHPArgv();
  60. $progname = PEAR_RUNTYPE;
  61. if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  62.     array_shift($argv);
  63.     $options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
  64. } else {
  65.     $options = Console_Getopt::getopt($argv, "c:C:d:D:Gh?sSqu:vV");
  66. }
  67. if (PEAR::isError($options)) {
  68.     usage($options);
  69. }
  70.  
  71. $opts = $options[0];
  72.  
  73. $fetype = 'CLI';
  74. if ($progname == 'gpear' || $progname == 'pear-gtk') {
  75.     $fetype = 'Gtk';
  76. } else {
  77.     foreach ($opts as $opt) {
  78.         if ($opt[0] == 'G') {
  79.             $fetype = 'Gtk';
  80.         }
  81.     }
  82. }
  83. $pear_user_config = '';
  84. $pear_system_config = '';
  85. $store_user_config = false;
  86. $store_system_config = false;
  87. $verbose = 1;
  88.  
  89. foreach ($opts as $opt) {
  90.     switch ($opt[0]) {
  91.         case 'c':
  92.             $pear_user_config = $opt[1];
  93.             break;
  94.         case 'C':
  95.             $pear_system_config = $opt[1];
  96.             break;
  97.     }
  98. }
  99.  
  100. $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
  101.  
  102. if (PEAR::isError($config)) {
  103.     $_file = '';
  104.     if ($pear_user_config !== false) {
  105.        $_file .= $pear_user_config;
  106.     }
  107.     if ($pear_system_config !== false) {
  108.        $_file .= '/' . $pear_system_config;
  109.     }
  110.     if ($_file == '/') {
  111.         $_file = 'The default config file';
  112.     }
  113.     $config->getMessage();
  114.     $ui->outputData("ERROR: $_file is not a valid config file or is corrupted.");
  115.     // We stop, we have no idea where we are :)
  116.     exit();    
  117. }
  118.  
  119. // this is used in the error handler to retrieve a relative path
  120. $_PEAR_PHPDIR = $config->get('php_dir');
  121. $ui->setConfig($config);
  122. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  123. if (ini_get('safe_mode')) {
  124.     $ui->outputData('WARNING: running in safe mode requires that all files created ' .
  125.         'be the same uid as the current script.  PHP reports this script is uid: ' .
  126.         @getmyuid() . ', and current user is: ' . @get_current_user());
  127. }
  128.  
  129. $verbose = $config->get("verbose");
  130. $cmdopts = array();
  131.  
  132. if ($raw) {
  133.     if (!$config->isDefinedLayer('user') && !$config->isDefinedLayer('system')) {
  134.         $found = false;
  135.         foreach ($opts as $opt) {
  136.             if ($opt[0] == 'd' || $opt[0] == 'D') {
  137.                 $found = true; // the user knows what they are doing, and are setting config values
  138.             }
  139.         }
  140.         if (!$found) {
  141.             // no prior runs, try to install PEAR
  142.             if (strpos(dirname(__FILE__), 'scripts')) {
  143.                 $packagexml = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'package2.xml';
  144.                 $pearbase = dirname(dirname(__FILE__));
  145.             } else {
  146.                 $packagexml = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package2.xml';
  147.                 $pearbase = dirname(__FILE__);
  148.             }
  149.             if (file_exists($packagexml)) {
  150.                 $options[1] = array(
  151.                     'install',
  152.                     $packagexml
  153.                 );
  154.                 $config->set('php_dir', $pearbase . DIRECTORY_SEPARATOR . 'php');
  155.                 $config->set('data_dir', $pearbase . DIRECTORY_SEPARATOR . 'data');
  156.                 $config->set('doc_dir', $pearbase . DIRECTORY_SEPARATOR . 'docs');
  157.                 $config->set('test_dir', $pearbase . DIRECTORY_SEPARATOR . 'tests');
  158.                 $config->set('ext_dir', $pearbase . DIRECTORY_SEPARATOR . 'extensions');
  159.                 $config->set('bin_dir', $pearbase);
  160.                 $config->mergeConfigFile($pearbase . 'pear.ini', false);
  161.                 $config->store();
  162.                 $config->set('auto_discover', 1);
  163.             }
  164.         }
  165.     }
  166. }
  167. foreach ($opts as $opt) {
  168.     $param = !empty($opt[1]) ? $opt[1] : true;
  169.     switch ($opt[0]) {
  170.         case 'd':
  171.             list($key, $value) = explode('=', $param);
  172.             $config->set($key, $value, 'user');
  173.             break;
  174.         case 'D':
  175.             list($key, $value) = explode('=', $param);
  176.             $config->set($key, $value, 'system');
  177.             break;
  178.         case 's':
  179.             $store_user_config = true;
  180.             break;
  181.         case 'S':
  182.             $store_system_config = true;
  183.             break;
  184.         case 'u':
  185.             $config->remove($param, 'user');
  186.             break;
  187.         case 'v':
  188.             $config->set('verbose', $config->get('verbose') + 1);
  189.             break;
  190.         case 'q':
  191.             $config->set('verbose', $config->get('verbose') - 1);
  192.             break;
  193.         case 'V':
  194.             usage(null, 'version');
  195.         case 'c':
  196.         case 'C':
  197.             break;
  198.         default:
  199.             // all non pear params goes to the command
  200.             $cmdopts[$opt[0]] = $param;
  201.             break;
  202.     }
  203. }
  204.  
  205. if ($store_system_config) {
  206.     $config->store('system');
  207. }
  208.  
  209. if ($store_user_config) {
  210.     $config->store('user');
  211. }
  212.  
  213. $command = (isset($options[1][0])) ? $options[1][0] : null;
  214.  
  215. if (empty($command) && ($store_user_config || $store_system_config)) {
  216.     exit;
  217. }
  218.  
  219. if ($fetype == 'Gtk') {
  220.     if (!$config->validConfiguration()) {
  221.         PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  222.             "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  223.             'file to one of these locations, or use the -c and -s options to create one');
  224.     }
  225.     Gtk::main();
  226. } else do {
  227.     if ($command == 'help') {
  228.         usage(null, @$options[1][1]);
  229.     }
  230.     if (!$config->validConfiguration()) {
  231.         PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  232.             "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  233.             'file to one of these locations, or use the -c and -s options to create one');
  234.     }
  235.  
  236.     $cmd = PEAR_Command::factory($command, $config);
  237.     if (PEAR::isError($cmd)) {
  238.         usage(null, @$options[1][0]);
  239.     }
  240.  
  241.     $short_args = $long_args = null;
  242.     PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
  243.     if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  244.         array_shift($options[1]);
  245.         $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
  246.     } else {
  247.         $tmp = Console_Getopt::getopt($options[1], $short_args, $long_args);
  248.     }
  249.     if (PEAR::isError($tmp)) {
  250.         break;
  251.     }
  252.     list($tmpopt, $params) = $tmp;
  253.     $opts = array();
  254.     foreach ($tmpopt as $foo => $tmp2) {
  255.         list($opt, $value) = $tmp2;
  256.         if ($value === null) {
  257.             $value = true; // options without args
  258.         }
  259.         if (strlen($opt) == 1) {
  260.             $cmdoptions = $cmd->getOptions($command);
  261.             foreach ($cmdoptions as $o => $d) {
  262.                 if (@$d['shortopt'] == $opt) {
  263.                     $opts[$o] = $value;
  264.                 }
  265.             }
  266.         } else {
  267.             if (substr($opt, 0, 2) == '--') {
  268.                 $opts[substr($opt, 2)] = $value;
  269.             }
  270.         }
  271.     }
  272.     $ok = $cmd->run($command, $opts, $params);
  273.     if ($ok === false) {
  274.         PEAR::raiseError("unknown command `$command'");
  275.     }
  276.     if (PEAR::isError($ok)) {
  277.         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  278.         PEAR::raiseError($ok);
  279.     }
  280. } while (false);
  281.  
  282. // {{{ usage()
  283.  
  284. function usage($error = null, $helpsubject = null)
  285. {
  286.     global $progname, $all_commands;
  287.     $stderr = fopen('php://stderr', 'w');
  288.     if (PEAR::isError($error)) {
  289.         fputs($stderr, $error->getMessage() . "\n");
  290.     } elseif ($error !== null) {
  291.         fputs($stderr, "$error\n");
  292.     }
  293.     if ($helpsubject != null) {
  294.         $put = cmdHelp($helpsubject);
  295.     } else {
  296.         $put =
  297.             "Commands:\n";
  298.         $maxlen = max(array_map("strlen", $all_commands));
  299.         $formatstr = "%-{$maxlen}s  %s\n";
  300.         ksort($all_commands);
  301.         foreach ($all_commands as $cmd => $class) {
  302.             $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
  303.         }
  304.         $put .=
  305.             "Usage: $progname [options] command [command-options] <parameters>\n".
  306.             "Type \"$progname help options\" to list all options.\n".
  307.             "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
  308.             "Type \"$progname help <command>\" to get the help for the specified command.";
  309.     }
  310.     fputs($stderr, "$put\n");
  311.     fclose($stderr);
  312.     exit;
  313. }
  314.  
  315. function cmdHelp($command)
  316. {
  317.     global $progname, $all_commands, $config;
  318.     if ($command == "options") {
  319.         return
  320.         "Options:\n".
  321.         "     -v         increase verbosity level (default 1)\n".
  322.         "     -q         be quiet, decrease verbosity level\n".
  323.         "     -c file    find user configuration in `file'\n".
  324.         "     -C file    find system configuration in `file'\n".
  325.         "     -d foo=bar set user config variable `foo' to `bar'\n".
  326.         "     -D foo=bar set system config variable `foo' to `bar'\n".
  327.         "     -G         start in graphical (Gtk) mode\n".
  328.         "     -s         store user configuration\n".
  329.         "     -S         store system configuration\n".
  330.         "     -u foo     unset `foo' in the user configuration\n".
  331.         "     -h, -?     display help/usage (this message)\n".
  332.         "     -V         version information\n";
  333.     } elseif ($command == "shortcuts") {
  334.         $sc = PEAR_Command::getShortcuts();
  335.         $ret = "Shortcuts:\n";
  336.         foreach ($sc as $s => $c) {
  337.             $ret .= sprintf("     %-8s %s\n", $s, $c);
  338.         }
  339.         return $ret;
  340.  
  341.     } elseif ($command == "version") {
  342.         return "PEAR Version: ".$GLOBALS['pear_package_version'].
  343.                "\nPHP Version: ".phpversion().
  344.                "\nZend Engine Version: ".zend_version().
  345.                "\nRunning on: ".php_uname();
  346.  
  347.     } elseif ($help = PEAR_Command::getHelp($command)) {
  348.         if (is_string($help)) {
  349.             return "$progname $command [options] $help\n";
  350.         }
  351.         if ($help[1] === null) {
  352.             return "$progname $command $help[0]";
  353.         } else {
  354.             return "$progname $command [options] $help[0]\n$help[1]";
  355.         }
  356.     }
  357.     return "Command '$command' is not valid, try 'pear help'";
  358. }
  359.  
  360. // }}}
  361.  
  362. function error_handler($errno, $errmsg, $file, $line, $vars) {
  363.     if ((defined('E_STRICT') && $errno & E_STRICT) || !error_reporting()) {
  364.         if (defined('E_STRICT') && $errno & E_STRICT) {
  365.             return; // E_STRICT
  366.         }
  367.         if ($GLOBALS['config']->get('verbose') < 4) {
  368.             return; // @silenced error, show all if debug is high enough
  369.         }
  370.     }
  371.     $errortype = array (
  372.         E_ERROR   =>  "Error",
  373.         E_WARNING   =>  "Warning",
  374.         E_PARSE   =>  "Parsing Error",
  375.         E_NOTICE   =>  "Notice",
  376.         E_CORE_ERROR  =>  "Core Error",
  377.         E_CORE_WARNING  =>  "Core Warning",
  378.         E_COMPILE_ERROR  =>  "Compile Error",
  379.         E_COMPILE_WARNING =>  "Compile Warning",
  380.         E_USER_ERROR =>  "User Error",
  381.         E_USER_WARNING =>  "User Warning",
  382.         E_USER_NOTICE =>  "User Notice"
  383.     );
  384.     $prefix = $errortype[$errno];
  385.     global $_PEAR_PHPDIR;
  386.     if (stristr($file, $_PEAR_PHPDIR)) {
  387.         $file = substr($file, strlen($_PEAR_PHPDIR) + 1);
  388.     } else {
  389.         $file = basename($file);
  390.     }
  391.     print "\n$prefix: $errmsg in $file on line $line\n";
  392. }
  393.  
  394.  
  395. /*
  396.  * Local variables:
  397.  * tab-width: 4
  398.  * c-basic-offset: 4
  399.  * indent-tabs-mode: nil
  400.  * mode: php
  401.  * End:
  402.  */
  403. // vim600:syn=php
  404.  
  405. ?>

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