From a9938a09e8214b778b8ab11f60501661bb2ac623 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 17 Jan 2014 14:55:56 +0200 Subject: Minor changes related to CI_User_agent Fixed a bug where both accept_charset() and accept_lang() improperly parsed headers if they contained spaces between data separators (which is valid). Also made is_referral() testable by replacing its static cache var with a class property and added some more unit tests for the library as a whole. --- system/libraries/User_agent.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/User_agent.php b/system/libraries/User_agent.php index 3a6b6bc98..1dfa3e72d 100644 --- a/system/libraries/User_agent.php +++ b/system/libraries/User_agent.php @@ -144,6 +144,15 @@ class CI_User_agent { */ public $robot = ''; + /** + * HTTP Referer + * + * @var mixed + */ + public $referer; + + // -------------------------------------------------------------------- + /** * Constructor * @@ -358,7 +367,7 @@ class CI_User_agent { { if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $this->languages = explode(',', preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])))); + $this->languages = explode(',', preg_replace('/(;\s?q=[0-9\.]+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])))); } if (count($this->languages) === 0) @@ -378,7 +387,7 @@ class CI_User_agent { { if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET'])) { - $this->charsets = explode(',', preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])))); + $this->charsets = explode(',', preg_replace('/(;\s?q=.+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])))); } if (count($this->charsets) === 0) @@ -471,24 +480,22 @@ class CI_User_agent { */ public function is_referral() { - static $result; - - if ( ! isset($result)) + if ( ! isset($this->referer)) { if (empty($_SERVER['HTTP_REFERER'])) { - $result = FALSE; + $this->referer = FALSE; } else { $referer_host = @parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); $own_host = parse_url(config_item('base_url'), PHP_URL_HOST); - $result = ($referer_host && $referer_host !== $own_host); + $this->referer = ($referer_host && $referer_host !== $own_host); } } - return $result; + return $this->referer; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b