PageRenderTime 58ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/pear/php/pearcmd.php

https://github.com/hnw/phpbugs
PHP | 450 lines | 395 code | 23 blank | 32 comment | 85 complexity | 0094d212214147e356fcd74ca68b3d7f MD5 | raw file
Possible License(s): LGPL-2.1
  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.39 2008/03/25 04:58:42 cellog Exp $
  22. ob_end_clean();
  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 ('/Users/masato/data/svk/bz2/php_parser/trunk/vendor/pear/php' != '@'.'include_path'.'@') {
  32. ini_set('include_path', '/Users/masato/data/svk/bz2/php_parser/trunk/vendor/pear/php');
  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. ob_implicit_flush(true);
  43. @ini_set('track_errors', true);
  44. @ini_set('html_errors', false);
  45. @ini_set('magic_quotes_runtime', false);
  46. $_PEAR_PHPDIR = '#$%^&*';
  47. set_error_handler('error_handler');
  48. $pear_package_version = "1.7.2";
  49. require_once 'PEAR.php';
  50. require_once 'PEAR/Frontend.php';
  51. require_once 'PEAR/Config.php';
  52. require_once 'PEAR/Command.php';
  53. require_once 'Console/Getopt.php';
  54. PEAR_Command::setFrontendType('CLI');
  55. $all_commands = PEAR_Command::getCommands();
  56. // remove this next part when we stop supporting that crap-ass PHP 4.2
  57. if (!isset($_SERVER['argv']) && !isset($argv) && !isset($HTTP_SERVER_VARS['argv'])) {
  58. echo 'ERROR: either use the CLI php executable, or set register_argc_argv=On in php.ini';
  59. exit(1);
  60. }
  61. $argv = Console_Getopt::readPHPArgv();
  62. // fix CGI sapi oddity - the -- in pear.bat/pear is not removed
  63. if (php_sapi_name() != 'cli' && isset($argv[1]) && $argv[1] == '--') {
  64. unset($argv[1]);
  65. $argv = array_values($argv);
  66. }
  67. $progname = PEAR_RUNTYPE;
  68. if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  69. array_shift($argv);
  70. $options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
  71. } else {
  72. $options = Console_Getopt::getopt($argv, "c:C:d:D:Gh?sSqu:vV");
  73. }
  74. if (PEAR::isError($options)) {
  75. usage($options);
  76. }
  77. $opts = $options[0];
  78. $fetype = 'CLI';
  79. if ($progname == 'gpear' || $progname == 'pear-gtk') {
  80. $fetype = 'Gtk';
  81. } else {
  82. foreach ($opts as $opt) {
  83. if ($opt[0] == 'G') {
  84. $fetype = 'Gtk';
  85. }
  86. }
  87. }
  88. //Check if Gtk and PHP >= 5.1.0
  89. if ($fetype == 'Gtk' && version_compare(phpversion(), '5.1.0', '>=')) {
  90. $fetype = 'Gtk2';
  91. }
  92. $pear_user_config = '';
  93. $pear_system_config = '';
  94. $store_user_config = false;
  95. $store_system_config = false;
  96. $verbose = 1;
  97. foreach ($opts as $opt) {
  98. switch ($opt[0]) {
  99. case 'c':
  100. $pear_user_config = $opt[1];
  101. break;
  102. case 'C':
  103. $pear_system_config = $opt[1];
  104. break;
  105. }
  106. }
  107. PEAR_Command::setFrontendType($fetype);
  108. $ui = &PEAR_Command::getFrontendObject();
  109. $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
  110. if (PEAR::isError($config)) {
  111. $_file = '';
  112. if ($pear_user_config !== false) {
  113. $_file .= $pear_user_config;
  114. }
  115. if ($pear_system_config !== false) {
  116. $_file .= '/' . $pear_system_config;
  117. }
  118. if ($_file == '/') {
  119. $_file = 'The default config file';
  120. }
  121. $config->getMessage();
  122. $ui->outputData("ERROR: $_file is not a valid config file or is corrupted.");
  123. // We stop, we have no idea where we are :)
  124. exit(1);
  125. }
  126. // this is used in the error handler to retrieve a relative path
  127. $_PEAR_PHPDIR = $config->get('php_dir');
  128. $ui->setConfig($config);
  129. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  130. if (ini_get('safe_mode')) {
  131. $ui->outputData('WARNING: running in safe mode requires that all files created ' .
  132. 'be the same uid as the current script. PHP reports this script is uid: ' .
  133. @getmyuid() . ', and current user is: ' . @get_current_user());
  134. }
  135. $verbose = $config->get("verbose");
  136. $cmdopts = array();
  137. if ($raw) {
  138. if (!$config->isDefinedLayer('user') && !$config->isDefinedLayer('system')) {
  139. $found = false;
  140. foreach ($opts as $opt) {
  141. if ($opt[0] == 'd' || $opt[0] == 'D') {
  142. $found = true; // the user knows what they are doing, and are setting config values
  143. }
  144. }
  145. if (!$found) {
  146. // no prior runs, try to install PEAR
  147. if (strpos(dirname(__FILE__), 'scripts')) {
  148. $packagexml = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'package2.xml';
  149. $pearbase = dirname(dirname(__FILE__));
  150. } else {
  151. $packagexml = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package2.xml';
  152. $pearbase = dirname(__FILE__);
  153. }
  154. if (file_exists($packagexml)) {
  155. $options[1] = array(
  156. 'install',
  157. $packagexml
  158. );
  159. $config->set('php_dir', $pearbase . DIRECTORY_SEPARATOR . 'php');
  160. $config->set('data_dir', $pearbase . DIRECTORY_SEPARATOR . 'data');
  161. $config->set('doc_dir', $pearbase . DIRECTORY_SEPARATOR . 'docs');
  162. $config->set('test_dir', $pearbase . DIRECTORY_SEPARATOR . 'tests');
  163. $config->set('ext_dir', $pearbase . DIRECTORY_SEPARATOR . 'extensions');
  164. $config->set('bin_dir', $pearbase);
  165. $config->mergeConfigFile($pearbase . 'pear.ini', false);
  166. $config->store();
  167. $config->set('auto_discover', 1);
  168. }
  169. }
  170. }
  171. }
  172. foreach ($opts as $opt) {
  173. $param = !empty($opt[1]) ? $opt[1] : true;
  174. switch ($opt[0]) {
  175. case 'd':
  176. if ($param === true) {
  177. die('Invalid usage of "-d" option, expected -d config_value=value, ' .
  178. 'received "-d"' . "\n");
  179. }
  180. $possible = explode('=', $param);
  181. if (count($possible) != 2) {
  182. die('Invalid usage of "-d" option, expected -d config_value=value, received "' .
  183. $param . '"' . "\n");
  184. }
  185. list($key, $value) = explode('=', $param);
  186. $config->set($key, $value, 'user');
  187. break;
  188. case 'D':
  189. if ($param === true) {
  190. die('Invalid usage of "-d" option, expected -d config_value=value, ' .
  191. 'received "-d"' . "\n");
  192. }
  193. $possible = explode('=', $param);
  194. if (count($possible) != 2) {
  195. die('Invalid usage of "-d" option, expected -d config_value=value, received "' .
  196. $param . '"' . "\n");
  197. }
  198. list($key, $value) = explode('=', $param);
  199. $config->set($key, $value, 'system');
  200. break;
  201. case 's':
  202. $store_user_config = true;
  203. break;
  204. case 'S':
  205. $store_system_config = true;
  206. break;
  207. case 'u':
  208. $config->remove($param, 'user');
  209. break;
  210. case 'v':
  211. $config->set('verbose', $config->get('verbose') + 1);
  212. break;
  213. case 'q':
  214. $config->set('verbose', $config->get('verbose') - 1);
  215. break;
  216. case 'V':
  217. usage(null, 'version');
  218. case 'c':
  219. case 'C':
  220. break;
  221. default:
  222. // all non pear params goes to the command
  223. $cmdopts[$opt[0]] = $param;
  224. break;
  225. }
  226. }
  227. if ($store_system_config) {
  228. $config->store('system');
  229. }
  230. if ($store_user_config) {
  231. $config->store('user');
  232. }
  233. $command = (isset($options[1][0])) ? $options[1][0] : null;
  234. if (empty($command) && ($store_user_config || $store_system_config)) {
  235. exit;
  236. }
  237. if ($fetype == 'Gtk' || $fetype == 'Gtk2') {
  238. if (!$config->validConfiguration()) {
  239. PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  240. "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  241. 'file to one of these locations, or use the -c and -s options to create one');
  242. }
  243. Gtk::main();
  244. } else do {
  245. if ($command == 'help') {
  246. usage(null, @$options[1][1]);
  247. }
  248. if (!$config->validConfiguration()) {
  249. PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
  250. "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
  251. 'file to one of these locations, or use the -c and -s options to create one');
  252. }
  253. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  254. $cmd = PEAR_Command::factory($command, $config);
  255. PEAR::popErrorHandling();
  256. if (PEAR::isError($cmd)) {
  257. usage(null, @$options[1][0]);
  258. }
  259. $short_args = $long_args = null;
  260. PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
  261. if (in_array('getopt2', get_class_methods('Console_Getopt'))) {
  262. array_shift($options[1]);
  263. $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
  264. } else {
  265. $tmp = Console_Getopt::getopt($options[1], $short_args, $long_args);
  266. }
  267. if (PEAR::isError($tmp)) {
  268. break;
  269. }
  270. list($tmpopt, $params) = $tmp;
  271. $opts = array();
  272. foreach ($tmpopt as $foo => $tmp2) {
  273. list($opt, $value) = $tmp2;
  274. if ($value === null) {
  275. $value = true; // options without args
  276. }
  277. if (strlen($opt) == 1) {
  278. $cmdoptions = $cmd->getOptions($command);
  279. foreach ($cmdoptions as $o => $d) {
  280. if (@$d['shortopt'] == $opt) {
  281. $opts[$o] = $value;
  282. }
  283. }
  284. } else {
  285. if (substr($opt, 0, 2) == '--') {
  286. $opts[substr($opt, 2)] = $value;
  287. }
  288. }
  289. }
  290. $ok = $cmd->run($command, $opts, $params);
  291. if ($ok === false) {
  292. PEAR::raiseError("unknown command `$command'");
  293. }
  294. if (PEAR::isError($ok)) {
  295. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  296. PEAR::raiseError($ok);
  297. }
  298. } while (false);
  299. // {{{ usage()
  300. function usage($error = null, $helpsubject = null)
  301. {
  302. global $progname, $all_commands;
  303. $stderr = fopen('php://stderr', 'w');
  304. if (PEAR::isError($error)) {
  305. fputs($stderr, $error->getMessage() . "\n");
  306. } elseif ($error !== null) {
  307. fputs($stderr, "$error\n");
  308. }
  309. if ($helpsubject != null) {
  310. $put = cmdHelp($helpsubject);
  311. } else {
  312. $put =
  313. "Commands:\n";
  314. $maxlen = max(array_map("strlen", $all_commands));
  315. $formatstr = "%-{$maxlen}s %s\n";
  316. ksort($all_commands);
  317. foreach ($all_commands as $cmd => $class) {
  318. $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
  319. }
  320. $put .=
  321. "Usage: $progname [options] command [command-options] <parameters>\n".
  322. "Type \"$progname help options\" to list all options.\n".
  323. "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
  324. "Type \"$progname help <command>\" to get the help for the specified command.";
  325. }
  326. fputs($stderr, "$put\n");
  327. fclose($stderr);
  328. exit(1);
  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. } elseif ($command == "version") {
  356. return "PEAR Version: ".$GLOBALS['pear_package_version'].
  357. "\nPHP Version: ".phpversion().
  358. "\nZend Engine Version: ".zend_version().
  359. "\nRunning on: ".php_uname();
  360. } elseif ($help = PEAR_Command::getHelp($command)) {
  361. if (is_string($help)) {
  362. return "$progname $command [options] $help\n";
  363. }
  364. if ($help[1] === null) {
  365. return "$progname $command $help[0]";
  366. } else {
  367. return "$progname $command [options] $help[0]\n$help[1]";
  368. }
  369. }
  370. return "Command '$command' is not valid, try '$progname help'";
  371. }
  372. // }}}
  373. function error_handler($errno, $errmsg, $file, $line, $vars) {
  374. if ((defined('E_STRICT') && $errno & E_STRICT) || (defined('E_DEPRECATED') &&
  375. $errno & E_DEPRECATED) || !error_reporting()) {
  376. if (defined('E_STRICT') && $errno & E_STRICT) {
  377. return; // E_STRICT
  378. }
  379. if (defined('E_DEPRECATED') && $errno & E_DEPRECATED) {
  380. return; // E_DEPRECATED
  381. }
  382. if ($GLOBALS['config']->get('verbose') < 4) {
  383. return false; // @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. return false;
  408. }
  409. /*
  410. * Local variables:
  411. * tab-width: 4
  412. * c-basic-offset: 4
  413. * indent-tabs-mode: nil
  414. * mode: php
  415. * End:
  416. */
  417. // vim600:syn=php
  418. ?>