203 $this->
log(1,
'Attempting to discover channel "' . $channel .
'"...');
205 $callback = $this->ui ? array(&$this,
'_downloadCallback') :
null;
206 if (!class_exists(
'System')) {
207 require_once
'System.php';
210 $tmpdir = $this->config->get(
'temp_dir');
211 $tmp = System::mktemp(
'-d -t "' . $tmpdir .
'"');
212 $a = $this->
downloadHttp(
'http://' . $channel .
'/channel.xml', $this->ui, $tmp, $callback,
false);
213 PEAR::popErrorHandling();
217 $this->
log(1,
'Attempting fallback to https instead of http on channel "' . $channel .
'"...');
218 $a = $this->
downloadHttp(
'https://' . $channel .
'/channel.xml', $this->ui, $tmp, $callback,
false);
219 PEAR::popErrorHandling();
225 list($a, $lastmodified) = $a;
226 if (!class_exists(
'PEAR_ChannelFile')) {
227 require_once
'PEAR/ChannelFile.php';
231 if ($b->fromXmlFile($a)) {
233 if ($this->config->get(
'auto_discover')) {
234 $this->_registry->addChannel($b, $lastmodified);
235 $alias = $b->getName();
236 if ($b->getName() == $this->_registry->channelName($b->getAlias())) {
237 $alias = $b->getAlias();
240 $this->
log(1,
'Auto-discovered channel "' . $channel .
241 '", alias "' . $alias .
'", adding to registry');
283 if (!count($params)) {
288 if (!isset($this->_registry)) {
289 $this->_registry = &$this->config->getRegistry();
292 $channelschecked = array();
294 foreach ($params as $i => $param) {
297 $err = $params[$i]->initialize($param);
305 if (!isset($this->_options[
'soft']) && $err->getMessage() !==
'') {
306 $this->
log(0, $err->getMessage());
310 if (is_object($param)) {
311 $param = $param->getChannel() .
'/' . $param->getPackage();
314 if (!isset($this->_options[
'soft'])) {
315 $this->
log(2,
'Package "' . $param .
'" is not valid');
322 if ($params[$i] && $params[$i]->getType() ==
'local') {
327 if ($params[$i] && !isset($channelschecked[$params[$i]->getChannel()]) &&
328 !isset($this->_options[
'offline'])
330 $channelschecked[$params[$i]->getChannel()] =
true;
332 if (!class_exists(
'System')) {
333 require_once
'System.php';
336 $curchannel = $this->_registry->getChannel($params[$i]->getChannel());
339 return $this->raiseError($curchannel);
347 $mirror = $this->config->get(
'preferred_mirror',
null, $params[$i]->getChannel());
348 $url =
'http://' . $mirror .
'/channel.xml';
349 $a = $this->
downloadHttp($url, $this->ui, $dir,
null, $curchannel->lastModified());
359 '/channel.xml', $this->ui, $dir,
null, $curchannel->lastModified());
366 $this->
log(0,
'WARNING: channel "' . $params[$i]->getChannel() .
'" has ' .
367 'updated its protocols, use "' .
PEAR_RUNTYPE .
' channel-update ' . $params[$i]->getChannel() .
372 if ($params[$i] && !isset($this->_options[
'downloadonly'])) {
373 if (isset($this->_options[
'packagingroot'])) {
375 $this->config->get(
'php_dir',
null, $params[$i]->getChannel()),
376 $this->_options[
'packagingroot']);
378 $checkdir = $this->config->get(
'php_dir',
379 null, $params[$i]->getChannel());
382 while ($checkdir && $checkdir !=
'/' && !file_exists($checkdir)) {
383 $checkdir = dirname($checkdir);
386 if ($checkdir ==
'.') {
390 if (!is_writeable($checkdir)) {
391 return PEAR::raiseError(
'Cannot install, php_dir for channel "' .
392 $params[$i]->getChannel() .
'" is not writeable by the current user');
398 unset($channelschecked);
400 if (!count($params)) {
405 if (!isset($this->_options[
'nodeps']) && !isset($this->_options[
'offline'])) {
409 foreach ($params as $i => $param) {
413 $ret = $params[$i]->detectDependencies($params);
418 if (!isset($this->_options[
'soft'])) {
419 $this->
log(0, $ret->getMessage());
427 if (isset($this->_options[
'offline'])) {
428 $this->
log(3,
'Skipping dependency download check, --offline specified');
431 if (!count($params)) {
438 $errorparams = array();
440 if (count($errorparams)) {
441 foreach ($errorparams as $param) {
442 $name = $this->_registry->parsedPackageNameToString($param->getParsedPackage());
451 if (!count($params)) {
459 PEAR::popErrorHandling();
460 if (!count($params)) {
467 $newparams = array();
468 if (isset($this->_options[
'pretend'])) {
473 foreach ($params as $i => $package) {
475 $pf = &$params[$i]->download();
478 if (!isset($this->_options[
'soft'])) {
479 $this->
log(1, $pf->getMessage());
480 $this->
log(0,
'Error: cannot download "' .
481 $this->_registry->parsedPackageNameToString($package->getParsedPackage(),
489 $newparams[] = &$params[$i];
491 'file' => $pf->getArchiveFile(),
493 'pkg' => $pf->getPackage()
501 PEAR::popErrorHandling();
502 if (!count($newparams)) {
509 $this->_downloadedPackages = $ret;
518 if (isset($this->_options[
'downloadonly'])) {
524 $reset = $hasfailed = $failed =
false;
527 foreach ($params as $i => $param) {
528 $deps = $param->getDeps();
532 $send = $param->getPackageFile();
534 $installcheck = $depchecker->validatePackage($send, $this, $params);
536 if (!isset($this->_options[
'soft'])) {
537 $this->
log(0, $installcheck->getMessage());
550 if (!$reset && $param->alreadyValidated() && !$force) {
557 $send = $param->getPackageFile();
558 if ($send ===
null) {
559 $send = $param->getDownloadURL();
562 $installcheck = $depchecker->validatePackage($send, $this, $params);
564 if (!isset($this->_options[
'soft'])) {
565 $this->
log(0, $installcheck->getMessage());
577 if (isset($deps[
'required']) && is_array($deps[
'required'])) {
578 foreach ($deps[
'required'] as $type => $dep) {
581 if (!isset($dep[0])) {
582 if (
PEAR::isError($e = $depchecker->{
"validate{$type}Dependency"}($dep,
585 if (!isset($this->_options[
'soft'])) {
586 $this->
log(0, $e->getMessage());
588 } elseif (is_array($e) && !$param->alreadyValidated()) {
589 if (!isset($this->_options[
'soft'])) {
590 $this->
log(0, $e[0]);
594 foreach ($dep as $d) {
596 $depchecker->{
"validate{$type}Dependency"}($d,
599 if (!isset($this->_options[
'soft'])) {
600 $this->
log(0, $e->getMessage());
602 } elseif (is_array($e) && !$param->alreadyValidated()) {
603 if (!isset($this->_options[
'soft'])) {
604 $this->
log(0, $e[0]);
611 if (isset($deps[
'optional']) && is_array($deps[
'optional'])) {
612 foreach ($deps[
'optional'] as $type => $dep) {
613 if (!isset($dep[0])) {
615 $depchecker->{
"validate{$type}Dependency"}($dep,
618 if (!isset($this->_options[
'soft'])) {
619 $this->
log(0, $e->getMessage());
621 } elseif (is_array($e) && !$param->alreadyValidated()) {
622 if (!isset($this->_options[
'soft'])) {
623 $this->
log(0, $e[0]);
627 foreach ($dep as $d) {
629 $depchecker->{
"validate{$type}Dependency"}($d,
632 if (!isset($this->_options[
'soft'])) {
633 $this->
log(0, $e->getMessage());
635 } elseif (is_array($e) && !$param->alreadyValidated()) {
636 if (!isset($this->_options[
'soft'])) {
637 $this->
log(0, $e[0]);
645 $groupname = $param->getGroup();
646 if (isset($deps[
'group']) && $groupname) {
647 if (!isset($deps[
'group'][0])) {
648 $deps[
'group'] = array($deps[
'group']);
652 foreach ($deps[
'group'] as $group) {
653 if ($group[
'attribs'][
'name'] == $groupname) {
660 unset($group[
'attribs']);
661 foreach ($group as $type => $dep) {
662 if (!isset($dep[0])) {
664 $depchecker->{
"validate{$type}Dependency"}($dep,
667 if (!isset($this->_options[
'soft'])) {
668 $this->
log(0, $e->getMessage());
670 } elseif (is_array($e) && !$param->alreadyValidated()) {
671 if (!isset($this->_options[
'soft'])) {
672 $this->
log(0, $e[0]);
676 foreach ($dep as $d) {
678 $depchecker->{
"validate{$type}Dependency"}($d,
681 if (!isset($this->_options[
'soft'])) {
682 $this->
log(0, $e->getMessage());
684 } elseif (is_array($e) && !$param->alreadyValidated()) {
685 if (!isset($this->_options[
'soft'])) {
686 $this->
log(0, $e[0]);
695 foreach ($deps as $dep) {
696 if (PEAR::isError($e = $depchecker->validateDependency1($dep, $params))) {
698 if (!isset($this->_options[
'soft'])) {
699 $this->log(0, $e->getMessage());
701 } elseif (is_array($e) && !$param->alreadyValidated()) {
702 if (!isset($this->_options[
'soft'])) {
703 $this->log(0, $e[0]);
708 $params[$i]->setValidated();
724 if ($hasfailed && (isset($this->_options[
'ignore-errors']) ||
725 isset($this->_options[
'nodeps']))) {
727 if (!isset($this->_options[
'soft'])) {
728 $this->log(0,
'WARNING: dependencies failed');
813 $curchannel = $this->config->get(
'default_channel');
814 $this->
configSet(
'default_channel', $parr[
'channel']);
818 $state = isset($parr[
'state']) ? $parr[
'state'] : $this->config->get(
'preferred_state');
819 if (!$this->_registry->channelExists($parr[
'channel'])) {
821 if ($this->config->get(
'auto_discover') && $this->discover($parr[
'channel'])) {
825 $this->
configSet(
'default_channel', $curchannel);
826 return PEAR::raiseError(
'Unknown remote channel: ' . $parr[
'channel']);
830 $chan = $this->_registry->getChannel($parr[
'channel']);
836 $version = $this->_registry->packageInfo($parr[
'package'],
'version', $parr[
'channel']);
837 $stability = $this->_registry->packageInfo($parr[
'package'],
'stability', $parr[
'channel']);
839 if (!isset($parr[
'state']) && $stability !==
null) {
840 $state = $stability[
'release'];
845 $preferred_mirror = $this->config->get(
'preferred_mirror');
846 if (!$chan->supportsREST($preferred_mirror) ||
848 !($base2 = $chan->getBaseURL(
'REST1.3', $preferred_mirror))
850 !($base = $chan->getBaseURL(
'REST1.0', $preferred_mirror))
853 return $this->raiseError($parr[
'channel'] .
' is using an unsupported protocol - This should never happen. Use --force to continue');
857 $rest = &$this->config->getREST(
'1.3', $this->_options);
860 $rest = &$this->config->getREST(
'1.0', $this->_options);
863 $downloadVersion =
false;
864 if (!isset($parr[
'version']) && !isset($parr[
'state']) && $version
866 && !isset($this->_options[
'downloadonly'])
868 $downloadVersion = $version;
871 $url = $rest->getDownloadURL($base, $parr, $state, $downloadVersion, $chan->getName());
873 $this->
configSet(
'default_channel', $curchannel);
877 if ($parr[
'channel'] != $curchannel) {
878 $this->
configSet(
'default_channel', $curchannel);
881 if (!is_array($url)) {
886 if (!is_array($url[
'info'])) {
887 return PEAR::raiseError(
'Invalid remote dependencies retrieved from REST - ' .
888 'this should never happen');
891 if (!isset($this->_options[
'force']) &&
892 !isset($this->_options[
'downloadonly']) &&
895 !isset($parr[
'group'])
897 if (version_compare($version, $url[
'version'],
'=')) {
898 return PEAR::raiseError($this->_registry->parsedPackageNameToString(
899 $parr,
true) .
' is already installed and is the same as the ' .
900 'released version ' . $url[
'version'], -976);
903 if (version_compare($version, $url[
'version'],
'>')) {
904 return PEAR::raiseError($this->_registry->parsedPackageNameToString(
905 $parr,
true) .
' is already installed and is newer than detected ' .
906 'released version ' . $url[
'version'], -976);
910 if (isset($url[
'info'][
'required']) || $url[
'compatible']) {
911 require_once
'PEAR/PackageFile/v2.php';
914 if ($url[
'compatible']) {
915 $pf->setRawCompatible($url[
'compatible']);
918 require_once
'PEAR/PackageFile/v1.php';
922 $pf->setRawPackage($url[
'package']);
923 $pf->setDeps($url[
'info']);
924 if ($url[
'compatible']) {
925 $pf->setCompatible($url[
'compatible']);
928 $pf->setRawState($url[
'stability']);
930 if (!extension_loaded(
"zlib") || isset($this->_options[
'nocompress'])) {
936 if (is_array($url) && isset($url[
'url'])) {
949 $xsdversion = isset($dep[
'rel']) ?
'1.0' :
'2.0';
950 $curchannel = $this->config->get(
'default_channel');
951 if (isset($dep[
'uri'])) {
953 $chan = $this->_registry->getChannel(
'__uri');
958 $version = $this->_registry->packageInfo($dep[
'name'],
'version',
'__uri');
959 $this->
configSet(
'default_channel',
'__uri');
961 if (isset($dep[
'channel'])) {
962 $remotechannel = $dep[
'channel'];
964 $remotechannel =
'pear.php.net';
967 if (!$this->_registry->channelExists($remotechannel)) {
969 if ($this->config->get(
'auto_discover')) {
970 if ($this->
discover($remotechannel)) {
974 return PEAR::raiseError(
'Unknown remote channel: ' . $remotechannel);
978 $chan = $this->_registry->getChannel($remotechannel);
983 $version = $this->_registry->packageInfo($dep[
'name'],
'version', $remotechannel);
984 $this->
configSet(
'default_channel', $remotechannel);
987 $state = isset($parr[
'state']) ? $parr[
'state'] : $this->config->get(
'preferred_state');
988 if (isset($parr[
'state']) && isset($parr[
'version'])) {
989 unset($parr[
'state']);
992 if (isset($dep[
'uri'])) {
995 $err = $info->initialize($dep);
999 return PEAR::raiseError(
'Cannot initialize dependency');
1003 if (!isset($this->_options[
'soft'])) {
1004 $this->
log(0, $err->getMessage());
1007 if (is_object($info)) {
1008 $param = $info->getChannel() .
'/' . $info->getPackage();
1010 return PEAR::raiseError(
'Package "' . $param .
'" is not valid');
1013 } elseif ($chan->supportsREST($this->config->get(
'preferred_mirror'))
1016 ($base2 = $chan->getBaseURL(
'REST1.3', $this->config->get(
'preferred_mirror')))
1018 ($base = $chan->getBaseURL(
'REST1.0', $this->config->get(
'preferred_mirror')))
1023 $rest = &$this->config->getREST(
'1.3', $this->_options);
1025 $rest = &$this->config->getREST(
'1.0', $this->_options);
1028 $url = $rest->getDepDownloadURL($base, $xsdversion, $dep, $parr,
1029 $state, $version, $chan->getName());
1034 if ($parr[
'channel'] != $curchannel) {
1035 $this->
configSet(
'default_channel', $curchannel);
1038 if (!is_array($url)) {
1042 $url[
'raw'] =
false;
1043 if (!is_array($url[
'info'])) {
1044 return PEAR::raiseError(
'Invalid remote dependencies retrieved from REST - ' .
1045 'this should never happen');
1048 if (isset($url[
'info'][
'required'])) {
1049 if (!class_exists(
'PEAR_PackageFile_v2')) {
1050 require_once
'PEAR/PackageFile/v2.php';
1055 if (!class_exists(
'PEAR_PackageFile_v1')) {
1056 require_once
'PEAR/PackageFile/v1.php';
1061 $pf->setRawPackage($url[
'package']);
1062 $pf->setDeps($url[
'info']);
1063 if ($url[
'compatible']) {
1064 $pf->setCompatible($url[
'compatible']);
1067 $pf->setRawState($url[
'stability']);
1068 $url[
'info'] = &$pf;
1069 if (!extension_loaded(
"zlib") || isset($this->_options[
'nocompress'])) {
1075 if (is_array($url) && isset($url[
'url'])) {
1076 $url[
'url'] .= $ext;
1082 return $this->raiseError($parr[
'channel'] .
' is using a unsupported protocol - This should never happen.');
1213 require_once
'Structures/Graph.php';
1214 require_once
'Structures/Graph/Node.php';
1215 require_once
'Structures/Graph/Manipulator/TopologicalSorter.php';
1218 $reg = &$this->config->getRegistry();
1219 foreach ($packages as $i => $package) {
1220 $pname = $reg->parsedPackageNameToString(
1222 'channel' => $package->getChannel(),
1223 'package' => strtolower($package->getPackage()),
1226 $nodes[$pname]->
setData($packages[$i]);
1227 $depgraph->addNode($nodes[$pname]);
1230 $deplinks = array();
1231 foreach ($nodes as $package => $node) {
1232 $pf = &$node->getData();
1233 $pdeps = $pf->getDeps(
true);
1238 if ($pf->getPackagexmlVersion() ==
'1.0') {
1239 foreach ($pdeps as $dep) {
1240 if ($dep[
'type'] !=
'pkg' ||
1241 (isset($dep[
'optional']) && $dep[
'optional'] ==
'yes')) {
1245 $dname = $reg->parsedPackageNameToString(
1247 'channel' =>
'pear.php.net',
1248 'package' => strtolower($dep[
'name']),
1251 if (isset($nodes[$dname])) {
1252 if (!isset($deplinks[$dname])) {
1253 $deplinks[$dname] = array();
1256 $deplinks[$dname][$package] = 1;
1261 $dname = $reg->parsedPackageNameToString(
1263 'channel' =>
'pecl.php.net',
1264 'package' => strtolower($dep[
'name']),
1267 if (isset($nodes[$dname])) {
1268 if (!isset($deplinks[$dname])) {
1269 $deplinks[$dname] = array();
1272 $deplinks[$dname][$package] = 1;
1281 if (isset($pdeps[
'required'][
'subpackage'])) {
1282 $t = $pdeps[
'required'][
'subpackage'];
1283 if (!isset($t[0])) {
1287 $this->
_setupGraph($t, $reg, $deplinks, $nodes, $package);
1290 if (isset($pdeps[
'group'])) {
1291 if (!isset($pdeps[
'group'][0])) {
1292 $pdeps[
'group'] = array($pdeps[
'group']);
1295 foreach ($pdeps[
'group'] as $group) {
1296 if (isset($group[
'subpackage'])) {
1297 $t = $group[
'subpackage'];
1298 if (!isset($t[0])) {
1302 $this->
_setupGraph($t, $reg, $deplinks, $nodes, $package);
1307 if (isset($pdeps[
'optional'][
'subpackage'])) {
1308 $t = $pdeps[
'optional'][
'subpackage'];
1309 if (!isset($t[0])) {
1313 $this->
_setupGraph($t, $reg, $deplinks, $nodes, $package);
1316 if (isset($pdeps[
'required'][
'package'])) {
1317 $t = $pdeps[
'required'][
'package'];
1318 if (!isset($t[0])) {
1322 $this->
_setupGraph($t, $reg, $deplinks, $nodes, $package);
1325 if (isset($pdeps[
'group'])) {
1326 if (!isset($pdeps[
'group'][0])) {
1327 $pdeps[
'group'] = array($pdeps[
'group']);
1330 foreach ($pdeps[
'group'] as $group) {
1331 if (isset($group[
'package'])) {
1332 $t = $group[
'package'];
1333 if (!isset($t[0])) {
1337 $this->
_setupGraph($t, $reg, $deplinks, $nodes, $package);
1345 foreach ($deplinks as $dependent => $parents) {
1346 foreach ($parents as $parent => $unused) {
1347 $nodes[$dependent]->connectTo($nodes[$parent]);
1353 for ($i = 0, $count = count($installOrder); $i < $count; $i++) {
1354 foreach ($installOrder[$i] as $index => $sortedpackage) {
1355 $data = &$installOrder[$i][$index]->getData();
1356 $ret[] = &$nodes[$reg->parsedPackageNameToString(
1358 'channel' => $data->getChannel(),
1359 'package' => strtolower($data->getPackage()),
1466 $checked[$channel][$package] =
true;
1467 if (!isset($this->_depTree[$channel][$package])) {
1471 if (isset($this->_depTree[$channel][$package][strtolower($b->getChannel())]
1472 [strtolower($b->getPackage())])) {
1476 foreach ($this->_depTree[$channel][$package] as $ch => $packages) {
1477 foreach ($packages as $pa => $true) {
1559 $object, $url, &
$ui, $save_dir =
'.', $callback =
null, $lastmodified =
null,
1560 $accept =
false, $channel =
false
1562 static $redirect = 0;
1564 $wasredirect = $redirect;
1567 call_user_func($callback,
'setup', array(&
$ui));
1570 $info = parse_url($url);
1571 if (!isset($info[
'scheme']) || !in_array($info[
'scheme'], array(
'http',
'https'))) {
1572 return PEAR::raiseError(
'Cannot download non-http URL "' . $url .
'"');
1575 if (!isset($info[
'host'])) {
1576 return PEAR::raiseError(
'Cannot download from non-URL "' . $url .
'"');
1579 $host = isset($info[
'host']) ? $info[
'host'] :
null;
1580 $port = isset($info[
'port']) ? $info[
'port'] :
null;
1581 $path = isset($info[
'path']) ? $info[
'path'] :
null;
1583 if ($object !==
null) {
1586 $config = &PEAR_Config::singleton();
1591 if ($proxy->isProxyConfigured() && $callback) {
1592 call_user_func($callback,
'message',
"Using HTTP proxy $host:$port");
1596 $port = (isset($info[
'scheme']) && $info[
'scheme'] ==
'https') ? 443 : 80;
1599 $scheme = (isset($info[
'scheme']) && $info[
'scheme'] ==
'https') ?
'https' :
'http';
1600 $secure = ($scheme ==
'https');
1602 $fp = $proxy->openSocket($host,
$port, $secure);
1605 $errno = $fp->getCode();
1606 $errstr = $fp->getMessage();
1607 call_user_func($callback,
'connfailed', array($host,
$port,
1613 $requestPath = $path;
1614 if ($proxy->isProxyConfigured()) {
1615 $requestPath = $url;
1618 if ($lastmodified ===
false || $lastmodified) {
1619 $request =
"GET $requestPath HTTP/1.1\r\n";
1621 $request =
"GET $requestPath HTTP/1.0\r\n";
1623 $request .=
"Host: $host\r\n";
1625 $ifmodifiedsince =
'';
1626 if (is_array($lastmodified)) {
1627 if (isset($lastmodified[
'Last-Modified'])) {
1628 $ifmodifiedsince =
'If-Modified-Since: ' . $lastmodified[
'Last-Modified'] .
"\r\n";
1631 if (isset($lastmodified[
'ETag'])) {
1632 $ifmodifiedsince .=
"If-None-Match: $lastmodified[ETag]\r\n";
1635 $ifmodifiedsince = ($lastmodified ?
"If-Modified-Since: $lastmodified\r\n" :
'');
1638 $request .= $ifmodifiedsince .
1639 "User-Agent: PEAR/1.10.18/PHP/" . PHP_VERSION .
"\r\n";
1641 if ($object !==
null) {
1642 $username =
$config->get(
'username',
null, $channel);
1643 $password =
$config->get(
'password',
null, $channel);
1644 if ($username && $password) {
1645 $tmp = base64_encode(
"$username:$password");
1646 $request .=
"Authorization: Basic $tmp\r\n";
1650 $proxyAuth = $proxy->getProxyAuth();
1652 $request .=
'Proxy-Authorization: Basic ' .
1653 $proxyAuth .
"\r\n";
1657 $request .=
'Accept: ' . implode(
', ', $accept) .
"\r\n";
1660 $request .=
"Connection: close\r\n";
1662 fwrite($fp, $request);
1665 while (trim($line = fgets($fp, 1024))) {
1666 if (preg_match(
'/^([^:]+):\s+(.*)\s*\\z/', $line, $matches)) {
1667 $headers[strtolower($matches[1])] = trim($matches[2]);
1668 } elseif (preg_match(
'|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
1669 $reply = (int)$matches[1];
1670 if ($reply == 304 && ($lastmodified || ($lastmodified ===
false))) {
1674 if (!in_array($reply, array(200, 301, 302, 303, 305, 307))) {
1675 return PEAR::raiseError(
"File $scheme://$host:$port$path not valid (received: $line)");
1680 if ($reply != 200) {
1681 if (!isset($headers[
'location'])) {
1682 return PEAR::raiseError(
"File $scheme://$host:$port$path not valid (redirected but no location)");
1685 if ($wasredirect > 4) {
1686 return PEAR::raiseError(
"File $scheme://$host:$port$path not valid (redirection looped more than 5 times)");
1689 $redirect = $wasredirect + 1;
1690 return static::_downloadHttp($object, $headers[
'location'],
1691 $ui, $save_dir, $callback, $lastmodified, $accept);
1694 if (isset($headers[
'content-disposition']) &&
1695 preg_match(
'/\sfilename=\"([^;]*\S)\"\s*(;|\\z)/', $headers[
'content-disposition'], $matches)) {
1696 $save_as = basename($matches[1]);
1698 $save_as = basename($url);
1702 $tmp = call_user_func($callback,
'saveas', $save_as);
1708 $dest_file = $save_dir . DIRECTORY_SEPARATOR . $save_as;
1709 if (is_link($dest_file)) {
1710 return PEAR::raiseError(
'SECURITY ERROR: Will not write to ' . $dest_file .
' as it is symlinked to ' . readlink($dest_file) .
' - Possible symlink attack');
1713 if (!$wp = @fopen($dest_file,
'wb')) {
1716 call_user_func($callback,
'writefailed',
1717 array($dest_file, error_get_last()[
"message"]));
1719 return PEAR::raiseError(
"could not open $dest_file for writing");
1722 $length = isset($headers[
'content-length']) ? $headers[
'content-length'] : -1;
1726 call_user_func($callback,
'start', array(basename($dest_file), $length));
1729 if (isset($headers[
'transfer-encoding']) && strtolower($headers[
'transfer-encoding']) ===
'chunked') {
1730 while (!feof($fp)) {
1731 $chunk_size = hexdec(fgets($fp));
1732 if ($chunk_size === 0) {
1735 $data = fread($fp, $chunk_size);
1736 $bytes += strlen($data);
1738 call_user_func($callback,
'bytesread', $bytes);
1740 if (!@fwrite($wp, $data)) {
1743 call_user_func($callback,
'writefailed',
1744 array($dest_file, error_get_last()[
"message"]));
1746 return PEAR::raiseError(
1747 "$dest_file: write failed (" . error_get_last()[
"message"] .
")");
1752 while ($data = fread($fp, 1024)) {
1753 $bytes += strlen($data);
1755 call_user_func($callback,
'bytesread', $bytes);
1757 if (!@fwrite($wp, $data)) {
1760 call_user_func($callback,
'writefailed',
1761 array($dest_file, error_get_last()[
"message"]));
1763 return PEAR::raiseError(
1764 "$dest_file: write failed (" . error_get_last()[
"message"] .
")");
1772 call_user_func($callback,
'done', $bytes);
1775 if ($lastmodified ===
false || $lastmodified) {
1776 if (isset($headers[
'etag'])) {
1777 $lastmodified = array(
'ETag' => $headers[
'etag']);
1780 if (isset($headers[
'last-modified'])) {
1781 if (is_array($lastmodified)) {
1782 $lastmodified[
'Last-Modified'] = $headers[
'last-modified'];
1784 $lastmodified = $headers[
'last-modified'];
1787 return array($dest_file, $lastmodified, $headers);