Browser.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use function donatj\UserAgent\parse_user_agent;
  10. /**
  11. * Internally uses the PhpUserAgent package https://github.com/donatj/PhpUserAgent
  12. */
  13. class Browser
  14. {
  15. protected $useragent = [];
  16. /**
  17. * Browser constructor.
  18. */
  19. public function __construct()
  20. {
  21. try {
  22. $this->useragent = parse_user_agent();
  23. } catch (\InvalidArgumentException $e) {
  24. $this->useragent = parse_user_agent("Mozilla/5.0 (compatible; Unknown;)");
  25. }
  26. }
  27. /**
  28. * Get the current browser identifier
  29. *
  30. * Currently detected browsers:
  31. *
  32. * Android Browser
  33. * BlackBerry Browser
  34. * Camino
  35. * Kindle / Silk
  36. * Firefox / Iceweasel
  37. * Safari
  38. * Internet Explorer
  39. * IEMobile
  40. * Chrome
  41. * Opera
  42. * Midori
  43. * Vivaldi
  44. * TizenBrowser
  45. * Lynx
  46. * Wget
  47. * Curl
  48. *
  49. * @return string the lowercase browser name
  50. */
  51. public function getBrowser()
  52. {
  53. return strtolower($this->useragent['browser']);
  54. }
  55. /**
  56. * Get the current platform identifier
  57. *
  58. * Currently detected platforms:
  59. *
  60. * Desktop
  61. * -> Windows
  62. * -> Linux
  63. * -> Macintosh
  64. * -> Chrome OS
  65. * Mobile
  66. * -> Android
  67. * -> iPhone
  68. * -> iPad / iPod Touch
  69. * -> Windows Phone OS
  70. * -> Kindle
  71. * -> Kindle Fire
  72. * -> BlackBerry
  73. * -> Playbook
  74. * -> Tizen
  75. * Console
  76. * -> Nintendo 3DS
  77. * -> New Nintendo 3DS
  78. * -> Nintendo Wii
  79. * -> Nintendo WiiU
  80. * -> PlayStation 3
  81. * -> PlayStation 4
  82. * -> PlayStation Vita
  83. * -> Xbox 360
  84. * -> Xbox One
  85. *
  86. * @return string the lowercase platform name
  87. */
  88. public function getPlatform()
  89. {
  90. return strtolower($this->useragent['platform']);
  91. }
  92. /**
  93. * Get the current full version identifier
  94. *
  95. * @return string the browser full version identifier
  96. */
  97. public function getLongVersion()
  98. {
  99. return $this->useragent['version'];
  100. }
  101. /**
  102. * Get the current major version identifier
  103. *
  104. * @return string the browser major version identifier
  105. */
  106. public function getVersion()
  107. {
  108. $version = explode('.', $this->getLongVersion());
  109. return (int)$version[0];
  110. }
  111. /**
  112. * Determine if the request comes from a human, or from a bot/crawler
  113. *
  114. * @return bool
  115. */
  116. public function isHuman()
  117. {
  118. $browser = $this->getBrowser();
  119. if (empty($browser)) {
  120. return false;
  121. }
  122. if (preg_match('~(bot|crawl)~i', $browser)) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. /**
  128. * Determine if “Do Not Track” is set by browser
  129. * @see https://www.w3.org/TR/tracking-dnt/
  130. *
  131. * @return bool
  132. */
  133. public function isTrackable(): bool
  134. {
  135. return !(isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] === '1');
  136. }
  137. }