Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Common.php
Go to the documentation of this file.
1<?php
18
22require_once 'PEAR.php';
23
27define('PEAR_COMMON_ERROR_INVALIDPHP', 1);
28define('_PEAR_COMMON_PACKAGE_NAME_PREG', '[A-Za-z][a-zA-Z0-9_]+');
29define('PEAR_COMMON_PACKAGE_NAME_PREG', '/^' . _PEAR_COMMON_PACKAGE_NAME_PREG . '\\z/');
30
31// this should allow: 1, 1.0, 1.0RC1, 1.0dev, 1.0dev123234234234, 1.0a1, 1.0b1, 1.0pl1
32define('_PEAR_COMMON_PACKAGE_VERSION_PREG', '\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?');
33define('PEAR_COMMON_PACKAGE_VERSION_PREG', '/^' . _PEAR_COMMON_PACKAGE_VERSION_PREG . '\\z/i');
34
35// XXX far from perfect :-)
36define('_PEAR_COMMON_PACKAGE_DOWNLOAD_PREG', '(' . _PEAR_COMMON_PACKAGE_NAME_PREG .
37 ')(-([.0-9a-zA-Z]+))?');
38define('PEAR_COMMON_PACKAGE_DOWNLOAD_PREG', '/^' . _PEAR_COMMON_PACKAGE_DOWNLOAD_PREG .
39 '\\z/');
40
41define('_PEAR_CHANNELS_NAME_PREG', '[A-Za-z][a-zA-Z0-9\.]+');
42define('PEAR_CHANNELS_NAME_PREG', '/^' . _PEAR_CHANNELS_NAME_PREG . '\\z/');
43
44// this should allow any dns or IP address, plus a path - NO UNDERSCORES ALLOWED
45define('_PEAR_CHANNELS_SERVER_PREG', '[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*(\/[a-zA-Z0-9\-]+)*');
46define('PEAR_CHANNELS_SERVER_PREG', '/^' . _PEAR_CHANNELS_SERVER_PREG . '\\z/i');
47
48define('_PEAR_CHANNELS_PACKAGE_PREG', '(' ._PEAR_CHANNELS_SERVER_PREG . ')\/('
50define('PEAR_CHANNELS_PACKAGE_PREG', '/^' . _PEAR_CHANNELS_PACKAGE_PREG . '\\z/i');
51
52define('_PEAR_COMMON_CHANNEL_DOWNLOAD_PREG', '(' . _PEAR_CHANNELS_NAME_PREG . ')::('
53 . _PEAR_COMMON_PACKAGE_NAME_PREG . ')(-([.0-9a-zA-Z]+))?');
54define('PEAR_COMMON_CHANNEL_DOWNLOAD_PREG', '/^' . _PEAR_COMMON_CHANNEL_DOWNLOAD_PREG . '\\z/');
55
61$GLOBALS['_PEAR_Common_tempfiles'] = array();
62
67$GLOBALS['_PEAR_Common_maintainer_roles'] = array('lead','developer','contributor','helper');
68
73$GLOBALS['_PEAR_Common_release_states'] = array('alpha','beta','stable','snapshot','devel');
74
79$GLOBALS['_PEAR_Common_dependency_types'] = array('pkg','ext','php','prog','ldlib','rtlib','os','websrv','sapi');
80
85$GLOBALS['_PEAR_Common_dependency_relations'] = array('has','eq','lt','le','gt','ge','not', 'ne');
86
91$GLOBALS['_PEAR_Common_file_roles'] = array('php','ext','test','doc','data','src','script');
92
97$GLOBALS['_PEAR_Common_replacement_types'] = array('php-const', 'pear-config', 'package-info');
98
103$GLOBALS['_PEAR_Common_provide_types'] = array('ext', 'prog', 'class', 'function', 'feature', 'api');
104
109$GLOBALS['_PEAR_Common_script_phases'] = array('pre-install', 'post-install', 'pre-uninstall', 'post-uninstall', 'pre-build', 'post-build', 'pre-configure', 'post-configure', 'pre-setup', 'post-setup');
110
126class PEAR_Common extends PEAR
127{
133 var $ui = null;
134
139 var $config = null;
140
142 var $element_stack = array();
143
146
148 var $current_attributes = array();
149
151 var $pkginfo = array();
152
153 var $current_path = null;
154
161
167 function __construct()
168 {
169 parent::__construct();
170 $this->config = &PEAR_Config::singleton();
171 $this->debug = $this->config->get('verbose');
172 }
173
179 function _PEAR_Common()
180 {
181 // doesn't work due to bug #14744
182 //$tempfiles = $this->_tempfiles;
183 $tempfiles =& $GLOBALS['_PEAR_Common_tempfiles'];
184 while ($file = array_shift($tempfiles)) {
185 if (@is_dir($file)) {
186 if (!class_exists('System')) {
187 require_once 'System.php';
188 }
189
190 System::rm(array('-rf', $file));
191 } elseif (file_exists($file)) {
192 unlink($file);
193 }
194 }
195 }
196
208 static function addTempFile($file)
209 {
210 if (!class_exists('PEAR_Frontend')) {
211 require_once 'PEAR/Frontend.php';
212 }
214 }
215
226 function mkDirHier($dir)
227 {
228 // Only used in Installer - move it there ?
229 $this->log(2, "+ create dir $dir");
230 if (!class_exists('System')) {
231 require_once 'System.php';
232 }
233 return System::mkDir(array('-p', $dir));
234 }
235
244 public function log($level, $msg, $append_crlf = true)
245 {
246 if ($this->debug >= $level) {
247 if (!class_exists('PEAR_Frontend')) {
248 require_once 'PEAR/Frontend.php';
249 }
250
252 if (is_a($ui, 'PEAR_Frontend')) {
253 $ui->log($msg, $append_crlf);
254 } else {
255 print "$msg\n";
256 }
257 }
258 }
259
271 function mkTempDir($tmpdir = '')
272 {
273 $topt = $tmpdir ? array('-t', $tmpdir) : array();
274 $topt = array_merge($topt, array('-d', 'pear'));
275 if (!class_exists('System')) {
276 require_once 'System.php';
277 }
278
279 if (!$tmpdir = System::mktemp($topt)) {
280 return false;
281 }
282
283 self::addTempFile($tmpdir);
284 return $tmpdir;
285 }
286
295 {
296 $this->ui = &$ui;
297 }
298
307 static function betterStates($state, $include = false)
308 {
309 static $states = array('snapshot', 'devel', 'alpha', 'beta', 'stable');
310 $i = array_search($state, $states);
311 if ($i === false) {
312 return false;
313 }
314 if ($include) {
315 $i--;
316 }
317 return array_slice($states, $i + 1);
318 }
319
325 public static function getUserRoles()
326 {
327 return $GLOBALS['_PEAR_Common_maintainer_roles'];
328 }
329
335 public static function getReleaseStates()
336 {
337 return $GLOBALS['_PEAR_Common_release_states'];
338 }
339
345 public static function getDependencyTypes()
346 {
347 return $GLOBALS['_PEAR_Common_dependency_types'];
348 }
349
355 public static function getDependencyRelations()
356 {
357 return $GLOBALS['_PEAR_Common_dependency_relations'];
358 }
359
365 public static function getFileRoles()
366 {
367 return $GLOBALS['_PEAR_Common_file_roles'];
368 }
369
375 public static function getReplacementTypes()
376 {
377 return $GLOBALS['_PEAR_Common_replacement_types'];
378 }
379
385 public static function getProvideTypes()
386 {
387 return $GLOBALS['_PEAR_Common_provide_types'];
388 }
389
395 public static function getScriptPhases()
396 {
397 return $GLOBALS['_PEAR_Common_script_phases'];
398 }
399
409 function validPackageName($name)
410 {
411 return (bool)preg_match(PEAR_COMMON_PACKAGE_NAME_PREG, $name);
412 }
413
423 function validPackageVersion($ver)
424 {
425 return (bool)preg_match(PEAR_COMMON_PACKAGE_VERSION_PREG, $ver);
426 }
427
432 public static function isIncludeable($path)
433 {
434 if (file_exists($path) && is_readable($path)) {
435 return true;
436 }
437
438 $ipath = explode(PATH_SEPARATOR, ini_get('include_path'));
439 foreach ($ipath as $include) {
440 $test = realpath($include . DIRECTORY_SEPARATOR . $path);
441 if (file_exists($test) && is_readable($test)) {
442 return true;
443 }
444 }
445
446 return false;
447 }
448
449 function _postProcessChecks($pf)
450 {
451 if (!PEAR::isError($pf)) {
452 return $this->_postProcessValidPackagexml($pf);
453 }
454
455 $errs = $pf->getUserinfo();
456 if (is_array($errs)) {
457 foreach ($errs as $error) {
458 $e = $this->raiseError($error['message'], $error['code'], null, null, $error);
459 }
460 }
461
462 return $pf;
463 }
464
477 function infoFromTgzFile($file)
478 {
479 $packagefile = new PEAR_PackageFile($this->config);
480 $pf = &$packagefile->fromTgzFile($file, PEAR_VALIDATE_NORMAL);
481 return $this->_postProcessChecks($pf);
482 }
483
496 function infoFromDescriptionFile($descfile)
497 {
498 $packagefile = new PEAR_PackageFile($this->config);
499 $pf = &$packagefile->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
500 return $this->_postProcessChecks($pf);
501 }
502
515 function infoFromString($data)
516 {
517 $packagefile = new PEAR_PackageFile($this->config);
518 $pf = &$packagefile->fromXmlString($data, PEAR_VALIDATE_NORMAL, false);
519 return $this->_postProcessChecks($pf);
520 }
521
527 {
528 if (!is_a($pf, 'PEAR_PackageFile_v2')) {
529 $this->pkginfo = $pf->toArray();
530 return $this->pkginfo;
531 }
532
533 // sort of make this into a package.xml 1.0-style array
534 // changelog is not converted to old format.
535 $arr = $pf->toArray(true);
536 $arr = array_merge($arr, $arr['old']);
537 unset($arr['old'], $arr['xsdversion'], $arr['contents'], $arr['compatible'],
538 $arr['channel'], $arr['uri'], $arr['dependencies'], $arr['phprelease'],
539 $arr['extsrcrelease'], $arr['zendextsrcrelease'], $arr['extbinrelease'],
540 $arr['zendextbinrelease'], $arr['bundle'], $arr['lead'], $arr['developer'],
541 $arr['helper'], $arr['contributor']);
542 $arr['filelist'] = $pf->getFilelist();
543 $this->pkginfo = $arr;
544 return $arr;
545 }
546
558 function infoFromAny($info)
559 {
560 if (is_string($info) && file_exists($info)) {
561 $packagefile = new PEAR_PackageFile($this->config);
562 $pf = &$packagefile->fromAnyFile($info, PEAR_VALIDATE_NORMAL);
563 if (PEAR::isError($pf)) {
564 $errs = $pf->getUserinfo();
565 if (is_array($errs)) {
566 foreach ($errs as $error) {
567 $e = $this->raiseError($error['message'], $error['code'], null, null, $error);
568 }
569 }
570
571 return $pf;
572 }
573
574 return $this->_postProcessValidPackagexml($pf);
575 }
576
577 return $info;
578 }
579
592 {
593 $config = &PEAR_Config::singleton();
594 $packagefile = new PEAR_PackageFile($config);
595 $pf = &$packagefile->fromArray($pkginfo);
596 $gen = &$pf->getDefaultGenerator();
597 return $gen->toXml(PEAR_VALIDATE_PACKAGING);
598 }
599
613 function validatePackageInfo($info, &$errors, &$warnings, $dir_prefix = '')
614 {
615 $config = &PEAR_Config::singleton();
616 $packagefile = new PEAR_PackageFile($config);
618 if (strpos($info, '<?xml') !== false) {
619 $pf = &$packagefile->fromXmlString($info, PEAR_VALIDATE_NORMAL, '');
620 } else {
621 $pf = &$packagefile->fromAnyFile($info, PEAR_VALIDATE_NORMAL);
622 }
623
625 if (PEAR::isError($pf)) {
626 $errs = $pf->getUserinfo();
627 if (is_array($errs)) {
628 foreach ($errs as $error) {
629 if ($error['level'] == 'error') {
630 $errors[] = $error['message'];
631 } else {
632 $warnings[] = $error['message'];
633 }
634 }
635 }
636
637 return false;
638 }
639
640 return true;
641 }
642
662 function buildProvidesArray($srcinfo)
663 {
664 $file = basename($srcinfo['source_file']);
665 $pn = '';
666 if (isset($this->_packageName)) {
667 $pn = $this->_packageName;
668 }
669
670 $pnl = strlen($pn);
671 foreach ($srcinfo['declared_classes'] as $class) {
672 $key = "class;$class";
673 if (isset($this->pkginfo['provides'][$key])) {
674 continue;
675 }
676
677 $this->pkginfo['provides'][$key] =
678 array('file'=> $file, 'type' => 'class', 'name' => $class);
679 if (isset($srcinfo['inheritance'][$class])) {
680 $this->pkginfo['provides'][$key]['extends'] =
681 $srcinfo['inheritance'][$class];
682 }
683 }
684
685 foreach ($srcinfo['declared_methods'] as $class => $methods) {
686 foreach ($methods as $method) {
687 $function = "$class::$method";
688 $key = "function;$function";
689 if ($method[0] == '_' || !strcasecmp($method, $class) ||
690 isset($this->pkginfo['provides'][$key])) {
691 continue;
692 }
693
694 $this->pkginfo['provides'][$key] =
695 array('file'=> $file, 'type' => 'function', 'name' => $function);
696 }
697 }
698
699 foreach ($srcinfo['declared_functions'] as $function) {
700 $key = "function;$function";
701 if ($function[0] == '_' || isset($this->pkginfo['provides'][$key])) {
702 continue;
703 }
704
705 if (!strstr($function, '::') && strncasecmp($function, $pn, $pnl)) {
706 $warnings[] = "in1 " . $file . ": function \"$function\" not prefixed with package name \"$pn\"";
707 }
708
709 $this->pkginfo['provides'][$key] =
710 array('file'=> $file, 'type' => 'function', 'name' => $function);
711 }
712 }
713
721 static function analyzeSourceCode($file)
722 {
723 if (!class_exists('PEAR_PackageFile_v2_Validator')) {
724 require_once 'PEAR/PackageFile/v2/Validator.php';
725 }
726
728 return $a->analyzeSourceCode($file);
729 }
730
731 function detectDependencies($any, $status_callback = null)
732 {
733 if (!function_exists("token_get_all")) {
734 return false;
735 }
736
737 if (PEAR::isError($info = $this->infoFromAny($any))) {
738 return $this->raiseError($info);
739 }
740
741 if (!is_array($info)) {
742 return false;
743 }
744
745 $deps = array();
746 $used_c = $decl_c = $decl_f = $decl_m = array();
747 foreach ($info['filelist'] as $file => $fa) {
748 $tmp = $this->analyzeSourceCode($file);
749 $used_c = @array_merge($used_c, $tmp['used_classes']);
750 $decl_c = @array_merge($decl_c, $tmp['declared_classes']);
751 $decl_f = @array_merge($decl_f, $tmp['declared_functions']);
752 $decl_m = @array_merge($decl_m, $tmp['declared_methods']);
753 $inheri = @array_merge($inheri, $tmp['inheritance']);
754 }
755
756 $used_c = array_unique($used_c);
757 $decl_c = array_unique($decl_c);
758 $undecl_c = array_diff($used_c, $decl_c);
759
760 return array('used_classes' => $used_c,
761 'declared_classes' => $decl_c,
762 'declared_methods' => $decl_m,
763 'declared_functions' => $decl_f,
764 'undeclared_classes' => $undecl_c,
765 'inheritance' => $inheri,
766 );
767 }
768
823 function downloadHttp(
824 $url, &$ui, $save_dir = '.', $callback = null, $lastmodified = null,
825 $accept = false, $channel = false
826 ) {
827 if (!class_exists('PEAR_Downloader')) {
828 require_once 'PEAR/Downloader.php';
829 }
831 $this, $url, $ui, $save_dir, $callback, $lastmodified,
832 $accept, $channel
833 );
834 }
835}
836
837require_once 'PEAR/Config.php';
838require_once 'PEAR/PackageFile.php';
const _PEAR_COMMON_CHANNEL_DOWNLOAD_PREG
Definition Common.php:52
const _PEAR_CHANNELS_NAME_PREG
Definition Common.php:41
const _PEAR_CHANNELS_SERVER_PREG
Definition Common.php:45
const PEAR_COMMON_PACKAGE_VERSION_PREG
Definition Common.php:33
const PEAR_COMMON_PACKAGE_NAME_PREG
Definition Common.php:29
$GLOBALS['_PEAR_Common_tempfiles']
Definition Common.php:61
const _PEAR_COMMON_PACKAGE_DOWNLOAD_PREG
Definition Common.php:36
const _PEAR_CHANNELS_PACKAGE_PREG
Definition Common.php:48
const _PEAR_COMMON_PACKAGE_NAME_PREG
Definition Common.php:28
const _PEAR_COMMON_PACKAGE_VERSION_PREG
Definition Common.php:32
const PEAR_ERROR_RETURN
Definition PEAR.php:24
$GLOBALS['_PEAR_default_error_mode']
Definition PEAR.php:46
const PEAR_VALIDATE_PACKAGING
Definition Validate.php:22
const PEAR_VALIDATE_NORMAL
Definition Validate.php:20
mkDirHier($dir)
Definition Common.php:226
static getFileRoles()
Definition Common.php:365
static getDependencyTypes()
Definition Common.php:345
log($level, $msg, $append_crlf=true)
Definition Common.php:244
static getProvideTypes()
Definition Common.php:385
static getDependencyRelations()
Definition Common.php:355
downloadHttp( $url, &$ui, $save_dir='.', $callback=null, $lastmodified=null, $accept=false, $channel=false)
Definition Common.php:823
validPackageName($name)
Definition Common.php:409
_postProcessValidPackagexml(&$pf)
Definition Common.php:526
$current_attributes
Definition Common.php:148
infoFromAny($info)
Definition Common.php:558
static betterStates($state, $include=false)
Definition Common.php:307
buildProvidesArray($srcinfo)
Definition Common.php:662
infoFromString($data)
Definition Common.php:515
static getReplacementTypes()
Definition Common.php:375
detectDependencies($any, $status_callback=null)
Definition Common.php:731
static addTempFile($file)
Definition Common.php:208
validPackageVersion($ver)
Definition Common.php:423
infoFromDescriptionFile($descfile)
Definition Common.php:496
validatePackageInfo($info, &$errors, &$warnings, $dir_prefix='')
Definition Common.php:613
static getScriptPhases()
Definition Common.php:395
static isIncludeable($path)
Definition Common.php:432
static getReleaseStates()
Definition Common.php:335
infoFromTgzFile($file)
Definition Common.php:477
xmlFromInfo($pkginfo)
Definition Common.php:591
setFrontendObject(&$ui)
Definition Common.php:294
_postProcessChecks($pf)
Definition Common.php:449
static analyzeSourceCode($file)
Definition Common.php:721
static getUserRoles()
Definition Common.php:325
mkTempDir($tmpdir='')
Definition Common.php:271
static _downloadHttp( $object, $url, &$ui, $save_dir='.', $callback=null, $lastmodified=null, $accept=false, $channel=false)
static addTempFile($file)
Definition Frontend.php:163
static & singleton($type=null)
Definition Frontend.php:51
analyzeSourceCode($file, $string=false)
static staticPopErrorHandling()
Definition PEAR.php:654
static staticPushErrorHandling($mode, $options=null)
Definition PEAR.php:619
static isError($data, $code=null)
Definition PEAR.php:306