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

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