From afc7142250dc02654a53a7222abe88df964d58f4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 28 Feb 2021 10:12:57 +0300 Subject: [PATCH] move all $fetch globals to UrlHelper --- backend.php | 2 +- classes/feeds.php | 12 +-- classes/rssutils.php | 29 ++---- classes/urlhelper.php | 93 +++++++++++-------- include/functions.php | 12 +-- plugins/af_comics/filters/af_comics_cad.php | 7 +- .../filters/af_comics_darklegacy.php | 6 +- .../af_comics/filters/af_comics_dilbert.php | 6 +- plugins/af_comics/filters/af_comics_whomp.php | 6 +- plugins/af_proxy_http/init.php | 8 +- plugins/af_readability/init.php | 6 +- plugins/af_redditimgur/init.php | 3 +- 12 files changed, 83 insertions(+), 107 deletions(-) diff --git a/backend.php b/backend.php index e310322f2..545d9634d 100644 --- a/backend.php +++ b/backend.php @@ -157,6 +157,6 @@ } header("Content-Type: text/json"); - print Errors::to_json(Errors::E_UNKNOWN_METHOD, ["info" => get_class($handler) . "->$method"]); + print Errors::to_json(Errors::E_UNKNOWN_METHOD, [ "info" => (isset($handler) ? get_class($handler) : "UNKNOWN:".$_REQUEST["op"]) . "->$method"]); ?> diff --git a/classes/feeds.php b/classes/feeds.php index 1ab0ca3e3..55a71aebb 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -989,10 +989,6 @@ class Feeds extends Handler_Protected { static function _subscribe($url, $cat_id = 0, $auth_login = '', $auth_pass = '') { - global $fetch_last_error; - global $fetch_last_error_content; - global $fetch_last_content_type; - $pdo = Db::pdo(); $url = UrlHelper::validate($url); @@ -1008,14 +1004,14 @@ class Feeds extends Handler_Protected { $contents, $url, $auth_login, $auth_pass); if (empty($contents)) { - if (preg_match("/cloudflare\.com/", $fetch_last_error_content)) { - $fetch_last_error .= " (feed behind Cloudflare)"; + if (preg_match("/cloudflare\.com/", UrlHelper::$fetch_last_error_content)) { + UrlHelper::$fetch_last_error .= " (feed behind Cloudflare)"; } - return array("code" => 5, "message" => $fetch_last_error); + return array("code" => 5, "message" => UrlHelper::$fetch_last_error); } - if (mb_strpos($fetch_last_content_type, "html") !== false && self::_is_html($contents)) { + if (mb_strpos(UrlHelper::$fetch_last_content_type, "html") !== false && self::_is_html($contents)) { $feedUrls = self::_get_feeds_from_html($url, $contents); if (count($feedUrls) == 0) { diff --git a/classes/rssutils.php b/classes/rssutils.php index 62f8d9687..d9f97e602 100755 --- a/classes/rssutils.php +++ b/classes/rssutils.php @@ -229,11 +229,9 @@ class RSSUtils { } else { try { if (!self::update_rss_feed($tline["id"], true)) { - global $fetch_last_error; - Logger::log(E_USER_NOTICE, sprintf("Update request for feed %d (%s, owner UID: %d) failed: %s.", - $tline["id"], clean($tline["title"]), $tline["owner_uid"], clean($fetch_last_error))); + $tline["id"], clean($tline["title"]), $tline["owner_uid"], clean(UrlHelper::$fetch_last_error))); } Debug::log(sprintf("<= %.4f (sec) (not using a separate process)", microtime(true) - $fstarted)); @@ -490,11 +488,8 @@ class RSSUtils { $feed_data = trim($feed_data); - global $fetch_effective_url; - global $fetch_effective_ip_addr; - Debug::log("fetch done.", Debug::$LOG_VERBOSE); - Debug::log("effective URL (after redirects): " . clean($fetch_effective_url) . " (IP: $fetch_effective_ip_addr)", Debug::$LOG_VERBOSE); + Debug::log("effective URL (after redirects): " . clean(UrlHelper::$fetch_effective_url) . " (IP: ".UrlHelper::$fetch_effective_ip_addr.")", Debug::$LOG_VERBOSE); Debug::log("source last modified: " . $fetch_last_modified, Debug::$LOG_VERBOSE); if ($feed_data && $fetch_last_modified != $stored_last_modified) { @@ -514,13 +509,10 @@ class RSSUtils { } if (!$feed_data) { - global $fetch_last_error; - global $fetch_last_error_code; - - Debug::log("unable to fetch: $fetch_last_error [$fetch_last_error_code]", Debug::$LOG_VERBOSE); + Debug::log("unable to fetch: ".UrlHelper::$fetch_last_error." [".UrlHelper::$fetch_last_error_code."]", Debug::$LOG_VERBOSE); // If-Modified-Since - if ($fetch_last_error_code == 304) { + if (UrlHelper::$fetch_last_error_code == 304) { Debug::log("source claims data not modified, nothing to do.", Debug::$LOG_VERBOSE); $error_message = ""; @@ -529,7 +521,7 @@ class RSSUtils { last_updated = NOW() WHERE id = ?"); } else { - $error_message = $fetch_last_error; + $error_message = UrlHelper::$fetch_last_error; $sth = $pdo->prepare("UPDATE ttrss_feeds SET last_error = ?, last_updated = NOW() WHERE id = ?"); @@ -1307,10 +1299,6 @@ class RSSUtils { Debug::log("cache_enclosures: downloading: $src to $local_filename", Debug::$LOG_VERBOSE); if (!$cache->exists($local_filename)) { - - global $fetch_last_error_code; - global $fetch_last_error; - $file_content = UrlHelper::fetch(array("url" => $src, "http_referrer" => $src, "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE))); @@ -1318,7 +1306,7 @@ class RSSUtils { if ($file_content) { $cache->put($local_filename, $file_content); } else { - Debug::log("cache_enclosures: failed with $fetch_last_error_code: $fetch_last_error"); + Debug::log("cache_enclosures: failed with ".UrlHelper::$fetch_last_error_code.": ".UrlHelper::$fetch_last_error); } } else if (is_writable($local_filename)) { $cache->touch($local_filename); @@ -1338,9 +1326,6 @@ class RSSUtils { if (!$cache->exists($local_filename)) { Debug::log("cache_media: downloading: $url to $local_filename", Debug::$LOG_VERBOSE); - global $fetch_last_error_code; - global $fetch_last_error; - $file_content = UrlHelper::fetch(array("url" => $url, "http_referrer" => $url, "max_size" => Config::get(Config::MAX_CACHE_FILE_SIZE))); @@ -1348,7 +1333,7 @@ class RSSUtils { if ($file_content) { $cache->put($local_filename, $file_content); } else { - Debug::log("cache_media: failed with $fetch_last_error_code: $fetch_last_error"); + Debug::log("cache_media: failed with ".UrlHelper::$fetch_last_error_code.": ".UrlHelper::$fetch_last_error); } } else if ($cache->is_writable($local_filename)) { $cache->touch($local_filename); diff --git a/classes/urlhelper.php b/classes/urlhelper.php index bf2e22a76..020210a53 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -1,5 +1,14 @@ loadHTML($res)) { $xpath = new DOMXPath($doc); diff --git a/plugins/af_comics/filters/af_comics_darklegacy.php b/plugins/af_comics/filters/af_comics_darklegacy.php index 978545431..359c56443 100644 --- a/plugins/af_comics/filters/af_comics_darklegacy.php +++ b/plugins/af_comics/filters/af_comics_darklegacy.php @@ -13,10 +13,8 @@ class Af_Comics_DarkLegacy extends Af_ComicFilter { false, false, 0, "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"); - global $fetch_last_error_content; - - if (!$res && $fetch_last_error_content) - $res = $fetch_last_error_content; + if (!$res && UrlHelper::$fetch_last_error_content) + $res = UrlHelper::$fetch_last_error_content; $doc = new DOMDocument(); diff --git a/plugins/af_comics/filters/af_comics_dilbert.php b/plugins/af_comics/filters/af_comics_dilbert.php index 31a72d88d..00aad78f2 100644 --- a/plugins/af_comics/filters/af_comics_dilbert.php +++ b/plugins/af_comics/filters/af_comics_dilbert.php @@ -14,10 +14,8 @@ class Af_Comics_Dilbert extends Af_ComicFilter { false, false, 0, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"); - global $fetch_last_error_content; - - if (!$res && $fetch_last_error_content) - $res = $fetch_last_error_content; + if (!$res && UrlHelper::$fetch_last_error_content) + $res = UrlHelper::$fetch_last_error_content; $doc = new DOMDocument(); diff --git a/plugins/af_comics/filters/af_comics_whomp.php b/plugins/af_comics/filters/af_comics_whomp.php index 021a2952a..f218890d7 100644 --- a/plugins/af_comics/filters/af_comics_whomp.php +++ b/plugins/af_comics/filters/af_comics_whomp.php @@ -12,10 +12,8 @@ class Af_Comics_Whomp extends Af_ComicFilter { false, false, 0, "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"); - global $fetch_last_error_content; - - if (!$res && $fetch_last_error_content) - $res = $fetch_last_error_content; + if (!$res && UrlHelper::$fetch_last_error_content) + $res = UrlHelper::$fetch_last_error_content; $doc = new DOMDocument(); diff --git a/plugins/af_proxy_http/init.php b/plugins/af_proxy_http/init.php index b03cacfe4..48c64567b 100644 --- a/plugins/af_proxy_http/init.php +++ b/plugins/af_proxy_http/init.php @@ -76,10 +76,6 @@ class Af_Proxy_Http extends Plugin { return; } } else { - global $fetch_last_error; - global $fetch_last_error_code; - global $fetch_last_error_content; - if (function_exists("imagecreate") && !isset($_REQUEST["text"])) { $img = imagecreate(450, 75); @@ -90,7 +86,7 @@ class Af_Proxy_Http extends Plugin { imagestring($img, 5, 5, 5, "Proxy request failed", $textcolor); imagestring($img, 5, 5, 30, truncate_middle($url, 46, "..."), $textcolor); - imagestring($img, 5, 5, 55, "HTTP Code: $fetch_last_error_code", $textcolor); + imagestring($img, 5, 5, 55, "HTTP Code: ".UrlHelper::$fetch_last_error_code, $textcolor); header("Content-type: image/png"); print imagepng($img); @@ -102,7 +98,7 @@ class Af_Proxy_Http extends Plugin { http_response_code(400); print "Proxy request failed.\n". - "Fetch error $fetch_last_error ($fetch_last_error_code)\n". + "Fetch error ".UrlHelper::$fetch_last_error." (".UrlHelper::$fetch_last_error_code.")\n". "Requested URL: $url"; } } diff --git a/plugins/af_readability/init.php b/plugins/af_readability/init.php index 172006d34..f8c7c125c 100755 --- a/plugins/af_readability/init.php +++ b/plugins/af_readability/init.php @@ -190,8 +190,6 @@ class Af_Readability extends Plugin { public function extract_content($url) { - global $fetch_effective_url; - $tmp = UrlHelper::fetch([ "url" => $url, "http_accept" => "text/*", @@ -224,13 +222,13 @@ class Af_Readability extends Plugin { foreach ($entries as $entry) { if ($entry->hasAttribute("href")) { $entry->setAttribute("href", - rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href"))); + rewrite_relative_url(UrlHelper::$fetch_effective_url, $entry->getAttribute("href"))); } if ($entry->hasAttribute("src")) { $entry->setAttribute("src", - rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src"))); + rewrite_relative_url(UrlHelper::$fetch_effective_url, $entry->getAttribute("src"))); } } diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php index dd9f57f55..507c495a0 100755 --- a/plugins/af_redditimgur/init.php +++ b/plugins/af_redditimgur/init.php @@ -276,8 +276,7 @@ class Af_RedditImgur extends Plugin { } } else { if (!$tmp) { - global $fetch_last_error; - Debug::log("JSON: failed to fetch post:" . $fetch_last_error, Debug::$LOG_EXTENDED); + Debug::log("JSON: failed to fetch post:" . UrlHelper::$fetch_last_error, Debug::$LOG_EXTENDED); } } } else if (!$anchor) {