|
@@ -77,29 +77,19 @@ abstract class FileDownloader implements DownloaderInterface
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Handle system proxy
|
|
|
- $ctx = stream_context_create();
|
|
|
+ $auth = $this->io->getAuthentification($package->getSourceUrl());
|
|
|
|
|
|
- if (isset($_SERVER['HTTP_PROXY'])) {
|
|
|
- // http(s):// is not supported in proxy
|
|
|
- $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
|
|
|
-
|
|
|
- if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
|
|
|
- throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
|
|
|
- }
|
|
|
-
|
|
|
- $ctx = stream_context_create(array(
|
|
|
- 'http' => array(
|
|
|
- 'proxy' => $proxy,
|
|
|
- 'request_fulluri' => true,
|
|
|
- ),
|
|
|
- ));
|
|
|
+ if (isset($_SERVER['HTTP_PROXY'])) {
|
|
|
+ // http(s):// is not supported in proxy
|
|
|
+ $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
|
|
|
+
|
|
|
+ if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
|
|
|
+ throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- stream_context_set_params($ctx, array("notification" => array($this, 'callbackDownload')));
|
|
|
-
|
|
|
$this->io->overwrite(" Downloading: <comment>connection...</comment>", 80);
|
|
|
- copy($url, $fileName, $ctx);
|
|
|
+ $this->copy($url, $fileName, $auth['username'], $auth['password']);
|
|
|
$this->io->overwriteln(" Downloading", 80);
|
|
|
|
|
|
if (!file_exists($fileName)) {
|
|
@@ -156,52 +146,88 @@ abstract class FileDownloader implements DownloaderInterface
|
|
|
/**
|
|
|
* Download notification action.
|
|
|
*
|
|
|
- * @param integer $notificationCode The notification code
|
|
|
- * @param integer $severity The severity level
|
|
|
- * @param string $message The message
|
|
|
- * @param integer $messageCode The message code
|
|
|
- * @param integer $bytesTransferred The loaded size
|
|
|
- * @param integer $bytesMax The total size
|
|
|
+ * @param integer $sizeTotal The total size
|
|
|
+ * @param integer $sizeLoaded The loaded size
|
|
|
*/
|
|
|
- protected function callbackDownload($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
|
|
|
+ protected function callbackDownload($sizeTotal, $sizeLoaded)
|
|
|
{
|
|
|
- switch ($notificationCode) {
|
|
|
- case STREAM_NOTIFY_AUTH_REQUIRED:
|
|
|
- break;
|
|
|
-
|
|
|
- case STREAM_NOTIFY_FILE_SIZE_IS:
|
|
|
- if ($this->bytesMax < $bytesMax) {
|
|
|
- $this->bytesMax = $bytesMax;
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
-
|
|
|
- case STREAM_NOTIFY_PROGRESS:
|
|
|
- if ($this->bytesMax > 0) {
|
|
|
- $progression = 0;
|
|
|
+ if ($sizeTotal > 1024) {
|
|
|
+ $progression = 0;
|
|
|
|
|
|
- if ($this->bytesMax > 0) {
|
|
|
- $progression = ($bytesTransferred / $this->bytesMax * 100);
|
|
|
- }
|
|
|
+ if ($sizeTotal > 0) {
|
|
|
+ $progression = ($sizeLoaded / $sizeTotal * 100);
|
|
|
+ }
|
|
|
|
|
|
- $levels = array(0, 5, 10, 15, 20, 25, 30, 35, 40, 35, 50, 55, 60,
|
|
|
- 65, 70, 75, 80, 85, 90, 95, 100);
|
|
|
+ $levels = array(0, 5, 10, 15, 20, 25, 30, 35, 40, 35, 50, 55, 60,
|
|
|
+ 65, 70, 75, 80, 85, 90, 95, 100);
|
|
|
|
|
|
- $progression = round($progression, 0);
|
|
|
+ $progression = round($progression, 0);
|
|
|
|
|
|
- if (in_array($progression, $levels)) {
|
|
|
- $this->io->overwrite(" Downloading: <comment>$progression%</comment>", 80);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
+ if (in_array($progression, $levels)) {
|
|
|
+ $this->io->overwrite(" Downloading: <comment>$progression%</comment>", 80);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- case STREAM_NOTIFY_AUTH_RESULT:
|
|
|
- break;
|
|
|
+ /**
|
|
|
+ * Copy the content in file directory.
|
|
|
+ *
|
|
|
+ * @param string $url The file URL
|
|
|
+ * @param string $filename The local path
|
|
|
+ * @param string $username The username
|
|
|
+ * @param string $password The password
|
|
|
+ */
|
|
|
+ protected function copy($url, $filename, $username = null, $password = null)
|
|
|
+ {
|
|
|
+ // create directory
|
|
|
+ if (!file_exists(dirname($filename))) {
|
|
|
+ mkdir(dirname($filename), 0777, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ $fh = fopen($filename, 'c+');
|
|
|
+
|
|
|
+ // curl options
|
|
|
+ $defaults = array(
|
|
|
+ CURLOPT_RETURNTRANSFER => true,
|
|
|
+ CURLOPT_BINARYTRANSFER => true,
|
|
|
+ CURLOPT_BUFFERSIZE => 128000,
|
|
|
+ CURLOPT_FOLLOWLOCATION => true,
|
|
|
+ CURLOPT_NOPROGRESS => false,
|
|
|
+ CURLOPT_PROGRESSFUNCTION => array($this, 'callbackDownload'),
|
|
|
+ CURLOPT_URL => $url,
|
|
|
+ CURLOPT_HTTPGET => true,
|
|
|
+ CURLOPT_SSL_VERIFYPEER => false,
|
|
|
+ CURLOPT_FILE => $fh,
|
|
|
+ );
|
|
|
+
|
|
|
+ // add authorization to curl options
|
|
|
+ if (null !== $username && null !== $password) {
|
|
|
+ $defaults[CURLOPT_USERPWD] = $username . ':' . $password;
|
|
|
+ }
|
|
|
+
|
|
|
+ // init curl
|
|
|
+ $ch = curl_init();
|
|
|
+
|
|
|
+ // curl options
|
|
|
+ curl_setopt_array($ch, $defaults);
|
|
|
+
|
|
|
+ // run curl
|
|
|
+ $curl_result = curl_exec($ch);
|
|
|
+ $curl_info = curl_getinfo($ch);
|
|
|
+ $curl_errorCode = curl_errno($ch);
|
|
|
+ $curl_error = curl_error($ch);
|
|
|
+ $code = $curl_info['http_code'];
|
|
|
+ $code = null ? 0 : $code;
|
|
|
+
|
|
|
+ //close streams
|
|
|
+ curl_close($ch);
|
|
|
+ fclose($fh);
|
|
|
|
|
|
- default:
|
|
|
- break;
|
|
|
+ if (200 !== $code) {
|
|
|
+ return false;
|
|
|
}
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
/**
|