| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\ControllerPolicy\Policies; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\ControllerPolicy\ControllerPolicy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This policy can be used to write or delete arbitrary headers. Set the header to empty string ("") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * to suppress that header. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Configuration: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * SilverStripe\Core\Injector\Injector: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  *   GeneralPolicy: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *     class: YourVendor\YourModule\CustomHeaderPolicy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *     properties: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *       headers: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *         Cache-Control: "public, max-age=600, no-transform" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *         Custom-Header: "Hello" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *         Vary: "" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * HomePageController: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *   dependencies: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *     Policies: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *       - '%$GeneralPolicy' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *   extensions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  *     - SilverStripe\ControllerPolicy\ControllerPolicyApplicator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class CustomHeaderPolicy implements ControllerPolicy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $headers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Set the full array of headers to apply to the response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function setHeaders(array $headers) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->headers = $headers; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Get the list of headers to apply to the response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function getHeaders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Add a specific header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string|int $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function addHeader($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->headers[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param object $originator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param HTTPResponse $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function applyToResponse($originator, HTTPRequest $request, HTTPResponse $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         foreach ($this->getHeaders() as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             if ($value !== "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $response->addHeader($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $response->removeHeader($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |