vendor/skorp/detect-incompatible-samesite-useragents/src/SameSite.php line 25

Open in your IDE?
  1. <?php
  2. namespace Skorp\Dissua;
  3. class SameSite {
  4. protected $userAgent = null;
  5. public function __construct($userAgent=null) {
  6. if($userAgent != null)
  7. $this->userAgent = $userAgent;
  8. }
  9. public static function handle($userAgent = null) : bool {
  10. return (new self($userAgent))->shouldSendSameSiteNone();
  11. }
  12. public function shouldSendSameSiteNone() : bool {
  13. return !$this->isSameSiteNoneIncompatible();
  14. }
  15. protected function isSameSiteNoneIncompatible() : bool {
  16. return $this->hasWebKitSameSiteBug() || $this->dropsUnrecognizedSameSiteCookies();
  17. }
  18. protected function hasWebKitSameSiteBug() : bool {
  19. return $this->isIosVersion(12) ||
  20. ($this->isMacosxVersion(10, 14) &&
  21. ($this->isSafari() || $this->isMacEmbeddedBrowser()));
  22. }
  23. protected function dropsUnrecognizedSameSiteCookies() : bool {
  24. if ($this->isUcBrowser())
  25. return !$this->isUcBrowserVersionAtLeast(12, 13, 2);
  26. return $this->isChromiumBased() &&
  27. $this->isChromiumVersionAtLeast(51) &&
  28. !$this->isChromiumVersionAtLeast(67);
  29. }
  30. protected function isChromiumBased() : bool {
  31. $regex = '/Chrom(e|ium)/';
  32. return preg_match($regex,$this->userAgent);
  33. }
  34. protected function isChromiumVersionAtLeast($version) : bool {
  35. $regex = '/Chrom[^ \/]+\/(\d+)[\.\d]*/';
  36. preg_match($regex,$this->userAgent,$matches);
  37. return ($matches[1]??null) >= $version;
  38. }
  39. protected function isIosVersion($major) : bool {
  40. $regex = "/\(iP.+; CPU .*OS (\d+)[_\d]*.*\) AppleWebKit\//";
  41. preg_match($regex,$this->userAgent,$matches);
  42. return ($matches[1]??null) == $major;
  43. }
  44. protected function isMacosxVersion($major,$minor) : bool {
  45. $regex = "/\(Macintosh;.*Mac OS X (\d+)_(\d+)[_\d]*.*\) AppleWebKit\//";
  46. preg_match($regex,$this->userAgent,$matches);
  47. return (($matches[1]??null) == $major && (($matches[2]??null) == $minor));
  48. }
  49. protected function isSafari() : bool {
  50. $regex = "/Version\/.* Safari\//";
  51. return preg_match($regex,$this->userAgent) && ! $this->isChromiumBased();
  52. }
  53. protected function isMacEmbeddedBrowser() : bool {
  54. $regex = "#/^Mozilla\/[\.\d]+ \(Macintosh;.*Mac OS X [_\d]+\) AppleWebKit\/[\.\d]+ \(KHTML, like Gecko\)$#";
  55. return preg_match($regex,$this->userAgent);
  56. }
  57. protected function isUcBrowser() : bool {
  58. $regex = '/UCBrowser\//';
  59. return preg_match($regex,$this->userAgent);
  60. }
  61. protected function isUcBrowserVersionAtLeast($major,$minor,$build) : bool {
  62. $regex = "/UCBrowser\/(\d+)\.(\d+)\.(\d+)[\.\d]* /";
  63. preg_match($regex,$this->userAgent,$matches);
  64. $major_version = $matches[1] ?? null;
  65. $minor_version = $matches[2] ?? null;
  66. $build_version = $matches[3] ?? null;
  67. if ($major_version != $major)
  68. return $major_version > $major;
  69. if ($minor_version != $minor)
  70. return $minor_version > $minor;
  71. return $build_version >= $build;
  72. }
  73. public function setUserAgent($useragent) {
  74. $this->userAgent = $useragent;
  75. }
  76. }