Browser.php 3.1 KB

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