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 * @link http://pear.php.net/package/PEAR
15 * @since File available since Release 0.1
21 require_once 'PEAR.php';
22 require_once 'PEAR/DependencyDB.php';
24 define('PEAR_REGISTRY_ERROR_LOCK', -2);
25 define('PEAR_REGISTRY_ERROR_FORMAT', -3);
26 define('PEAR_REGISTRY_ERROR_FILE', -4);
27 define('PEAR_REGISTRY_ERROR_CONFLICT', -5);
28 define('PEAR_REGISTRY_ERROR_CHANNEL_FILE', -6);
31 * Administration class used to maintain the installed package database.
34 * @author Stig Bakken <ssb@php.net>
35 * @author Tomas V. V. Cox <cox@idecnet.com>
36 * @author Greg Beaver <cellog@php.net>
37 * @copyright 1997-2009 The Authors
38 * @license http://opensource.org/licenses/bsd-license.php New BSD License
39 * @version Release: 1.10.1
40 * @link http://pear.php.net/package/PEAR
41 * @since Class available since Release 1.4.0a1
43 class PEAR_Registry extends PEAR
46 * File containing all channel information.
51 /** Directory where registry files are stored.
56 /** File where the file map is stored
61 /** Directory where registry files for channels are stored.
64 var $channelsdir = '';
66 /** Name of file used for locking the registry
71 /** File descriptor used during locking
76 /** Mode used during locking
79 var $lock_mode = 0; // XXX UNUSED
81 /** Cache of package information. Structure:
83 * 'package' => array('id' => ... ),
87 var $pkginfo_cache = array();
89 /** Cache of file map. Structure:
90 * array( '/path/to/file' => 'package', ... )
93 var $filemap_cache = array();
96 * @var false|PEAR_ChannelFile
101 * @var false|PEAR_ChannelFile
106 * @var false|PEAR_ChannelFile
111 * @var PEAR_DependencyDB
121 * PEAR_Registry constructor.
123 * @param string (optional) PEAR install directory (for .php files)
124 * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PEAR channel, if
125 * default values are not desired. Only used the very first time a PEAR
126 * repository is initialized
127 * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PECL channel, if
128 * default values are not desired. Only used the very first time a PEAR
129 * repository is initialized
133 function __construct($pear_install_dir = PEAR_INSTALL_DIR, $pear_channel = false,
134 $pecl_channel = false, $pear_metadata_dir = '')
136 parent::__construct();
137 $this->setInstallDir($pear_install_dir, $pear_metadata_dir);
138 $this->_pearChannel = $pear_channel;
139 $this->_peclChannel = $pecl_channel;
140 $this->_config = false;
143 function setInstallDir($pear_install_dir = PEAR_INSTALL_DIR, $pear_metadata_dir = '')
145 $ds = DIRECTORY_SEPARATOR;
146 $this->install_dir = $pear_install_dir;
147 if (!$pear_metadata_dir) {
148 $pear_metadata_dir = $pear_install_dir;
150 $this->channelsdir = $pear_metadata_dir.$ds.'.channels';
151 $this->statedir = $pear_metadata_dir.$ds.'.registry';
152 $this->filemap = $pear_metadata_dir.$ds.'.filemap';
153 $this->lockfile = $pear_metadata_dir.$ds.'.lock';
156 function hasWriteAccess()
158 if (!file_exists($this->install_dir)) {
159 $dir = $this->install_dir;
160 while ($dir && $dir != '.') {
162 $dir = dirname($dir);
163 if ($dir != '.' && file_exists($dir)) {
164 if (is_writeable($dir)) {
171 if ($dir == $olddir) { // this can happen in safe mode
172 return @is_writable($dir);
179 return is_writeable($this->install_dir);
182 function setConfig(&$config, $resetInstallDir = true)
184 $this->_config = &$config;
185 if ($resetInstallDir) {
186 $this->setInstallDir($config->get('php_dir'), $config->get('metadata_dir'));
190 function _initializeChannelDirs()
192 static $running = false;
195 $ds = DIRECTORY_SEPARATOR;
196 if (!is_dir($this->channelsdir) ||
197 !file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
198 !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
199 !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
200 !file_exists($this->channelsdir . $ds . '__uri.reg')) {
201 if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
202 $pear_channel = $this->_pearChannel;
203 if (!is_a($pear_channel, 'PEAR_ChannelFile') || !$pear_channel->validate()) {
204 if (!class_exists('PEAR_ChannelFile')) {
205 require_once 'PEAR/ChannelFile.php';
208 $pear_channel = new PEAR_ChannelFile;
209 $pear_channel->setAlias('pear');
210 $pear_channel->setServer('pear.php.net');
211 $pear_channel->setSummary('PHP Extension and Application Repository');
212 $pear_channel->setDefaultPEARProtocols();
213 $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
214 $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
215 $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
216 //$pear_channel->setBaseURL('REST1.4', 'http://pear.php.net/rest/');
218 $pear_channel->setServer('pear.php.net');
219 $pear_channel->setAlias('pear');
222 $pear_channel->validate();
223 $this->_addChannel($pear_channel);
226 if (!file_exists($this->channelsdir . $ds . 'pecl.php.net.reg')) {
227 $pecl_channel = $this->_peclChannel;
228 if (!is_a($pecl_channel, 'PEAR_ChannelFile') || !$pecl_channel->validate()) {
229 if (!class_exists('PEAR_ChannelFile')) {
230 require_once 'PEAR/ChannelFile.php';
233 $pecl_channel = new PEAR_ChannelFile;
234 $pecl_channel->setAlias('pecl');
235 $pecl_channel->setServer('pecl.php.net');
236 $pecl_channel->setSummary('PHP Extension Community Library');
237 $pecl_channel->setDefaultPEARProtocols();
238 $pecl_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
239 $pecl_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
240 $pecl_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
242 $pecl_channel->setServer('pecl.php.net');
243 $pecl_channel->setAlias('pecl');
246 $pecl_channel->validate();
247 $this->_addChannel($pecl_channel);
250 if (!file_exists($this->channelsdir . $ds . 'doc.php.net.reg')) {
251 $doc_channel = $this->_docChannel;
252 if (!is_a($doc_channel, 'PEAR_ChannelFile') || !$doc_channel->validate()) {
253 if (!class_exists('PEAR_ChannelFile')) {
254 require_once 'PEAR/ChannelFile.php';
257 $doc_channel = new PEAR_ChannelFile;
258 $doc_channel->setAlias('phpdocs');
259 $doc_channel->setServer('doc.php.net');
260 $doc_channel->setSummary('PHP Documentation Team');
261 $doc_channel->setDefaultPEARProtocols();
262 $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
263 $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
264 $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
266 $doc_channel->setServer('doc.php.net');
267 $doc_channel->setAlias('doc');
270 $doc_channel->validate();
271 $this->_addChannel($doc_channel);
274 if (!file_exists($this->channelsdir . $ds . '__uri.reg')) {
275 if (!class_exists('PEAR_ChannelFile')) {
276 require_once 'PEAR/ChannelFile.php';
279 $private = new PEAR_ChannelFile;
280 $private->setName('__uri');
281 $private->setDefaultPEARProtocols();
282 $private->setBaseURL('REST1.0', '****');
283 $private->setSummary('Pseudo-channel for static packages');
284 $this->_addChannel($private);
286 $this->_rebuildFileMap();
293 function _initializeDirs()
295 $ds = DIRECTORY_SEPARATOR;
296 // XXX Compatibility code should be removed in the future
297 // rename all registry files if any to lowercase
298 if (!OS_WINDOWS && file_exists($this->statedir) && is_dir($this->statedir) &&
299 $handle = opendir($this->statedir)) {
300 $dest = $this->statedir . $ds;
301 while (false !== ($file = readdir($handle))) {
302 if (preg_match('/^.*[A-Z].*\.reg\\z/', $file)) {
303 rename($dest . $file, $dest . strtolower($file));
309 $this->_initializeChannelDirs();
310 if (!file_exists($this->filemap)) {
311 $this->_rebuildFileMap();
313 $this->_initializeDepDB();
316 function _initializeDepDB()
318 if (!isset($this->_dependencyDB)) {
319 static $initializing = false;
320 if (!$initializing) {
321 $initializing = true;
322 if (!$this->_config) { // never used?
323 $file = OS_WINDOWS ? 'pear.ini' : '.pearrc';
324 $this->_config = new PEAR_Config($this->statedir . DIRECTORY_SEPARATOR .
326 $this->_config->setRegistry($this);
327 $this->_config->set('php_dir', $this->install_dir);
330 $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
331 if (PEAR::isError($this->_dependencyDB)) {
332 // attempt to recover by removing the dep db
333 if (file_exists($this->_config->get('metadata_dir', null, 'pear.php.net') .
334 DIRECTORY_SEPARATOR . '.depdb')) {
335 @unlink($this->_config->get('metadata_dir', null, 'pear.php.net') .
336 DIRECTORY_SEPARATOR . '.depdb');
339 $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
340 if (PEAR::isError($this->_dependencyDB)) {
341 echo $this->_dependencyDB->getMessage();
342 echo 'Unrecoverable error';
347 $initializing = false;
353 * PEAR_Registry destructor. Makes sure no locks are forgotten.
357 function _PEAR_Registry()
360 if (is_resource($this->lock_fp)) {
366 * Make sure the directory where we keep registry files exists.
368 * @return bool TRUE if directory exists, FALSE if it could not be
373 function _assertStateDir($channel = false)
375 if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
376 return $this->_assertChannelStateDir($channel);
379 static $init = false;
380 if (!file_exists($this->statedir)) {
381 if (!$this->hasWriteAccess()) {
385 require_once 'System.php';
386 if (!System::mkdir(array('-p', $this->statedir))) {
387 return $this->raiseError("could not create directory '{$this->statedir}'");
390 } elseif (!is_dir($this->statedir)) {
391 return $this->raiseError('Cannot create directory ' . $this->statedir . ', ' .
392 'it already exists and is not a directory');
395 $ds = DIRECTORY_SEPARATOR;
396 if (!file_exists($this->channelsdir)) {
397 if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
398 !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
399 !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
400 !file_exists($this->channelsdir . $ds . '__uri.reg')) {
403 } elseif (!is_dir($this->channelsdir)) {
404 return $this->raiseError('Cannot create directory ' . $this->channelsdir . ', ' .
405 'it already exists and is not a directory');
409 static $running = false;
412 $this->_initializeDirs();
417 $this->_initializeDepDB();
424 * Make sure the directory where we keep registry files exists for a non-standard channel.
426 * @param string channel name
427 * @return bool TRUE if directory exists, FALSE if it could not be
432 function _assertChannelStateDir($channel)
434 $ds = DIRECTORY_SEPARATOR;
435 if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
436 if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
437 $this->_initializeChannelDirs();
439 return $this->_assertStateDir($channel);
442 $channelDir = $this->_channelDirectoryName($channel);
443 if (!is_dir($this->channelsdir) ||
444 !file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
445 $this->_initializeChannelDirs();
448 if (!file_exists($channelDir)) {
449 if (!$this->hasWriteAccess()) {
453 require_once 'System.php';
454 if (!System::mkdir(array('-p', $channelDir))) {
455 return $this->raiseError("could not create directory '" . $channelDir .
458 } elseif (!is_dir($channelDir)) {
459 return $this->raiseError("could not create directory '" . $channelDir .
460 "', already exists and is not a directory");
467 * Make sure the directory where we keep registry files for channels exists
469 * @return bool TRUE if directory exists, FALSE if it could not be
474 function _assertChannelDir()
476 if (!file_exists($this->channelsdir)) {
477 if (!$this->hasWriteAccess()) {
481 require_once 'System.php';
482 if (!System::mkdir(array('-p', $this->channelsdir))) {
483 return $this->raiseError("could not create directory '{$this->channelsdir}'");
485 } elseif (!is_dir($this->channelsdir)) {
486 return $this->raiseError("could not create directory '{$this->channelsdir}" .
487 "', it already exists and is not a directory");
490 if (!file_exists($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
491 if (!$this->hasWriteAccess()) {
495 require_once 'System.php';
496 if (!System::mkdir(array('-p', $this->channelsdir . DIRECTORY_SEPARATOR . '.alias'))) {
497 return $this->raiseError("could not create directory '{$this->channelsdir}/.alias'");
499 } elseif (!is_dir($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
500 return $this->raiseError("could not create directory '{$this->channelsdir}" .
501 "/.alias', it already exists and is not a directory");
508 * Get the name of the file where data for a given package is stored.
510 * @param string channel name, or false if this is a PEAR package
511 * @param string package name
513 * @return string registry file name
517 function _packageFileName($package, $channel = false)
519 if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
520 return $this->_channelDirectoryName($channel) . DIRECTORY_SEPARATOR .
521 strtolower($package) . '.reg';
524 return $this->statedir . DIRECTORY_SEPARATOR . strtolower($package) . '.reg';
528 * Get the name of the file where data for a given channel is stored.
529 * @param string channel name
530 * @return string registry file name
532 function _channelFileName($channel, $noaliases = false)
535 if (file_exists($this->_getChannelAliasFileName($channel))) {
536 $channel = implode('', file($this->_getChannelAliasFileName($channel)));
539 return $this->channelsdir . DIRECTORY_SEPARATOR . str_replace('/', '_',
540 strtolower($channel)) . '.reg';
547 function _getChannelAliasFileName($alias)
549 return $this->channelsdir . DIRECTORY_SEPARATOR . '.alias' .
550 DIRECTORY_SEPARATOR . str_replace('/', '_', strtolower($alias)) . '.txt';
554 * Get the name of a channel from its alias
556 function _getChannelFromAlias($channel)
558 if (!$this->_channelExists($channel)) {
559 if ($channel == 'pear.php.net') {
560 return 'pear.php.net';
563 if ($channel == 'pecl.php.net') {
564 return 'pecl.php.net';
567 if ($channel == 'doc.php.net') {
568 return 'doc.php.net';
571 if ($channel == '__uri') {
578 $channel = strtolower($channel);
579 if (file_exists($this->_getChannelAliasFileName($channel))) {
580 // translate an alias to an actual channel
581 return implode('', file($this->_getChannelAliasFileName($channel)));
588 * Get the alias of a channel from its alias or its name
590 function _getAlias($channel)
592 if (!$this->_channelExists($channel)) {
593 if ($channel == 'pear.php.net') {
597 if ($channel == 'pecl.php.net') {
601 if ($channel == 'doc.php.net') {
608 $channel = $this->_getChannel($channel);
609 if (PEAR::isError($channel)) {
613 return $channel->getAlias();
617 * Get the name of the file where data for a given package is stored.
619 * @param string channel name, or false if this is a PEAR package
620 * @param string package name
622 * @return string registry file name
626 function _channelDirectoryName($channel)
628 if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
629 return $this->statedir;
632 $ch = $this->_getChannelFromAlias($channel);
637 return $this->statedir . DIRECTORY_SEPARATOR . strtolower('.channel.' .
638 str_replace('/', '_', $ch));
641 function _openPackageFile($package, $mode, $channel = false)
643 if (!$this->_assertStateDir($channel)) {
647 if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
651 $file = $this->_packageFileName($package, $channel);
652 if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
656 $fp = @fopen($file, $mode);
664 function _closePackageFile($fp)
669 function _openChannelFile($channel, $mode)
671 if (!$this->_assertChannelDir()) {
675 if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
679 $file = $this->_channelFileName($channel);
680 if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
684 $fp = @fopen($file, $mode);
692 function _closeChannelFile($fp)
697 function _rebuildFileMap()
699 if (!class_exists('PEAR_Installer_Role')) {
700 require_once 'PEAR/Installer/Role.php';
703 $channels = $this->_listAllPackages();
705 foreach ($channels as $channel => $packages) {
706 foreach ($packages as $package) {
707 $version = $this->_packageInfo($package, 'version', $channel);
708 $filelist = $this->_packageInfo($package, 'filelist', $channel);
709 if (!is_array($filelist)) {
713 foreach ($filelist as $name => $attrs) {
714 if (isset($attrs['attribs'])) {
715 $attrs = $attrs['attribs'];
718 // it is possible for conflicting packages in different channels to
719 // conflict with data files/doc files
720 if ($name == 'dirtree') {
724 if (isset($attrs['role']) && !in_array($attrs['role'],
725 PEAR_Installer_Role::getInstallableRoles())) {
726 // these are not installed
730 if (isset($attrs['role']) && !in_array($attrs['role'],
731 PEAR_Installer_Role::getBaseinstallRoles())) {
732 $attrs['baseinstalldir'] = $package;
735 if (isset($attrs['baseinstalldir'])) {
736 $file = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
741 $file = preg_replace(',^/+,', '', $file);
742 if ($channel != 'pear.php.net') {
743 if (!isset($files[$attrs['role']])) {
744 $files[$attrs['role']] = array();
746 $files[$attrs['role']][$file] = array(strtolower($channel),
747 strtolower($package));
749 if (!isset($files[$attrs['role']])) {
750 $files[$attrs['role']] = array();
752 $files[$attrs['role']][$file] = strtolower($package);
759 $this->_assertStateDir();
760 if (!$this->hasWriteAccess()) {
764 $fp = @fopen($this->filemap, 'wb');
769 $this->filemap_cache = $files;
770 fwrite($fp, serialize($files));
775 function _readFileMap()
777 if (!file_exists($this->filemap)) {
781 $fp = @fopen($this->filemap, 'r');
783 return $this->raiseError('PEAR_Registry: could not open filemap "' . $this->filemap . '"', PEAR_REGISTRY_ERROR_FILE, null, null, $php_errormsg);
787 $fsize = filesize($this->filemap);
789 $data = file_get_contents($this->filemap);
790 $tmp = unserialize($data);
791 if (!$tmp && $fsize > 7) {
792 return $this->raiseError('PEAR_Registry: invalid filemap data', PEAR_REGISTRY_ERROR_FORMAT, null, null, $data);
795 $this->filemap_cache = $tmp;
802 * @param integer lock mode, one of LOCK_EX, LOCK_SH or LOCK_UN.
803 * See flock manual for more information.
805 * @return bool TRUE on success, FALSE if locking failed, or a
806 * PEAR error if some other error occurs (such as the
807 * lock file not being writable).
811 function _lock($mode = LOCK_EX)
813 if (stristr(php_uname(), 'Windows 9')) {
817 if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
818 // XXX does not check type of lock (LOCK_SH/LOCK_EX)
822 if (!$this->_assertStateDir()) {
823 if ($mode == LOCK_EX) {
824 return $this->raiseError('Registry directory is not writeable by the current user');
831 // XXX People reported problems with LOCK_SH and 'w'
832 if ($mode === LOCK_SH || $mode === LOCK_UN) {
833 if (!file_exists($this->lockfile)) {
834 touch($this->lockfile);
839 if (!is_resource($this->lock_fp)) {
840 $this->lock_fp = @fopen($this->lockfile, $open_mode);
843 if (!is_resource($this->lock_fp)) {
844 $this->lock_fp = null;
845 return $this->raiseError("could not create lock file" .
846 (isset($php_errormsg) ? ": " . $php_errormsg : ""));
849 if (!(int)flock($this->lock_fp, $mode)) {
851 case LOCK_SH: $str = 'shared'; break;
852 case LOCK_EX: $str = 'exclusive'; break;
853 case LOCK_UN: $str = 'unlock'; break;
854 default: $str = 'unknown'; break;
857 //is resource at this point, close it on error.
858 fclose($this->lock_fp);
859 $this->lock_fp = null;
860 return $this->raiseError("could not acquire $str lock ($this->lockfile)",
861 PEAR_REGISTRY_ERROR_LOCK);
869 $ret = $this->_lock(LOCK_UN);
870 if (is_resource($this->lock_fp)) {
871 fclose($this->lock_fp);
874 $this->lock_fp = null;
878 function _packageExists($package, $channel = false)
880 return file_exists($this->_packageFileName($package, $channel));
884 * Determine whether a channel exists in the registry
886 * @param string Channel name
887 * @param bool if true, then aliases will be ignored
890 function _channelExists($channel, $noaliases = false)
892 $a = file_exists($this->_channelFileName($channel, $noaliases));
893 if (!$a && $channel == 'pear.php.net') {
897 if (!$a && $channel == 'pecl.php.net') {
901 if (!$a && $channel == 'doc.php.net') {
909 * Determine whether a mirror exists within the deafult channel in the registry
911 * @param string Channel name
912 * @param string Mirror name
916 function _mirrorExists($channel, $mirror)
918 $data = $this->_channelInfo($channel);
919 if (!isset($data['servers']['mirror'])) {
923 foreach ($data['servers']['mirror'] as $m) {
924 if ($m['attribs']['host'] == $mirror) {
933 * @param PEAR_ChannelFile Channel object
935 * @param string Last-Modified HTTP tag from remote request
936 * @return boolean|PEAR_Error True on creation, false if it already exists
938 function _addChannel($channel, $update = false, $lastmodified = false)
940 if (!is_a($channel, 'PEAR_ChannelFile')) {
944 if (!$channel->validate()) {
948 if (file_exists($this->_channelFileName($channel->getName()))) {
953 $checker = $this->_getChannel($channel->getName());
954 if (PEAR::isError($checker)) {
958 if ($channel->getAlias() != $checker->getAlias()) {
959 if (file_exists($this->_getChannelAliasFileName($checker->getAlias()))) {
960 @unlink($this->_getChannelAliasFileName($checker->getAlias()));
964 if ($update && !in_array($channel->getName(), array('pear.php.net', 'pecl.php.net', 'doc.php.net'))) {
969 $ret = $this->_assertChannelDir();
970 if (PEAR::isError($ret)) {
974 $ret = $this->_assertChannelStateDir($channel->getName());
975 if (PEAR::isError($ret)) {
979 if ($channel->getAlias() != $channel->getName()) {
980 if (file_exists($this->_getChannelAliasFileName($channel->getAlias())) &&
981 $this->_getChannelFromAlias($channel->getAlias()) != $channel->getName()) {
982 $channel->setAlias($channel->getName());
985 if (!$this->hasWriteAccess()) {
989 $fp = @fopen($this->_getChannelAliasFileName($channel->getAlias()), 'w');
994 fwrite($fp, $channel->getName());
998 if (!$this->hasWriteAccess()) {
1002 $fp = @fopen($this->_channelFileName($channel->getName()), 'wb');
1007 $info = $channel->toArray();
1008 if ($lastmodified) {
1009 $info['_lastmodified'] = $lastmodified;
1011 $info['_lastmodified'] = date('r');
1014 fwrite($fp, serialize($info));
1020 * Deletion fails if there are any packages installed from the channel
1021 * @param string|PEAR_ChannelFile channel name
1022 * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
1024 function _deleteChannel($channel)
1026 if (!is_string($channel)) {
1027 if (!is_a($channel, 'PEAR_ChannelFile')) {
1031 if (!$channel->validate()) {
1034 $channel = $channel->getName();
1037 if ($this->_getChannelFromAlias($channel) == '__uri') {
1041 if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
1045 if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
1049 if (!$this->_channelExists($channel)) {
1053 if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
1057 $channel = $this->_getChannelFromAlias($channel);
1058 if ($channel == 'pear.php.net') {
1062 $test = $this->_listChannelPackages($channel);
1067 $test = @rmdir($this->_channelDirectoryName($channel));
1072 $file = $this->_getChannelAliasFileName($this->_getAlias($channel));
1073 if (file_exists($file)) {
1074 $test = @unlink($file);
1080 $file = $this->_channelFileName($channel);
1082 if (file_exists($file)) {
1083 $ret = @unlink($file);
1090 * Determine whether a channel exists in the registry
1091 * @param string Channel Alias
1094 function _isChannelAlias($alias)
1096 return file_exists($this->_getChannelAliasFileName($alias));
1100 * @param string|null
1101 * @param string|null
1102 * @param string|null
1103 * @return array|null
1106 function _packageInfo($package = null, $key = null, $channel = 'pear.php.net')
1108 if ($package === null) {
1109 if ($channel === null) {
1110 $channels = $this->_listChannels();
1112 foreach ($channels as $channel) {
1113 $channel = strtolower($channel);
1114 $ret[$channel] = array();
1115 $packages = $this->_listPackages($channel);
1116 foreach ($packages as $package) {
1117 $ret[$channel][] = $this->_packageInfo($package, null, $channel);
1124 $ps = $this->_listPackages($channel);
1128 return array_map(array(&$this, '_packageInfo'),
1129 $ps, array_fill(0, count($ps), null),
1130 array_fill(0, count($ps), $channel));
1133 $fp = $this->_openPackageFile($package, 'r', $channel);
1139 $this->_closePackageFile($fp);
1140 $data = file_get_contents($this->_packageFileName($package, $channel));
1141 $data = unserialize($data);
1142 if ($key === null) {
1146 // compatibility for package.xml version 2.0
1147 if (isset($data['old'][$key])) {
1148 return $data['old'][$key];
1151 if (isset($data[$key])) {
1159 * @param string Channel name
1160 * @param bool whether to strictly retrieve info of channels, not just aliases
1161 * @return array|null
1163 function _channelInfo($channel, $noaliases = false)
1165 if (!$this->_channelExists($channel, $noaliases)) {
1169 $fp = $this->_openChannelFile($channel, 'r');
1175 $this->_closeChannelFile($fp);
1176 $data = file_get_contents($this->_channelFileName($channel));
1177 $data = unserialize($data);
1181 function _listChannels()
1183 $channellist = array();
1184 if (!file_exists($this->channelsdir) || !is_dir($this->channelsdir)) {
1185 return array('pear.php.net', 'pecl.php.net', 'doc.php.net', '__uri');
1188 $dp = opendir($this->channelsdir);
1189 while ($ent = readdir($dp)) {
1190 if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1194 if ($ent == '__uri.reg') {
1195 $channellist[] = '__uri';
1199 $channellist[] = str_replace('_', '/', substr($ent, 0, -4));
1203 if (!in_array('pear.php.net', $channellist)) {
1204 $channellist[] = 'pear.php.net';
1207 if (!in_array('pecl.php.net', $channellist)) {
1208 $channellist[] = 'pecl.php.net';
1211 if (!in_array('doc.php.net', $channellist)) {
1212 $channellist[] = 'doc.php.net';
1216 if (!in_array('__uri', $channellist)) {
1217 $channellist[] = '__uri';
1220 natsort($channellist);
1221 return $channellist;
1224 function _listPackages($channel = false)
1226 if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
1227 return $this->_listChannelPackages($channel);
1230 if (!file_exists($this->statedir) || !is_dir($this->statedir)) {
1235 $dp = opendir($this->statedir);
1240 while ($ent = readdir($dp)) {
1241 if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1245 $pkglist[] = substr($ent, 0, -4);
1251 function _listChannelPackages($channel)
1254 if (!file_exists($this->_channelDirectoryName($channel)) ||
1255 !is_dir($this->_channelDirectoryName($channel))) {
1259 $dp = opendir($this->_channelDirectoryName($channel));
1264 while ($ent = readdir($dp)) {
1265 if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1268 $pkglist[] = substr($ent, 0, -4);
1275 function _listAllPackages()
1278 foreach ($this->_listChannels() as $channel) {
1279 $ret[$channel] = $this->_listPackages($channel);
1286 * Add an installed package to the registry
1287 * @param string package name
1288 * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
1289 * @return bool success of saving
1292 function _addPackage($package, $info)
1294 if ($this->_packageExists($package)) {
1298 $fp = $this->_openPackageFile($package, 'wb');
1303 $info['_lastmodified'] = time();
1304 fwrite($fp, serialize($info));
1305 $this->_closePackageFile($fp);
1306 if (isset($info['filelist'])) {
1307 $this->_rebuildFileMap();
1314 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1318 function _addPackage2($info)
1320 if (!is_a($info, 'PEAR_PackageFile_v1') && !is_a($info, 'PEAR_PackageFile_v2')) {
1324 if (!$info->validate()) {
1325 if (class_exists('PEAR_Common')) {
1326 $ui = PEAR_Frontend::singleton();
1328 foreach ($info->getValidationWarnings() as $err) {
1329 $ui->log($err['message'], true);
1336 $channel = $info->getChannel();
1337 $package = $info->getPackage();
1339 if ($this->_packageExists($package, $channel)) {
1343 if (!$this->_channelExists($channel, true)) {
1347 $info = $info->toArray(true);
1352 $fp = $this->_openPackageFile($package, 'wb', $channel);
1357 $info['_lastmodified'] = time();
1358 fwrite($fp, serialize($info));
1359 $this->_closePackageFile($fp);
1360 $this->_rebuildFileMap();
1365 * @param string Package name
1366 * @param array parsed package.xml 1.0
1367 * @param bool this parameter is only here for BC. Don't use it.
1370 function _updatePackage($package, $info, $merge = true)
1372 $oldinfo = $this->_packageInfo($package);
1373 if (empty($oldinfo)) {
1377 $fp = $this->_openPackageFile($package, 'w');
1382 if (is_object($info)) {
1383 $info = $info->toArray();
1385 $info['_lastmodified'] = time();
1389 $info = array_merge($oldinfo, $info);
1394 fwrite($fp, serialize($info));
1395 $this->_closePackageFile($fp);
1396 if (isset($newinfo['filelist'])) {
1397 $this->_rebuildFileMap();
1404 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1408 function _updatePackage2($info)
1410 if (!$this->_packageExists($info->getPackage(), $info->getChannel())) {
1414 $fp = $this->_openPackageFile($info->getPackage(), 'w', $info->getChannel());
1420 $info = $save->getArray(true);
1421 $info['_lastmodified'] = time();
1422 fwrite($fp, serialize($info));
1423 $this->_closePackageFile($fp);
1424 $this->_rebuildFileMap();
1429 * @param string Package name
1430 * @param string Channel name
1431 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
1434 function &_getPackage($package, $channel = 'pear.php.net')
1436 $info = $this->_packageInfo($package, null, $channel);
1437 if ($info === null) {
1441 $a = $this->_config;
1443 $this->_config = new PEAR_Config;
1444 $this->_config->set('php_dir', $this->statedir);
1447 if (!class_exists('PEAR_PackageFile')) {
1448 require_once 'PEAR/PackageFile.php';
1451 $pkg = new PEAR_PackageFile($this->_config);
1452 $pf = &$pkg->fromArray($info);
1457 * @param string channel name
1458 * @param bool whether to strictly retrieve channel names
1459 * @return PEAR_ChannelFile|PEAR_Error
1462 function &_getChannel($channel, $noaliases = false)
1465 if ($this->_channelExists($channel, $noaliases)) {
1466 $chinfo = $this->_channelInfo($channel, $noaliases);
1468 if (!class_exists('PEAR_ChannelFile')) {
1469 require_once 'PEAR/ChannelFile.php';
1472 $ch = &PEAR_ChannelFile::fromArrayWithErrors($chinfo);
1477 if ($ch->validate()) {
1481 foreach ($ch->getErrors(true) as $err) {
1482 $message = $err['message'] . "\n";
1485 $ch = PEAR::raiseError($message);
1489 if ($this->_getChannelFromAlias($channel) == 'pear.php.net') {
1490 // the registry is not properly set up, so use defaults
1491 if (!class_exists('PEAR_ChannelFile')) {
1492 require_once 'PEAR/ChannelFile.php';
1495 $pear_channel = new PEAR_ChannelFile;
1496 $pear_channel->setServer('pear.php.net');
1497 $pear_channel->setAlias('pear');
1498 $pear_channel->setSummary('PHP Extension and Application Repository');
1499 $pear_channel->setDefaultPEARProtocols();
1500 $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
1501 $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
1502 $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
1503 return $pear_channel;
1506 if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
1507 // the registry is not properly set up, so use defaults
1508 if (!class_exists('PEAR_ChannelFile')) {
1509 require_once 'PEAR/ChannelFile.php';
1511 $pear_channel = new PEAR_ChannelFile;
1512 $pear_channel->setServer('pecl.php.net');
1513 $pear_channel->setAlias('pecl');
1514 $pear_channel->setSummary('PHP Extension Community Library');
1515 $pear_channel->setDefaultPEARProtocols();
1516 $pear_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
1517 $pear_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
1518 $pear_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
1519 return $pear_channel;
1522 if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
1523 // the registry is not properly set up, so use defaults
1524 if (!class_exists('PEAR_ChannelFile')) {
1525 require_once 'PEAR/ChannelFile.php';
1528 $doc_channel = new PEAR_ChannelFile;
1529 $doc_channel->setServer('doc.php.net');
1530 $doc_channel->setAlias('phpdocs');
1531 $doc_channel->setSummary('PHP Documentation Team');
1532 $doc_channel->setDefaultPEARProtocols();
1533 $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
1534 $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
1535 $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
1536 return $doc_channel;
1540 if ($this->_getChannelFromAlias($channel) == '__uri') {
1541 // the registry is not properly set up, so use defaults
1542 if (!class_exists('PEAR_ChannelFile')) {
1543 require_once 'PEAR/ChannelFile.php';
1546 $private = new PEAR_ChannelFile;
1547 $private->setName('__uri');
1548 $private->setDefaultPEARProtocols();
1549 $private->setBaseURL('REST1.0', '****');
1550 $private->setSummary('Pseudo-channel for static packages');
1558 * @param string Package name
1559 * @param string Channel name
1562 function packageExists($package, $channel = 'pear.php.net')
1564 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1567 $ret = $this->_packageExists($package, $channel);
1574 // {{{ channelExists()
1577 * @param string channel name
1578 * @param bool if true, then aliases will be ignored
1581 function channelExists($channel, $noaliases = false)
1583 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1586 $ret = $this->_channelExists($channel, $noaliases);
1594 * @param string channel name mirror is in
1595 * @param string mirror name
1599 function mirrorExists($channel, $mirror)
1601 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1605 $ret = $this->_mirrorExists($channel, $mirror);
1613 * Determines whether the parameter is an alias of a channel
1617 function isAlias($alias)
1619 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1622 $ret = $this->_isChannelAlias($alias);
1628 // {{{ packageInfo()
1631 * @param string|null
1632 * @param string|null
1634 * @return array|null
1636 function packageInfo($package = null, $key = null, $channel = 'pear.php.net')
1638 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1641 $ret = $this->_packageInfo($package, $key, $channel);
1647 // {{{ channelInfo()
1650 * Retrieve a raw array of channel data.
1652 * Do not use this, instead use {@link getChannel()} for normal
1653 * operations. Array structure is undefined in this method
1654 * @param string channel name
1655 * @param bool whether to strictly retrieve information only on non-aliases
1656 * @return array|null|PEAR_Error
1658 function channelInfo($channel = null, $noaliases = false)
1660 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1663 $ret = $this->_channelInfo($channel, $noaliases);
1673 function channelName($channel)
1675 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1678 $ret = $this->_getChannelFromAlias($channel);
1686 function channelAlias($channel)
1688 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1691 $ret = $this->_getAlias($channel);
1695 // {{{ listPackages()
1697 function listPackages($channel = false)
1699 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1702 $ret = $this->_listPackages($channel);
1708 // {{{ listAllPackages()
1710 function listAllPackages()
1712 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1715 $ret = $this->_listAllPackages();
1721 // {{{ listChannel()
1723 function listChannels()
1725 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1728 $ret = $this->_listChannels();
1737 * Add an installed package to the registry
1738 * @param string|PEAR_PackageFile_v1|PEAR_PackageFile_v2 package name or object
1739 * that will be passed to {@link addPackage2()}
1740 * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
1741 * @return bool success of saving
1743 function addPackage($package, $info)
1745 if (is_object($info)) {
1746 return $this->addPackage2($info);
1748 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1751 $ret = $this->_addPackage($package, $info);
1754 if (!class_exists('PEAR_PackageFile_v1')) {
1755 require_once 'PEAR/PackageFile/v1.php';
1757 $pf = new PEAR_PackageFile_v1;
1758 $pf->setConfig($this->_config);
1759 $pf->fromArray($info);
1760 $this->_dependencyDB->uninstallPackage($pf);
1761 $this->_dependencyDB->installPackage($pf);
1767 // {{{ addPackage2()
1769 function addPackage2($info)
1771 if (!is_object($info)) {
1772 return $this->addPackage($info['package'], $info);
1774 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1777 $ret = $this->_addPackage2($info);
1780 $this->_dependencyDB->uninstallPackage($info);
1781 $this->_dependencyDB->installPackage($info);
1787 // {{{ updateChannel()
1790 * For future expandibility purposes, separate this
1791 * @param PEAR_ChannelFile
1793 function updateChannel($channel, $lastmodified = null)
1795 if ($channel->getName() == '__uri') {
1798 return $this->addChannel($channel, $lastmodified, true);
1802 // {{{ deleteChannel()
1805 * Deletion fails if there are any packages installed from the channel
1806 * @param string|PEAR_ChannelFile channel name
1807 * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
1809 function deleteChannel($channel)
1811 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1815 $ret = $this->_deleteChannel($channel);
1817 if ($ret && is_a($this->_config, 'PEAR_Config')) {
1818 $this->_config->setChannels($this->listChannels());
1828 * @param PEAR_ChannelFile Channel object
1829 * @param string Last-Modified header from HTTP for caching
1830 * @return boolean|PEAR_Error True on creation, false if it already exists
1832 function addChannel($channel, $lastmodified = false, $update = false)
1834 if (!is_a($channel, 'PEAR_ChannelFile') || !$channel->validate()) {
1838 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1842 $ret = $this->_addChannel($channel, $update, $lastmodified);
1844 if (!$update && $ret && is_a($this->_config, 'PEAR_Config')) {
1845 $this->_config->setChannels($this->listChannels());
1852 // {{{ deletePackage()
1854 function deletePackage($package, $channel = 'pear.php.net')
1856 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1860 $file = $this->_packageFileName($package, $channel);
1861 $ret = file_exists($file) ? @unlink($file) : false;
1862 $this->_rebuildFileMap();
1864 $p = array('channel' => $channel, 'package' => $package);
1865 $this->_dependencyDB->uninstallPackage($p);
1870 // {{{ updatePackage()
1872 function updatePackage($package, $info, $merge = true)
1874 if (is_object($info)) {
1875 return $this->updatePackage2($info, $merge);
1877 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1880 $ret = $this->_updatePackage($package, $info, $merge);
1883 if (!class_exists('PEAR_PackageFile_v1')) {
1884 require_once 'PEAR/PackageFile/v1.php';
1886 $pf = new PEAR_PackageFile_v1;
1887 $pf->setConfig($this->_config);
1888 $pf->fromArray($this->packageInfo($package));
1889 $this->_dependencyDB->uninstallPackage($pf);
1890 $this->_dependencyDB->installPackage($pf);
1896 // {{{ updatePackage2()
1898 function updatePackage2($info)
1901 if (!is_object($info)) {
1902 return $this->updatePackage($info['package'], $info, $merge);
1905 if (!$info->validate(PEAR_VALIDATE_DOWNLOADING)) {
1909 if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1913 $ret = $this->_updatePackage2($info);
1916 $this->_dependencyDB->uninstallPackage($info);
1917 $this->_dependencyDB->installPackage($info);
1926 * @param string channel name
1927 * @param bool whether to strictly return raw channels (no aliases)
1928 * @return PEAR_ChannelFile|PEAR_Error
1930 function getChannel($channel, $noaliases = false)
1932 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1935 $ret = $this->_getChannel($channel, $noaliases);
1938 return PEAR::raiseError('Unknown channel: ' . $channel);
1946 * @param string package name
1947 * @param string channel name
1948 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
1950 function &getPackage($package, $channel = 'pear.php.net')
1952 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1955 $pf = &$this->_getPackage($package, $channel);
1963 * Get PEAR_PackageFile_v[1/2] objects representing the contents of
1964 * a dependency group that are installed.
1966 * This is used at uninstall-time
1968 * @return array|false
1970 function getInstalledGroup($group)
1973 if (isset($group['package'])) {
1974 if (!isset($group['package'][0])) {
1975 $group['package'] = array($group['package']);
1977 foreach ($group['package'] as $package) {
1978 $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
1979 $p = &$this->getPackage($package['name'], $depchannel);
1986 if (isset($group['subpackage'])) {
1987 if (!isset($group['subpackage'][0])) {
1988 $group['subpackage'] = array($group['subpackage']);
1990 foreach ($group['subpackage'] as $package) {
1991 $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
1992 $p = &$this->getPackage($package['name'], $depchannel);
2005 // {{{ getChannelValidator()
2007 * @param string channel name
2008 * @return PEAR_Validate|false
2010 function &getChannelValidator($channel)
2012 $chan = $this->getChannel($channel);
2013 if (PEAR::isError($chan)) {
2016 $val = $chan->getValidationObject();
2020 // {{{ getChannels()
2022 * @param string channel name
2023 * @return array an array of PEAR_ChannelFile objects representing every installed channel
2025 function &getChannels()
2028 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
2031 foreach ($this->_listChannels() as $channel) {
2032 $e = &$this->_getChannel($channel);
2033 if (!$e || PEAR::isError($e)) {
2043 // {{{ checkFileMap()
2046 * Test whether a file or set of files belongs to a package.
2048 * If an array is passed in
2049 * @param string|array file path, absolute or relative to the pear
2051 * @param string|array name of PEAR package or array('package' => name, 'channel' =>
2052 * channel) of a package that will be ignored
2053 * @param string API version - 1.1 will exclude any files belonging to a package
2054 * @param array private recursion variable
2055 * @return array|false which package and channel the file belongs to, or an empty
2056 * string if the file does not belong to an installed package,
2057 * or belongs to the second parameter's package
2059 function checkFileMap($path, $package = false, $api = '1.0', $attrs = false)
2061 if (is_array($path)) {
2063 if (empty($notempty)) {
2064 if (!class_exists('PEAR_Installer_Role')) {
2065 require_once 'PEAR/Installer/Role.php';
2067 $notempty = create_function('$a','return !empty($a);');
2069 $package = is_array($package) ? array(strtolower($package[0]), strtolower($package[1]))
2070 : strtolower($package);
2072 foreach ($path as $name => $attrs) {
2073 if (is_array($attrs)) {
2074 if (isset($attrs['install-as'])) {
2075 $name = $attrs['install-as'];
2077 if (!in_array($attrs['role'], PEAR_Installer_Role::getInstallableRoles())) {
2078 // these are not installed
2081 if (!in_array($attrs['role'], PEAR_Installer_Role::getBaseinstallRoles())) {
2082 $attrs['baseinstalldir'] = is_array($package) ? $package[1] : $package;
2084 if (isset($attrs['baseinstalldir'])) {
2085 $name = $attrs['baseinstalldir'] . DIRECTORY_SEPARATOR . $name;
2088 $pkgs[$name] = $this->checkFileMap($name, $package, $api, $attrs);
2089 if (PEAR::isError($pkgs[$name])) {
2090 return $pkgs[$name];
2093 return array_filter($pkgs, $notempty);
2095 if (empty($this->filemap_cache)) {
2096 if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
2099 $err = $this->_readFileMap();
2101 if (PEAR::isError($err)) {
2106 $attrs = array('role' => 'php'); // any old call would be for PHP role only
2108 if (isset($this->filemap_cache[$attrs['role']][$path])) {
2109 if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
2112 return $this->filemap_cache[$attrs['role']][$path];
2114 $l = strlen($this->install_dir);
2115 if (substr($path, 0, $l) == $this->install_dir) {
2116 $path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
2118 if (isset($this->filemap_cache[$attrs['role']][$path])) {
2119 if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
2122 return $this->filemap_cache[$attrs['role']][$path];
2130 * Force a reload of the filemap
2133 function flushFileMap()
2135 $this->filemap_cache = null;
2136 clearstatcache(); // ensure that the next read gets the full, current filemap
2142 * Get the expected API version. Channels API is version 1.1, as it is backwards
2143 * compatible with 1.0
2146 function apiVersion()
2154 * Parse a package name, or validate a parsed package name array
2155 * @param string|array pass in an array of format
2157 * 'package' => 'pname',
2158 * ['channel' => 'channame',]
2159 * ['version' => 'version',]
2160 * ['state' => 'state',]
2161 * ['group' => 'groupname'])
2162 * or a string of format
2163 * [channel://][channame/]pname[-version|-state][/group=groupname]
2164 * @return array|PEAR_Error
2166 function parsePackageName($param, $defaultchannel = 'pear.php.net')
2168 $saveparam = $param;
2169 if (is_array($param)) {
2170 // convert to string for error messages
2171 $saveparam = $this->parsedPackageNameToString($param);
2172 // process the array
2173 if (!isset($param['package'])) {
2174 return PEAR::raiseError('parsePackageName(): array $param ' .
2175 'must contain a valid package name in index "param"',
2176 'package', null, null, $param);
2178 if (!isset($param['uri'])) {
2179 if (!isset($param['channel'])) {
2180 $param['channel'] = $defaultchannel;
2183 $param['channel'] = '__uri';
2186 $components = @parse_url((string) $param);
2187 if (isset($components['scheme'])) {
2188 if ($components['scheme'] == 'http') {
2190 $param = array('uri' => $param, 'channel' => '__uri');
2191 } elseif($components['scheme'] != 'channel') {
2192 return PEAR::raiseError('parsePackageName(): only channel:// uris may ' .
2193 'be downloaded, not "' . $param . '"', 'invalid', null, null, $param);
2196 if (!isset($components['path'])) {
2197 return PEAR::raiseError('parsePackageName(): array $param ' .
2198 'must contain a valid package name in "' . $param . '"',
2199 'package', null, null, $param);
2201 if (isset($components['host'])) {
2202 // remove the leading "/"
2203 $components['path'] = substr($components['path'], 1);
2205 if (!isset($components['scheme'])) {
2206 if (strpos($components['path'], '/') !== false) {
2207 if ($components['path']{0} == '/') {
2208 return PEAR::raiseError('parsePackageName(): this is not ' .
2209 'a package name, it begins with "/" in "' . $param . '"',
2210 'invalid', null, null, $param);
2212 $parts = explode('/', $components['path']);
2213 $components['host'] = array_shift($parts);
2214 if (count($parts) > 1) {
2215 $components['path'] = array_pop($parts);
2216 $components['host'] .= '/' . implode('/', $parts);
2218 $components['path'] = implode('/', $parts);
2221 $components['host'] = $defaultchannel;
2224 if (strpos($components['path'], '/')) {
2225 $parts = explode('/', $components['path']);
2226 $components['path'] = array_pop($parts);
2227 $components['host'] .= '/' . implode('/', $parts);
2231 if (is_array($param)) {
2232 $param['package'] = $components['path'];
2235 'package' => $components['path']
2237 if (isset($components['host'])) {
2238 $param['channel'] = $components['host'];
2241 if (isset($components['fragment'])) {
2242 $param['group'] = $components['fragment'];
2244 if (isset($components['user'])) {
2245 $param['user'] = $components['user'];
2247 if (isset($components['pass'])) {
2248 $param['pass'] = $components['pass'];
2250 if (isset($components['query'])) {
2251 parse_str($components['query'], $param['opts']);
2253 // check for extension
2254 $pathinfo = pathinfo($param['package']);
2255 if (isset($pathinfo['extension']) &&
2256 in_array(strtolower($pathinfo['extension']), array('tgz', 'tar'))) {
2257 $param['extension'] = $pathinfo['extension'];
2258 $param['package'] = substr($pathinfo['basename'], 0,
2259 strlen($pathinfo['basename']) - 4);
2261 // check for version
2262 if (strpos($param['package'], '-')) {
2263 $test = explode('-', $param['package']);
2264 if (count($test) != 2) {
2265 return PEAR::raiseError('parsePackageName(): only one version/state ' .
2266 'delimiter "-" is allowed in "' . $saveparam . '"',
2267 'version', null, null, $param);
2269 list($param['package'], $param['version']) = $test;
2273 $info = $this->channelExists($param['channel']);
2274 if (PEAR::isError($info)) {
2278 return PEAR::raiseError('unknown channel "' . $param['channel'] .
2279 '" in "' . $saveparam . '"', 'channel', null, null, $param);
2281 $chan = $this->getChannel($param['channel']);
2282 if (PEAR::isError($chan)) {
2286 return PEAR::raiseError("Exception: corrupt registry, could not " .
2287 "retrieve channel " . $param['channel'] . " information",
2288 'registry', null, null, $param);
2290 $param['channel'] = $chan->getName();
2291 $validate = $chan->getValidationObject();
2292 $vpackage = $chan->getValidationPackage();
2293 // validate package name
2294 if (!$validate->validPackageName($param['package'], $vpackage['_content'])) {
2295 return PEAR::raiseError('parsePackageName(): invalid package name "' .
2296 $param['package'] . '" in "' . $saveparam . '"',
2297 'package', null, null, $param);
2299 if (isset($param['group'])) {
2300 if (!PEAR_Validate::validGroupName($param['group'])) {
2301 return PEAR::raiseError('parsePackageName(): dependency group "' . $param['group'] .
2302 '" is not a valid group name in "' . $saveparam . '"', 'group', null, null,
2306 if (isset($param['state'])) {
2307 if (!in_array(strtolower($param['state']), $validate->getValidStates())) {
2308 return PEAR::raiseError('parsePackageName(): state "' . $param['state']
2309 . '" is not a valid state in "' . $saveparam . '"',
2310 'state', null, null, $param);
2313 if (isset($param['version'])) {
2314 if (isset($param['state'])) {
2315 return PEAR::raiseError('parsePackageName(): cannot contain both ' .
2316 'a version and a stability (state) in "' . $saveparam . '"',
2317 'version/state', null, null, $param);
2319 // check whether version is actually a state
2320 if (in_array(strtolower($param['version']), $validate->getValidStates())) {
2321 $param['state'] = strtolower($param['version']);
2322 unset($param['version']);
2324 if (!$validate->validVersion($param['version'])) {
2325 return PEAR::raiseError('parsePackageName(): "' . $param['version'] .
2326 '" is neither a valid version nor a valid state in "' .
2327 $saveparam . '"', 'version/state', null, null, $param);
2338 function parsedPackageNameToString($parsed, $brief = false)
2340 if (is_string($parsed)) {
2343 if (is_object($parsed)) {
2346 'package' => $p->getPackage(),
2347 'channel' => $p->getChannel(),
2348 'version' => $p->getVersion(),
2351 if (isset($parsed['uri'])) {
2352 return $parsed['uri'];
2355 if ($channel = $this->channelAlias($parsed['channel'])) {
2356 return $channel . '/' . $parsed['package'];
2360 if (isset($parsed['user'])) {
2361 $upass = $parsed['user'];
2362 if (isset($parsed['pass'])) {
2363 $upass .= ':' . $parsed['pass'];
2367 $ret = 'channel://' . $upass . $parsed['channel'] . '/' . $parsed['package'];
2368 if (isset($parsed['version']) || isset($parsed['state'])) {
2369 $ver = isset($parsed['version']) ? $parsed['version'] : '';
2370 $ver .= isset($parsed['state']) ? $parsed['state'] : '';
2373 if (isset($parsed['extension'])) {
2374 $ret .= '.' . $parsed['extension'];
2376 if (isset($parsed['opts'])) {
2378 foreach ($parsed['opts'] as $name => $value) {
2379 $parsed['opts'][$name] = "$name=$value";
2381 $ret .= implode('&', $parsed['opts']);
2383 if (isset($parsed['group'])) {
2384 $ret .= '#' . $parsed['group'];