home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / tmp / PEAR-1.7.1 / scripts / pearcmd.php < prev    next >
Encoding:
PHP Script  |  2008-02-15  |  15.0 KB  |  447 lines

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