public function downloadAndReturn($url, $from = null, $to = null, array $params = array())
{
$ch = curl_init();
if (empty($from)) {
$from = 0;
}
if (empty($to)) {
$to = 0;
}
if ($to < $from) {
$temp = $to;
$to = $from;
$from = $temp;
unset($temp);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSLVERSION, 0);
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cacert.pem');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'reponseHeaderCallback'));
if (!(empty($from) && empty($to))) {
curl_setopt($ch, CURLOPT_RANGE, "{$from}-{$to}");
}
if (!empty($params)) {
foreach ($params as $k => $v) {
@curl_setopt($ch, $k, $v);
}
}
$result = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
$error = '';
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === false) {
$error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_CURL_ERROR', $errno, $errmsg);
} elseif ($http_status >= 300 && $http_status <= 399 && isset($this->headers['Location']) && !empty($this->headers['Location'])) {
return $this->downloadAndReturn($this->headers['Location'], $from, $to, $params);
} elseif ($http_status > 399) {
$result = false;
$errno = $http_status;
$error = JText::sprintf('LIB_FOF_DOWNLOAD_ERR_HTTPERROR', $http_status);
}
curl_close($ch);
if ($result === false) {
throw new DownloadError($error, $errno);
} else {
return $result;
}
}