Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple downloadurl tags in update stream #18553

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 80 additions & 1 deletion libraries/src/Updater/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,26 @@ public function _startElement($parser, $name, $attrs = array())
// Don't do anything
case 'UPDATES':
break;

// The downloadurl tag may be multiple, so get it as an array
case 'DOWNLOADURL':
if (!isset($this->currentUpdate->downloadurl))
{
$this->currentUpdate->downloadurl = array();
}

$new_element = new \stdClass;
$new_element->_data = '';

foreach ($attrs as $key => $data)
{
$key = strtolower($key);
$new_element->$key = $data;
}

array_push($this->currentUpdate->downloadurl, $new_element);

break;

// For everything else there's...the default!
default:
Expand Down Expand Up @@ -439,7 +459,22 @@ public function _characterData($parser, $data)

if (isset($this->currentUpdate->$tag))
{
$this->currentUpdate->$tag->_data .= $data;
// It's weird, but I ran into the situation where data contains only spaces
$data = trim($data);

// The downloadurl is an array
if ($tag == 'downloadurl')
{
if (!empty($data))
{
end($this->currentUpdate->downloadurl);
$this->currentUpdate->downloadurl[key($this->currentUpdate->downloadurl)]->_data = $data;
}
}
else
{
$this->currentUpdate->$tag->_data .= $data;
}
}
}

Expand Down Expand Up @@ -498,7 +533,51 @@ public function loadFromXml($url, $minimum_stability = Updater::STABILITY_STABLE
}

xml_parser_free($this->xmlParser);

// Why do I use this garbage code instead of Http instance? It's faster!
if (isset($this->currentUpdate->downloadurl))
{
$found = false;

foreach ($this->currentUpdate->downloadurl as $downloadurl)
{
$url = $downloadurl->_data;
$headers = get_headers($url, 1);
$code_pattern = '#[0-9]{3}#';
$type_pattern = '#application/#';

while (isset($headers['Location']))
{
$url = $headers['Location'];
$headers = get_headers($url, 1);
}

preg_match($code_pattern, array_shift($headers), $matches);
$code = count($matches) ? $matches[0] : null;

if (is_array($headers['Content-Type']))
{
$found = !empty(preg_grep($type_pattern, $headers['Content-Type']));
}
else
{
$found = preg_match($type_pattern, $headers['Content-Type']);
}

if ($found && ($code == 200 || $code == 310))
{
$this->currentUpdate->downloadurl = $downloadurl;
break;
}
}

// Should we pass at least 1 downloadurl, even it's not working?
if (!$found)
{
$this->currentUpdate->downloadurl = $this->currentUpdate->downloadurl[0];
}
}

return true;
}

Expand Down