UserAgentLib.php 3.4 KB

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