| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\CMS\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\CMS\Model\SiteTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\Director; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\NestedController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\RequestHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Control\HTTPResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\Control\HTTPResponse_Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Core\ClassInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SilverStripe\Dev\Debug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SilverStripe\Dev\Deprecation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SilverStripe\ORM\DataModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SilverStripe\ORM\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Translatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * ModelAsController deals with mapping the initial request to the first {@link SiteTree}/{@link ContentController} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * pair, which are then used to handle the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class ModelAsController extends Controller implements NestedController { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	private static $extensions = array('SilverStripe\\CMS\\Controllers\\OldPageRedirector'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * return it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @param SiteTree $sitetree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @return ContentController | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	public static function controller_for(SiteTree $sitetree, $action = null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 		$controller = $sitetree->getControllerName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		if ($action && class_exists($controller . '_' . ucfirst($action))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 			$controller = $controller . '_' . ucfirst($action); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		return Injector::inst()->create($controller, $sitetree); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	public function init() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		singleton('SilverStripe\\CMS\\Model\\SiteTree')->extend('modelascontrollerInit', $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 55 |  | View Code Duplication | 	protected function beforeHandleRequest(HTTPRequest $request, DataModel $model) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		parent::beforeHandleRequest($request, $model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		// If the database has not yet been created, redirect to the build page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		/** @skipUpgrade */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			$this->getResponse()->redirect(Controller::join_links( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				Director::absoluteBaseURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 				'dev/build', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 				'?' . http_build_query(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 					'returnURL' => isset($_GET['url']) ? $_GET['url'] : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 				)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @uses ModelAsController::getNestedController() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * @param HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param DataModel $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @return HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	public function handleRequest(HTTPRequest $request, DataModel $model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$this->beforeHandleRequest($request, $model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		// If we had a redirection or something, halt processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		if($this->getResponse()->isFinished()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			$this->popCurrent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			return $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		// If the database has not yet been created, redirect to the build page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		/** @skipUpgrade */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 87 |  | View Code Duplication | 		if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			$this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$this->popCurrent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			return $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			$result = $this->getNestedController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			if($result instanceof RequestHandler) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				$result = $result->handleRequest($this->getRequest(), $model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			} else if(!($result instanceof HTTPResponse)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 				user_error("ModelAsController::getNestedController() returned bad object type '" . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 					get_class($result)."'", E_USER_WARNING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} catch(HTTPResponse_Exception $responseException) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			$result = $responseException->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$this->popCurrent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @return ContentController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * @throws Exception If URLSegment not passed in as a request parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	public function getNestedController() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$request = $this->getRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		if(!$URLSegment = $request->param('URLSegment')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		// Find page by link, regardless of current locale settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		if(class_exists('Translatable')) Translatable::disable_locale_filter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		// Select child page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		if(SiteTree::config()->nested_urls) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$conditions[] = array('"SiteTree"."ParentID"' => 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		/** @var SiteTree $sitetree */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$sitetree = DataObject::get_one('SilverStripe\\CMS\\Model\\SiteTree', $conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		// Check translation module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		// @todo Refactor out module specific code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		if(class_exists('Translatable')) Translatable::enable_locale_filter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		if(!$sitetree) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			$this->httpError(404, 'The requested page could not be found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		// Enforce current locale setting to the loaded SiteTree object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		if(class_exists('Translatable') && $sitetree->Locale) Translatable::set_current_locale($sitetree->Locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		if(isset($_REQUEST['debug'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		return self::controller_for($sitetree, $this->getRequest()->param('Action')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @deprecated 4.0 Use OldPageRedirector::find_old_page instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @param int $parent The ID of the parent of the page the URLSegment belongs to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * @param bool $ignoreNestedURLs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 * @return SiteTree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	static public function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		Deprecation::notice('4.0', 'Use OldPageRedirector::find_old_page instead'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		if ($parent) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			$parent = SiteTree::get()->byID($parent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$url = OldPageRedirector::find_old_page(array($URLSegment), $parent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		return SiteTree::get_by_link($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: