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

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