UserAgentLib.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. *
  55. * @param string $userAgent
  56. * @return string
  57. * //TODO use browscap here too if necessary
  58. */
  59. public function getAgent($agent) {
  60. if (empty($agent)) {
  61. return '';
  62. }
  63. foreach ($this->searchBots as $name => $pattern) {
  64. if (eregi($pattern, $agent)) {
  65. return $name;
  66. }
  67. }
  68. return '';
  69. }
  70. /**
  71. * Checks user against known platforms
  72. *
  73. * @param string $userAgent
  74. * @return string
  75. */
  76. public function getPlatform($agent) {
  77. if (strpos($agent, "Win95") || strpos($agent, "Windows 95")) {
  78. return "Windows 95";
  79. }
  80. if (strpos($agent, "Win98") || strpos($agent, "Windows 98")) {
  81. return "Windows 98";
  82. }
  83. if (strpos($agent, "WinNT") || strpos($agent, "Windows NT")) {
  84. return "Windows NT";
  85. }
  86. if (strpos($agent, "WinNT 5.0") || strpos($agent, "Windows NT 5.0")) {
  87. return "Windows 2000";
  88. }
  89. if (strpos($agent, "WinNT 5.1") || strpos($agent, "Windows NT 5.1")) {
  90. return "Windows XP";
  91. }
  92. if (strpos($agent, "Windows")) { # OWN ONE
  93. return "Windows";
  94. }
  95. if (strpos($agent, "Linux")) {
  96. return "Linux";
  97. }
  98. if (strpos($agent, "OS/2")) {
  99. return "OS/2";
  100. }
  101. if (strpos($agent, "Sun")) {
  102. return "Sun OS";
  103. }
  104. if (strpos($agent, "Macintosh") || strpos($agent, "Mac_PowerPC")) {
  105. return "Mac OS";
  106. }
  107. return "";
  108. }
  109. /**
  110. * Fetches url with curl if available
  111. * fallbacks: cake and php
  112. */
  113. public function getMobileDevices() {
  114. $is = array(); //$this->RequestHandler->mobileUA;
  115. $is = $this->_detectors['mobile']['options'];
  116. $is = array_merge($is, $this->_getMobileWhitelist());
  117. $blacklist = $this->_getMobileBlacklist();
  118. foreach ($blacklist as $agent) {
  119. if (in_array($agent, $is)) {
  120. $keys = array_keys($is, $agent);
  121. $key = array_shift($keys);
  122. unset($is[$key]);
  123. }
  124. }
  125. return $is;
  126. }
  127. protected function _getMobileWhitelist() {
  128. $res = $this->whitelist;
  129. /*
  130. $file = $this->path.'mobile_devices.txt';
  131. if (file_exists($file)) {
  132. }
  133. */
  134. return $res;
  135. }
  136. protected function _getMobileBlacklist() {
  137. $res = $this->blacklist;
  138. return $res;
  139. }
  140. }