function requestRates() { $error = array('code' => self::OK, 'description' => ezpI18n::tr('kernel/shop', "'Autorates' were retrieved successfully")); $serverName = $this->serverName(); $serverPort = $this->serverPort(); $ratesURI = $this->ratesURI(); $ratesList = array(); $buf = eZHTTPTool::sendHTTPRequest("{$serverName}/{$ratesURI}", $serverPort, false, 'eZ Publish', false); if ($buf) { $header = false; $body = false; if (eZHTTPTool::parseHTTPResponse($buf, $header, $body)) { if ($header['content-type'] === 'text/xml') { // parse xml $dom = new DOMDocument('1.0', 'utf-8'); $dom->preserveWhiteSpace = false; $success = $dom->loadXML($body); $xpath = new DOMXPath($dom); $xpath->registerNamespace('eurofxref', 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'); $rootNode = $dom->documentElement; $cubeNode = $xpath->query('eurofxref:Cube', $rootNode)->item(0); $timeNode = $cubeNode->firstChild; foreach ($timeNode->childNodes as $currencyNode) { $currencyCode = $currencyNode->getAttribute('currency'); $rateValue = $currencyNode->getAttribute('rate'); $ratesList[$currencyCode] = $rateValue; } } else { $error['code'] = self::FAILED; $error['description'] = ezpI18n::tr('kernel/shop', "Unknown body format in HTTP response. Expected 'text/xml'"); } } else { $error['code'] = self::FAILED; $error['description'] = ezpI18n::tr('kernel/shop', "Invalid HTTP response"); } } else { $error['code'] = self::FAILED; $error['description'] = ezpI18n::tr('kernel/shop', "Unable to send http request: %1:%2/%3", null, array($serverName, $serverPort, $ratesURI)); } $this->setRateList($ratesList); return $error; }
/** * 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; }