From 2c931df77ccba5e76bc1865584e870219596ff69 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 23 Aug 2021 10:56:31 +0300 Subject: [PATCH] remove SELF_USER_AGENT custom constant, replaced with configurable Config::HTTP_USER_AGENT / Config::get_user_agent() --- classes/config.php | 9 +++++++++ classes/urlhelper.php | 3 +-- include/functions.php | 3 +-- plugins/af_redditimgur/init.php | 9 +++++---- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/classes/config.php b/classes/config.php index 4ae4a2407..be4ecde36 100644 --- a/classes/config.php +++ b/classes/config.php @@ -170,6 +170,9 @@ class Config { const AUTH_MIN_INTERVAL = "AUTH_MIN_INTERVAL"; // minimum amount of seconds required between authentication attempts + const HTTP_USER_AGENT = "HTTP_USER_AGENT"; + // http user agent (changing this is not recommended) + // default values for all of the above: private const _DEFAULTS = [ Config::DB_TYPE => [ "pgsql", Config::T_STRING ], @@ -224,6 +227,8 @@ class Config { Config::CHECK_FOR_PLUGIN_UPDATES => [ "true", Config::T_BOOL ], Config::ENABLE_PLUGIN_INSTALLER => [ "true", Config::T_BOOL ], Config::AUTH_MIN_INTERVAL => [ 5, Config::T_INT ], + Config::HTTP_USER_AGENT => [ 'Tiny Tiny RSS/%s (https://tt-rss.org/)', + Config::T_STRING ], ]; private static $instance; @@ -632,4 +637,8 @@ class Config { return $rv; } + + static function get_user_agent() { + return sprintf(self::get(self::HTTP_USER_AGENT), self::get_version()); + } } diff --git a/classes/urlhelper.php b/classes/urlhelper.php index 710b32b00..0e4834b72 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -281,8 +281,7 @@ class UrlHelper { curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent : - SELF_USER_AGENT); + curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent : Config::get_user_agent()); curl_setopt($ch, CURLOPT_ENCODING, ""); if ($http_referrer) diff --git a/include/functions.php b/include/functions.php index 922d3765c..36519fd44 100644 --- a/include/functions.php +++ b/include/functions.php @@ -157,8 +157,7 @@ require_once 'controls.php'; require_once 'controls_compat.php'; - define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . Config::get_version() . ' (http://tt-rss.org/)'); - ini_set('user_agent', SELF_USER_AGENT); + ini_set('user_agent', Config::get_user_agent()); /* compat shims */ diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php index 3b4094b1b..732067557 100755 --- a/plugins/af_redditimgur/init.php +++ b/plugins/af_redditimgur/init.php @@ -785,7 +785,8 @@ class Af_RedditImgur extends Plugin { } - private function get_header($url, $header, $useragent = SELF_USER_AGENT) { + /** $useragent defaults to Config::get_user_agent() */ + private function get_header($url, $header, $useragent = false) { $ret = false; if (function_exists("curl_init")) { @@ -795,7 +796,7 @@ class Af_RedditImgur extends Plugin { curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir")); - curl_setopt($ch, CURLOPT_USERAGENT, $useragent); + curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent : Config::get_user_agent()); @curl_exec($ch); $ret = curl_getinfo($ch, $header); @@ -804,11 +805,11 @@ class Af_RedditImgur extends Plugin { return $ret; } - private function get_content_type($url, $useragent = SELF_USER_AGENT) { + private function get_content_type($url, $useragent = false) { return $this->get_header($url, CURLINFO_CONTENT_TYPE, $useragent); } - private function get_location($url, $useragent = SELF_USER_AGENT) { + private function get_location($url, $useragent = false) { return $this->get_header($url, CURLINFO_EFFECTIVE_URL, $useragent); }