UserAgentLib.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. App::uses('CakeRequest', 'Network');
  3. /**
  4. * A wrapper to access not only cakes request data about known mobile agents.
  5. * It also allows to whitelist and blacklist certain agents.
  6. * Last but not least it should be capable of detecting if it is a real user or a bot
  7. *
  8. * @author Mark Scherer
  9. * @license MIT
  10. * @cakephp 2
  11. *
  12. */
  13. class UserAgentLib extends CakeRequest {
  14. public $whitelist = array(
  15. 'OMNIA7',
  16. );
  17. public $blacklist = array(
  18. 'UP\.Browser'
  19. );
  20. public $searchBots = array(
  21. 'Mirago' => 'HenryTheMiragorobot',
  22. 'Google' => 'Googlebot',
  23. 'Scooter' => 'Scooter',
  24. 'MSN' => 'msnbot',
  25. 'Yahoo' => 'YahooSeeker',
  26. 'GigaBot' => 'GigaBot',
  27. 'Linguee' => 'Linguee Bot',
  28. 'WebAlta' => 'WebAlta Crawler',
  29. 'Yandex' => 'Yandex',
  30. 'Bot (no details)' => 'PF:INET',
  31. 'Sitedomain' => 'Sitedomain-Bot',
  32. 'Askpeter' => 'askpeter_bot'
  33. );
  34. public $path = null;
  35. public function __construct($agents = array()) {
  36. $this->path = VENDORS . 'files' . DS;
  37. }
  38. public function isBot() {
  39. $file = $this->path . 'bots.txt';
  40. if (file_exists($file)) {
  41. }
  42. }
  43. /**
  44. * Better handling of mobile agents
  45. * including whitelist and blacklist
  46. */
  47. public function isMobile() {
  48. $devices = $this->getMobileDevices();
  49. $pattern = '/' . implode('|', $devices) . '/i';
  50. return (bool)preg_match($pattern, env('HTTP_USER_AGENT'));
  51. }
  52. /**
  53. * Checks bot against list
  54. * @param string $userAgent
  55. * @return string
  56. * //TODO use browscap here too if necessary
  57. */
  58. public function getAgent($agent) {
  59. if (empty($agent)) {
  60. return '';
  61. }
  62. foreach ($this->searchBots as $name => $pattern) {
  63. if (eregi($pattern, $agent)) {
  64. return $name;
  65. }
  66. }
  67. return '';
  68. }
  69. /**
  70. * Checks user against known platforms
  71. * @param string $userAgent
  72. * @return string
  73. */
  74. public function getPlatform($agent) {
  75. if (strpos($agent, "Win95") || strpos($agent, "Windows 95")) {
  76. return "Windows 95";
  77. }
  78. if (strpos($agent, "Win98") || strpos($agent, "Windows 98")) {
  79. return "Windows 98";
  80. }
  81. if (strpos($agent, "WinNT") || strpos($agent, "Windows NT")) {
  82. return "Windows NT";
  83. }
  84. if (strpos($agent, "WinNT 5.0") || strpos($agent, "Windows NT 5.0")) {
  85. return "Windows 2000";
  86. }
  87. if (strpos($agent, "WinNT 5.1") || strpos($agent, "Windows NT 5.1")) {
  88. return "Windows XP";
  89. }
  90. if (strpos($agent, "Windows")) { # OWN ONE
  91. return "Windows";
  92. }
  93. if (strpos($agent, "Linux")) {
  94. return "Linux";
  95. }
  96. if (strpos($agent, "OS/2")) {
  97. return "OS/2";
  98. }
  99. if (strpos($agent, "Sun")) {
  100. return "Sun OS";
  101. }
  102. if (strpos($agent, "Macintosh") || strpos($agent, "Mac_PowerPC")) {
  103. return "Mac OS";
  104. }
  105. return "";
  106. }
  107. /**
  108. * Fetches url with curl if available
  109. * fallbacks: cake and php
  110. */
  111. public function getMobileDevices() {
  112. $is = array(); //$this->RequestHandler->mobileUA;
  113. $is = $this->_detectors['mobile']['options'];
  114. $is = array_merge($is, $this->_getMobileWhitelist());
  115. $blacklist = $this->_getMobileBlacklist();
  116. foreach ($blacklist as $agent) {
  117. if (in_array($agent, $is)) {
  118. $keys = array_keys($is, $agent);
  119. $key = array_shift($keys);
  120. unset($is[$key]);
  121. }
  122. }
  123. return $is;
  124. }
  125. protected function _getMobileWhitelist() {
  126. $res = $this->whitelist;
  127. /*
  128. $file = $this->path.'mobile_devices.txt';
  129. if (file_exists($file)) {
  130. }
  131. */
  132. return $res;
  133. }
  134. protected function _getMobileBlacklist() {
  135. $res = $this->blacklist;
  136. return $res;
  137. }
  138. }