Workaround for curl follow redirect + open base dir restrictions

This commit is contained in:
Frédéric Guillot 2013-08-14 21:18:23 -04:00
parent 68a8e8f954
commit 9af4f3db52
2 changed files with 32 additions and 4 deletions

View File

@ -73,7 +73,7 @@ abstract class Client
foreach ($lines as $line) {
if (strpos($line, 'HTTP') === 0 && strpos($line, '301') === false && strpos($line, '302') === false) {
if (strpos($line, 'HTTP') === 0/* && strpos($line, '301') === false && strpos($line, '302') === false*/) {
$status = (int) substr($line, 9, 3);
}

View File

@ -18,7 +18,6 @@ class Curl extends \PicoFeed\Client
$this->body_length += $length;
if ($this->body_length > $this->max_body_size) return -1;
$this->body .= $buffer;
return $length;
@ -45,7 +44,7 @@ class Curl extends \PicoFeed\Client
}
public function doRequest()
public function doRequest($follow_location = true)
{
$request_headers = array('Connection: close');
@ -59,7 +58,7 @@ class Curl extends \PicoFeed\Client
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ini_get('open_basedir') === '');
curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For auto-signed certificates...
@ -84,6 +83,35 @@ class Curl extends \PicoFeed\Client
list($status, $headers) = $this->parseHeaders(explode("\r\n", $this->headers[$this->headers_counter - 1]));
if ($follow_location && ini_get('open_basedir') !== '' && ($status == 301 || $status == 302)) {
$nb_redirects = 0;
$this->url = $headers['Location'];
$this->body = '';
$this->body_length = 0;
$this->headers = array();
$this->headers_counter = 0;
while (true) {
$nb_redirects++;
if ($nb_redirects >= $this->max_redirects) return false;
$result = $this->doRequest(false);
if ($result['status'] == 301 || $result['status'] == 302) {
$this->url = $result['headers']['Location'];
$this->body = '';
$this->body_length = 0;
$this->headers = array();
$this->headers_counter = 0;
}
else {
return $result;
}
}
}
return array(
'status' => $status,
'body' => $this->body,