9 * @author Stig Bakken <ssb@php.net>
10 * @author Tomas V. V. Cox <cox@idecnet.com>
11 * @author Greg Beaver <cellog@php.net>
12 * @copyright 1997-2009 The Authors
13 * @license http://opensource.org/licenses/bsd-license.php New BSD License
14 * @version CVS: $Id: Registry.php 313023 2011-07-06 19:17:11Z dufuz $
15 * @link http://pear.php.net/package/PEAR
16 * @since File available since Release 0.1
22 require_once 'PEAR.php';
23 require_once 'PEAR/DependencyDB.php';
25 define('PEAR_REGISTRY_ERROR_LOCK', -2);
26 define('PEAR_REGISTRY_ERROR_FORMAT', -3);
27 define('PEAR_REGISTRY_ERROR_FILE', -4);
28 define('PEAR_REGISTRY_ERROR_CONFLICT', -5);
29 define('PEAR_REGISTRY_ERROR_CHANNEL_FILE', -6);
32 * Administration class used to maintain the installed package database.
35 * @author Stig Bakken <ssb@php.net>
36 * @author Tomas V. V. Cox <cox@idecnet.com>
37 * @author Greg Beaver <cellog@php.net>
38 * @copyright 1997-2009 The Authors
39 * @license http://opensource.org/licenses/bsd-license.php New BSD License
40 * @version Release: 1.9.4
41 * @link http://pear.php.net/package/PEAR
42 * @since Class available since Release 1.4.0a1
44 class PEAR_Registry extends PEAR
47 * File containing all channel information.
52 /** Directory where registry files are stored.
57 /** File where the file map is stored
62 /** Directory where registry files for channels are stored.
65 var $channelsdir = '';
67 /** Name of file used for locking the registry
72 /** File descriptor used during locking
77 /** Mode used during locking
80 var $lock_mode = 0; // XXX UNUSED
82 /** Cache of package information. Structure:
84 * 'package' => array('id' => ... ),
88 var $pkginfo_cache = array();
90 /** Cache of file map. Structure:
91 * array( '/path/to/file' => 'package', ... )
94 var $filemap_cache = array();
97 * @var false|PEAR_ChannelFile
102 * @var false|PEAR_ChannelFile
107 * @var false|PEAR_ChannelFile
112 * @var PEAR_DependencyDB
122 * PEAR_Registry constructor.
124 * @param string (optional) PEAR install directory (for .php files)
125 * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PEAR channel, if
126 * default values are not desired. Only used the very first time a PEAR
127 * repository is initialized
128 * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PECL channel, if
129 * default values are not desired. Only used the very first time a PEAR
130 * repository is initialized
134 function PEAR_Registry($pear_install_dir = PEAR_INSTALL_DIR, $pear_channel = false,
135 $pecl_channel = false)
138 $this->setInstallDir($pear_install_dir);
139 $this->_pearChannel = $pear_channel;
140 $this->_peclChannel = $pecl_channel;
141 $this->_config = false;
144 function setInstallDir($pear_install_dir = PEAR_INSTALL_DIR)
146 $ds = DIRECTORY_SEPARATOR;
147 $this->install_dir = $pear_install_dir;
148 $this->channelsdir = $pear_install_dir.$ds.'.channels';
149 $this->statedir = $pear_install_dir.$ds.'.registry';
150 $this->filemap = $pear_install_dir.$ds.'.filemap';
151 $this->lockfile = $pear_install_dir.$ds.'.lock';
154 function hasWriteAccess()
156 if (!file_exists($this->install_dir)) {
157 $dir = $this->install_dir;
158 while ($dir && $dir != '.') {
160 $dir = dirname($dir);
161 if ($dir != '.' && file_exists($dir)) {
162 if (is_writeable($dir)) {
169 if ($dir == $olddir) { // this can happen in safe mode
170 return @is_writable($dir);
177 return is_writeable($this->install_dir);
180 function setConfig(&$config, $resetInstallDir = true)
182 $this->_config = &$config;
183 if ($resetInstallDir) {
184 $this->setInstallDir($config->get('php_dir'));
188 function _initializeChannelDirs()
190 static $running = false;
193 $ds = DIRECTORY_SEPARATOR;
194 if (!is_dir($this->channelsdir) ||
195 !file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
196 !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
197 !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
198 !file_exists($this->channelsdir . $ds . '__uri.reg')) {
199 if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
200 $pear_channel = $this->_pearChannel;
201 if (!is_a($pear_channel, 'PEAR_ChannelFile') || !$pear_channel->validate()) {
202 if (!class_exists('PEAR_ChannelFile')) {
203 require_once 'PEAR/ChannelFile.php';
206 $pear_channel = new PEAR_ChannelFile;
207 $pear_channel->setAlias('pear');
208 $pear_channel->setServer('pear.php.net');
209 $pear_channel->setSummary('PHP Extension and Application Repository');
210 $pear_channel->setDefaultPEARProtocols();
211 $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
212 $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
213 $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
214 //$pear_channel->setBaseURL('REST1.4', 'http://pear.php.net/rest/');
216 $pear_channel->setServer('pear.php.net');
217 $pear_channel->setAlias('pear');
220 $pear_channel->validate();
221 $this->_addChannel($pear_channel);
224 if (!file_exists($this->channelsdir . $ds . 'pecl.php.net.reg')) {
225 $pecl_channel = $this->_peclChannel;
226 if (!is_a($pecl_channel, 'PEAR_ChannelFile') || !$pecl_channel->validate()) {
227 if (!class_exists('PEAR_ChannelFile')) {
228 require_once 'PEAR/ChannelFile.php';
231 $pecl_channel = new PEAR_ChannelFile;
232 $pecl_channel->setAlias('pecl');
233 $pecl_channel->setServer('pecl.php.net');
234 $pecl_channel->setSummary('PHP Extension Community Library');
235 $pecl_channel->setDefaultPEARProtocols();
236 $pecl_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
237 $pecl_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
238 $pecl_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
240 $pecl_channel->setServer('pecl.php.net');
241 $pecl_channel->setAlias('pecl');
244 $pecl_channel->validate();
245 $this->_addChannel($pecl_channel);
248 if (!file_exists($this->channelsdir . $ds . 'doc.php.net.reg')) {
249 $doc_channel = $this->_docChannel;
250 if (!is_a($doc_channel, 'PEAR_ChannelFile') || !$doc_channel->validate()) {
251 if (!class_exists('PEAR_ChannelFile')) {
252 require_once 'PEAR/ChannelFile.php';
255 $doc_channel = new PEAR_ChannelFile;
256 $doc_channel->setAlias('phpdocs');
257 $doc_channel->setServer('doc.php.net');
258 $doc_channel->setSummary('PHP Documentation Team');
259 $doc_channel->setDefaultPEARProtocols();
260 $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
261 $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
262 $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
264 $doc_channel->setServer('doc.php.net');
265 $doc_channel->setAlias('doc');
268 $doc_channel->validate();
269 $this->_addChannel($doc_channel);
272 if (!file_exists($this->channelsdir . $ds . '__uri.reg')) {
273 if (!class_exists('PEAR_ChannelFile')) {
274 require_once 'PEAR/ChannelFile.php';
277 $private = new PEAR_ChannelFile;
278 $private->setName('__uri');
279 $private->setDefaultPEARProtocols();
280 $private->setBaseURL('REST1.0', '****');
281 $private->setSummary('Pseudo-channel for static packages');
282 $this->_addChannel($private);
284 $this->_rebuildFileMap();
291 function _initializeDirs()
293 $ds = DIRECTORY_SEPARATOR;
294 // XXX Compatibility code should be removed in the future
295 // rename all registry files if any to lowercase
296 if (!OS_WINDOWS && file_exists($this->statedir) && is_dir($this->statedir) &&
297 $handle = opendir($this->statedir)) {
298 $dest = $this->statedir . $ds;
299 while (false !== ($file = readdir($handle))) {
300 if (preg_match('/^.*[A-Z].*\.reg\\z/', $file)) {
301 rename($dest . $file, $dest . strtolower($file));
307 $this->_initializeChannelDirs();
308 if (!file_exists($this->filemap)) {
309 $this->_rebuildFileMap();
311 $this->_initializeDepDB();
314 function _initializeDepDB()
316 if (!isset($this->_dependencyDB)) {
317 static $initializing = false;
318 if (!$initializing) {
319 $initializing = true;
320 if (!$this->_config) { // never used?
321 $file = OS_WINDOWS ? 'pear.ini' : '.pearrc';
322 $this->_config = &new PEAR_Config($this->statedir . DIRECTORY_SEPARATOR .
324 $this->_config->setRegistry($this);
325 $this->_config->set('php_dir', $this->install_dir);
328 $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
329 if (PEAR::isError($this->_dependencyDB)) {
330 // attempt to recover by removing the dep db
331 if (file_exists($this->_config->get('php_dir', null, 'pear.php.net') .
332 DIRECTORY_SEPARATOR . '.depdb')) {
333 @unlink($this->_config->get('php_dir', null, 'pear.php.net') .
334 DIRECTORY_SEPARATOR . '.depdb');
337 $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
338 if (PEAR::isError($this->_dependencyDB)) {
339 echo $this->_dependencyDB->getMessage();
340 echo 'Unrecoverable error';
345 $initializing = false;
351 * PEAR_Registry destructor. Makes sure no locks are forgotten.
355 function _PEAR_Registry()
358 if (is_resource($this->lock_fp)) {
364 * Make sure the directory where we keep registry files exists.
366 * @return bool TRUE if directory exists, FALSE if it could not be
371 function _assertStateDir($channel = false)
373 if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
374 return $this->_assertChannelStateDir($channel);
377 static $init = false;
378 if (!file_exists($this->statedir)) {
379 if (!$this->hasWriteAccess()) {
383 require_once 'System.php';
384 if (!System::mkdir(array('-p', $this->statedir))) {
385 return $this->raiseError("could not create directory '{$this->statedir}'");
388 } elseif (!is_dir($this->statedir)) {
389 return $this->raiseError('Cannot create directory ' . $this->statedir . ', ' .
390 'it already exists and is not a directory');
393 $ds = DIRECTORY_SEPARATOR;
394 if (!file_exists($this->channelsdir)) {
395 if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
396 !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
397 !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
398 !file_exists($this->channelsdir . $ds . '__uri.reg')) {
401 } elseif (!is_dir($this->channelsdir)) {
402 return $this->raiseError('Cannot create directory ' . $this->channelsdir . ', ' .
403 'it already exists and is not a directory');
407 static $running = false;
410 $this->_initializeDirs();
415 $this->_initializeDepDB();
422 * Make sure the directory where we keep registry files exists for a non-standard channel.
424 * @param string channel name
425 * @return bool TRUE if directory exists, FALSE if it could not be
430 function _assertChannelStateDir($channel)
432 $ds = DIRECTORY_SEPARATOR;
433 if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
434 if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
435 $this->_initializeChannelDirs();
437 return $this->_assertStateDir($channel);
440 $channelDir = $this->_channelDirectoryName($channel);
441 if (!is_dir($this->channelsdir) ||
442 !file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
443 $this->_initializeChannelDirs();
446 if (!file_exists($channelDir)) {
447 if (!$this->hasWriteAccess()) {
451 require_once 'System.php';
452 if (!System::mkdir(array('-p', $channelDir))) {
453 return $this->raiseError("could not create directory '" . $channelDir .
456 } elseif (!is_dir($channelDir)) {
457 return $this->raiseError("could not create directory '" . $channelDir .
458 "', already exists and is not a directory");
465 * Make sure the directory where we keep registry files for channels exists
467 * @return bool TRUE if directory exists, FALSE if it could not be
472 function _assertChannelDir()
474 if (!file_exists($this->channelsdir)) {
475 if (!$this->hasWriteAccess()) {
479 require_once 'System.php';
480 if (!System::mkdir(array('-p', $this->channelsdir))) {
481 return $this->raiseError("could not create directory '{$this->channelsdir}'");
483 } elseif (!is_dir($this->channelsdir)) {
484 return $this->raiseError("could not create directory '{$this->channelsdir}" .
485 "', it already exists and is not a directory");
488 if (!file_exists($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
489 if (!$this->hasWriteAccess()) {
493 require_once 'System.php';
494 if (!System::mkdir(array('-p', $this->channelsdir . DIRECTORY_SEPARATOR . '.alias'))) {
495 return $this->raiseError("could not create directory '{$this->channelsdir}/.alias'");
497 } elseif (!is_dir($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
498 return $this->raiseError("could not create directory '{$this->channelsdir}" .
499 "/.alias', it already exists and is not a directory");
506 * Get the name of the file where data for a given package is stored.
508 * @param string channel name, or false if this is a PEAR package
509 * @param string package name
511 * @return string registry file name
515 function _packageFileName($package, $channel = false)
517 if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
518 return $this->_channelDirectoryName($channel) . DIRECTORY_SEPARATOR .
519 strtolower($package) . '.reg';
522 return $this->statedir . DIRECTORY_SEPARATOR . strtolower($package) . '.reg';
526 * Get the name of the file where data for a given channel is stored.
527 * @param string channel name
528 * @return string registry file name
530 function _channelFileName($channel, $noaliases = false)
533 if (file_exists($this->_getChannelAliasFileName($channel))) {
534 $channel = implode('', file($this->_getChannelAliasFileName($channel)));
537 return $this->channelsdir . DIRECTORY_SEPARATOR . str_replace('/', '_',
538 strtolower($channel)) . '.reg';
545 function _getChannelAliasFileName($alias)
547 return $this->channelsdir . DIRECTORY_SEPARATOR . '.alias' .
548 DIRECTORY_SEPARATOR . str_replace('/', '_', strtolower($alias)) . '.txt';
552 * Get the name of a channel from its alias
554 function _getChannelFromAlias($channel)
556 if (!$this->_channelExists($channel)) {
557 if ($channel == 'pear.php.net') {
558 return 'pear.php.net';
561 if ($channel == 'pecl.php.net') {
562 return 'pecl.php.net';
565 if ($channel == 'doc.php.net') {
566 return 'doc.php.net';
569 if ($channel == '__uri') {
576 $channel = strtolower($channel);
577 if (file_exists($this->_getChannelAliasFileName($channel))) {
578 // translate an alias to an actual channel
579 return implode('', file($this->_getChannelAliasFileName($channel)));
586 * Get the alias of a channel from its alias or its name
588 function _getAlias($channel)
590 if (!$this->_channelExists($channel)) {
591 if ($channel == 'pear.php.net') {
595 if ($channel == 'pecl.php.net') {
599 if ($channel == 'doc.php.net') {
606 $channel = $this->_getChannel($channel);
607 if (PEAR::isError($channel)) {
611 return $channel->getAlias();
615 * Get the name of the file where data for a given package is stored.
617 * @param string channel name, or false if this is a PEAR package
618 * @param string package name
620 * @return string registry file name
624 function _channelDirectoryName($channel)
626 if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
627 return $this->statedir;
630 $ch = $this->_getChannelFromAlias($channel);
635 return $this->statedir . DIRECTORY_SEPARATOR . strtolower('.channel.' .
636 str_replace('/', '_', $ch));
639 function _openPackageFile($package, $mode, $channel = false)
641 if (!$this->_assertStateDir($channel)) {
645 if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
649 $file = $this->_packageFileName($package, $channel);
650 if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
654 $fp = @fopen($file, $mode);
662 function _closePackageFile($fp)
667 function _openChannelFile($channel, $mode)
669 if (!$this->_assertChannelDir()) {
673 if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
677 $file = $this->_channelFileName($channel);
678 if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
682 $fp = @fopen($file, $mode);
690 function _closeChannelFile($fp)
695 function _rebuildFileMap()
697 if (!class_exists('PEAR_Installer_Role')) {
698 require_once 'PEAR/Installer/Role.php';
701 $channels = $this->_listAllPackages();
703 foreach ($channels as $channel => $packages) {
704 foreach ($packages as $package) {
705 $version = $this->_packageInfo($package, 'version', $channel);
706 $filelist = $this->_packageInfo($package, 'filelist', $channel);
707 if (!is_array($filelist)) {
711 foreach ($filelist as $name => $attrs) {
712 if (isset($attrs['attribs'])) {
713 $attrs = $attrs['attribs'];
716 // it is possible for conflicting packages in different channels to
717 // conflict with data files/doc files
718 if ($name == 'dirtree') {
722 if (isset($attrs['role']) && !in_array($attrs['role'],
723 PEAR_Installer_Role::getInstallableRoles())) {
724 // these are not installed
728 if (isset($attrs['role']) && !in_array($attrs['role'],
729 PEAR_Installer_Role::getBaseinstallRoles())) {
730 $attrs['baseinstalldir'] = $package;
733 if (isset($attrs['baseinstalldir'])) {
734 $file = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
739 $file = preg_replace(',^/+,', '', $file);
740 if ($channel != 'pear.php.net') {
741 if (!isset($files[$attrs['role']])) {
742 $files[$attrs['role']] = array();
744 $files[$attrs['role']][$file] = array(strtolower($channel),
745 strtolower($package));
747 if (!isset($files[$attrs['role']])) {
748 $files[$attrs['role']] = array();
750 $files[$attrs['role']][$file] = strtolower($package);
757 $this->_assertStateDir();
758 if (!$this->hasWriteAccess()) {
762 $fp = @fopen($this->filemap, 'wb');
767 $this->filemap_cache = $files;
768 fwrite($fp, serialize($files));
773 function _readFileMap()
775 if (!file_exists($this->filemap)) {
779 $fp = @fopen($this->filemap, 'r');
781 return $this->raiseError('PEAR_Registry: could not open filemap "' . $this->filemap . '"', PEAR_REGISTRY_ERROR_FILE, null, null, $php_errormsg);
785 $rt = get_magic_quotes_runtime();
786 set_magic_quotes_runtime(0);
787 $fsize = filesize($this->filemap);
789 $data = file_get_contents($this->filemap);
790 set_magic_quotes_runtime($rt);
791 $tmp = unserialize($data);
792 if (!$tmp && $fsize > 7) {
793 return $this->raiseError('PEAR_Registry: invalid filemap data', PEAR_REGISTRY_ERROR_FORMAT, null, null, $data);
796 $this->filemap_cache = $tmp;
803 * @param integer lock mode, one of LOCK_EX, LOCK_SH or LOCK_UN.
804 * See flock manual for more information.
806 * @return bool TRUE on success, FALSE if locking failed, or a
807 * PEAR error if some other error occurs (such as the
808 * lock file not being writable).
812 function _lock($mode = LOCK_EX)
814 if (stristr(php_uname(), 'Windows 9')) {
818 if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
819 // XXX does not check type of lock (LOCK_SH/LOCK_EX)
823 if (!$this->_assertStateDir()) {
824 if ($mode == LOCK_EX) {
825 return $this->raiseError('Registry directory is not writeable by the current user');
832 // XXX People reported problems with LOCK_SH and 'w'
833 if ($mode === LOCK_SH || $mode === LOCK_UN) {
834 if (!file_exists($this->lockfile)) {
835 touch($this->lockfile);
840 if (!is_resource($this->lock_fp)) {
841 $this->lock_fp = @fopen($this->lockfile, $open_mode);
844 if (!is_resource($this->lock_fp)) {
845 $this->lock_fp = null;
846 return $this->raiseError("could not create lock file" .
847 (isset($php_errormsg) ? ": " . $php_errormsg : ""));
850 if (!(int)flock($this->lock_fp, $mode)) {
852 case LOCK_SH: $str = 'shared'; break;
853 case LOCK_EX: $str = 'exclusive'; break;
854 case LOCK_UN: $str = 'unlock'; break;
855 default: $str = 'unknown'; break;
858 //is resource at this point, close it on error.
859 fclose($this->lock_fp);
860 $this->lock_fp = null;
861 return $this->raiseError("could not acquire $str lock ($this->lockfile)",
862 PEAR_REGISTRY_ERROR_LOCK);
870 $ret = $this->_lock(LOCK_UN);
871 if (is_resource($this->lock_fp)) {
872 fclose($this->lock_fp);
875 $this->lock_fp = null;
879 function _packageExists($package, $channel = false)
881 return file_exists($this->_packageFileName($package, $channel));
885 * Determine whether a channel exists in the registry
887 * @param string Channel name
888 * @param bool if true, then aliases will be ignored
891 function _channelExists($channel, $noaliases = false)
893 $a = file_exists($this->_channelFileName($channel, $noaliases));
894 if (!$a && $channel == 'pear.php.net') {
898 if (!$a && $channel == 'pecl.php.net') {
902 if (!$a && $channel == 'doc.php.net') {
910 * Determine whether a mirror exists within the deafult channel in the registry
912 * @param string Channel name
913 * @param string Mirror name
917 function _mirrorExists($channel, $mirror)
919 $data = $this->_channelInfo($channel);
920 if (!isset($data['servers']['mirror'])) {
924 foreach ($data['servers']['mirror'] as $m) {
925 if ($m['attribs']['host'] == $mirror) {
934 * @param PEAR_ChannelFile Channel object
936 * @param string Last-Modified HTTP tag from remote request
937 * @return boolean|PEAR_Error True on creation, false if it already exists
939 function _addChannel($channel, $update = false, $lastmodified = false)
941 if (!is_a($channel, 'PEAR_ChannelFile')) {
945 if (!$channel->validate()) {
949 if (file_exists($this->_channelFileName($channel->getName()))) {
954 $checker = $this->_getChannel($channel->getName());
955 if (PEAR::isError($checker)) {
959 if ($channel->getAlias() != $checker->getAlias()) {
960 if (file_exists($this->_getChannelAliasFileName($checker->getAlias()))) {
961 @unlink($this->_getChannelAliasFileName($checker->getAlias()));
965 if ($update && !in_array($channel->getName(), array('pear.php.net', 'pecl.php.net', 'doc.php.net'))) {
970 $ret = $this->_assertChannelDir();
971 if (PEAR::isError($ret)) {
975 $ret = $this->_assertChannelStateDir($channel->getName());
976 if (PEAR::isError($ret)) {
980 if ($channel->getAlias() != $channel->getName()) {
981 if (file_exists($this->_getChannelAliasFileName($channel->getAlias())) &&
982 $this->_getChannelFromAlias($channel->getAlias()) != $channel->getName()) {
983 $channel->setAlias($channel->getName());
986 if (!$this->hasWriteAccess()) {
990 $fp = @fopen($this->_getChannelAliasFileName($channel->getAlias()), 'w');
995 fwrite($fp, $channel->getName());
999 if (!$this->hasWriteAccess()) {
1003 $fp = @fopen($this->_channelFileName($channel->getName()), 'wb');
1008 $info = $channel->toArray();
1009 if ($lastmodified) {
1010 $info['_lastmodified'] = $lastmodified;
1012 $info['_lastmodified'] = date('r');
1015 fwrite($fp, serialize($info));
1021 * Deletion fails if there are any packages installed from the channel
1022 * @param string|PEAR_ChannelFile channel name
1023 * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
1025 function _deleteChannel($channel)
1027 if (!is_string($channel)) {
1028 if (!is_a($channel, 'PEAR_ChannelFile')) {
1032 if (!$channel->validate()) {
1035 $channel = $channel->getName();
1038 if ($this->_getChannelFromAlias($channel) == '__uri') {
1042 if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
1046 if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
1050 if (!$this->_channelExists($channel)) {
1054 if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
1058 $channel = $this->_getChannelFromAlias($channel);
1059 if ($channel == 'pear.php.net') {
1063 $test = $this->_listChannelPackages($channel);
1068 $test = @rmdir($this->_channelDirectoryName($channel));
1073 $file = $this->_getChannelAliasFileName($this->_getAlias($channel));
1074 if (file_exists($file)) {
1075 $test = @unlink($file);
1081 $file = $this->_channelFileName($channel);
1083 if (file_exists($file)) {
1084 $ret = @unlink($file);
1091 * Determine whether a channel exists in the registry
1092 * @param string Channel Alias
1095 function _isChannelAlias($alias)
1097 return file_exists($this->_getChannelAliasFileName($alias));
1101 * @param string|null
1102 * @param string|null
1103 * @param string|null
1104 * @return array|null
1107 function _packageInfo($package = null, $key = null, $channel = 'pear.php.net')
1109 if ($package === null) {
1110 if ($channel === null) {
1111 $channels = $this->_listChannels();
1113 foreach ($channels as $channel) {
1114 $channel = strtolower($channel);
1115 $ret[$channel] = array();
1116 $packages = $this->_listPackages($channel);
1117 foreach ($packages as $package) {
1118 $ret[$channel][] = $this->_packageInfo($package, null, $channel);
1125 $ps = $this->_listPackages($channel);
1129 return array_map(array(&$this, '_packageInfo'),
1130 $ps, array_fill(0, count($ps), null),
1131 array_fill(0, count($ps), $channel));
1134 $fp = $this->_openPackageFile($package, 'r', $channel);
1139 $rt = get_magic_quotes_runtime();
1140 set_magic_quotes_runtime(0);
1142 $this->_closePackageFile($fp);
1143 $data = file_get_contents($this->_packageFileName($package, $channel));
1144 set_magic_quotes_runtime($rt);
1145 $data = unserialize($data);
1146 if ($key === null) {
1150 // compatibility for package.xml version 2.0
1151 if (isset($data['old'][$key])) {
1152 return $data['old'][$key];
1155 if (isset($data[$key])) {
1163 * @param string Channel name
1164 * @param bool whether to strictly retrieve info of channels, not just aliases
1165 * @return array|null
1167 function _channelInfo($channel, $noaliases = false)
1169 if (!$this->_channelExists($channel, $noaliases)) {
1173 $fp = $this->_openChannelFile($channel, 'r');
1178 $rt = get_magic_quotes_runtime();
1179 set_magic_quotes_runtime(0);
1181 $this->_closeChannelFile($fp);
1182 $data = file_get_contents($this->_channelFileName($channel));
1183 set_magic_quotes_runtime($rt);
1184 $data = unserialize($data);
1188 function _listChannels()
1190 $channellist = array();
1191 if (!file_exists($this->channelsdir) || !is_dir($this->channelsdir)) {
1192 return array('pear.php.net', 'pecl.php.net', 'doc.php.net', '__uri');
1195 $dp = opendir($this->channelsdir);
1196 while ($ent = readdir($dp)) {
1197 if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1201 if ($ent == '__uri.reg') {
1202 $channellist[] = '__uri';
1206 $channellist[] = str_replace('_', '/', substr($ent, 0, -4));
1210 if (!in_array('pear.php.net', $channellist)) {
1211 $channellist[] = 'pear.php.net';
1214 if (!in_array('pecl.php.net', $channellist)) {
1215 $channellist[] = 'pecl.php.net';
1218 if (!in_array('doc.php.net', $channellist)) {
1219 $channellist[] = 'doc.php.net';
1223 if (!in_array('__uri', $channellist)) {
1224 $channellist[] = '__uri';
1227 natsort($channellist);
1228 return $channellist;
1231 function _listPackages($channel = false)
1233 if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
1234 return $this->_listChannelPackages($channel);
1237 if (!file_exists($this->statedir) || !is_dir($this->statedir)) {
1242 $dp = opendir($this->statedir);
1247 while ($ent = readdir($dp)) {
1248 if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1252 $pkglist[] = substr($ent, 0, -4);
1258 function _listChannelPackages($channel)
1261 if (!file_exists($this->_channelDirectoryName($channel)) ||
1262 !is_dir($this->_channelDirectoryName($channel))) {
1266 $dp = opendir($this->_channelDirectoryName($channel));
1271 while ($ent = readdir($dp)) {
1272 if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1275 $pkglist[] = substr($ent, 0, -4);
1282 function _listAllPackages()
1285 foreach ($this->_listChannels() as $channel) {
1286 $ret[$channel] = $this->_listPackages($channel);
1293 * Add an installed package to the registry
1294 * @param string package name
1295 * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
1296 * @return bool success of saving
1299 function _addPackage($package, $info)
1301 if ($this->_packageExists($package)) {
1305 $fp = $this->_openPackageFile($package, 'wb');
1310 $info['_lastmodified'] = time();
1311 fwrite($fp, serialize($info));
1312 $this->_closePackageFile($fp);
1313 if (isset($info['filelist'])) {
1314 $this->_rebuildFileMap();
1321 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1325 function _addPackage2($info)
1327 if (!is_a($info, 'PEAR_PackageFile_v1') && !is_a($info, 'PEAR_PackageFile_v2')) {
1331 if (!$info->validate()) {
1332 if (class_exists('PEAR_Common')) {
1333 $ui = PEAR_Frontend::singleton();
1335 foreach ($info->getValidationWarnings() as $err) {
1336 $ui->log($err['message'], true);
1343 $channel = $info->getChannel();
1344 $package = $info->getPackage();
1346 if ($this->_packageExists($package, $channel)) {
1350 if (!$this->_channelExists($channel, true)) {
1354 $info = $info->toArray(true);
1359 $fp = $this->_openPackageFile($package, 'wb', $channel);
1364 $info['_lastmodified'] = time();
1365 fwrite($fp, serialize($info));
1366 $this->_closePackageFile($fp);
1367 $this->_rebuildFileMap();
1372 * @param string Package name
1373 * @param array parsed package.xml 1.0
1374 * @param bool this parameter is only here for BC. Don't use it.
1377 function _updatePackage($package, $info, $merge = true)
1379 $oldinfo = $this->_packageInfo($package);
1380 if (empty($oldinfo)) {
1384 $fp = $this->_openPackageFile($package, 'w');
1389 if (is_object($info)) {
1390 $info = $info->toArray();
1392 $info['_lastmodified'] = time();
1396 $info = array_merge($oldinfo, $info);
1401 fwrite($fp, serialize($info));
1402 $this->_closePackageFile($fp);
1403 if (isset($newinfo['filelist'])) {
1404 $this->_rebuildFileMap();
1411 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1415 function _updatePackage2($info)
1417 if (!$this->_packageExists($info->getPackage(), $info->getChannel())) {
1421 $fp = $this->_openPackageFile($info->getPackage(), 'w', $info->getChannel());
1427 $info = $save->getArray(true);
1428 $info['_lastmodified'] = time();
1429 fwrite($fp, serialize($info));
1430 $this->_closePackageFile($fp);
1431 $this->_rebuildFileMap();
1436 * @param string Package name
1437 * @param string Channel name
1438 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
1441 function &_getPackage($package, $channel = 'pear.php.net')
1443 $info = $this->_packageInfo($package, null, $channel);
1444 if ($info === null) {
1448 $a = $this->_config;
1450 $this->_config = &new PEAR_Config;
1451 $this->_config->set('php_dir', $this->statedir);
1454 if (!class_exists('PEAR_PackageFile')) {
1455 require_once 'PEAR/PackageFile.php';
1458 $pkg = &new PEAR_PackageFile($this->_config);
1459 $pf = &$pkg->fromArray($info);
1464 * @param string channel name
1465 * @param bool whether to strictly retrieve channel names
1466 * @return PEAR_ChannelFile|PEAR_Error
1469 function &_getChannel($channel, $noaliases = false)
1472 if ($this->_channelExists($channel, $noaliases)) {
1473 $chinfo = $this->_channelInfo($channel, $noaliases);
1475 if (!class_exists('PEAR_ChannelFile')) {
1476 require_once 'PEAR/ChannelFile.php';
1479 $ch = &PEAR_ChannelFile::fromArrayWithErrors($chinfo);
1484 if ($ch->validate()) {
1488 foreach ($ch->getErrors(true) as $err) {
1489 $message = $err['message'] . "\n";
1492 $ch = PEAR::raiseError($message);
1496 if ($this->_getChannelFromAlias($channel) == 'pear.php.net') {
1497 // the registry is not properly set up, so use defaults
1498 if (!class_exists('PEAR_ChannelFile')) {
1499 require_once 'PEAR/ChannelFile.php';
1502 $pear_channel = new PEAR_ChannelFile;
1503 $pear_channel->setServer('pear.php.net');
1504 $pear_channel->setAlias('pear');
1505 $pear_channel->setSummary('PHP Extension and Application Repository');
1506 $pear_channel->setDefaultPEARProtocols();
1507 $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
1508 $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
1509 $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
1510 return $pear_channel;
1513 if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
1514 // the registry is not properly set up, so use defaults
1515 if (!class_exists('PEAR_ChannelFile')) {
1516 require_once 'PEAR/ChannelFile.php';
1518 $pear_channel = new PEAR_ChannelFile;
1519 $pear_channel->setServer('pecl.php.net');
1520 $pear_channel->setAlias('pecl');
1521 $pear_channel->setSummary('PHP Extension Community Library');
1522 $pear_channel->setDefaultPEARProtocols();
1523 $pear_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
1524 $pear_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
1525 $pear_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
1526 return $pear_channel;
1529 if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
1530 // the registry is not properly set up, so use defaults
1531 if (!class_exists('PEAR_ChannelFile')) {
1532 require_once 'PEAR/ChannelFile.php';
1535 $doc_channel = new PEAR_ChannelFile;
1536 $doc_channel->setServer('doc.php.net');
1537 $doc_channel->setAlias('phpdocs');
1538 $doc_channel->setSummary('PHP Documentation Team');
1539 $doc_channel->setDefaultPEARProtocols();
1540 $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
1541 $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
1542 $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
1543 return $doc_channel;
1547 if ($this->_getChannelFromAlias($channel) == '__uri') {
1548 // the registry is not properly set up, so use defaults
1549 if (!class_exists('PEAR_ChannelFile')) {
1550 require_once 'PEAR/ChannelFile.php';
1553 $private = new PEAR_ChannelFile;
1554 $private->setName('__uri');
1555 $private->setDefaultPEARProtocols();
1556 $private->setBaseURL('REST1.0', '****');
1557 $private->setSummary('Pseudo-channel for static packages');
1565 * @param string Package name
1566 * @param string Channel name
1569 function packageExists($package, $channel = 'pear.php.net')
1571 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1574 $ret = $this->_packageExists($package, $channel);
1581 // {{{ channelExists()
1584 * @param string channel name
1585 * @param bool if true, then aliases will be ignored
1588 function channelExists($channel, $noaliases = false)
1590 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1593 $ret = $this->_channelExists($channel, $noaliases);
1601 * @param string channel name mirror is in
1602 * @param string mirror name
1606 function mirrorExists($channel, $mirror)
1608 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1612 $ret = $this->_mirrorExists($channel, $mirror);
1620 * Determines whether the parameter is an alias of a channel
1624 function isAlias($alias)
1626 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1629 $ret = $this->_isChannelAlias($alias);
1635 // {{{ packageInfo()
1638 * @param string|null
1639 * @param string|null
1641 * @return array|null
1643 function packageInfo($package = null, $key = null, $channel = 'pear.php.net')
1645 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1648 $ret = $this->_packageInfo($package, $key, $channel);
1654 // {{{ channelInfo()
1657 * Retrieve a raw array of channel data.
1659 * Do not use this, instead use {@link getChannel()} for normal
1660 * operations. Array structure is undefined in this method
1661 * @param string channel name
1662 * @param bool whether to strictly retrieve information only on non-aliases
1663 * @return array|null|PEAR_Error
1665 function channelInfo($channel = null, $noaliases = false)
1667 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1670 $ret = $this->_channelInfo($channel, $noaliases);
1680 function channelName($channel)
1682 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1685 $ret = $this->_getChannelFromAlias($channel);
1693 function channelAlias($channel)
1695 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1698 $ret = $this->_getAlias($channel);
1702 // {{{ listPackages()
1704 function listPackages($channel = false)
1706 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1709 $ret = $this->_listPackages($channel);
1715 // {{{ listAllPackages()
1717 function listAllPackages()
1719 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1722 $ret = $this->_listAllPackages();
1728 // {{{ listChannel()
1730 function listChannels()
1732 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1735 $ret = $this->_listChannels();
1744 * Add an installed package to the registry
1745 * @param string|PEAR_PackageFile_v1|PEAR_PackageFile_v2 package name or object
1746 * that will be passed to {@link addPackage2()}
1747 * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
1748 * @return bool success of saving
1750 function addPackage($package, $info)
1752 if (is_object($info)) {
1753 return $this->addPackage2($info);
1755 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1758 $ret = $this->_addPackage($package, $info);
1761 if (!class_exists('PEAR_PackageFile_v1')) {
1762 require_once 'PEAR/PackageFile/v1.php';
1764 $pf = new PEAR_PackageFile_v1;
1765 $pf->setConfig($this->_config);
1766 $pf->fromArray($info);
1767 $this->_dependencyDB->uninstallPackage($pf);
1768 $this->_dependencyDB->installPackage($pf);
1774 // {{{ addPackage2()
1776 function addPackage2($info)
1778 if (!is_object($info)) {
1779 return $this->addPackage($info['package'], $info);
1781 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1784 $ret = $this->_addPackage2($info);
1787 $this->_dependencyDB->uninstallPackage($info);
1788 $this->_dependencyDB->installPackage($info);
1794 // {{{ updateChannel()
1797 * For future expandibility purposes, separate this
1798 * @param PEAR_ChannelFile
1800 function updateChannel($channel, $lastmodified = null)
1802 if ($channel->getName() == '__uri') {
1805 return $this->addChannel($channel, $lastmodified, true);
1809 // {{{ deleteChannel()
1812 * Deletion fails if there are any packages installed from the channel
1813 * @param string|PEAR_ChannelFile channel name
1814 * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
1816 function deleteChannel($channel)
1818 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1822 $ret = $this->_deleteChannel($channel);
1824 if ($ret && is_a($this->_config, 'PEAR_Config')) {
1825 $this->_config->setChannels($this->listChannels());
1835 * @param PEAR_ChannelFile Channel object
1836 * @param string Last-Modified header from HTTP for caching
1837 * @return boolean|PEAR_Error True on creation, false if it already exists
1839 function addChannel($channel, $lastmodified = false, $update = false)
1841 if (!is_a($channel, 'PEAR_ChannelFile') || !$channel->validate()) {
1845 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1849 $ret = $this->_addChannel($channel, $update, $lastmodified);
1851 if (!$update && $ret && is_a($this->_config, 'PEAR_Config')) {
1852 $this->_config->setChannels($this->listChannels());
1859 // {{{ deletePackage()
1861 function deletePackage($package, $channel = 'pear.php.net')
1863 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1867 $file = $this->_packageFileName($package, $channel);
1868 $ret = file_exists($file) ? @unlink($file) : false;
1869 $this->_rebuildFileMap();
1871 $p = array('channel' => $channel, 'package' => $package);
1872 $this->_dependencyDB->uninstallPackage($p);
1877 // {{{ updatePackage()
1879 function updatePackage($package, $info, $merge = true)
1881 if (is_object($info)) {
1882 return $this->updatePackage2($info, $merge);
1884 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1887 $ret = $this->_updatePackage($package, $info, $merge);
1890 if (!class_exists('PEAR_PackageFile_v1')) {
1891 require_once 'PEAR/PackageFile/v1.php';
1893 $pf = new PEAR_PackageFile_v1;
1894 $pf->setConfig($this->_config);
1895 $pf->fromArray($this->packageInfo($package));
1896 $this->_dependencyDB->uninstallPackage($pf);
1897 $this->_dependencyDB->installPackage($pf);
1903 // {{{ updatePackage2()
1905 function updatePackage2($info)
1908 if (!is_object($info)) {
1909 return $this->updatePackage($info['package'], $info, $merge);
1912 if (!$info->validate(PEAR_VALIDATE_DOWNLOADING)) {
1916 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1920 $ret = $this->_updatePackage2($info);
1923 $this->_dependencyDB->uninstallPackage($info);
1924 $this->_dependencyDB->installPackage($info);
1933 * @param string channel name
1934 * @param bool whether to strictly return raw channels (no aliases)
1935 * @return PEAR_ChannelFile|PEAR_Error
1937 function &getChannel($channel, $noaliases = false)
1939 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1942 $ret = &$this->_getChannel($channel, $noaliases);
1945 return PEAR::raiseError('Unknown channel: ' . $channel);
1953 * @param string package name
1954 * @param string channel name
1955 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
1957 function &getPackage($package, $channel = 'pear.php.net')
1959 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1962 $pf = &$this->_getPackage($package, $channel);
1970 * Get PEAR_PackageFile_v[1/2] objects representing the contents of
1971 * a dependency group that are installed.
1973 * This is used at uninstall-time
1975 * @return array|false
1977 function getInstalledGroup($group)
1980 if (isset($group['package'])) {
1981 if (!isset($group['package'][0])) {
1982 $group['package'] = array($group['package']);
1984 foreach ($group['package'] as $package) {
1985 $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
1986 $p = &$this->getPackage($package['name'], $depchannel);
1993 if (isset($group['subpackage'])) {
1994 if (!isset($group['subpackage'][0])) {
1995 $group['subpackage'] = array($group['subpackage']);
1997 foreach ($group['subpackage'] as $package) {
1998 $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
1999 $p = &$this->getPackage($package['name'], $depchannel);
2012 // {{{ getChannelValidator()
2014 * @param string channel name
2015 * @return PEAR_Validate|false
2017 function &getChannelValidator($channel)
2019 $chan = $this->getChannel($channel);
2020 if (PEAR::isError($chan)) {
2023 $val = $chan->getValidationObject();
2027 // {{{ getChannels()
2029 * @param string channel name
2030 * @return array an array of PEAR_ChannelFile objects representing every installed channel
2032 function &getChannels()
2035 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
2038 foreach ($this->_listChannels() as $channel) {
2039 $e = &$this->_getChannel($channel);
2040 if (!$e || PEAR::isError($e)) {
2050 // {{{ checkFileMap()
2053 * Test whether a file or set of files belongs to a package.
2055 * If an array is passed in
2056 * @param string|array file path, absolute or relative to the pear
2058 * @param string|array name of PEAR package or array('package' => name, 'channel' =>
2059 * channel) of a package that will be ignored
2060 * @param string API version - 1.1 will exclude any files belonging to a package
2061 * @param array private recursion variable
2062 * @return array|false which package and channel the file belongs to, or an empty
2063 * string if the file does not belong to an installed package,
2064 * or belongs to the second parameter's package
2066 function checkFileMap($path, $package = false, $api = '1.0', $attrs = false)
2068 if (is_array($path)) {
2070 if (empty($notempty)) {
2071 if (!class_exists('PEAR_Installer_Role')) {
2072 require_once 'PEAR/Installer/Role.php';
2074 $notempty = create_function('$a','return !empty($a);');
2076 $package = is_array($package) ? array(strtolower($package[0]), strtolower($package[1]))
2077 : strtolower($package);
2079 foreach ($path as $name => $attrs) {
2080 if (is_array($attrs)) {
2081 if (isset($attrs['install-as'])) {
2082 $name = $attrs['install-as'];
2084 if (!in_array($attrs['role'], PEAR_Installer_Role::getInstallableRoles())) {
2085 // these are not installed
2088 if (!in_array($attrs['role'], PEAR_Installer_Role::getBaseinstallRoles())) {
2089 $attrs['baseinstalldir'] = is_array($package) ? $package[1] : $package;
2091 if (isset($attrs['baseinstalldir'])) {
2092 $name = $attrs['baseinstalldir'] . DIRECTORY_SEPARATOR . $name;
2095 $pkgs[$name] = $this->checkFileMap($name, $package, $api, $attrs);
2096 if (PEAR::isError($pkgs[$name])) {
2097 return $pkgs[$name];
2100 return array_filter($pkgs, $notempty);
2102 if (empty($this->filemap_cache)) {
2103 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
2106 $err = $this->_readFileMap();
2108 if (PEAR::isError($err)) {
2113 $attrs = array('role' => 'php'); // any old call would be for PHP role only
2115 if (isset($this->filemap_cache[$attrs['role']][$path])) {
2116 if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
2119 return $this->filemap_cache[$attrs['role']][$path];
2121 $l = strlen($this->install_dir);
2122 if (substr($path, 0, $l) == $this->install_dir) {
2123 $path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
2125 if (isset($this->filemap_cache[$attrs['role']][$path])) {
2126 if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
2129 return $this->filemap_cache[$attrs['role']][$path];
2137 * Force a reload of the filemap
2140 function flushFileMap()
2142 $this->filemap_cache = null;
2143 clearstatcache(); // ensure that the next read gets the full, current filemap
2149 * Get the expected API version. Channels API is version 1.1, as it is backwards
2150 * compatible with 1.0
2153 function apiVersion()
2161 * Parse a package name, or validate a parsed package name array
2162 * @param string|array pass in an array of format
2164 * 'package' => 'pname',
2165 * ['channel' => 'channame',]
2166 * ['version' => 'version',]
2167 * ['state' => 'state',]
2168 * ['group' => 'groupname'])
2169 * or a string of format
2170 * [channel://][channame/]pname[-version|-state][/group=groupname]
2171 * @return array|PEAR_Error
2173 function parsePackageName($param, $defaultchannel = 'pear.php.net')
2175 $saveparam = $param;
2176 if (is_array($param)) {
2177 // convert to string for error messages
2178 $saveparam = $this->parsedPackageNameToString($param);
2179 // process the array
2180 if (!isset($param['package'])) {
2181 return PEAR::raiseError('parsePackageName(): array $param ' .
2182 'must contain a valid package name in index "param"',
2183 'package', null, null, $param);
2185 if (!isset($param['uri'])) {
2186 if (!isset($param['channel'])) {
2187 $param['channel'] = $defaultchannel;
2190 $param['channel'] = '__uri';
2193 $components = @parse_url((string) $param);
2194 if (isset($components['scheme'])) {
2195 if ($components['scheme'] == 'http') {
2197 $param = array('uri' => $param, 'channel' => '__uri');
2198 } elseif($components['scheme'] != 'channel') {
2199 return PEAR::raiseError('parsePackageName(): only channel:// uris may ' .
2200 'be downloaded, not "' . $param . '"', 'invalid', null, null, $param);
2203 if (!isset($components['path'])) {
2204 return PEAR::raiseError('parsePackageName(): array $param ' .
2205 'must contain a valid package name in "' . $param . '"',
2206 'package', null, null, $param);
2208 if (isset($components['host'])) {
2209 // remove the leading "/"
2210 $components['path'] = substr($components['path'], 1);
2212 if (!isset($components['scheme'])) {
2213 if (strpos($components['path'], '/') !== false) {
2214 if ($components['path']{0} == '/') {
2215 return PEAR::raiseError('parsePackageName(): this is not ' .
2216 'a package name, it begins with "/" in "' . $param . '"',
2217 'invalid', null, null, $param);
2219 $parts = explode('/', $components['path']);
2220 $components['host'] = array_shift($parts);
2221 if (count($parts) > 1) {
2222 $components['path'] = array_pop($parts);
2223 $components['host'] .= '/' . implode('/', $parts);
2225 $components['path'] = implode('/', $parts);
2228 $components['host'] = $defaultchannel;
2231 if (strpos($components['path'], '/')) {
2232 $parts = explode('/', $components['path']);
2233 $components['path'] = array_pop($parts);
2234 $components['host'] .= '/' . implode('/', $parts);
2238 if (is_array($param)) {
2239 $param['package'] = $components['path'];
2242 'package' => $components['path']
2244 if (isset($components['host'])) {
2245 $param['channel'] = $components['host'];
2248 if (isset($components['fragment'])) {
2249 $param['group'] = $components['fragment'];
2251 if (isset($components['user'])) {
2252 $param['user'] = $components['user'];
2254 if (isset($components['pass'])) {
2255 $param['pass'] = $components['pass'];
2257 if (isset($components['query'])) {
2258 parse_str($components['query'], $param['opts']);
2260 // check for extension
2261 $pathinfo = pathinfo($param['package']);
2262 if (isset($pathinfo['extension']) &&
2263 in_array(strtolower($pathinfo['extension']), array('tgz', 'tar'))) {
2264 $param['extension'] = $pathinfo['extension'];
2265 $param['package'] = substr($pathinfo['basename'], 0,
2266 strlen($pathinfo['basename']) - 4);
2268 // check for version
2269 if (strpos($param['package'], '-')) {
2270 $test = explode('-', $param['package']);
2271 if (count($test) != 2) {
2272 return PEAR::raiseError('parsePackageName(): only one version/state ' .
2273 'delimiter "-" is allowed in "' . $saveparam . '"',
2274 'version', null, null, $param);
2276 list($param['package'], $param['version']) = $test;
2280 $info = $this->channelExists($param['channel']);
2281 if (PEAR::isError($info)) {
2285 return PEAR::raiseError('unknown channel "' . $param['channel'] .
2286 '" in "' . $saveparam . '"', 'channel', null, null, $param);
2288 $chan = $this->getChannel($param['channel']);
2289 if (PEAR::isError($chan)) {
2293 return PEAR::raiseError("Exception: corrupt registry, could not " .
2294 "retrieve channel " . $param['channel'] . " information",
2295 'registry', null, null, $param);
2297 $param['channel'] = $chan->getName();
2298 $validate = $chan->getValidationObject();
2299 $vpackage = $chan->getValidationPackage();
2300 // validate package name
2301 if (!$validate->validPackageName($param['package'], $vpackage['_content'])) {
2302 return PEAR::raiseError('parsePackageName(): invalid package name "' .
2303 $param['package'] . '" in "' . $saveparam . '"',
2304 'package', null, null, $param);
2306 if (isset($param['group'])) {
2307 if (!PEAR_Validate::validGroupName($param['group'])) {
2308 return PEAR::raiseError('parsePackageName(): dependency group "' . $param['group'] .
2309 '" is not a valid group name in "' . $saveparam . '"', 'group', null, null,
2313 if (isset($param['state'])) {
2314 if (!in_array(strtolower($param['state']), $validate->getValidStates())) {
2315 return PEAR::raiseError('parsePackageName(): state "' . $param['state']
2316 . '" is not a valid state in "' . $saveparam . '"',
2317 'state', null, null, $param);
2320 if (isset($param['version'])) {
2321 if (isset($param['state'])) {
2322 return PEAR::raiseError('parsePackageName(): cannot contain both ' .
2323 'a version and a stability (state) in "' . $saveparam . '"',
2324 'version/state', null, null, $param);
2326 // check whether version is actually a state
2327 if (in_array(strtolower($param['version']), $validate->getValidStates())) {
2328 $param['state'] = strtolower($param['version']);
2329 unset($param['version']);
2331 if (!$validate->validVersion($param['version'])) {
2332 return PEAR::raiseError('parsePackageName(): "' . $param['version'] .
2333 '" is neither a valid version nor a valid state in "' .
2334 $saveparam . '"', 'version/state', null, null, $param);
2345 function parsedPackageNameToString($parsed, $brief = false)
2347 if (is_string($parsed)) {
2350 if (is_object($parsed)) {
2353 'package' => $p->getPackage(),
2354 'channel' => $p->getChannel(),
2355 'version' => $p->getVersion(),
2358 if (isset($parsed['uri'])) {
2359 return $parsed['uri'];
2362 if ($channel = $this->channelAlias($parsed['channel'])) {
2363 return $channel . '/' . $parsed['package'];
2367 if (isset($parsed['user'])) {
2368 $upass = $parsed['user'];
2369 if (isset($parsed['pass'])) {
2370 $upass .= ':' . $parsed['pass'];
2374 $ret = 'channel://' . $upass . $parsed['channel'] . '/' . $parsed['package'];
2375 if (isset($parsed['version']) || isset($parsed['state'])) {
2376 $ver = isset($parsed['version']) ? $parsed['version'] : '';
2377 $ver .= isset($parsed['state']) ? $parsed['state'] : '';
2380 if (isset($parsed['extension'])) {
2381 $ret .= '.' . $parsed['extension'];
2383 if (isset($parsed['opts'])) {
2385 foreach ($parsed['opts'] as $name => $value) {
2386 $parsed['opts'][$name] = "$name=$value";
2388 $ret .= implode('&', $parsed['opts']);
2390 if (isset($parsed['group'])) {
2391 $ret .= '#' . $parsed['group'];