Bearsampp 2026.9.23
Loading...
Searching...
No Matches
pearcmd.php
Go to the documentation of this file.
1<?php
17
18@ob_end_clean();
19if (!defined('PEAR_RUNTYPE')) {
20 // this is defined in peclcmd.php as 'pecl'
21 define('PEAR_RUNTYPE', 'pear');
22}
23define('PEAR_IGNORE_BACKTRACE', 1);
27//the space is needed for windows include paths with trailing backslash
28// http://pear.php.net/bugs/bug.php?id=19482
29require_once dirname(__DIR__, 4) . '/classes/class.path.php';
30Path::init(dirname(dirname(__DIR__, 4)), dirname(__DIR__, 4));
32ini_set('include_path', $pearDir . PATH_SEPARATOR . get_include_path());
33
47{
48 $runtimeRoot = str_replace('\\', '/', rtrim(dirname($pearDir), '/\\'));
49 $runtimeRootWin = str_replace('/', '\\', $runtimeRoot);
50 $appRootWin = str_replace('/', '\\', dirname($pearDir, 5));
51 $registryDir = $pearDir . '/.registry';
52
53 if (!is_dir($registryDir)) {
54 return;
55 }
56
57 foreach (glob($registryDir . '/*.reg') as $registryFile) {
58 $content = @file_get_contents($registryFile);
59 if ($content === false) {
60 continue;
61 }
62
63 $relocated = preg_replace_callback(
64 '/(s:)\d+(:")([A-Za-z]:\\\\[^"]*|core\\\\libs\\\\php\\\\pear(?:\\\\[^"]*)?)(")/',
65 function ($match) use ($runtimeRoot, $runtimeRootWin, $appRootWin) {
66 $path = $match[3];
67
68 if (stripos($path, $runtimeRootWin) === 0 ||
69 stripos($path, $runtimeRoot) === 0) {
70 return $match[0]; // already points at the runtime location
71 }
72
73 // root-relative value committed for portability:
74 // core\libs\php\pear[anything] -> <approot>\core\libs\php\pear[anything]
75 if (preg_match('#^core\\\\libs\\\\php\\\\pear(?:\\\\[^"]*)?$#i', $path)) {
76 $path = $appRootWin . '\\' . $path;
77 return $match[1] . strlen($path) . $match[2] . $path . $match[4];
78 }
79
80 // legacy absolute path from a previously assembled bundle
81 if (preg_match('#^[A-Za-z]:\\\\[^\r\n"]*?\\\\pear$#i', $path)) {
82 return $match[1] . strlen($runtimeRootWin) . $match[2] .
83 $runtimeRootWin . $match[4];
84 }
85
86 if (preg_match(
87 '#^[A-Za-z]:\\\\[^\r\n"]*?\\\\pear\\\\(pear|tests|docs|data|cfg|scripts|doc)(.*)$#i',
88 $path,
89 $layout
90 )) {
91 $path = $runtimeRootWin . '\\' . $layout[1] . $layout[2];
92 return $match[1] . strlen($path) . $match[2] . $path . $match[4];
93 }
94
95 if (preg_match(
96 '#^[A-Za-z]:\\\\[^\r\n"]*?\\\\pear\\\\([^"\\\\]+)$#i',
97 $path,
98 $file
99 )) {
100 $path = $runtimeRootWin . '\\' . $file[1];
101 return $match[1] . strlen($path) . $match[2] . $path . $match[4];
102 }
103
104 return $match[0];
105 },
106 $content
107 );
108
109 if ($relocated !== null && $relocated !== $content) {
110 @file_put_contents($registryFile, $relocated);
111 }
112 }
113}
114
116$raw = false;
117@ini_set('allow_url_fopen', true);
118@set_time_limit(0);
119ob_implicit_flush(true);
120@ini_set('track_errors', true);
121@ini_set('html_errors', false);
122$_PEAR_PHPDIR = '#$%^&*';
123set_error_handler('error_handler');
124
126
127require_once 'PEAR.php';
128require_once 'PEAR/Frontend.php';
129require_once 'PEAR/Config.php';
130require_once 'PEAR/Command.php';
131require_once 'Console/Getopt.php';
132
133
136
137$argv = Console_Getopt::readPHPArgv();
138// fix CGI sapi oddity - the -- in pear.bat/pear is not removed
139if (php_sapi_name() != 'cli' && isset($argv[1]) && $argv[1] == '--') {
140 unset($argv[1]);
141 $argv = array_values($argv);
142}
144array_shift($argv);
145$options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
146if (PEAR::isError($options)) {
148}
149
151
152$fetype = 'CLI';
153if ($progname == 'gpear' || $progname == 'pear-gtk') {
154 $fetype = 'Gtk2';
155} else {
156 foreach ($opts as $opt) {
157 if ($opt[0] == 'G') {
158 $fetype = 'Gtk2';
159 }
160 }
161}
162
163$pear_user_config = '';
168
169foreach ($opts as $opt) {
170 switch ($opt[0]) {
171 case 'c':
172 $pear_user_config = $opt[1];
173 break;
174 case 'C':
175 $pear_system_config = $opt[1];
176 break;
177 }
178}
179
182$config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
183
184if (PEAR::isError($config)) {
185 $_file = '';
186 if ($pear_user_config !== false) {
187 $_file .= $pear_user_config;
188 }
189 if ($pear_system_config !== false) {
190 $_file .= '/' . $pear_system_config;
191 }
192 if ($_file == '/') {
193 $_file = 'The default config file';
194 }
195 $config->getMessage();
196 $ui->outputData("ERROR: $_file is not a valid config file or is corrupted.");
197 // We stop, we have no idea where we are :)
198 exit(1);
199}
200
201// this is used in the error handler to retrieve a relative path
202$_PEAR_PHPDIR = $config->get('php_dir');
203$ui->setConfig($config);
204PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
205
206$verbose = $config->get("verbose");
207$cmdopts = array();
208
209if ($raw) {
210 if (!$config->isDefinedLayer('user') && !$config->isDefinedLayer('system')) {
211 $found = false;
212 foreach ($opts as $opt) {
213 if ($opt[0] == 'd' || $opt[0] == 'D') {
214 // the user knows what they are doing, and are setting config values
215 $found = true;
216 }
217 }
218 if (!$found) {
219 // no prior runs, try to install PEAR
220 $parent = dirname(__FILE__);
221 if (strpos($parent, 'scripts')) {
222 $grandparent = dirname($parent);
223 $packagexml = $grandparent . DIRECTORY_SEPARATOR . 'package2.xml';
224 $pearbase = $grandparent;
225 } else {
226 $packagexml = $parent . DIRECTORY_SEPARATOR . 'package2.xml';
227 $pearbase = $parent;
228 }
229 if (file_exists($packagexml)) {
230 $options[1] = array(
231 'install',
232 $packagexml
233 );
234 $config->set('php_dir', $pearbase . DIRECTORY_SEPARATOR . 'php');
235 $config->set('data_dir', $pearbase . DIRECTORY_SEPARATOR . 'data');
236 $config->set('doc_dir', $pearbase . DIRECTORY_SEPARATOR . 'docs');
237 $config->set('test_dir', $pearbase . DIRECTORY_SEPARATOR . 'tests');
238 $config->set(
239 'ext_dir',
240 $pearbase . DIRECTORY_SEPARATOR . 'extensions'
241 );
242 $config->set('bin_dir', $pearbase);
243 $config->mergeConfigFile($pearbase . 'pear.ini', false);
244 $config->store();
245 $config->set('auto_discover', 1);
246 }
247 }
248 }
249}
250foreach ($opts as $opt) {
251 $param = !empty($opt[1]) ? $opt[1] : true;
252 switch ($opt[0]) {
253 case 'd':
254 if ($param === true) {
255 die(
256 'Invalid usage of "-d" option, expected -d config_value=value, ' .
257 'received "-d"' . "\n"
258 );
259 }
260 $possible = explode('=', $param);
261 if (count($possible) != 2) {
262 die(
263 'Invalid usage of "-d" option, expected ' .
264 '-d config_value=value, received "' . $param . '"' . "\n"
265 );
266 }
267 list($key, $value) = explode('=', $param);
268 $config->set($key, $value, 'user');
269 break;
270 case 'D':
271 if ($param === true) {
272 die(
273 'Invalid usage of "-d" option, expected ' .
274 '-d config_value=value, received "-d"' . "\n"
275 );
276 }
277 $possible = explode('=', $param);
278 if (count($possible) != 2) {
279 die(
280 'Invalid usage of "-d" option, expected ' .
281 '-d config_value=value, received "' . $param . '"' . "\n"
282 );
283 }
284 list($key, $value) = explode('=', $param);
285 $config->set($key, $value, 'system');
286 break;
287 case 's':
288 $store_user_config = true;
289 break;
290 case 'S':
292 break;
293 case 'u':
294 $config->remove($param, 'user');
295 break;
296 case 'v':
297 $config->set('verbose', $config->get('verbose') + 1);
298 break;
299 case 'q':
300 $config->set('verbose', $config->get('verbose') - 1);
301 break;
302 case 'V':
303 usage(null, 'version');
304 case 'c':
305 case 'C':
306 break;
307 default:
308 // all non pear params goes to the command
309 $cmdopts[$opt[0]] = $param;
310 break;
311 }
312}
313
315 $config->store('system');
316}
317
319 $config->store('user');
320}
321
322$command = (isset($options[1][0])) ? $options[1][0] : null;
324 exit;
325}
326
327if ($fetype == 'Gtk2') {
328 if (!$config->validConfiguration()) {
329 PEAR::raiseError(
330 "CRITICAL ERROR: no existing valid configuration files found in " .
331 "files '$pear_user_config' or '$pear_system_config', " .
332 "please copy an existing configuration file to one of these " .
333 "locations, or use the -c and -s options to create one"
334 );
335 }
336 Gtk::main();
337} else {
338 do {
339 if ($command == 'help') {
340 usage(null, isset($options[1][1]) ? $options[1][1] : null);
341 }
342
343 if (!$config->validConfiguration()) {
344 PEAR::raiseError(
345 "CRITICAL ERROR: no existing valid configuration files found " .
346 "in files '$pear_user_config' or '$pear_system_config', " .
347 "please copy an existing configuration file to one of " .
348 "these locations, or use the -c and -s options to create one"
349 );
350 }
351
352 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
354 PEAR::popErrorHandling();
355 if (PEAR::isError($cmd)) {
356 usage(null, isset($options[1][0]) ? $options[1][0] : null);
357 }
358
359 $short_args = $long_args = null;
360 PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
361 array_shift($options[1]);
362 $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
363
364 if (PEAR::isError($tmp)) {
365 break;
366 }
367
368 list($tmpopt, $params) = $tmp;
369 $opts = array();
370 foreach ($tmpopt as $foo => $tmp2) {
371 list($opt, $value) = $tmp2;
372 if ($value === null) {
373 $value = true; // options without args
374 }
375
376 if (strlen($opt) == 1) {
377 $cmdoptions = $cmd->getOptions($command);
378 foreach ($cmdoptions as $o => $d) {
379 if (isset($d['shortopt']) && $d['shortopt'] == $opt) {
380 $opts[$o] = $value;
381 }
382 }
383 } else {
384 if (substr($opt, 0, 2) == '--') {
385 $opts[substr($opt, 2)] = $value;
386 }
387 }
388 }
389
390 $ok = $cmd->run($command, $opts, $params);
391 if ($ok === false) {
392 PEAR::raiseError("unknown command `$command'");
393 }
394
395 if (PEAR::isError($ok)) {
396 PEAR::setErrorHandling(
397 PEAR_ERROR_CALLBACK, array($ui, "displayFatalError")
398 );
399 PEAR::raiseError($ok);
400 }
401 } while (false);
402}
403
404// {{{ usage()
405
414function usage($error = null, $helpsubject = null)
415{
416 global $progname, $all_commands;
417 $stdout = fopen('php://stdout', 'w');
418 if (PEAR::isError($error)) {
419 fputs($stdout, $error->getMessage() . "\n");
420 } elseif ($error !== null) {
421 fputs($stdout, "$error\n");
422 }
423
424 if ($helpsubject != null) {
425 $put = cmdHelp($helpsubject);
426 } else {
427 $put = "Commands:\n";
428 $maxlen = max(array_map("strlen", $all_commands));
429 $formatstr = "%-{$maxlen}s %s\n";
430 ksort($all_commands);
431 foreach ($all_commands as $cmd => $class) {
432 $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
433 }
434 $put .=
435 "Usage: $progname [options] command [command-options] <parameters>\n".
436 "Type \"$progname help options\" to list all options.\n".
437 "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
438 "Type \"$progname help version\" or ".
439 "\"$progname version\" to list version information.\n".
440 "Type \"$progname help <command>\" to get the help ".
441 "for the specified command.";
442 }
443 fputs($stdout, "$put\n");
444 fclose($stdout);
445
446 if ($error === null) {
447 exit(0);
448 }
449 exit(1);
450}
451
460{
462 if ($command == "options") {
463 return
464 "Options:\n".
465 " -v increase verbosity level (default 1)\n".
466 " -q be quiet, decrease verbosity level\n".
467 " -c file find user configuration in `file'\n".
468 " -C file find system configuration in `file'\n".
469 " -d foo=bar set user config variable `foo' to `bar'\n".
470 " -D foo=bar set system config variable `foo' to `bar'\n".
471 " -G start in graphical (Gtk) mode\n".
472 " -s store user configuration\n".
473 " -S store system configuration\n".
474 " -u foo unset `foo' in the user configuration\n".
475 " -h, -? display help/usage (this message)\n".
476 " -V version information\n";
477 } elseif ($command == "shortcuts") {
479 $ret = "Shortcuts:\n";
480 foreach ($sc as $s => $c) {
481 $ret .= sprintf(" %-8s %s\n", $s, $c);
482 }
483 return $ret;
484
485 } elseif ($command == "version") {
486 return "PEAR Version: ".$GLOBALS['pear_package_version'].
487 "\nPHP Version: ".phpversion().
488 "\nZend Engine Version: ".zend_version().
489 "\nRunning on: ".php_uname();
490
491 } elseif ($help = PEAR_Command::getHelp($command)) {
492 if (is_string($help)) {
493 return "$progname $command [options] $help\n";
494 }
495
496 if ($help[1] === null) {
497 return "$progname $command $help[0]";
498 }
499
500 return "$progname $command [options] $help[0]\n$help[1]";
501 }
502
503 return "Command '$command' is not valid, try '$progname help'";
504}
505
506// }}}
507
519function error_handler($errno, $errmsg, $file, $line)
520{
521 if ((!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80400) && ($errno & E_STRICT)) {
522 return; // E_STRICT
523 }
524 if ($errno & E_DEPRECATED) {
525 return; // E_DEPRECATED
526 }
527 if (!(error_reporting() & $errno) &&
528 isset($GLOBALS['config']) &&
529 $GLOBALS['config']->get('verbose') < 4
530 ) {
531 return false; // @silenced error, show all if debug is high enough
532 }
533 $errortype = array (
534 E_DEPRECATED => 'Deprecated Warning',
535 E_ERROR => "Error",
536 E_WARNING => "Warning",
537 E_PARSE => "Parsing Error",
538 E_NOTICE => "Notice",
539 E_CORE_ERROR => "Core Error",
540 E_CORE_WARNING => "Core Warning",
541 E_COMPILE_ERROR => "Compile Error",
542 E_COMPILE_WARNING => "Compile Warning",
543 E_USER_ERROR => "User Error",
544 E_USER_WARNING => "User Warning",
545 E_USER_NOTICE => "User Notice"
546 );
547 if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 80400) {
548 $errortype[E_STRICT] = 'Strict Warning';
549 }
550 $prefix = $errortype[$errno];
551 global $_PEAR_PHPDIR;
552 if (stristr($file, $_PEAR_PHPDIR)) {
553 $file = substr($file, strlen($_PEAR_PHPDIR) + 1);
554 } else {
555 $file = basename($file);
556 }
557 print "\n$prefix: $errmsg in $file on line $line\n";
558 return false;
559}
560
561
562/*
563 * Local variables:
564 * tab-width: 4
565 * c-basic-offset: 4
566 * indent-tabs-mode: nil
567 * mode: php
568 * End:
569 */
570// vim600:syn=php
const PEAR_ERROR_RETURN
Definition PEAR.php:24
const PEAR_ERROR_CALLBACK
Definition PEAR.php:28
$GLOBALS['_PEAR_default_error_mode']
Definition PEAR.php:46
static getopt2($args, $short_options, $long_options=null, $skip_unknown=false)
Definition Getopt.php:69
static getGetoptArgs($command, &$short_args, &$long_args)
Definition Command.php:335
static getShortcuts()
Definition Command.php:315
static & factory($command, &$config)
Definition Command.php:112
static getCommands()
Definition Command.php:299
static & getFrontendObject()
Definition Command.php:162
static getHelp($command)
Definition Command.php:376
static setFrontendType($uitype)
Definition Command.php:194
static getDescription($command)
Definition Command.php:360
static init($rootPath, $corePath)
static getPearPath($aetrayPath=false)
$options
Definition pearcmd.php:145
$_PEAR_PHPDIR
Definition pearcmd.php:122
$verbose
Definition pearcmd.php:167
$config
Definition pearcmd.php:182
$fetype
Definition pearcmd.php:152
$ui
Definition pearcmd.php:181
$all_commands
Definition pearcmd.php:135
$pear_package_version
Definition pearcmd.php:125
cmdHelp($command)
Definition pearcmd.php:459
if( $raw) foreach($opts as $opt) if( $store_system_config) if($store_user_config) $command
Definition pearcmd.php:322
bearsamppRelocatePearRegistry($pearDir)
Definition pearcmd.php:46
$pearDir
Definition pearcmd.php:31
error_handler($errno, $errmsg, $file, $line)
Definition pearcmd.php:519
$cmdopts
Definition pearcmd.php:207
usage($error=null, $helpsubject=null)
Definition pearcmd.php:414
$pear_system_config
Definition pearcmd.php:164
$store_user_config
Definition pearcmd.php:165
$raw
Definition pearcmd.php:116
if(PEAR::isError($options)) $opts
Definition pearcmd.php:150
$store_system_config
Definition pearcmd.php:166
if(php_sapi_name() !='cli' &&isset($argv[1]) && $argv[1]=='--') $progname
Definition pearcmd.php:143
$argv
Definition pearcmd.php:137
const PEAR_RUNTYPE
Definition peclcmd.php:28
if(!extension_loaded('winbinder')) if(!dl('php_winbinder.dll')) trigger_error("WinBinder extension could not be loaded.\n" E_USER_ERROR
Definition winbinder.php:14