Merge pull request #9 from oohlaf/fixes

HTTP redirect fixes and a include file fix
This commit is contained in:
friendica 2012-12-26 12:57:52 -08:00
commit 91dac0f18a
2 changed files with 34 additions and 22 deletions

View File

@ -72,7 +72,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
$base = substr($base,strlen($chunk)); $base = substr($base,strlen($chunk));
} }
if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) { if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) {
$matches = array(); $matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches)); $newurl = trim(array_pop($matches));
@ -81,6 +81,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
$url_parsed = @parse_url($newurl); $url_parsed = @parse_url($newurl);
if (isset($url_parsed)) { if (isset($url_parsed)) {
$redirects++; $redirects++;
@curl_close($ch);
return fetch_url($newurl,$binary,$redirects,$timeout); return fetch_url($newurl,$binary,$redirects,$timeout);
} }
} }
@ -161,7 +162,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
$base = substr($base,strlen($chunk)); $base = substr($base,strlen($chunk));
} }
if($http_code == 301 || $http_code == 302 || $http_code == 303) { if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) {
$matches = array(); $matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches)); $newurl = trim(array_pop($matches));
@ -170,7 +171,12 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
$url_parsed = @parse_url($newurl); $url_parsed = @parse_url($newurl);
if (isset($url_parsed)) { if (isset($url_parsed)) {
$redirects++; $redirects++;
@curl_close($ch);
if($http_code == 303) {
return fetch_url($newurl,false,$redirects,$timeout); return fetch_url($newurl,false,$redirects,$timeout);
} else {
return post_url($newurl,$params,$redirects,$timeout);
}
} }
} }
$a->set_curl_code($http_code); $a->set_curl_code($http_code);
@ -247,7 +253,7 @@ function z_fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accep
$base = substr($base,strlen($chunk)); $base = substr($base,strlen($chunk));
} }
if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) { if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) {
$matches = array(); $matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches)); $newurl = trim(array_pop($matches));
@ -339,7 +345,7 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0
$base = substr($base,strlen($chunk)); $base = substr($base,strlen($chunk));
} }
if($http_code == 301 || $http_code == 302 || $http_code == 303) { if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) {
$matches = array(); $matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches)); $newurl = trim(array_pop($matches));
@ -349,9 +355,13 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0
if (isset($url_parsed)) { if (isset($url_parsed)) {
$redirects++; $redirects++;
curl_close($ch); curl_close($ch);
if($http_code == 303) {
return z_fetch_url($newurl,false,$headers,$redirects,$timeout);
} else {
return z_post_url($newurl,$params,$headers,$redirects,$timeout); return z_post_url($newurl,$params,$headers,$redirects,$timeout);
} }
} }
}
$rc = intval($http_code); $rc = intval($http_code);
$ret['return_code'] = $rc; $ret['return_code'] = $rc;
$ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false); $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false);

View File

@ -1,5 +1,7 @@
<?php <?php
@require_once('include/zot.php');
function magic_init(&$a) { function magic_init(&$a) {
$url = ((x($_REQUEST,'url')) ? $_REQUEST['url'] : ''); $url = ((x($_REQUEST,'url')) ? $_REQUEST['url'] : '');