PageRenderTime 824ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/bak/PEAR.OLD/pearcmd.php

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