Browser.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Grav\Common;
  3. /**
  4. * Simple wrapper for the very simple parse_user_agent() function
  5. */
  6. class Browser
  7. {
  8. protected $useragent = [];
  9. public function __construct()
  10. {
  11. try {
  12. $this->useragent = parse_user_agent();
  13. } catch (\InvalidArgumentException $e) {
  14. $this->useragent = parse_user_agent("Mozilla/5.0 (compatible; Unknown;)");
  15. }
  16. }
  17. public function getBrowser()
  18. {
  19. return strtolower($this->useragent['browser']);
  20. }
  21. public function getPlatform()
  22. {
  23. return strtolower($this->useragent['platform']);
  24. }
  25. public function getLongVersion()
  26. {
  27. return $this->useragent['version'];
  28. }
  29. public function getVersion()
  30. {
  31. $version = explode('.', $this->getLongVersion());
  32. return intval($version[0]);
  33. }
  34. /**
  35. * Determine if the request comes from a human, or from a bot/crawler
  36. */
  37. public function isHuman()
  38. {
  39. $browser = $this->getBrowser();
  40. if (empty($browser)) {
  41. return false;
  42. }
  43. if (preg_match('~(bot|crawl)~i', $browser)) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. }