Fixing RemoteFile and Proxy Usage
authorTim Düsterhus <timwolla@arcor.de>
Thu, 17 May 2012 13:59:11 +0000 (15:59 +0200)
committerTim Düsterhus <timwolla@arcor.de>
Thu, 17 May 2012 13:59:11 +0000 (15:59 +0200)
- RemoteFile no longer accepts an array of options, fsockopen does not support them
- Rewrite FileUtil::downloadFileFromHTTP to support proxy

wcfsetup/install/files/lib/system/io/RemoteFile.class.php
wcfsetup/install/files/lib/util/FileUtil.class.php

index 2c141b3b359ea96801d84bd456b756592061dc04..286c50b243280578d1571d8861073ddb836f7d98 100644 (file)
@@ -45,16 +45,11 @@ class RemoteFile extends File {
         * @param       integer         $timeout
         * @param       array           $options
         */
-       public function __construct($host, $port, $timeout = 30, $options = array()) {
+       public function __construct($host, $port, $timeout = 3) {
                $this->host = $host;
                $this->port = $port;
-               if (count($options)) {
-                       $context = stream_context_create($options);
-                       $this->resource = fsockopen($host, $port, $this->errorNumber, $this->errorDesc, $timeout, $context);
-               }
-               else {
-                       $this->resource = fsockopen($host, $port, $this->errorNumber, $this->errorDesc, $timeout);
-               }
+               
+               $this->resource = fsockopen($host, $port, $this->errorNumber, $this->errorDesc, $timeout);
                if ($this->resource === false) {
                        throw new SystemException('Can not connect to ' . $host);
                }
index 6df24f453e5e6e8d007fb975194bef98c5dcf1d0..f9c7128b16536c04f240e9243e68d1fe5a03529f 100644 (file)
@@ -399,20 +399,11 @@ final class FileUtil {
                $newFileName = self::getTemporaryFilename($prefix.'_');
                $localFile = new File($newFileName); // the file to write.
                
-               // parse options
-               if (PROXY_SERVER_HTTP) {
-                       $options['http']['proxy'] = PROXY_SERVER_HTTP;
-                       $options['http']['request_fulluri'] = true;
-               }
-               $timeout = 30;
-               if (isset($options['timeout']))  {
-                       $timeout = $options['timeout'];
-                       unset($options['timeout']);
+               if (!isset($options['timeout'])) {
+                       $options['timeout'] = 30;
                }
-               $method = (!empty($postParameters) ? 'POST' : 'GET');
-               if (isset($options['method'])) {
-                       $method = $options['method'];
-                       unset($options['method']);
+               if (!isset($options['method'])) {
+                       $options['method'] = (!empty($postParameters) ? 'POST' : 'GET');
                }
                
                // parse URL
@@ -420,6 +411,14 @@ final class FileUtil {
                $port = ($parsedUrl['scheme'] == 'https' ? 443 : 80);
                $host = $parsedUrl['host'];
                $path = (isset($parsedUrl['path']) ? $parsedUrl['path'] : '/');
+               // proxy is set
+               if (PROXY_SERVER_HTTP) {
+                       $parsedProxy = parse_url(PROXY_SERVER_HTTP);
+                       $host = $parsedProxy['host'];
+                       $port = ($parsedProxy['scheme'] == 'https' ? 443 : 80);
+                       $path = $httpUrl;
+                       $parsedUrl['query'] = '';
+               }
                
                // build parameter-string
                $parameterString = '';
@@ -437,7 +436,7 @@ final class FileUtil {
                
                // connect
                try {
-                       $remoteFile = new RemoteFile(($parsedUrl['scheme'] == 'https' ? 'ssl://' : '').$host, $port, $timeout, $options);
+                       $remoteFile = new RemoteFile(($port === 443 ? 'ssl://' : '').$host, $port, $options['timeout']);
                }
                catch (SystemException $e) {
                        $localFile->close();
@@ -446,7 +445,7 @@ final class FileUtil {
                }
                
                // build and send the http request.
-               $request = $method." ".$path.(!empty($parsedUrl['query']) ? '?'.$parsedUrl['query'] : '')." HTTP/1.0\r\n";
+               $request = $options['method']." ".$path.(!empty($parsedUrl['query']) ? '?'.$parsedUrl['query'] : '')." HTTP/1.0\r\n";
                $request .= "User-Agent: HTTP.PHP (FileUtil.class.php; WoltLab Community Framework/".WCF_VERSION."; ".WCF::getLanguage()->languageCode.")\r\n";
                $request .= "Accept: */*\r\n";
                $request .= "Accept-Language: ".WCF::getLanguage()->languageCode."\r\n";