示例#1
0
 /**
  * Downloads file.
  *
  * Sets $this->ErrorMsg in case of an error.
  *
  * \private
  * \param $url            URL.
  * \param $outDir         Directory where to put downloaded file to.
  * \param $forcedFileName Force saving downloaded file under this name.
  * \return false on error, path to downloaded package otherwise.
  */
 function downloadFile($url, $outDir, $forcedFileName = false)
 {
     $fileName = $outDir . "/" . ($forcedFileName ? $forcedFileName : basename($url));
     eZDebug::writeNotice("Downloading file '{$fileName}' from {$url}");
     // Create the out directory if not exists.
     if (!file_exists($outDir)) {
         eZDir::mkdir($outDir, false, true);
     }
     // First try CURL
     if (extension_loaded('curl')) {
         $ch = curl_init($url);
         $fp = eZStepSiteTypes::fopen($fileName, 'wb');
         if ($fp === false) {
             $this->ErrorMsg = ezpI18n::tr('design/standard/setup/init', 'Cannot write to file') . ': ' . $this->FileOpenErrorMsg;
             return false;
         }
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_FAILONERROR, 1);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
         // Get proxy
         $ini = eZINI::instance();
         $proxy = $ini->hasVariable('ProxySettings', 'ProxyServer') ? $ini->variable('ProxySettings', 'ProxyServer') : false;
         if ($proxy) {
             curl_setopt($ch, CURLOPT_PROXY, $proxy);
             $userName = $ini->hasVariable('ProxySettings', 'User') ? $ini->variable('ProxySettings', 'User') : false;
             $password = $ini->hasVariable('ProxySettings', 'Password') ? $ini->variable('ProxySettings', 'Password') : false;
             if ($userName) {
                 curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$userName}:{$password}");
             }
         }
         if (!curl_exec($ch)) {
             $this->ErrorMsg = curl_error($ch);
             return false;
         }
         curl_close($ch);
         fclose($fp);
     } else {
         $parsedUrl = parse_url($url);
         $checkIP = isset($parsedUrl['host']) ? ip2long(gethostbyname($parsedUrl['host'])) : false;
         if ($checkIP === false) {
             return false;
         }
         // If we don't have CURL installed we used standard fopen urlwrappers
         // Note: Could be blocked by not allowing remote calls.
         if (!copy($url, $fileName)) {
             $buf = eZHTTPTool::sendHTTPRequest($url, 80, false, 'eZ Publish', false);
             $header = false;
             $body = false;
             if (eZHTTPTool::parseHTTPResponse($buf, $header, $body)) {
                 eZFile::create($fileName, false, $body);
             } else {
                 $this->ErrorMsg = ezpI18n::tr('design/standard/setup/init', 'Failed to copy %url to local file %filename', null, array("%url" => $url, "%filename" => $fileName));
                 return false;
             }
         }
     }
     return $fileName;
 }