/system/libraries/User_agent.php

https://gitlab.com/zanderwong/admin · PHP · 486 lines · 207 code · 77 blank · 202 comment · 42 complexity · 482f3012221b9113a3cabe45f403e081 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * User Agent Class
  18. *
  19. * Identifies the platform, browser, robot, or mobile devise of the browsing agent
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category User Agent
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/user_agent.html
  26. */
  27. class CI_User_agent {
  28. var $agent = NULL;
  29. var $is_browser = FALSE;
  30. var $is_robot = FALSE;
  31. var $is_mobile = FALSE;
  32. var $languages = array();
  33. var $charsets = array();
  34. var $platforms = array();
  35. var $browsers = array();
  36. var $mobiles = array();
  37. var $robots = array();
  38. var $platform = '';
  39. var $browser = '';
  40. var $version = '';
  41. var $mobile = '';
  42. var $robot = '';
  43. /**
  44. * Constructor
  45. *
  46. * Sets the User Agent and runs the compilation routine
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. public function __construct() {
  52. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  53. $this->agent = trim($_SERVER['HTTP_USER_AGENT']);
  54. }
  55. if (!is_null($this->agent)) {
  56. if ($this->_load_agent_file()) {
  57. $this->_compile_data();
  58. }
  59. }
  60. log_message('debug', "User Agent Class Initialized");
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * Compile the User Agent Data
  65. *
  66. * @access private
  67. * @return bool
  68. */
  69. private function _load_agent_file() {
  70. if (defined('ENVIRONMENT') AND is_file(APPPATH . 'config/' . ENVIRONMENT . '/user_agents.php')) {
  71. include(APPPATH . 'config/' . ENVIRONMENT . '/user_agents.php');
  72. } elseif (is_file(APPPATH . 'config/user_agents.php')) {
  73. include(APPPATH . 'config/user_agents.php');
  74. } else {
  75. return FALSE;
  76. }
  77. $return = FALSE;
  78. if (isset($platforms)) {
  79. $this->platforms = $platforms;
  80. unset($platforms);
  81. $return = TRUE;
  82. }
  83. if (isset($browsers)) {
  84. $this->browsers = $browsers;
  85. unset($browsers);
  86. $return = TRUE;
  87. }
  88. if (isset($mobiles)) {
  89. $this->mobiles = $mobiles;
  90. unset($mobiles);
  91. $return = TRUE;
  92. }
  93. if (isset($robots)) {
  94. $this->robots = $robots;
  95. unset($robots);
  96. $return = TRUE;
  97. }
  98. return $return;
  99. }
  100. // --------------------------------------------------------------------
  101. /**
  102. * Compile the User Agent Data
  103. *
  104. * @access private
  105. * @return bool
  106. */
  107. private function _compile_data() {
  108. $this->_set_platform();
  109. foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function) {
  110. if ($this->$function() === TRUE) {
  111. break;
  112. }
  113. }
  114. }
  115. // --------------------------------------------------------------------
  116. /**
  117. * Set the Platform
  118. *
  119. * @access private
  120. * @return mixed
  121. */
  122. private function _set_platform() {
  123. if (is_array($this->platforms) AND count($this->platforms) > 0) {
  124. foreach ($this->platforms as $key => $val) {
  125. if (preg_match("|" . preg_quote($key) . "|i", $this->agent)) {
  126. $this->platform = $val;
  127. return TRUE;
  128. }
  129. }
  130. }
  131. $this->platform = 'Unknown Platform';
  132. }
  133. // --------------------------------------------------------------------
  134. /**
  135. * Set the Browser
  136. *
  137. * @access private
  138. * @return bool
  139. */
  140. private function _set_browser() {
  141. if (is_array($this->browsers) AND count($this->browsers) > 0) {
  142. foreach ($this->browsers as $key => $val) {
  143. if (preg_match("|" . preg_quote($key) . ".*?([0-9\.]+)|i", $this->agent, $match)) {
  144. $this->is_browser = TRUE;
  145. $this->version = $match[1];
  146. $this->browser = $val;
  147. $this->_set_mobile();
  148. return TRUE;
  149. }
  150. }
  151. }
  152. return FALSE;
  153. }
  154. // --------------------------------------------------------------------
  155. /**
  156. * Set the Robot
  157. *
  158. * @access private
  159. * @return bool
  160. */
  161. private function _set_robot() {
  162. if (is_array($this->robots) AND count($this->robots) > 0) {
  163. foreach ($this->robots as $key => $val) {
  164. if (preg_match("|" . preg_quote($key) . "|i", $this->agent)) {
  165. $this->is_robot = TRUE;
  166. $this->robot = $val;
  167. return TRUE;
  168. }
  169. }
  170. }
  171. return FALSE;
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Set the Mobile Device
  176. *
  177. * @access private
  178. * @return bool
  179. */
  180. private function _set_mobile() {
  181. if (is_array($this->mobiles) AND count($this->mobiles) > 0) {
  182. foreach ($this->mobiles as $key => $val) {
  183. if (FALSE !== (strpos(strtolower($this->agent), $key))) {
  184. $this->is_mobile = TRUE;
  185. $this->mobile = $val;
  186. return TRUE;
  187. }
  188. }
  189. }
  190. return FALSE;
  191. }
  192. // --------------------------------------------------------------------
  193. /**
  194. * Set the accepted languages
  195. *
  196. * @access private
  197. * @return void
  198. */
  199. private function _set_languages() {
  200. if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '') {
  201. $languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
  202. $this->languages = explode(',', $languages);
  203. }
  204. if (count($this->languages) == 0) {
  205. $this->languages = array('Undefined');
  206. }
  207. }
  208. // --------------------------------------------------------------------
  209. /**
  210. * Set the accepted character sets
  211. *
  212. * @access private
  213. * @return void
  214. */
  215. private function _set_charsets() {
  216. if ((count($this->charsets) == 0) AND isset($_SERVER['HTTP_ACCEPT_CHARSET']) AND $_SERVER['HTTP_ACCEPT_CHARSET'] != '') {
  217. $charsets = preg_replace('/(;q=.+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])));
  218. $this->charsets = explode(',', $charsets);
  219. }
  220. if (count($this->charsets) == 0) {
  221. $this->charsets = array('Undefined');
  222. }
  223. }
  224. // --------------------------------------------------------------------
  225. /**
  226. * Is Browser
  227. *
  228. * @access public
  229. * @return bool
  230. */
  231. public function is_browser($key = NULL) {
  232. if (!$this->is_browser) {
  233. return FALSE;
  234. }
  235. // No need to be specific, it's a browser
  236. if ($key === NULL) {
  237. return TRUE;
  238. }
  239. // Check for a specific browser
  240. return array_key_exists($key, $this->browsers) AND $this->browser === $this->browsers[$key];
  241. }
  242. // --------------------------------------------------------------------
  243. /**
  244. * Is Robot
  245. *
  246. * @access public
  247. * @return bool
  248. */
  249. public function is_robot($key = NULL) {
  250. if (!$this->is_robot) {
  251. return FALSE;
  252. }
  253. // No need to be specific, it's a robot
  254. if ($key === NULL) {
  255. return TRUE;
  256. }
  257. // Check for a specific robot
  258. return array_key_exists($key, $this->robots) AND $this->robot === $this->robots[$key];
  259. }
  260. // --------------------------------------------------------------------
  261. /**
  262. * Is Mobile
  263. *
  264. * @access public
  265. * @return bool
  266. */
  267. public function is_mobile($key = NULL) {
  268. if (!$this->is_mobile) {
  269. return FALSE;
  270. }
  271. // No need to be specific, it's a mobile
  272. if ($key === NULL) {
  273. return TRUE;
  274. }
  275. // Check for a specific robot
  276. return array_key_exists($key, $this->mobiles) AND $this->mobile === $this->mobiles[$key];
  277. }
  278. // --------------------------------------------------------------------
  279. /**
  280. * Is this a referral from another site?
  281. *
  282. * @access public
  283. * @return bool
  284. */
  285. public function is_referral() {
  286. if (!isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') {
  287. return FALSE;
  288. }
  289. return TRUE;
  290. }
  291. // --------------------------------------------------------------------
  292. /**
  293. * Agent String
  294. *
  295. * @access public
  296. * @return string
  297. */
  298. public function agent_string() {
  299. return $this->agent;
  300. }
  301. // --------------------------------------------------------------------
  302. /**
  303. * Get Platform
  304. *
  305. * @access public
  306. * @return string
  307. */
  308. public function platform() {
  309. return $this->platform;
  310. }
  311. // --------------------------------------------------------------------
  312. /**
  313. * Get Browser Name
  314. *
  315. * @access public
  316. * @return string
  317. */
  318. public function browser() {
  319. return $this->browser;
  320. }
  321. // --------------------------------------------------------------------
  322. /**
  323. * Get the Browser Version
  324. *
  325. * @access public
  326. * @return string
  327. */
  328. public function version() {
  329. return $this->version;
  330. }
  331. // --------------------------------------------------------------------
  332. /**
  333. * Get The Robot Name
  334. *
  335. * @access public
  336. * @return string
  337. */
  338. public function robot() {
  339. return $this->robot;
  340. }
  341. // --------------------------------------------------------------------
  342. /**
  343. * Get the Mobile Device
  344. *
  345. * @access public
  346. * @return string
  347. */
  348. public function mobile() {
  349. return $this->mobile;
  350. }
  351. // --------------------------------------------------------------------
  352. /**
  353. * Get the referrer
  354. *
  355. * @access public
  356. * @return bool
  357. */
  358. public function referrer() {
  359. return (!isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
  360. }
  361. // --------------------------------------------------------------------
  362. /**
  363. * Get the accepted languages
  364. *
  365. * @access public
  366. * @return array
  367. */
  368. public function languages() {
  369. if (count($this->languages) == 0) {
  370. $this->_set_languages();
  371. }
  372. return $this->languages;
  373. }
  374. // --------------------------------------------------------------------
  375. /**
  376. * Get the accepted Character Sets
  377. *
  378. * @access public
  379. * @return array
  380. */
  381. public function charsets() {
  382. if (count($this->charsets) == 0) {
  383. $this->_set_charsets();
  384. }
  385. return $this->charsets;
  386. }
  387. // --------------------------------------------------------------------
  388. /**
  389. * Test for a particular language
  390. *
  391. * @access public
  392. * @return bool
  393. */
  394. public function accept_lang($lang = 'en') {
  395. return (in_array(strtolower($lang), $this->languages(), TRUE));
  396. }
  397. // --------------------------------------------------------------------
  398. /**
  399. * Test for a particular character set
  400. *
  401. * @access public
  402. * @return bool
  403. */
  404. public function accept_charset($charset = 'utf-8') {
  405. return (in_array(strtolower($charset), $this->charsets(), TRUE));
  406. }
  407. }
  408. /* End of file User_agent.php */
  409. /* Location: ./system/libraries/User_agent.php */