diff options
author | Andrew Dolgov <[email protected]> | 2021-02-28 10:12:57 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2021-02-28 10:12:57 +0300 |
commit | afc7142250dc02654a53a7222abe88df964d58f4 (patch) | |
tree | 9c31e51cb00fc8bff550995517acbf19b55b0607 /classes | |
parent | e2cbb54b2c8f5666a9eabcbf80fb6a3e4d0676a3 (diff) |
move all $fetch globals to UrlHelper
Diffstat (limited to 'classes')
-rwxr-xr-x | classes/feeds.php | 12 | ||||
-rwxr-xr-x | classes/rssutils.php | 29 | ||||
-rw-r--r-- | classes/urlhelper.php | 93 |
3 files changed, 63 insertions, 71 deletions
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 @@ <?php class UrlHelper { + static $fetch_last_error; + static $fetch_last_error_code; + static $fetch_last_error_content; + static $fetch_last_content_type; + static $fetch_last_modified; + static $fetch_effective_url; + static $fetch_effective_ip_addr; + static $fetch_curl_used; + static function build_url($parts) { $tmp = $parts['scheme'] . "://" . $parts['host']; @@ -158,6 +167,7 @@ class UrlHelper { public static function fetch($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false, 4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) { + /* global $fetch_last_error; global $fetch_last_error_code; global $fetch_last_error_content; @@ -166,15 +176,16 @@ class UrlHelper { global $fetch_effective_url; global $fetch_effective_ip_addr; global $fetch_curl_used; + */ - $fetch_last_error = false; - $fetch_last_error_code = -1; - $fetch_last_error_content = ""; - $fetch_last_content_type = ""; - $fetch_curl_used = false; - $fetch_last_modified = ""; - $fetch_effective_url = ""; - $fetch_effective_ip_addr = ""; + self::$fetch_last_error = false; + self::$fetch_last_error_code = -1; + self::$fetch_last_error_content = ""; + self::$fetch_last_content_type = ""; + self::$fetch_curl_used = false; + self::$fetch_last_modified = ""; + self::$fetch_effective_url = ""; + self::$fetch_effective_ip_addr = ""; if (!is_array($options)) { @@ -219,7 +230,7 @@ class UrlHelper { $url = self::validate($url, true); if (!$url) { - $fetch_last_error = "Requested URL failed extended validation."; + self::$fetch_last_error = "Requested URL failed extended validation."; return false; } @@ -227,13 +238,13 @@ class UrlHelper { $ip_addr = gethostbyname($url_host); if (!$ip_addr || strpos($ip_addr, "127.") === 0) { - $fetch_last_error = "URL hostname failed to resolve or resolved to a loopback address ($ip_addr)"; + self::$fetch_last_error = "URL hostname failed to resolve or resolved to a loopback address ($ip_addr)"; return false; } if (function_exists('curl_init') && !ini_get("open_basedir")) { - $fetch_curl_used = true; + self::$fetch_curl_used = true; $ch = curl_init($url); @@ -306,13 +317,13 @@ class UrlHelper { list ($key, $value) = explode(": ", $header); if (strtolower($key) == "last-modified") { - $fetch_last_modified = $value; + self::$fetch_last_modified = $value; } } if (substr(strtolower($header), 0, 7) == 'http/1.') { - $fetch_last_error_code = (int) substr($header, 9, 3); - $fetch_last_error = $header; + self::$fetch_last_error_code = (int) substr($header, 9, 3); + self::$fetch_last_error = $header; } } @@ -322,39 +333,39 @@ class UrlHelper { } $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - $fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); + self::$fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); - $fetch_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + self::$fetch_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); - if (!self::validate($fetch_effective_url, true)) { - $fetch_last_error = "URL received after redirection failed extended validation."; + if (!self::validate(self::$fetch_effective_url, true)) { + self::$fetch_last_error = "URL received after redirection failed extended validation."; return false; } - $fetch_effective_ip_addr = gethostbyname(parse_url($fetch_effective_url, PHP_URL_HOST)); + self::$fetch_effective_ip_addr = gethostbyname(parse_url(self::$fetch_effective_url, PHP_URL_HOST)); - if (!$fetch_effective_ip_addr || strpos($fetch_effective_ip_addr, "127.") === 0) { - $fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address ($fetch_effective_ip_addr)"; + if (!self::$fetch_effective_ip_addr || strpos(self::$fetch_effective_ip_addr, "127.") === 0) { + self::$fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address (".self::$fetch_effective_ip_addr.")"; return false; } - $fetch_last_error_code = $http_code; + self::$fetch_last_error_code = $http_code; - if ($http_code != 200 || $type && strpos($fetch_last_content_type, "$type") === false) { + if ($http_code != 200 || $type && strpos(self::$fetch_last_content_type, "$type") === false) { if (curl_errno($ch) != 0) { - $fetch_last_error .= "; " . curl_errno($ch) . " " . curl_error($ch); + self::$fetch_last_error .= "; " . curl_errno($ch) . " " . curl_error($ch); } - $fetch_last_error_content = $contents; + self::$fetch_last_error_content = $contents; curl_close($ch); return false; } if (!$contents) { - $fetch_last_error = curl_errno($ch) . " " . curl_error($ch); + self::$fetch_last_error = curl_errno($ch) . " " . curl_error($ch); curl_close($ch); return false; } @@ -372,7 +383,7 @@ class UrlHelper { return $contents; } else { - $fetch_curl_used = false; + self::$fetch_curl_used = false; if ($login && $pass){ $url_parts = array(); @@ -417,18 +428,18 @@ class UrlHelper { $old_error = error_get_last(); - $fetch_effective_url = self::resolve_redirects($url, $timeout ? $timeout : Config::get(Config::FILE_FETCH_CONNECT_TIMEOUT)); + self::$fetch_effective_url = self::resolve_redirects($url, $timeout ? $timeout : Config::get(Config::FILE_FETCH_CONNECT_TIMEOUT)); - if (!self::validate($fetch_effective_url, true)) { - $fetch_last_error = "URL received after redirection failed extended validation."; + if (!self::validate(self::$fetch_effective_url, true)) { + self::$fetch_last_error = "URL received after redirection failed extended validation."; return false; } - $fetch_effective_ip_addr = gethostbyname(parse_url($fetch_effective_url, PHP_URL_HOST)); + self::$fetch_effective_ip_addr = gethostbyname(parse_url(self::$fetch_effective_url, PHP_URL_HOST)); - if (!$fetch_effective_ip_addr || strpos($fetch_effective_ip_addr, "127.") === 0) { - $fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address ($fetch_effective_ip_addr)"; + if (!self::$fetch_effective_ip_addr || strpos(self::$fetch_effective_ip_addr, "127.") === 0) { + self::$fetch_last_error = "URL hostname received after redirection failed to resolve or resolved to a loopback address (".self::$fetch_effective_ip_addr.")"; return false; } @@ -442,30 +453,30 @@ class UrlHelper { $key = strtolower($key); if ($key == 'content-type') { - $fetch_last_content_type = $value; + self::$fetch_last_content_type = $value; // don't abort here b/c there might be more than one // e.g. if we were being redirected -- last one is the right one } else if ($key == 'last-modified') { - $fetch_last_modified = $value; + self::$fetch_last_modified = $value; } else if ($key == 'location') { - $fetch_effective_url = $value; + self::$fetch_effective_url = $value; } } if (substr(strtolower($header), 0, 7) == 'http/1.') { - $fetch_last_error_code = (int) substr($header, 9, 3); - $fetch_last_error = $header; + self::$fetch_last_error_code = (int) substr($header, 9, 3); + self::$fetch_last_error = $header; } } - if ($fetch_last_error_code != 200) { + if (self::$fetch_last_error_code != 200) { $error = error_get_last(); if ($error['message'] != $old_error['message']) { - $fetch_last_error .= "; " . $error["message"]; + self::$fetch_last_error .= "; " . $error["message"]; } - $fetch_last_error_content = $data; + self::$fetch_last_error_content = $data; return false; } |