3 * PEAR_Downloader, the PEAR Installer's download utility class
9 * @author Greg Beaver <cellog@php.net>
10 * @author Stig Bakken <ssb@php.net>
11 * @author Tomas V. V. Cox <cox@idecnet.com>
12 * @author Martin Jansen <mj@php.net>
13 * @copyright 1997-2009 The Authors
14 * @license http://opensource.org/licenses/bsd-license.php New BSD License
15 * @version CVS: $Id: Downloader.php 313024 2011-07-06 19:51:24Z dufuz $
16 * @link http://pear.php.net/package/PEAR
17 * @since File available since Release 1.3.0
21 * Needed for constants, extending
23 require_once 'PEAR/Common.php';
25 define('PEAR_INSTALLER_OK', 1);
26 define('PEAR_INSTALLER_FAILED', 0);
27 define('PEAR_INSTALLER_SKIPPED', -1);
28 define('PEAR_INSTALLER_ERROR_NO_PREF_STATE', 2);
31 * Administration class used to download anything from the internet (PEAR Packages,
32 * static URLs, xml files)
36 * @author Greg Beaver <cellog@php.net>
37 * @author Stig Bakken <ssb@php.net>
38 * @author Tomas V. V. Cox <cox@idecnet.com>
39 * @author Martin Jansen <mj@php.net>
40 * @copyright 1997-2009 The Authors
41 * @license http://opensource.org/licenses/bsd-license.php New BSD License
42 * @version Release: 1.9.4
43 * @link http://pear.php.net/package/PEAR
44 * @since Class available since Release 1.3.0
46 class PEAR_Downloader extends PEAR_Common
55 * Preferred Installation State (snapshot, devel, alpha, beta, stable)
62 * Options from command-line passed to Install.
64 * Recognized options:<br />
65 * - onlyreqdeps : install all required dependencies as well
66 * - alldeps : install all dependencies, including optional
67 * - installroot : base relative path to install files in
68 * - force : force a download even if warnings would prevent it
69 * - nocompress : download uncompressed tarballs
70 * @see PEAR_Command_Install
77 * Downloaded Packages after a call to download().
79 * Format of each entry:
82 * array('pkg' => 'package_name', 'file' => '/path/to/local/file',
83 * 'info' => array() // parsed package.xml
89 var $_downloadedPackages = array();
92 * Packages slated for download.
94 * This is used to prevent downloading a package more than once should it be a dependency
95 * for two packages to be installed.
96 * Format of each entry:
99 * array('package_name1' => parsed package.xml, 'package_name2' => parsed package.xml,
105 var $_toDownload = array();
108 * Array of every package installed, with names lower-cased.
112 * array('package1' => 0, 'package2' => 1, );
116 var $_installed = array();
122 var $_errorStack = array();
128 var $_internalDownload = false;
131 * Temporary variable used in sorting packages by dependency in {@link sortPkgDeps()}
135 var $_packageSortTree;
138 * Temporary directory, or configuration value where downloads will occur
144 * @param PEAR_Frontend_*
148 function PEAR_Downloader(&$ui, $options, &$config)
150 parent::PEAR_Common();
151 $this->_options = $options;
152 $this->config = &$config;
153 $this->_preferredState = $this->config->get('preferred_state');
155 if (!$this->_preferredState) {
156 // don't inadvertantly use a non-set preferred_state
157 $this->_preferredState = null;
160 if (isset($this->_options['installroot'])) {
161 $this->config->setInstallRoot($this->_options['installroot']);
163 $this->_registry = &$config->getRegistry();
165 if (isset($this->_options['alldeps']) || isset($this->_options['onlyreqdeps'])) {
166 $this->_installed = $this->_registry->listAllPackages();
167 foreach ($this->_installed as $key => $unused) {
168 if (!count($unused)) {
171 $strtolower = create_function('$a','return strtolower($a);');
172 array_walk($this->_installed[$key], $strtolower);
178 * Attempt to discover a channel's remote capabilities from
183 function discover($channel)
185 $this->log(1, 'Attempting to discover channel "' . $channel . '"...');
186 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
187 $callback = $this->ui ? array(&$this, '_downloadCallback') : null;
188 if (!class_exists('System')) {
189 require_once 'System.php';
192 $tmpdir = $this->config->get('temp_dir');
193 $tmp = System::mktemp('-d -t "' . $tmpdir . '"');
194 $a = $this->downloadHttp('http://' . $channel . '/channel.xml', $this->ui, $tmp, $callback, false);
195 PEAR::popErrorHandling();
196 if (PEAR::isError($a)) {
197 // Attempt to fallback to https automatically.
198 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
199 $this->log(1, 'Attempting fallback to https instead of http on channel "' . $channel . '"...');
200 $a = $this->downloadHttp('https://' . $channel . '/channel.xml', $this->ui, $tmp, $callback, false);
201 PEAR::popErrorHandling();
202 if (PEAR::isError($a)) {
207 list($a, $lastmodified) = $a;
208 if (!class_exists('PEAR_ChannelFile')) {
209 require_once 'PEAR/ChannelFile.php';
212 $b = new PEAR_ChannelFile;
213 if ($b->fromXmlFile($a)) {
215 if ($this->config->get('auto_discover')) {
216 $this->_registry->addChannel($b, $lastmodified);
217 $alias = $b->getName();
218 if ($b->getName() == $this->_registry->channelName($b->getAlias())) {
219 $alias = $b->getAlias();
222 $this->log(1, 'Auto-discovered channel "' . $channel .
223 '", alias "' . $alias . '", adding to registry');
234 * For simpler unit-testing
235 * @param PEAR_Downloader
236 * @return PEAR_Downloader_Package
238 function &newDownloaderPackage(&$t)
240 if (!class_exists('PEAR_Downloader_Package')) {
241 require_once 'PEAR/Downloader/Package.php';
243 $a = &new PEAR_Downloader_Package($t);
248 * For simpler unit-testing
254 function &getDependency2Object(&$c, $i, $p, $s)
256 if (!class_exists('PEAR_Dependency2')) {
257 require_once 'PEAR/Dependency2.php';
259 $z = &new PEAR_Dependency2($c, $i, $p, $s);
263 function &download($params)
265 if (!count($params)) {
270 if (!isset($this->_registry)) {
271 $this->_registry = &$this->config->getRegistry();
274 $channelschecked = array();
275 // convert all parameters into PEAR_Downloader_Package objects
276 foreach ($params as $i => $param) {
277 $params[$i] = &$this->newDownloaderPackage($this);
278 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
279 $err = $params[$i]->initialize($param);
280 PEAR::staticPopErrorHandling();
282 // skip parameters that were missed by preferred_state
286 if (PEAR::isError($err)) {
287 if (!isset($this->_options['soft']) && $err->getMessage() !== '') {
288 $this->log(0, $err->getMessage());
292 if (is_object($param)) {
293 $param = $param->getChannel() . '/' . $param->getPackage();
296 if (!isset($this->_options['soft'])) {
297 $this->log(2, 'Package "' . $param . '" is not valid');
300 // Message logged above in a specific verbose mode, passing null to not show up on CLI
301 $this->pushError(null, PEAR_INSTALLER_SKIPPED);
304 if ($params[$i] && $params[$i]->getType() == 'local') {
305 // bug #7090 skip channel.xml check for local packages
309 if ($params[$i] && !isset($channelschecked[$params[$i]->getChannel()]) &&
310 !isset($this->_options['offline'])
312 $channelschecked[$params[$i]->getChannel()] = true;
313 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
314 if (!class_exists('System')) {
315 require_once 'System.php';
318 $curchannel = &$this->_registry->getChannel($params[$i]->getChannel());
319 if (PEAR::isError($curchannel)) {
320 PEAR::staticPopErrorHandling();
321 return $this->raiseError($curchannel);
324 if (PEAR::isError($dir = $this->getDownloadDir())) {
325 PEAR::staticPopErrorHandling();
329 $mirror = $this->config->get('preferred_mirror', null, $params[$i]->getChannel());
330 $url = 'http://' . $mirror . '/channel.xml';
331 $a = $this->downloadHttp($url, $this->ui, $dir, null, $curchannel->lastModified());
333 PEAR::staticPopErrorHandling();
334 if (PEAR::isError($a) || !$a) {
335 // Attempt fallback to https automatically
336 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
337 $a = $this->downloadHttp('https://' . $mirror .
338 '/channel.xml', $this->ui, $dir, null, $curchannel->lastModified());
340 PEAR::staticPopErrorHandling();
341 if (PEAR::isError($a) || !$a) {
345 $this->log(0, 'WARNING: channel "' . $params[$i]->getChannel() . '" has ' .
346 'updated its protocols, use "' . PEAR_RUNTYPE . ' channel-update ' . $params[$i]->getChannel() .
351 if ($params[$i] && !isset($this->_options['downloadonly'])) {
352 if (isset($this->_options['packagingroot'])) {
353 $checkdir = $this->_prependPath(
354 $this->config->get('php_dir', null, $params[$i]->getChannel()),
355 $this->_options['packagingroot']);
357 $checkdir = $this->config->get('php_dir',
358 null, $params[$i]->getChannel());
361 while ($checkdir && $checkdir != '/' && !file_exists($checkdir)) {
362 $checkdir = dirname($checkdir);
365 if ($checkdir == '.') {
369 if (!is_writeable($checkdir)) {
370 return PEAR::raiseError('Cannot install, php_dir for channel "' .
371 $params[$i]->getChannel() . '" is not writeable by the current user');
377 unset($channelschecked);
378 PEAR_Downloader_Package::removeDuplicates($params);
379 if (!count($params)) {
384 if (!isset($this->_options['nodeps']) && !isset($this->_options['offline'])) {
388 foreach ($params as $i => $param) {
389 //PHP Bug 40768 / PEAR Bug #10944
390 //Nested foreaches fail in PHP 5.2.1
392 $ret = $params[$i]->detectDependencies($params);
393 if (PEAR::isError($ret)) {
396 PEAR_Downloader_Package::removeDuplicates($params);
397 if (!isset($this->_options['soft'])) {
398 $this->log(0, $ret->getMessage());
406 if (isset($this->_options['offline'])) {
407 $this->log(3, 'Skipping dependency download check, --offline specified');
410 if (!count($params)) {
415 while (PEAR_Downloader_Package::mergeDependencies($params));
416 PEAR_Downloader_Package::removeDuplicates($params, true);
417 $errorparams = array();
418 if (PEAR_Downloader_Package::detectStupidDuplicates($params, $errorparams)) {
419 if (count($errorparams)) {
420 foreach ($errorparams as $param) {
421 $name = $this->_registry->parsedPackageNameToString($param->getParsedPackage());
422 $this->pushError('Duplicate package ' . $name . ' found', PEAR_INSTALLER_FAILED);
429 PEAR_Downloader_Package::removeInstalled($params);
430 if (!count($params)) {
431 $this->pushError('No valid packages found', PEAR_INSTALLER_FAILED);
436 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
437 $err = $this->analyzeDependencies($params);
438 PEAR::popErrorHandling();
439 if (!count($params)) {
440 $this->pushError('No valid packages found', PEAR_INSTALLER_FAILED);
446 $newparams = array();
447 if (isset($this->_options['pretend'])) {
452 foreach ($params as $i => $package) {
453 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
454 $pf = &$params[$i]->download();
455 PEAR::staticPopErrorHandling();
456 if (PEAR::isError($pf)) {
457 if (!isset($this->_options['soft'])) {
458 $this->log(1, $pf->getMessage());
459 $this->log(0, 'Error: cannot download "' .
460 $this->_registry->parsedPackageNameToString($package->getParsedPackage(),
468 $newparams[] = &$params[$i];
470 'file' => $pf->getArchiveFile(),
472 'pkg' => $pf->getPackage()
477 // remove params that did not download successfully
478 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
479 $err = $this->analyzeDependencies($newparams, true);
480 PEAR::popErrorHandling();
481 if (!count($newparams)) {
482 $this->pushError('Download failed', PEAR_INSTALLER_FAILED);
488 $this->_downloadedPackages = $ret;
493 * @param array all packages to be installed
495 function analyzeDependencies(&$params, $force = false)
497 if (isset($this->_options['downloadonly'])) {
501 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
503 $reset = $hasfailed = $failed = false;
506 foreach ($params as $i => $param) {
507 $deps = $param->getDeps();
509 $depchecker = &$this->getDependency2Object($this->config, $this->getOptions(),
510 $param->getParsedPackage(), PEAR_VALIDATE_DOWNLOADING);
511 $send = $param->getPackageFile();
513 $installcheck = $depchecker->validatePackage($send, $this, $params);
514 if (PEAR::isError($installcheck)) {
515 if (!isset($this->_options['soft'])) {
516 $this->log(0, $installcheck->getMessage());
523 PEAR_Downloader_Package::removeDuplicates($params);
529 if (!$reset && $param->alreadyValidated() && !$force) {
534 $depchecker = &$this->getDependency2Object($this->config, $this->getOptions(),
535 $param->getParsedPackage(), PEAR_VALIDATE_DOWNLOADING);
536 $send = $param->getPackageFile();
537 if ($send === null) {
538 $send = $param->getDownloadURL();
541 $installcheck = $depchecker->validatePackage($send, $this, $params);
542 if (PEAR::isError($installcheck)) {
543 if (!isset($this->_options['soft'])) {
544 $this->log(0, $installcheck->getMessage());
551 PEAR_Downloader_Package::removeDuplicates($params);
556 if (isset($deps['required']) && is_array($deps['required'])) {
557 foreach ($deps['required'] as $type => $dep) {
558 // note: Dependency2 will never return a PEAR_Error if ignore-errors
559 // is specified, so soft is needed to turn off logging
560 if (!isset($dep[0])) {
561 if (PEAR::isError($e = $depchecker->{"validate{$type}Dependency"}($dep,
564 if (!isset($this->_options['soft'])) {
565 $this->log(0, $e->getMessage());
567 } elseif (is_array($e) && !$param->alreadyValidated()) {
568 if (!isset($this->_options['soft'])) {
569 $this->log(0, $e[0]);
573 foreach ($dep as $d) {
574 if (PEAR::isError($e =
575 $depchecker->{"validate{$type}Dependency"}($d,
578 if (!isset($this->_options['soft'])) {
579 $this->log(0, $e->getMessage());
581 } elseif (is_array($e) && !$param->alreadyValidated()) {
582 if (!isset($this->_options['soft'])) {
583 $this->log(0, $e[0]);
590 if (isset($deps['optional']) && is_array($deps['optional'])) {
591 foreach ($deps['optional'] as $type => $dep) {
592 if (!isset($dep[0])) {
593 if (PEAR::isError($e =
594 $depchecker->{"validate{$type}Dependency"}($dep,
597 if (!isset($this->_options['soft'])) {
598 $this->log(0, $e->getMessage());
600 } elseif (is_array($e) && !$param->alreadyValidated()) {
601 if (!isset($this->_options['soft'])) {
602 $this->log(0, $e[0]);
606 foreach ($dep as $d) {
607 if (PEAR::isError($e =
608 $depchecker->{"validate{$type}Dependency"}($d,
611 if (!isset($this->_options['soft'])) {
612 $this->log(0, $e->getMessage());
614 } elseif (is_array($e) && !$param->alreadyValidated()) {
615 if (!isset($this->_options['soft'])) {
616 $this->log(0, $e[0]);
624 $groupname = $param->getGroup();
625 if (isset($deps['group']) && $groupname) {
626 if (!isset($deps['group'][0])) {
627 $deps['group'] = array($deps['group']);
631 foreach ($deps['group'] as $group) {
632 if ($group['attribs']['name'] == $groupname) {
639 unset($group['attribs']);
640 foreach ($group as $type => $dep) {
641 if (!isset($dep[0])) {
642 if (PEAR::isError($e =
643 $depchecker->{"validate{$type}Dependency"}($dep,
646 if (!isset($this->_options['soft'])) {
647 $this->log(0, $e->getMessage());
649 } elseif (is_array($e) && !$param->alreadyValidated()) {
650 if (!isset($this->_options['soft'])) {
651 $this->log(0, $e[0]);
655 foreach ($dep as $d) {
656 if (PEAR::isError($e =
657 $depchecker->{"validate{$type}Dependency"}($d,
660 if (!isset($this->_options['soft'])) {
661 $this->log(0, $e->getMessage());
663 } elseif (is_array($e) && !$param->alreadyValidated()) {
664 if (!isset($this->_options['soft'])) {
665 $this->log(0, $e[0]);
674 foreach ($deps as $dep) {
675 if (PEAR::isError($e = $depchecker->validateDependency1($dep, $params))) {
677 if (!isset($this->_options['soft'])) {
678 $this->log(0, $e->getMessage());
680 } elseif (is_array($e) && !$param->alreadyValidated()) {
681 if (!isset($this->_options['soft'])) {
682 $this->log(0, $e[0]);
687 $params[$i]->setValidated();
696 PEAR_Downloader_Package::removeDuplicates($params);
702 PEAR::staticPopErrorHandling();
703 if ($hasfailed && (isset($this->_options['ignore-errors']) ||
704 isset($this->_options['nodeps']))) {
705 // this is probably not needed, but just in case
706 if (!isset($this->_options['soft'])) {
707 $this->log(0, 'WARNING: dependencies failed');
713 * Retrieve the directory that downloads will happen in
717 function getDownloadDir()
719 if (isset($this->_downloadDir)) {
720 return $this->_downloadDir;
723 $downloaddir = $this->config->get('download_dir');
724 if (empty($downloaddir) || (is_dir($downloaddir) && !is_writable($downloaddir))) {
725 if (is_dir($downloaddir) && !is_writable($downloaddir)) {
726 $this->log(0, 'WARNING: configuration download directory "' . $downloaddir .
727 '" is not writeable. Change download_dir config variable to ' .
728 'a writeable dir to avoid this warning');
731 if (!class_exists('System')) {
732 require_once 'System.php';
735 if (PEAR::isError($downloaddir = System::mktemp('-d'))) {
738 $this->log(3, '+ tmp dir created at ' . $downloaddir);
741 if (!is_writable($downloaddir)) {
742 if (PEAR::isError(System::mkdir(array('-p', $downloaddir))) ||
743 !is_writable($downloaddir)) {
744 return PEAR::raiseError('download directory "' . $downloaddir .
745 '" is not writeable. Change download_dir config variable to ' .
750 return $this->_downloadDir = $downloaddir;
753 function setDownloadDir($dir)
755 if (!@is_writable($dir)) {
756 if (PEAR::isError(System::mkdir(array('-p', $dir)))) {
757 return PEAR::raiseError('download directory "' . $dir .
758 '" is not writeable. Change download_dir config variable to ' .
762 $this->_downloadDir = $dir;
765 function configSet($key, $value, $layer = 'user', $channel = false)
767 $this->config->set($key, $value, $layer, $channel);
768 $this->_preferredState = $this->config->get('preferred_state', null, $channel);
769 if (!$this->_preferredState) {
770 // don't inadvertantly use a non-set preferred_state
771 $this->_preferredState = null;
775 function setOptions($options)
777 $this->_options = $options;
780 function getOptions()
782 return $this->_options;
787 * @param array output of {@link parsePackageName()}
790 function _getPackageDownloadUrl($parr)
792 $curchannel = $this->config->get('default_channel');
793 $this->configSet('default_channel', $parr['channel']);
794 // getDownloadURL returns an array. On error, it only contains information
795 // on the latest release as array(version, info). On success it contains
796 // array(version, info, download url string)
797 $state = isset($parr['state']) ? $parr['state'] : $this->config->get('preferred_state');
798 if (!$this->_registry->channelExists($parr['channel'])) {
800 if ($this->config->get('auto_discover') && $this->discover($parr['channel'])) {
804 $this->configSet('default_channel', $curchannel);
805 return PEAR::raiseError('Unknown remote channel: ' . $parr['channel']);
809 $chan = &$this->_registry->getChannel($parr['channel']);
810 if (PEAR::isError($chan)) {
814 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
815 $version = $this->_registry->packageInfo($parr['package'], 'version', $parr['channel']);
816 $stability = $this->_registry->packageInfo($parr['package'], 'stability', $parr['channel']);
817 // package is installed - use the installed release stability level
818 if (!isset($parr['state']) && $stability !== null) {
819 $state = $stability['release'];
821 PEAR::staticPopErrorHandling();
824 $preferred_mirror = $this->config->get('preferred_mirror');
825 if (!$chan->supportsREST($preferred_mirror) ||
827 !($base2 = $chan->getBaseURL('REST1.3', $preferred_mirror))
829 !($base = $chan->getBaseURL('REST1.0', $preferred_mirror))
832 return $this->raiseError($parr['channel'] . ' is using a unsupported protocol - This should never happen.');
836 $rest = &$this->config->getREST('1.3', $this->_options);
839 $rest = &$this->config->getREST('1.0', $this->_options);
842 $downloadVersion = false;
843 if (!isset($parr['version']) && !isset($parr['state']) && $version
844 && !PEAR::isError($version)
845 && !isset($this->_options['downloadonly'])
847 $downloadVersion = $version;
850 $url = $rest->getDownloadURL($base, $parr, $state, $downloadVersion, $chan->getName());
851 if (PEAR::isError($url)) {
852 $this->configSet('default_channel', $curchannel);
856 if ($parr['channel'] != $curchannel) {
857 $this->configSet('default_channel', $curchannel);
860 if (!is_array($url)) {
864 $url['raw'] = false; // no checking is necessary for REST
865 if (!is_array($url['info'])) {
866 return PEAR::raiseError('Invalid remote dependencies retrieved from REST - ' .
867 'this should never happen');
870 if (!isset($this->_options['force']) &&
871 !isset($this->_options['downloadonly']) &&
873 !PEAR::isError($version) &&
874 !isset($parr['group'])
876 if (version_compare($version, $url['version'], '=')) {
877 return PEAR::raiseError($this->_registry->parsedPackageNameToString(
878 $parr, true) . ' is already installed and is the same as the ' .
879 'released version ' . $url['version'], -976);
882 if (version_compare($version, $url['version'], '>')) {
883 return PEAR::raiseError($this->_registry->parsedPackageNameToString(
884 $parr, true) . ' is already installed and is newer than detected ' .
885 'released version ' . $url['version'], -976);
889 if (isset($url['info']['required']) || $url['compatible']) {
890 require_once 'PEAR/PackageFile/v2.php';
891 $pf = new PEAR_PackageFile_v2;
892 $pf->setRawChannel($parr['channel']);
893 if ($url['compatible']) {
894 $pf->setRawCompatible($url['compatible']);
897 require_once 'PEAR/PackageFile/v1.php';
898 $pf = new PEAR_PackageFile_v1;
901 $pf->setRawPackage($url['package']);
902 $pf->setDeps($url['info']);
903 if ($url['compatible']) {
904 $pf->setCompatible($url['compatible']);
907 $pf->setRawState($url['stability']);
909 if (!extension_loaded("zlib") || isset($this->_options['nocompress'])) {
915 if (is_array($url) && isset($url['url'])) {
923 * @param array dependency array
926 function _getDepPackageDownloadUrl($dep, $parr)
928 $xsdversion = isset($dep['rel']) ? '1.0' : '2.0';
929 $curchannel = $this->config->get('default_channel');
930 if (isset($dep['uri'])) {
932 $chan = &$this->_registry->getChannel('__uri');
933 if (PEAR::isError($chan)) {
937 $version = $this->_registry->packageInfo($dep['name'], 'version', '__uri');
938 $this->configSet('default_channel', '__uri');
940 if (isset($dep['channel'])) {
941 $remotechannel = $dep['channel'];
943 $remotechannel = 'pear.php.net';
946 if (!$this->_registry->channelExists($remotechannel)) {
948 if ($this->config->get('auto_discover')) {
949 if ($this->discover($remotechannel)) {
953 return PEAR::raiseError('Unknown remote channel: ' . $remotechannel);
957 $chan = &$this->_registry->getChannel($remotechannel);
958 if (PEAR::isError($chan)) {
962 $version = $this->_registry->packageInfo($dep['name'], 'version', $remotechannel);
963 $this->configSet('default_channel', $remotechannel);
966 $state = isset($parr['state']) ? $parr['state'] : $this->config->get('preferred_state');
967 if (isset($parr['state']) && isset($parr['version'])) {
968 unset($parr['state']);
971 if (isset($dep['uri'])) {
972 $info = &$this->newDownloaderPackage($this);
973 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
974 $err = $info->initialize($dep);
975 PEAR::staticPopErrorHandling();
977 // skip parameters that were missed by preferred_state
978 return PEAR::raiseError('Cannot initialize dependency');
981 if (PEAR::isError($err)) {
982 if (!isset($this->_options['soft'])) {
983 $this->log(0, $err->getMessage());
986 if (is_object($info)) {
987 $param = $info->getChannel() . '/' . $info->getPackage();
989 return PEAR::raiseError('Package "' . $param . '" is not valid');
992 } elseif ($chan->supportsREST($this->config->get('preferred_mirror'))
995 ($base2 = $chan->getBaseURL('REST1.3', $this->config->get('preferred_mirror')))
997 ($base = $chan->getBaseURL('REST1.0', $this->config->get('preferred_mirror')))
1002 $rest = &$this->config->getREST('1.3', $this->_options);
1004 $rest = &$this->config->getREST('1.0', $this->_options);
1007 $url = $rest->getDepDownloadURL($base, $xsdversion, $dep, $parr,
1008 $state, $version, $chan->getName());
1009 if (PEAR::isError($url)) {
1013 if ($parr['channel'] != $curchannel) {
1014 $this->configSet('default_channel', $curchannel);
1017 if (!is_array($url)) {
1021 $url['raw'] = false; // no checking is necessary for REST
1022 if (!is_array($url['info'])) {
1023 return PEAR::raiseError('Invalid remote dependencies retrieved from REST - ' .
1024 'this should never happen');
1027 if (isset($url['info']['required'])) {
1028 if (!class_exists('PEAR_PackageFile_v2')) {
1029 require_once 'PEAR/PackageFile/v2.php';
1031 $pf = new PEAR_PackageFile_v2;
1032 $pf->setRawChannel($remotechannel);
1034 if (!class_exists('PEAR_PackageFile_v1')) {
1035 require_once 'PEAR/PackageFile/v1.php';
1037 $pf = new PEAR_PackageFile_v1;
1040 $pf->setRawPackage($url['package']);
1041 $pf->setDeps($url['info']);
1042 if ($url['compatible']) {
1043 $pf->setCompatible($url['compatible']);
1046 $pf->setRawState($url['stability']);
1047 $url['info'] = &$pf;
1048 if (!extension_loaded("zlib") || isset($this->_options['nocompress'])) {
1054 if (is_array($url) && isset($url['url'])) {
1055 $url['url'] .= $ext;
1061 return $this->raiseError($parr['channel'] . ' is using a unsupported protocol - This should never happen.');
1065 * @deprecated in favor of _getPackageDownloadUrl
1067 function getPackageDownloadUrl($package, $version = null, $channel = false)
1070 $package .= "-$version";
1072 if ($this === null || $this->_registry === null) {
1073 $package = "http://pear.php.net/get/$package";
1075 $chan = $this->_registry->getChannel($channel);
1076 if (PEAR::isError($chan)) {
1079 $package = "http://" . $chan->getServer() . "/get/$package";
1081 if (!extension_loaded("zlib")) {
1082 $package .= '?uncompress=yes';
1088 * Retrieve a list of downloaded packages after a call to {@link download()}.
1090 * Also resets the list of downloaded packages.
1093 function getDownloadedPackages()
1095 $ret = $this->_downloadedPackages;
1096 $this->_downloadedPackages = array();
1097 $this->_toDownload = array();
1101 function _downloadCallback($msg, $params = null)
1105 $this->log(1, "downloading $params ...");
1108 $this->log(1, '...done: ' . number_format($params, 0, '', ',') . ' bytes');
1112 if (empty($bytes)) {
1115 if (!($bytes % 10240)) {
1116 $this->log(1, '.', false);
1121 if($params[1] == -1) {
1122 $length = "Unknown size";
1124 $length = number_format($params[1], 0, '', ',')." bytes";
1126 $this->log(1, "Starting to download {$params[0]} ($length)");
1129 if (method_exists($this->ui, '_downloadCallback'))
1130 $this->ui->_downloadCallback($msg, $params);
1133 function _prependPath($path, $prepend)
1135 if (strlen($prepend) > 0) {
1136 if (OS_WINDOWS && preg_match('/^[a-z]:/i', $path)) {
1137 if (preg_match('/^[a-z]:/i', $prepend)) {
1138 $prepend = substr($prepend, 2);
1139 } elseif ($prepend{0} != '\\') {
1140 $prepend = "\\$prepend";
1142 $path = substr($path, 0, 2) . $prepend . substr($path, 2);
1144 $path = $prepend . $path;
1154 function pushError($errmsg, $code = -1)
1156 array_push($this->_errorStack, array($errmsg, $code));
1159 function getErrorMsgs()
1162 $errs = $this->_errorStack;
1163 foreach ($errs as $err) {
1166 $this->_errorStack = array();
1175 function sortPkgDeps(&$packages, $uninstall = false)
1178 $this->sortPackagesForUninstall($packages) :
1179 $this->sortPackagesForInstall($packages);
1183 * Sort a list of arrays of array(downloaded packagefilename) by dependency.
1185 * This uses the topological sort method from graph theory, and the
1186 * Structures_Graph package to properly sort dependencies for installation.
1187 * @param array an array of downloaded PEAR_Downloader_Packages
1188 * @return array array of array(packagefilename, package.xml contents)
1190 function sortPackagesForInstall(&$packages)
1192 require_once 'Structures/Graph.php';
1193 require_once 'Structures/Graph/Node.php';
1194 require_once 'Structures/Graph/Manipulator/TopologicalSorter.php';
1195 $depgraph = new Structures_Graph(true);
1197 $reg = &$this->config->getRegistry();
1198 foreach ($packages as $i => $package) {
1199 $pname = $reg->parsedPackageNameToString(
1201 'channel' => $package->getChannel(),
1202 'package' => strtolower($package->getPackage()),
1204 $nodes[$pname] = new Structures_Graph_Node;
1205 $nodes[$pname]->setData($packages[$i]);
1206 $depgraph->addNode($nodes[$pname]);
1209 $deplinks = array();
1210 foreach ($nodes as $package => $node) {
1211 $pf = &$node->getData();
1212 $pdeps = $pf->getDeps(true);
1217 if ($pf->getPackagexmlVersion() == '1.0') {
1218 foreach ($pdeps as $dep) {
1219 if ($dep['type'] != 'pkg' ||
1220 (isset($dep['optional']) && $dep['optional'] == 'yes')) {
1224 $dname = $reg->parsedPackageNameToString(
1226 'channel' => 'pear.php.net',
1227 'package' => strtolower($dep['name']),
1230 if (isset($nodes[$dname])) {
1231 if (!isset($deplinks[$dname])) {
1232 $deplinks[$dname] = array();
1235 $deplinks[$dname][$package] = 1;
1236 // dependency is in installed packages
1240 $dname = $reg->parsedPackageNameToString(
1242 'channel' => 'pecl.php.net',
1243 'package' => strtolower($dep['name']),
1246 if (isset($nodes[$dname])) {
1247 if (!isset($deplinks[$dname])) {
1248 $deplinks[$dname] = array();
1251 $deplinks[$dname][$package] = 1;
1252 // dependency is in installed packages
1257 // the only ordering we care about is:
1258 // 1) subpackages must be installed before packages that depend on them
1259 // 2) required deps must be installed before packages that depend on them
1260 if (isset($pdeps['required']['subpackage'])) {
1261 $t = $pdeps['required']['subpackage'];
1262 if (!isset($t[0])) {
1266 $this->_setupGraph($t, $reg, $deplinks, $nodes, $package);
1269 if (isset($pdeps['group'])) {
1270 if (!isset($pdeps['group'][0])) {
1271 $pdeps['group'] = array($pdeps['group']);
1274 foreach ($pdeps['group'] as $group) {
1275 if (isset($group['subpackage'])) {
1276 $t = $group['subpackage'];
1277 if (!isset($t[0])) {
1281 $this->_setupGraph($t, $reg, $deplinks, $nodes, $package);
1286 if (isset($pdeps['optional']['subpackage'])) {
1287 $t = $pdeps['optional']['subpackage'];
1288 if (!isset($t[0])) {
1292 $this->_setupGraph($t, $reg, $deplinks, $nodes, $package);
1295 if (isset($pdeps['required']['package'])) {
1296 $t = $pdeps['required']['package'];
1297 if (!isset($t[0])) {
1301 $this->_setupGraph($t, $reg, $deplinks, $nodes, $package);
1304 if (isset($pdeps['group'])) {
1305 if (!isset($pdeps['group'][0])) {
1306 $pdeps['group'] = array($pdeps['group']);
1309 foreach ($pdeps['group'] as $group) {
1310 if (isset($group['package'])) {
1311 $t = $group['package'];
1312 if (!isset($t[0])) {
1316 $this->_setupGraph($t, $reg, $deplinks, $nodes, $package);
1323 $this->_detectDepCycle($deplinks);
1324 foreach ($deplinks as $dependent => $parents) {
1325 foreach ($parents as $parent => $unused) {
1326 $nodes[$dependent]->connectTo($nodes[$parent]);
1330 $installOrder = Structures_Graph_Manipulator_TopologicalSorter::sort($depgraph);
1332 for ($i = 0, $count = count($installOrder); $i < $count; $i++) {
1333 foreach ($installOrder[$i] as $index => $sortedpackage) {
1334 $data = &$installOrder[$i][$index]->getData();
1335 $ret[] = &$nodes[$reg->parsedPackageNameToString(
1337 'channel' => $data->getChannel(),
1338 'package' => strtolower($data->getPackage()),
1348 * Detect recursive links between dependencies and break the cycles
1353 function _detectDepCycle(&$deplinks)
1357 foreach ($deplinks as $dep => $parents) {
1358 foreach ($parents as $parent => $unused) {
1359 // reset the parent cycle detector
1360 $this->_testCycle(null, null, null);
1361 if ($this->_testCycle($dep, $deplinks, $parent)) {
1363 unset($deplinks[$dep][$parent]);
1364 if (count($deplinks[$dep]) == 0) {
1365 unset($deplinks[$dep]);
1372 } while ($keepgoing);
1375 function _testCycle($test, $deplinks, $dep)
1377 static $visited = array();
1378 if ($test === null) {
1383 // this happens when a parent has a dep cycle on another dependency
1384 // but the child is not part of the cycle
1385 if (isset($visited[$dep])) {
1390 if ($test == $dep) {
1394 if (isset($deplinks[$dep])) {
1395 if (in_array($test, array_keys($deplinks[$dep]), true)) {
1399 foreach ($deplinks[$dep] as $parent => $unused) {
1400 if ($this->_testCycle($test, $deplinks, $parent)) {
1410 * Set up the dependency for installation parsing
1412 * @param array $t dependency information
1413 * @param PEAR_Registry $reg
1414 * @param array $deplinks list of dependency links already established
1415 * @param array $nodes all existing package nodes
1416 * @param string $package parent package name
1419 function _setupGraph($t, $reg, &$deplinks, &$nodes, $package)
1421 foreach ($t as $dep) {
1422 $depchannel = !isset($dep['channel']) ? '__uri': $dep['channel'];
1423 $dname = $reg->parsedPackageNameToString(
1425 'channel' => $depchannel,
1426 'package' => strtolower($dep['name']),
1429 if (isset($nodes[$dname])) {
1430 if (!isset($deplinks[$dname])) {
1431 $deplinks[$dname] = array();
1433 $deplinks[$dname][$package] = 1;
1438 function _dependsOn($a, $b)
1440 return $this->_checkDepTree(strtolower($a->getChannel()), strtolower($a->getPackage()), $b);
1443 function _checkDepTree($channel, $package, $b, $checked = array())
1445 $checked[$channel][$package] = true;
1446 if (!isset($this->_depTree[$channel][$package])) {
1450 if (isset($this->_depTree[$channel][$package][strtolower($b->getChannel())]
1451 [strtolower($b->getPackage())])) {
1455 foreach ($this->_depTree[$channel][$package] as $ch => $packages) {
1456 foreach ($packages as $pa => $true) {
1457 if ($this->_checkDepTree($ch, $pa, $b, $checked)) {
1466 function _sortInstall($a, $b)
1468 if (!$a->getDeps() && !$b->getDeps()) {
1469 return 0; // neither package has dependencies, order is insignificant
1471 if ($a->getDeps() && !$b->getDeps()) {
1472 return 1; // $a must be installed after $b because $a has dependencies
1474 if (!$a->getDeps() && $b->getDeps()) {
1475 return -1; // $b must be installed after $a because $b has dependencies
1477 // both packages have dependencies
1478 if ($this->_dependsOn($a, $b)) {
1481 if ($this->_dependsOn($b, $a)) {
1488 * Download a file through HTTP. Considers suggested file name in
1489 * Content-disposition: header and can run a callback function for
1490 * different events. The callback will be called with two
1491 * parameters: the callback type, and parameters. The implemented
1492 * callback types are:
1494 * 'setup' called at the very beginning, parameter is a UI object
1495 * that should be used for all output
1496 * 'message' the parameter is a string with an informational message
1497 * 'saveas' may be used to save with a different file name, the
1498 * parameter is the filename that is about to be used.
1499 * If a 'saveas' callback returns a non-empty string,
1500 * that file name will be used as the filename instead.
1501 * Note that $save_dir will not be affected by this, only
1502 * the basename of the file.
1503 * 'start' download is starting, parameter is number of bytes
1504 * that are expected, or -1 if unknown
1505 * 'bytesread' parameter is the number of bytes read so far
1506 * 'done' download is complete, parameter is the total number
1508 * 'connfailed' if the TCP/SSL connection fails, this callback is called
1509 * with array(host,port,errno,errmsg)
1510 * 'writefailed' if writing to disk fails, this callback is called
1511 * with array(destfile,errmsg)
1513 * If an HTTP proxy has been configured (http_proxy PEAR_Config
1514 * setting), the proxy will be used.
1516 * @param string $url the URL to download
1517 * @param object $ui PEAR_Frontend_* instance
1518 * @param object $config PEAR_Config instance
1519 * @param string $save_dir directory to save file in
1520 * @param mixed $callback function/method to call for status
1522 * @param false|string|array $lastmodified header values to check against for caching
1523 * use false to return the header values from this download
1524 * @param false|array $accept Accept headers to send
1525 * @param false|string $channel Channel to use for retrieving authentication
1526 * @return string|array Returns the full path of the downloaded file or a PEAR
1527 * error on failure. If the error is caused by
1528 * socket-related errors, the error object will
1529 * have the fsockopen error code available through
1530 * getCode(). If caching is requested, then return the header
1535 function downloadHttp($url, &$ui, $save_dir = '.', $callback = null, $lastmodified = null,
1536 $accept = false, $channel = false)
1538 static $redirect = 0;
1539 // always reset , so we are clean case of error
1540 $wasredirect = $redirect;
1543 call_user_func($callback, 'setup', array(&$ui));
1546 $info = parse_url($url);
1547 if (!isset($info['scheme']) || !in_array($info['scheme'], array('http', 'https'))) {
1548 return PEAR::raiseError('Cannot download non-http URL "' . $url . '"');
1551 if (!isset($info['host'])) {
1552 return PEAR::raiseError('Cannot download from non-URL "' . $url . '"');
1555 $host = isset($info['host']) ? $info['host'] : null;
1556 $port = isset($info['port']) ? $info['port'] : null;
1557 $path = isset($info['path']) ? $info['path'] : null;
1560 $config = &$this->config;
1562 $config = &PEAR_Config::singleton();
1565 $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
1566 if ($config->get('http_proxy') &&
1567 $proxy = parse_url($config->get('http_proxy'))) {
1568 $proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
1569 if (isset($proxy['scheme']) && $proxy['scheme'] == 'https') {
1570 $proxy_host = 'ssl://' . $proxy_host;
1572 $proxy_port = isset($proxy['port']) ? $proxy['port'] : 8080;
1573 $proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null;
1574 $proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
1577 call_user_func($callback, 'message', "Using HTTP proxy $host:$port");
1582 $port = (isset($info['scheme']) && $info['scheme'] == 'https') ? 443 : 80;
1585 $scheme = (isset($info['scheme']) && $info['scheme'] == 'https') ? 'https' : 'http';
1587 if ($proxy_host != '') {
1588 $fp = @fsockopen($proxy_host, $proxy_port, $errno, $errstr);
1591 call_user_func($callback, 'connfailed', array($proxy_host, $proxy_port,
1594 return PEAR::raiseError("Connection to `$proxy_host:$proxy_port' failed: $errstr", $errno);
1597 if ($lastmodified === false || $lastmodified) {
1598 $request = "GET $url HTTP/1.1\r\n";
1599 $request .= "Host: $host\r\n";
1601 $request = "GET $url HTTP/1.0\r\n";
1602 $request .= "Host: $host\r\n";
1605 $network_host = $host;
1606 if (isset($info['scheme']) && $info['scheme'] == 'https') {
1607 $network_host = 'ssl://' . $host;
1610 $fp = @fsockopen($network_host, $port, $errno, $errstr);
1613 call_user_func($callback, 'connfailed', array($host, $port,
1616 return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
1619 if ($lastmodified === false || $lastmodified) {
1620 $request = "GET $path HTTP/1.1\r\n";
1621 $request .= "Host: $host\r\n";
1623 $request = "GET $path HTTP/1.0\r\n";
1624 $request .= "Host: $host\r\n";
1628 $ifmodifiedsince = '';
1629 if (is_array($lastmodified)) {
1630 if (isset($lastmodified['Last-Modified'])) {
1631 $ifmodifiedsince = 'If-Modified-Since: ' . $lastmodified['Last-Modified'] . "\r\n";
1634 if (isset($lastmodified['ETag'])) {
1635 $ifmodifiedsince .= "If-None-Match: $lastmodified[ETag]\r\n";
1638 $ifmodifiedsince = ($lastmodified ? "If-Modified-Since: $lastmodified\r\n" : '');
1641 $request .= $ifmodifiedsince .
1642 "User-Agent: PEAR/1.9.4/PHP/" . PHP_VERSION . "\r\n";
1644 if (isset($this)) { // only pass in authentication for non-static calls
1645 $username = $config->get('username', null, $channel);
1646 $password = $config->get('password', null, $channel);
1647 if ($username && $password) {
1648 $tmp = base64_encode("$username:$password");
1649 $request .= "Authorization: Basic $tmp\r\n";
1653 if ($proxy_host != '' && $proxy_user != '') {
1654 $request .= 'Proxy-Authorization: Basic ' .
1655 base64_encode($proxy_user . ':' . $proxy_pass) . "\r\n";
1659 $request .= 'Accept: ' . implode(', ', $accept) . "\r\n";
1662 $request .= "Connection: close\r\n";
1664 fwrite($fp, $request);
1667 while (trim($line = fgets($fp, 1024))) {
1668 if (preg_match('/^([^:]+):\s+(.*)\s*\\z/', $line, $matches)) {
1669 $headers[strtolower($matches[1])] = trim($matches[2]);
1670 } elseif (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
1671 $reply = (int)$matches[1];
1672 if ($reply == 304 && ($lastmodified || ($lastmodified === false))) {
1676 if (!in_array($reply, array(200, 301, 302, 303, 305, 307))) {
1677 return PEAR::raiseError("File $scheme://$host:$port$path not valid (received: $line)");
1682 if ($reply != 200) {
1683 if (!isset($headers['location'])) {
1684 return PEAR::raiseError("File $scheme://$host:$port$path not valid (redirected but no location)");
1687 if ($wasredirect > 4) {
1688 return PEAR::raiseError("File $scheme://$host:$port$path not valid (redirection looped more than 5 times)");
1691 $redirect = $wasredirect + 1;
1692 return $this->downloadHttp($headers['location'],
1693 $ui, $save_dir, $callback, $lastmodified, $accept);
1696 if (isset($headers['content-disposition']) &&
1697 preg_match('/\sfilename=\"([^;]*\S)\"\s*(;|\\z)/', $headers['content-disposition'], $matches)) {
1698 $save_as = basename($matches[1]);
1700 $save_as = basename($url);
1704 $tmp = call_user_func($callback, 'saveas', $save_as);
1710 $dest_file = $save_dir . DIRECTORY_SEPARATOR . $save_as;
1711 if (is_link($dest_file)) {
1712 return PEAR::raiseError('SECURITY ERROR: Will not write to ' . $dest_file . ' as it is symlinked to ' . readlink($dest_file) . ' - Possible symlink attack');
1715 if (!$wp = @fopen($dest_file, 'wb')) {
1718 call_user_func($callback, 'writefailed', array($dest_file, $php_errormsg));
1720 return PEAR::raiseError("could not open $dest_file for writing");
1723 $length = isset($headers['content-length']) ? $headers['content-length'] : -1;
1727 call_user_func($callback, 'start', array(basename($dest_file), $length));
1730 while ($data = fread($fp, 1024)) {
1731 $bytes += strlen($data);
1733 call_user_func($callback, 'bytesread', $bytes);
1735 if (!@fwrite($wp, $data)) {
1738 call_user_func($callback, 'writefailed', array($dest_file, $php_errormsg));
1740 return PEAR::raiseError("$dest_file: write failed ($php_errormsg)");
1747 call_user_func($callback, 'done', $bytes);
1750 if ($lastmodified === false || $lastmodified) {
1751 if (isset($headers['etag'])) {
1752 $lastmodified = array('ETag' => $headers['etag']);
1755 if (isset($headers['last-modified'])) {
1756 if (is_array($lastmodified)) {
1757 $lastmodified['Last-Modified'] = $headers['last-modified'];
1759 $lastmodified = $headers['last-modified'];
1762 return array($dest_file, $lastmodified, $headers);