Add UserUtil::getBrowser() method
authorjoshuaruesweg <ruesweg@woltlab.com>
Tue, 27 Oct 2020 18:01:48 +0000 (19:01 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Mon, 2 Nov 2020 14:43:05 +0000 (15:43 +0100)
wcfsetup/install/files/lib/data/user/online/UserOnline.class.php
wcfsetup/install/files/lib/util/UserUtil.class.php

index bc36c76f0e5db17a201bfa3f79814d4c245015ea..e69426859df0f95aef81414eab32099edb9089b3 100644 (file)
@@ -134,172 +134,7 @@ class UserOnline extends UserProfile {
                        return $parameters['browser'];
                }
                
-               // lunascape
-               if (preg_match('~lunascape[ /]([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Lunascape '.$match[1];
-               }
-               
-               // sleipnir
-               if (preg_match('~sleipnir/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Sleipnir '.$match[1];
-               }
-               
-               // uc browser
-               if (preg_match('~(?:ucbrowser|uc browser|ucweb)[ /]?([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'UC Browser '.$match[1];
-               }
-               
-               // baidu browser
-               if (preg_match('~(?:baidubrowser|flyflow)[ /]?([\d\.x]+)~i', $this->userAgent, $match)) {
-                       return 'Baidubrowser '.$match[1];
-               }
-               
-               // blackberry
-               if (preg_match('~blackberry.*version/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Blackberry '.$match[1];
-               }
-               
-               // opera mobile
-               if (preg_match('~opera/([\d\.]+).*(mobi|mini)~i', $this->userAgent, $match)) {
-                       return 'Opera Mobile '.$match[1];
-               }
-               
-               // opera
-               if (preg_match('~opera.*version/([\d\.]+)|opr/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Opera '.(isset($match[2]) ? $match[2] : $match[1]);
-               }
-               
-               // thunderbird
-               if (preg_match('~thunderbird/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Thunderbird '.$match[1];
-               }
-               
-               // icedragon
-               if (preg_match('~icedragon/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'IceDragon '.$match[1];
-               }
-               
-               // palemoon
-               if (preg_match('~palemoon/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'PaleMoon '.$match[1];
-               }
-               
-               // flock
-               if (preg_match('~flock/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Flock '.$match[1];
-               }
-               
-               // iceweasel
-               if (preg_match('~iceweasel/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Iceweasel '.$match[1];
-               }
-               
-               // firefox mobile
-               if (preg_match('~(?:mobile.*firefox|fxios)/([\d\.]+)|fennec/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Firefox Mobile '.(isset($match[2]) ? $match[2] : $match[1]);
-               }
-               
-               // tapatalk 4
-               if (preg_match('~tapatalk/([\d\.]+)?~i', $this->userAgent, $match)) {
-                       return 'Tapatalk '.(isset($match[1]) ? $match[1] : 4);
-               }
-               
-               // firefox
-               if (preg_match('~firefox/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Firefox '.$match[1];
-               }
-               
-               // maxthon
-               if (preg_match('~maxthon[ /]([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Maxthon '.$match[1];
-               }
-               
-               // iemobile
-               if (preg_match('~iemobile[ /]([\d\.]+)|MSIE ([\d\.]+).*XBLWP7~i', $this->userAgent, $match)) {
-                       return 'Internet Explorer Mobile '.(isset($match[2]) ? $match[2] : $match[1]);
-               }
-               
-               // ie
-               if (preg_match('~msie ([\d\.]+)|Trident\/\d{1,2}.\d{1,2}; .*rv:([0-9]*)~i', $this->userAgent, $match)) {
-                       return 'Internet Explorer '.(isset($match[2]) ? $match[2] : $match[1]);
-               }
-               
-               // edge
-               if (preg_match('~edge?/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Microsoft Edge '.$match[1];
-               }
-               
-               // edge mobile
-               if (preg_match('~edga/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Microsoft Edge Mobile '.$match[1];
-               }
-               
-               // vivaldi
-               if (preg_match('~vivaldi/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Vivaldi '.$match[1];
-               }
-               
-               // iron
-               if (preg_match('~iron/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Iron '.$match[1];
-               }
-               
-               // coowon
-               if (preg_match('~coowon/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Coowon '.$match[1];
-               }
-               
-               // coolnovo
-               if (preg_match('~(?:coolnovo|chromeplus)/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'CoolNovo '.$match[1];
-               }
-               
-               // yandex
-               if (preg_match('~yabrowser/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Yandex '.$match[1];
-               }
-               
-               // midori
-               if (preg_match('~midori/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Midori '.$match[1];
-               }
-               
-               // chrome mobile
-               if (preg_match('~(?:crios|crmo)/([\d\.]+)|chrome/([\d\.]+).*mobile~i', $this->userAgent, $match)) {
-                       return 'Chrome Mobile '.(isset($match[2]) ? $match[2] : $match[1]);
-               }
-               
-               // kindle
-               if (preg_match('~kindle/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Kindle '.$match[1];
-               }
-               
-               // silk
-               if (preg_match('~silk/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Silk '.$match[1];
-               }
-               
-               // android browser
-               if (preg_match('~Android ([\d\.]+).*AppleWebKit~i', $this->userAgent, $match)) {
-                       return 'Android Browser '.$match[1];
-               }
-               
-               // safari mobile
-               if (preg_match('~([\d\.]+) Mobile/\w+ safari~i', $this->userAgent, $match)) {
-                       return 'Safari Mobile '.$match[1];
-               }
-               
-               // chrome
-               if (preg_match('~(?:chromium|chrome)/([\d\.]+)~i', $this->userAgent, $match)) {
-                       return 'Chrome '.$match[1];
-               }
-               
-               // safari
-               if (preg_match('~([\d\.]+) safari~i', $this->userAgent, $match)) {
-                       return 'Safari '.$match[1];
-               }
-               
-               return $this->userAgent;
+               return UserUtil::getBrowser($this->userAgent);
        }
        
        /**
index 0c3fe772277b30990ac00838fe6a7edea1cb6171..f2d96ca1b056be1e24bcba6f2174264aa8214485 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\util;
+use wcf\system\event\EventHandler;
 use wcf\system\WCF;
 
 /**
@@ -139,6 +140,186 @@ final class UserUtil {
                return false;
        }
        
+       /**
+        * Returns the readable browser for a given user agent.
+        * 
+        * @since       5.4
+        */
+       public static function getBrowser(string $userAgent): string {
+               $parameters = ['browser' => '', 'userAgent' => $userAgent];
+               EventHandler::getInstance()->fireAction(self::class, 'getBrowser', $parameters);
+               if (!empty($parameters['browser'])) {
+                       return $parameters['browser'];
+               }
+               
+               // lunascape
+               if (preg_match('~lunascape[ /]([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Lunascape '.$match[1];
+               }
+               
+               // sleipnir
+               if (preg_match('~sleipnir/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Sleipnir '.$match[1];
+               }
+               
+               // uc browser
+               if (preg_match('~(?:ucbrowser|uc browser|ucweb)[ /]?([\d\.]+)~i', $userAgent, $match)) {
+                       return 'UC Browser '.$match[1];
+               }
+               
+               // baidu browser
+               if (preg_match('~(?:baidubrowser|flyflow)[ /]?([\d\.x]+)~i', $userAgent, $match)) {
+                       return 'Baidubrowser '.$match[1];
+               }
+               
+               // blackberry
+               if (preg_match('~blackberry.*version/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Blackberry '.$match[1];
+               }
+               
+               // opera mobile
+               if (preg_match('~opera/([\d\.]+).*(mobi|mini)~i', $userAgent, $match)) {
+                       return 'Opera Mobile '.$match[1];
+               }
+               
+               // opera
+               if (preg_match('~opera.*version/([\d\.]+)|opr/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Opera '.(isset($match[2]) ? $match[2] : $match[1]);
+               }
+               
+               // thunderbird
+               if (preg_match('~thunderbird/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Thunderbird '.$match[1];
+               }
+               
+               // icedragon
+               if (preg_match('~icedragon/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'IceDragon '.$match[1];
+               }
+               
+               // palemoon
+               if (preg_match('~palemoon/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'PaleMoon '.$match[1];
+               }
+               
+               // flock
+               if (preg_match('~flock/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Flock '.$match[1];
+               }
+               
+               // iceweasel
+               if (preg_match('~iceweasel/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Iceweasel '.$match[1];
+               }
+               
+               // firefox mobile
+               if (preg_match('~(?:mobile.*firefox|fxios)/([\d\.]+)|fennec/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Firefox Mobile '.(isset($match[2]) ? $match[2] : $match[1]);
+               }
+               
+               // tapatalk 4
+               if (preg_match('~tapatalk/([\d\.]+)?~i', $userAgent, $match)) {
+                       return 'Tapatalk '.(isset($match[1]) ? $match[1] : 4);
+               }
+               
+               // firefox
+               if (preg_match('~firefox/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Firefox '.$match[1];
+               }
+               
+               // maxthon
+               if (preg_match('~maxthon[ /]([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Maxthon '.$match[1];
+               }
+               
+               // iemobile
+               if (preg_match('~iemobile[ /]([\d\.]+)|MSIE ([\d\.]+).*XBLWP7~i', $userAgent, $match)) {
+                       return 'Internet Explorer Mobile '.(isset($match[2]) ? $match[2] : $match[1]);
+               }
+               
+               // ie
+               if (preg_match('~msie ([\d\.]+)|Trident\/\d{1,2}.\d{1,2}; .*rv:([0-9]*)~i', $userAgent, $match)) {
+                       return 'Internet Explorer '.(isset($match[2]) ? $match[2] : $match[1]);
+               }
+               
+               // edge
+               if (preg_match('~edge?/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Microsoft Edge '.$match[1];
+               }
+               
+               // edge mobile
+               if (preg_match('~edga/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Microsoft Edge Mobile '.$match[1];
+               }
+               
+               // vivaldi
+               if (preg_match('~vivaldi/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Vivaldi '.$match[1];
+               }
+               
+               // iron
+               if (preg_match('~iron/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Iron '.$match[1];
+               }
+               
+               // coowon
+               if (preg_match('~coowon/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Coowon '.$match[1];
+               }
+               
+               // coolnovo
+               if (preg_match('~(?:coolnovo|chromeplus)/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'CoolNovo '.$match[1];
+               }
+               
+               // yandex
+               if (preg_match('~yabrowser/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Yandex '.$match[1];
+               }
+               
+               // midori
+               if (preg_match('~midori/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Midori '.$match[1];
+               }
+               
+               // chrome mobile
+               if (preg_match('~(?:crios|crmo)/([\d\.]+)|chrome/([\d\.]+).*mobile~i', $userAgent, $match)) {
+                       return 'Chrome Mobile '.(isset($match[2]) ? $match[2] : $match[1]);
+               }
+               
+               // kindle
+               if (preg_match('~kindle/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Kindle '.$match[1];
+               }
+               
+               // silk
+               if (preg_match('~silk/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Silk '.$match[1];
+               }
+               
+               // android browser
+               if (preg_match('~Android ([\d\.]+).*AppleWebKit~i', $userAgent, $match)) {
+                       return 'Android Browser '.$match[1];
+               }
+               
+               // safari mobile
+               if (preg_match('~([\d\.]+) Mobile/\w+ safari~i', $userAgent, $match)) {
+                       return 'Safari Mobile '.$match[1];
+               }
+               
+               // chrome
+               if (preg_match('~(?:chromium|chrome)/([\d\.]+)~i', $userAgent, $match)) {
+                       return 'Chrome '.$match[1];
+               }
+               
+               // safari
+               if (preg_match('~([\d\.]+) safari~i', $userAgent, $match)) {
+                       return 'Safari '.$match[1];
+               }
+               
+               return $userAgent;
+       }
+       
        /**
         * Returns the ipv6 address of the client.
         *