PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/User_agent.php

https://github.com/lmorchard/friendfeedarchiver
PHP | 162 lines | 96 code | 17 blank | 49 comment | 11 complexity | 2570c174c93fea7891737962aac6b8ba MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * User agent library.
  4. *
  5. * $Id: User_agent.php 2030 2008-02-11 16:16:19Z Geert $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class User_agent_Core {
  13. public static $agent;
  14. protected static $referrer = '';
  15. protected static $languages = array();
  16. protected static $charsets = array();
  17. protected $platform = '';
  18. protected $browser = '';
  19. protected $version = '';
  20. protected $mobile = '';
  21. protected $robot = '';
  22. /**
  23. * Loads user agent data.
  24. */
  25. public function __construct()
  26. {
  27. // Make sure the user agent is set
  28. if (empty(self::$agent) AND (self::$agent = Kohana::$user_agent) === '')
  29. {
  30. Log::add('debug', 'Could not determine user agent type.');
  31. return;
  32. }
  33. // Set the user agent data
  34. foreach(Config::item('user_agents') as $type => $data)
  35. {
  36. if (isset($this->$type))
  37. {
  38. foreach($data as $agent => $name)
  39. {
  40. if (stripos(self::$agent, $agent) !== FALSE)
  41. {
  42. if ($type == 'browser' AND preg_match('|'.preg_quote($agent).'[^0-9.]*([0-9.]+)|i', self::$agent, $match))
  43. {
  44. $this->version = $match[1];
  45. unset($match);
  46. }
  47. $this->$type = $name;
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. // Set the accepted languages
  54. if (empty(self::$languages) AND ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  55. {
  56. self::$languages = (preg_match_all('/[-a-z]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])), $matches)) ? $matches[0] : array();
  57. }
  58. // Set the accepted charsets
  59. if (empty(self::$charsets) AND ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
  60. {
  61. self::$charsets = (preg_match_all('/[-a-z0-9]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])), $matches)) ? $matches[0] : array();
  62. }
  63. // Set the referrer
  64. if (empty(self::$referrer) AND ! empty($_SERVER['HTTP_REFERER']))
  65. {
  66. self::$referrer = trim($_SERVER['HTTP_REFERER']);
  67. }
  68. Log::add('debug', 'User Agent Library initialized');
  69. }
  70. /**
  71. * Fetch information about the user agent, examples:
  72. * is_browser, is_mobile, is_robot
  73. * agent, browser, mobile, version, referrer
  74. *
  75. * @param string key name
  76. * @return string
  77. */
  78. public function __get($key)
  79. {
  80. if (empty($key))
  81. {
  82. return;
  83. }
  84. elseif (strpos($key, 'is_') === 0)
  85. {
  86. $key = substr($key, 3);
  87. return isset($this->$key) ? (bool) $this->$key : FALSE;
  88. }
  89. elseif (isset($this->$key))
  90. {
  91. return $this->$key;
  92. }
  93. elseif (isset(self::$$key))
  94. {
  95. return self::$$key;
  96. }
  97. else
  98. {
  99. return FALSE;
  100. }
  101. }
  102. /**
  103. * So that users can use $user_agent->is_robot() or $user_agent->is_robot.
  104. *
  105. * @param string function name
  106. * @return string
  107. */
  108. public function __call($func, $args = FALSE)
  109. {
  110. return $this->__get($func);
  111. }
  112. /**
  113. * Returns the full user agent string when the object is turned into a string.
  114. *
  115. * @return string
  116. */
  117. public function __toString()
  118. {
  119. return self::$agent;
  120. }
  121. /**
  122. * Test for a particular language.
  123. *
  124. * @param string language to test for
  125. * @return boolean
  126. */
  127. public function accept_lang($lang = 'en')
  128. {
  129. if (empty($lang) OR ! is_string($lang))
  130. return FALSE;
  131. return in_array(strtolower($lang), self::$languages);
  132. }
  133. /**
  134. * Test for a particular character set.
  135. *
  136. * @param string character set to test for
  137. * @return boolean
  138. */
  139. public function accept_charset($charset = 'utf-8')
  140. {
  141. if (empty($charset) OR ! is_string($charset))
  142. return FALSE;
  143. return in_array(strtolower($charset), $this->charsets());
  144. }
  145. } // End User_Agent Class