| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\MultiDomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\Director; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Control\RequestFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Control\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\ORM\DataModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * A request filter for handling multi-domain logic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package  silverstripe-multi-domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author  Aaron Carlino <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class MultiDomainRequestFilter implements RequestFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Gets the active domain, and sets its URL to the native one, with a vanity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * URL in the request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param  HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param  Session     $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param  DataModel   $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function preRequest(HTTPRequest $request, Session $session, DataModel $model) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         if (Director::is_cli()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // Not the best place for validation, but _config.php is too early. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (!MultiDomain::get_primary_domain()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             throw new Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 'MultiDomain must define a "' . MultiDomain::KEY_PRIMARY . '" domain in the config, under "domains"' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         foreach (MultiDomain::get_all_domains() as $domain) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if (!$domain->isActive()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $url = $this->createNativeURLForDomain($domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $parts = explode('?', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $request->setURL($parts[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Post request noop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param  HTTPRequest  $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param  HTTPResponse $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param  DataModel    $model | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     public function postRequest(HTTPRequest $request, HTTPResponse $response, DataModel $model) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Creates a native URL for a domain. This functionality is abstracted so | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * that other modules can overload it, e.g. translatable modules that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * have their own custom URLs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param  MultiDomainDomain $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     protected function createNativeURLForDomain(MultiDomainDomain $domain) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return Controller::join_links( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             Director::baseURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $domain->getNativeURL($domain->getRequestUri()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |