move all $fetch globals to UrlHelper

master
Andrew Dolgov 2 years ago
parent e2cbb54b2c
commit afc7142250
  1. 2
      backend.php
  2. 12
      classes/feeds.php
  3. 29
      classes/rssutils.php
  4. 93
      classes/urlhelper.php
  5. 12
      include/functions.php
  6. 7
      plugins/af_comics/filters/af_comics_cad.php
  7. 6
      plugins/af_comics/filters/af_comics_darklegacy.php
  8. 6
      plugins/af_comics/filters/af_comics_dilbert.php
  9. 6
      plugins/af_comics/filters/af_comics_whomp.php
  10. 8
      plugins/af_proxy_http/init.php
  11. 6
      plugins/af_readability/init.php
  12. 3
      plugins/af_redditimgur/init.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"]);
?>

@ -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) {

@ -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);

@ -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;
}

@ -4,12 +4,12 @@
define('LABEL_BASE_INDEX', -1024);
define('PLUGIN_FEED_BASE_INDEX', -128);
$fetch_last_error = false;
$fetch_last_error_code = false;
$fetch_last_content_type = false;
$fetch_last_error_content = false; // curl only for the time being
$fetch_effective_url = false;
$fetch_curl_used = false;
UrlHelper::$fetch_last_error = false;
UrlHelper::$fetch_last_error_code = false;
UrlHelper::$fetch_last_content_type = false;
UrlHelper::$fetch_last_error_content = false; // curl only for the time being
UrlHelper::$fetch_effective_url = false;
UrlHelper::$fetch_curl_used = false;
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
libxml_disable_entity_loader(true);

@ -8,17 +8,14 @@ class Af_Comics_Cad extends Af_ComicFilter {
function process(&$article) {
if (strpos($article["link"], "cad-comic.com") !== false) {
if (strpos($article["title"], "News:") === false) {
global $fetch_last_error_content;
$doc = new DOMDocument();
$res = UrlHelper::fetch($article["link"], false, false, false,
false, false, 0,
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");
if (!$res && $fetch_last_error_content)
$res = $fetch_last_error_content;
if (!$res && UrlHelper::$fetch_last_error_content)
$res = UrlHelper::$fetch_last_error_content;
if (@$doc->loadHTML($res)) {
$xpath = new DOMXPath($doc);

@ -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();

@ -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();

@ -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();

@ -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";
}
}

@ -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")));
}
}

@ -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) {

Loading…
Cancel
Save