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

Avoid storing whole file content in memory #192

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions classes/Tools14.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,14 @@ public static function file_exists_cache($filename)
return self::$file_exists_cache[$filename];
}

/**
* Check config & source file to settle which dl method to use
*/
public static function shouldUseFopen($url)
{
return in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url);
}

public static function file_get_contents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 5)
{
if (!extension_loaded('openssl') and strpos('https://', $url) === true) {
Expand All @@ -1396,7 +1404,7 @@ public static function file_get_contents($url, $use_include_path = false, $strea
if ($stream_context == null && preg_match('/^https?:\/\//', $url)) {
$stream_context = @stream_context_create(array('http' => array('timeout' => $curl_timeout, 'header' => "User-Agent:MyAgent/1.0\r\n")));
}
if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url)) {
if (self::shouldUseFopen($url)) {
$var = @file_get_contents($url, $use_include_path, $stream_context);

/* PSCSX-3205 buffer output ? */
Expand Down Expand Up @@ -2435,12 +2443,37 @@ public static function nl2br($str)
return str_replace(array("\r\n", "\r", "\n"), '<br />', $str);
}

/**
* Copy a file to another place
*
* @return bool True if the copy succeded
*/
public static function copy($source, $destination, $stream_context = null)
{
if (is_null($stream_context) && !preg_match('/^https?:\/\//', $source)) {
return @copy($source, $destination);
}

return @file_put_contents($destination, self::file_get_contents($source, false, $stream_context, 60));
$destFile = fopen($destination, 'wb');
if (!is_resource($destFile)) {
return false;
}

if (self::shouldUseFopen($source)) {
$sourceFile = fopen($source);
// If something else than false, the data was stored
$result = (file_put_contents($destination, $sourceFile) !== false);
fclose($sourceFile);
} elseif (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_FILE, $destFile);
$result = curl_exec($ch);
curl_close($ch);
}

fclose($destFile);

return $result;
}
}
11 changes: 10 additions & 1 deletion classes/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace PrestaShop\Module\AutoUpgrade;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Configuration;

class Upgrader
Expand Down Expand Up @@ -104,7 +106,14 @@ public function downloadLast($dest, $filename = 'prestashop.zip')

$destPath = realpath($dest) . DIRECTORY_SEPARATOR . $filename;

Tools14::copy($this->link, $destPath);
try {
$filesystem = new Filesystem();
$filesystem->copy($this->link, $destPath);
} catch (IOException $e) {
// If the Symfony filesystem failed, we can try with
// the legacy method which uses curl.
Tools14::copy($this->link, $destPath);
}

return is_file($destPath);
}
Expand Down