| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\Control; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Core\Config\Configurable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * A set of static methods for manipulating cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Cookie | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use Configurable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private static $report_errors = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Must be "Strict", "Lax", or "None" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private static string $default_samesite = 'Lax'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Fetch the current instance of the cookie backend. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return Cookie_Backend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public static function get_inst() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return Injector::inst()->get('SilverStripe\\Control\\Cookie_Backend'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Set a cookie variable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Expiry time is set in days, and defaults to 90. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param float $expiry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param bool $secure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param bool $httpOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * See http://php.net/set_session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public static function set( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $expiry = 90, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $path = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $domain = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $secure = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $httpOnly = true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return self::get_inst()->set($name, $value, $expiry, $path, $domain, $secure, $httpOnly); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Get the cookie value by name. Returns null if not set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param bool $includeUnsent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public static function get($name, $includeUnsent = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return self::get_inst()->get($name, $includeUnsent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Get all the cookies. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param bool $includeUnsent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     public static function get_all($includeUnsent = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         return self::get_inst()->getAll($includeUnsent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param null|string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param null|string $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param bool $secure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param bool $httpOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public static function force_expiry($name, $path = null, $domain = null, $secure = false, $httpOnly = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return self::get_inst()->forceExpiry($name, $path, $domain, $secure, $httpOnly); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Validate if the samesite value for a cookie is valid for the current request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Logs a warning if the samesite value is "None" for an insecure request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @throws LogicException if the value is not "Strict", "Lax", or "None". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public static function validateSameSite(string $sameSite): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $validValues = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'Strict', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'Lax', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'None', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if (!in_array($sameSite, $validValues)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             throw new LogicException('samesite must be "Strict", "Lax", or "None"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if ($sameSite === 'None' && !Director::is_https(self::getRequest())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             Injector::inst()->get(LoggerInterface::class)->warning('Cookie samesite cannot be "None" for insecure requests.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Get the current request, if any. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     private static function getRequest(): ?HTTPRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $request = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if (Controller::has_curr()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $request = Controller::curr()->getRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return ($request instanceof NullHTTPRequest) ? null : $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  |