From 3ada0093f9c09489a765810ebc9b1cc4a2886a33 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Mon, 25 Jan 2016 07:41:56 +0100 Subject: [PATCH 1/4] Note the final URL used to retrieve content after all redirects --- boot.php | 9 +++++++++ include/network.php | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/boot.php b/boot.php index 2242ba476..9ee0c98df 100644 --- a/boot.php +++ b/boot.php @@ -526,6 +526,7 @@ class App { private $curl_code; private $curl_content_type; private $curl_headers; + private $curl_redirect_url; private $cached_profile_image; private $cached_profile_picdate; @@ -909,6 +910,14 @@ class App { return $this->curl_headers; } + function set_curl_redirect_url($url) { + $this->curl_redirect_url = $url; + } + + function get_curl_redirect_url() { + return $this->curl_redirect_url; + } + function get_cached_avatar_image($avatar_image){ return $avatar_image; diff --git a/include/network.php b/include/network.php index cc267966e..1fd379275 100644 --- a/include/network.php +++ b/include/network.php @@ -136,6 +136,10 @@ function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) { $base = substr($base,strlen($chunk)); } + $a->set_curl_code($http_code); + $a->set_curl_content_type($curl_info['content_type']); + $a->set_curl_headers($header); + if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) { $new_location_info = @parse_url($curl_info["redirect_url"]); $old_location_info = @parse_url($curl_info["url"]); @@ -154,16 +158,17 @@ function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) { if (filter_var($newurl, FILTER_VALIDATE_URL)) { $redirects++; @curl_close($ch); + $a->set_curl_redirect_url($newurl); return z_fetch_url($newurl,$binary, $redirects, $opts); } } + $a->set_curl_redirect_url($url); $a->set_curl_code($http_code); $a->set_curl_content_type($curl_info['content_type']); $body = substr($s,strlen($header)); - $a->set_curl_headers($header); From 930da0aa44632e852f41008ff00cfc883cb1c17e Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Mon, 25 Jan 2016 11:05:17 +0100 Subject: [PATCH 2/4] Return redirect_url as part of the return array from z_fetch_url instead of via global state --- include/network.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/network.php b/include/network.php index 1fd379275..86d5eda62 100644 --- a/include/network.php +++ b/include/network.php @@ -158,13 +158,11 @@ function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) { if (filter_var($newurl, FILTER_VALIDATE_URL)) { $redirects++; @curl_close($ch); - $a->set_curl_redirect_url($newurl); return z_fetch_url($newurl,$binary, $redirects, $opts); } } - $a->set_curl_redirect_url($url); $a->set_curl_code($http_code); $a->set_curl_content_type($curl_info['content_type']); @@ -175,6 +173,7 @@ function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) { $rc = intval($http_code); $ret['return_code'] = $rc; $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false); + $ret['redirect_url'] = $url; if(! $ret['success']) { $ret['error'] = curl_error($ch); $ret['debug'] = $curl_info; From cfd40896de0aa29511dccf0adfce958453be3940 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Mon, 25 Jan 2016 11:06:10 +0100 Subject: [PATCH 3/4] Remove global state for redirect_url --- boot.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/boot.php b/boot.php index 9ee0c98df..2242ba476 100644 --- a/boot.php +++ b/boot.php @@ -526,7 +526,6 @@ class App { private $curl_code; private $curl_content_type; private $curl_headers; - private $curl_redirect_url; private $cached_profile_image; private $cached_profile_picdate; @@ -910,14 +909,6 @@ class App { return $this->curl_headers; } - function set_curl_redirect_url($url) { - $this->curl_redirect_url = $url; - } - - function get_curl_redirect_url() { - return $this->curl_redirect_url; - } - function get_cached_avatar_image($avatar_image){ return $avatar_image; From 4ff8c9e3a1f3e81ecec7fdeca2a92c1738c57b78 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Mon, 25 Jan 2016 11:38:36 +0100 Subject: [PATCH 4/4] Document new redirect_url return value --- include/network.php | 1 + 1 file changed, 1 insertion(+) diff --git a/include/network.php b/include/network.php index 86d5eda62..c6379e407 100644 --- a/include/network.php +++ b/include/network.php @@ -42,6 +42,7 @@ if(!function_exists('z_fetch_url')){ * @return array an assoziative array with: * * \e int \b return_code => HTTP return code or 0 if timeout or failure * * \e boolean \b success => boolean true (if HTTP 2xx result) or false + * * \e string \b redirect_url => in case of redirect, content was finally retrieved from this URL * * \e string \b header => HTTP headers * * \e string \b body => fetched content */