PageRenderTime 83ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/pear/scripts/pearcmd.php

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