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

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