Skip to content

Commit

Permalink
Only download via curl if the url is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer authored Aug 1, 2018
1 parent 1abfbf7 commit f1f19c0
Showing 1 changed file with 63 additions and 23 deletions.
86 changes: 63 additions & 23 deletions src/AutoUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,18 @@ public function checkUpdate()
$this->_log->addDebug(sprintf('Get new updates from %s', $updateFile));

// Read update file from update server
if (function_exists('curl_version')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $updateFile);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$update = curl_exec($curl);
curl_close($curl);
} elseif (ini_get('allow_url_fopen')) {
if (function_exists('curl_version') && $this->_isValidUrl($updateFile)) {
$update = $this->_downloadCurl($updateUrl);
if ($update === false) {
return false;
}
} else {
$update = @file_get_contents($updateFile, false, $this->_useBasicAuth());
}
if ($update === false) {
$this->_log->addInfo(sprintf('Could not download update file "%s"!', $updateFile));
if ($update === false) {
$this->_log->addError(sprintf('Could not download update file "%s"!', $updateFile));

return false;
return false;
}
}

// Parse update file
Expand Down Expand Up @@ -546,6 +545,50 @@ public function newVersionAvailable()
return Comparator::greaterThan($this->_latestVersion, $this->_currentVersion);
}

/**
* Check if url is valid.
*
* @param string $url
*
* @return boolean
*/
protected function _isValidUrl($url)
{
return (filter_var($url, FILTER_VALIDATE_URL) !== false);
}

/**
* Download file via curl.
*
* @param string $url URL to file
*
* @return string|false
*/
protected function _downloadCurl($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $updateUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$update = curl_exec($curl);

$error = false;
if (curl_error($ch)) {
$error = true;
$this->_log->addError(sprintf(
'Could not download update "%s" via curl: %s!',
$updateUrl,
curl_error($ch)
));
}
curl_close($curl);

if ($error === true) {
return false;
}

return $update;
}

/**
* Download the update
*
Expand All @@ -557,24 +600,21 @@ public function newVersionAvailable()
protected function _downloadUpdate($updateUrl, $updateFile)
{
$this->_log->addInfo(sprintf('Downloading update "%s" to "%s"', $updateUrl, $updateFile));
if (function_exists('curl_version')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $updateUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$update = curl_exec($curl);
curl_close($curl);
if (function_exists('curl_version') && $this->_isValidUrl($updateUrl)) {
$update = $this->_downloadCurl($updateUrl);
if ($update === false) {
return false;
}
} elseif (ini_get('allow_url_fopen')) {
$update = @file_get_contents($updateUrl, false, $this->_useBasicAuth());
}

if ($update === false) {
$this->_log->addError(sprintf('Could not download update "%s"!', $updateUrl));
if ($update === false) {
$this->_log->addError(sprintf('Could not download update "%s"!', $updateUrl));

return false;
return false;
}
}

$handle = fopen($updateFile, 'w');

if (!$handle) {
$this->_log->addError(sprintf('Could not open file handle to save update to "%s"!', $updateFile));

Expand Down

6 comments on commit f1f19c0

@ptdesign
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelfensdrfer this merge request gives an error with enabled curl - 7.61.0 in server with php 7.2

[2018-10-04 08:24:16] auto-update.NOTICE: Checking for a new update... [] []
[2018-10-04 08:24:16] auto-update.DEBUG: Get new updates from http://192.168.1.80/1.1/update.json [] []

@thelfensdrfer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ptdesign What is the error?

@ptdesign
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelfensdrfer well, no (error) seems doesn´t find any update. If i revert this commit it works.

This is the log:
[2018-10-04 08:24:16] auto-update.NOTICE: Checking for a new update... [] []
[2018-10-04 08:24:16] auto-update.DEBUG: Get new updates from http://192.168.1.80/1.1/update.json [] []

@thelfensdrfer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try again with version 0.12? It should not be fixed but it's strange that you don't get any further log messages or errors. I added a few exceptions as suggested in issue #42, maybe this will show something.

@ptdesign
Copy link

@ptdesign ptdesign commented on f1f19c0 Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelfensdrfer ok, so now i get the error:

Warning: curl_error() expects parameter 1 to be resource, null given in /home/mysite/public_html/site/lib/src/AutoUpdate.php on line 582

Fatal error: Uncaught VisualAppeal\Exceptions\DownloadException: https://192.168.1.81/migration/1.1/update.json in /home/mysite/public_html/site/lib/src/AutoUpdate.php:459 Stack trace: #0 /home/mysite/public_html/site/update/update.php(17): VisualAppeal\AutoUpdate->checkUpdate() #1 {main} thrown in /home/mysite/public_html/site/lib/src/AutoUpdate.php on line 459

and in log:

[2018-10-05 15:33:33] auto-update.DEBUG: Get new updates from https://192.168.1.81/migration/1.1/update.json [] []
[2018-10-05 15:33:33] auto-update.ERROR: Could not download update file "https://192.168.1.81/migration/1.1/update.json" via curl! [] []

@thelfensdrfer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try again with the newest version 0.12.1?

Please sign in to comment.