Browser.php 3.2 KB

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