PageRenderTime 133ms CodeModel.GetById 70ms app.highlight 12ms RepoModel.GetById 49ms app.codeStats 0ms

/modules/mod_roknavmenu/lib/common/RokBrowserCheck.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 196 lines | 168 code | 13 blank | 15 comment | 25 complexity | 3b709e2523118ef6342bddd7771a741e MD5 | raw file
  1<?php
  2/**
  3 * @version   1.13 July 2, 2012
  4 * @author    RocketTheme http://www.rockettheme.com
  5 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7 */
  8
  9if (!class_exists('RokBrowserCheck')) {
 10
 11    class RokBrowserCheck {
 12        var $_ua;
 13
 14        var $name;
 15        var $version;
 16        var $shortversion;
 17        var $platform;
 18        var $engine;
 19        var $_checks = array();
 20
 21        function __construct() {
 22            $this->_ua = $_SERVER['HTTP_USER_AGENT'];
 23            $this->_checkPlatform();
 24            $this->_checkBrowser();
 25            $this->_checkEngine();
 26
 27            // add short version
 28            if ($this->version != 'unknown') $this->shortversion = substr($this->version, 0, strpos($this->version, '.'));
 29            else $this->shortversion = 'unknown';
 30
 31            $this->_createChecks();
 32        }
 33
 34        function _checkPlatform() {
 35            if (preg_match("/iPhone/", $this->_ua) || preg_match("/iPod/", $this->_ua)) {
 36                $this->platform = "iphone";
 37            }
 38            elseif (preg_match("/iPad/", $this->_ua)) {
 39                $this->platform = "ipad";
 40            }
 41            elseif (preg_match("/Android/", $this->_ua)) {
 42                $this->platform = "android";
 43            }
 44            elseif (preg_match("/Mobile/i", $this->_ua)) {
 45                $this->platform = "mobile";
 46            }
 47            elseif (preg_match("/win/i", $this->_ua)) {
 48                $this->platform = "win";
 49            }
 50            elseif (preg_match("/mac/i", $this->_ua)) {
 51                $this->platform = "mac";
 52            }
 53            elseif (preg_match("/linux/i", $this->_ua)) {
 54                $this->platform = "linux";
 55            } else {
 56                $this->platform = "unknown";
 57            }
 58
 59            return $this->platform;
 60        }
 61
 62        function _checkEngine() {
 63            switch ($this->name) {
 64                case 'ie':
 65                    $this->engine = 'trident';
 66                    break;
 67                case 'firefox':
 68                    $this->engine = 'gecko';
 69                    break;
 70                case 'android':
 71                case 'ipad':
 72                case 'iphone':
 73                case 'chrome':
 74                case 'safari':
 75                    $this->engine = 'webkit';
 76                    break;
 77                case 'opera':
 78                    $this->engine = 'presto';
 79                    break;
 80                default:
 81                    $this->engine = 'unknown';
 82                    break;
 83            }
 84        }
 85
 86        function _checkBrowser() {
 87            // IE
 88            if (preg_match('/msie/i', $this->_ua) && !preg_match('/opera/i', $this->_ua)) {
 89                $result = explode(' ', stristr(str_replace(';', ' ', $this->_ua), 'msie'));
 90                $this->name = 'ie';
 91                $this->version = $result[1];
 92            }
 93                // Firefox
 94            elseif (preg_match('/Firefox/', $this->_ua)) {
 95                $result = explode('/', stristr($this->_ua, 'Firefox'));
 96                $version = explode(' ', $result[1]);
 97                $this->name = 'firefox';
 98                $this->version = $version[0];
 99            }
100                // Chrome
101            elseif (preg_match('/Chrome/', $this->_ua)) {
102                $result = explode('/', stristr($this->_ua, 'Chrome'));
103                $version = explode(' ', $result[1]);
104                $this->name = 'chrome';
105                $this->version = $version[0];
106            }
107                //Safari
108            elseif (preg_match('/Safari/', $this->_ua) && !preg_match('/iPhone/', $this->_ua) && !preg_match('/iPod/', $this->_ua) && !preg_match('/iPad/', $this->_ua)) {
109                $result = explode('/', stristr($this->_ua, 'Version'));
110                $this->name = 'safari';
111                if (isset ($result[1])) {
112                    $version = explode(' ', $result[1]);
113                    $this->version = $version[0];
114                } else {
115                    $this->version = 'unknown';
116                }
117            }
118                // Opera
119            elseif (preg_match('/opera/i', $this->_ua)) {
120                $result = stristr($this->_ua, 'opera');
121
122                if (preg_match('/\//', $result)) {
123                    $result = explode('/', $result);
124                    $version = explode(' ', $result[1]);
125                    $this->name = 'opera';
126                    $this->version = $version[0];
127                } else {
128                    $version = explode(' ', stristr($result, 'opera'));
129                    $this->name = 'opera';
130                    $this->version = $version[1];
131                }
132            }
133                // iPhone/iPod
134            elseif (preg_match('/iPhone/', $this->_ua) || preg_match('/iPod/', $this->_ua)) {
135                $result = explode('/', stristr($this->_ua, 'Version'));
136                $this->name = 'iphone';
137                if (isset ($result[1])) {
138                    $version = explode(' ', $result[1]);
139                    $this->version = $version[0];
140                } else {
141                    $this->version = 'unknown';
142                }
143            }
144                // iPad
145            elseif (preg_match('/iPad/', $this->_ua)) {
146                $result = explode('/', stristr($this->_ua, 'Version'));
147                $this->name = 'ipad';
148                if (isset ($result[1])) {
149                    $version = explode(' ', $result[1]);
150                    $this->version = $version[0];
151                } else {
152                    $this->version = 'unknown';
153                }
154            }
155                // Android
156            elseif (preg_match('/Android/', $this->_ua)) {
157                $result = explode('/', stristr($this->_ua, 'Version'));
158                $this->name = 'android';
159                if (isset ($result[1])) {
160                    $version = explode(' ', $result[1]);
161                    $this->version = $version[0];
162                } else {
163                    $this->version = "unknown";
164                }
165            } else {
166                $this->name = "unknown";
167                $this->version = "unknown";
168            }
169        }
170
171        function _createChecks() {
172            $this->_checks = array(
173                $this->name, // browser check
174                $this->platform, // platform check
175                $this->engine, // render engine
176                $this->name . '-' . $this->platform, // browser + platform check
177                $this->name . $this->shortversion, // short browser version check
178                $this->name . $this->version, // longbrowser version check
179                $this->name . $this->shortversion . '-' . $this->platform, // short browser version + platform check
180                $this->name . $this->version . '-' . $this->platform // longbrowser version + platform check
181            );
182        }
183
184        function getChecks($file, $keep_path = false) {
185            $checkfiles = array();
186            $ext = substr($file, strrpos($file, '.'));
187            $path = ($keep_path) ? dirname($file) . DS : '';
188            $filename = basename($file, $ext);
189            $checkfiles[] = $path . $filename . $ext;
190            foreach ($this->_checks as $suffix) {
191                $checkfiles[] = $path . $filename . '-' . $suffix . $ext;
192            }
193            return $checkfiles;
194        }
195    }
196}