pearcmd.php 15 KB

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