/** * Redirct to the given URL. * It is generally recommended to call Director::redirect() rather than calling this function directly. */ function redirect($url, $code = 302) { if ($this->response->getHeader('Location')) { user_error("Already directed to " . $this->response->getHeader('Location') . "; now trying to direct to {$url}", E_USER_WARNING); return; } // Attach site-root to relative links, if they have a slash in them if ($url == "" || $url[0] == '?' || substr($url, 0, 4) != "http" && $url[0] != "/" && strpos($url, '/') !== false) { $url = Director::baseURL() . $url; } $this->response->redirect($url, $code); }
public function getNestedController() { if($this->urlParams['URLSegment']) { $SQL_URLSegment = Convert::raw2sql($this->urlParams['URLSegment']); $child = SiteTree::get_by_url($SQL_URLSegment); if(!$child) { if($child = $this->findOldPage($SQL_URLSegment)) { $url = Controller::join_links( Director::baseURL(), $child->URLSegment, $this->urlParams['Action'], $this->urlParams['ID'], $this->urlParams['OtherID'] ); $response = new HTTPResponse(); $response->redirect($url, 301); return $response; } $child = $this->get404Page(); } if($child) { if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}"); $controllerClass = "{$child->class}_Controller"; if($this->urlParams['Action'] && ClassInfo::exists($controllerClass.'_'.$this->urlParams['Action'])) { $controllerClass = $controllerClass.'_'.$this->urlParams['Action']; } if(ClassInfo::exists($controllerClass)) { $controller = new $controllerClass($child); } else { $controller = $child; } return $controller; } else { return new HTTPResponse("The requested page couldn't be found.",404); } } else { user_error("ModelAsController not geting a URLSegment. It looks like the site isn't redirecting to home", E_USER_ERROR); } }
public function getNestedController() { $url = $this->request->getUrl(); $getVars = $this->request->getVars(); if(isset($getVars['url'])) unset($getVars['url']); if($getVars) $url .= '?' . http_build_query($getVars); if($url) { $SQL_URLSegment = Convert::raw2sql($url); $page = DataObject::get_one( 'Page', "`SiteTree`.LegacyURL = '{$SQL_URLSegment}'" ); if($page) { $response = new HTTPResponse(); $newUrl = Director::baseURL() . $page->URLSegment; $response->redirect($newUrl, 301); return $response; } } return parent::getNestedController(); }
/** * Process the given URL, creating the appropriate controller and executing it. * * Request processing is handled as folows: * - Director::direct() creates a new HTTPResponse object and passes this to Director::handleRequest(). * - Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle this * request. * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, and call the rule * handling method. * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a * RequestHandler object. * * In addition to request processing, Director will manage the session, and perform the output of the actual response * to the browser. * * @param $url String, the URL the user is visiting, without the querystring. * @uses handleRequest() rule-lookup logic is handled by this. * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. */ static function direct($url) { // Validate $_FILES array before merging it with $_POST foreach ($_FILES as $k => $v) { if (is_array($v['tmp_name'])) { foreach ($v['tmp_name'] as $tmpFile) { if ($tmpFile && !is_uploaded_file($tmpFile)) { user_error("File upload '{$k}' doesn't appear to be a valid upload", E_USER_ERROR); } } } else { if ($v['tmp_name'] && !is_uploaded_file($v['tmp_name'])) { user_error("File upload '{$k}' doesn't appear to be a valid upload", E_USER_ERROR); } } } $req = new HTTPRequest(isset($_SERVER['X-HTTP-Method-Override']) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'], $url, $_GET, array_merge((array) $_POST, (array) $_FILES), @file_get_contents('php://input')); // @todo find better way to extract HTTP headers if (isset($_SERVER['HTTP_ACCEPT'])) { $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']); } if (isset($_SERVER['CONTENT_TYPE'])) { $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']); } if (isset($_SERVER['HTTP_REFERER'])) { $req->addHeader("Referer", $_SERVER['HTTP_REFERER']); } // Load the session into the controller $session = new Session($_SESSION); $result = Director::handleRequest($req, $session); $session->inst_save(); // Return code for a redirection request if (is_string($result) && substr($result, 0, 9) == 'redirect:') { $response = new HTTPResponse(); $response->redirect(substr($result, 9)); $response->output(); // Handle a controller } else { if ($result) { if ($result instanceof HTTPResponse) { $response = $result; } else { $response = new HTTPResponse(); $response->setBody($result); } // ?debug_memory=1 will output the number of bytes of memory used for this request if (isset($_REQUEST['debug_memory']) && $_REQUEST['debug_memory']) { echo number_format(memory_get_peak_usage(), 0); } else { $response->output(); } //$controllerObj->getSession()->inst_save(); } } }
public function getNestedController() { $url = Director::urlParams(); $hasUrlSegment = false; $parent = null; $counter = 1; foreach ($this->urlParams as $urlSegment) { $hasUrlSegment = true; $SQL_URLSegment = Convert::raw2sql($urlSegment); $filter = "URLSegment = '{$SQL_URLSegment}'"; if (isset($child)) { $filter .= "AND ParentID = {$child->ID}"; } else { $filter .= "AND ParentID = 0"; } $child = DataObject::get_one('SiteTree', $filter); if (!$child) { $key = $counter - 2; if ($key < 0) { $key = 0; } $this->mapParams($key); $c = new ModelAsController(); $this->request->setParams($this->urlParams); return $c->handleRequest($this->request); } if (isset($child->UrlParentID) && $child->UrlParentID != 0) { $parent = $child->UrlParent(); } else { if (isset($child->ParentID) && $child->ParentID != 0) { $parent = $child->Parent(); } else { $parent = null; } } if ($parent && $parent->URLSegment != 'home') { $keyint = $counter - 1; $key = "url" . $keyint; if (!isset($this->urlParams[$key]) || $parent->URLSegment != $this->urlParams[$key]) { $child = $this->get404Page(); break; } } $counter++; } if ($hasUrlSegment) { if (!$child) { if ($child = $this->findOldPage($SQL_URLSegment)) { $url = Controller::join_links(Director::baseURL(), $child->URLSegment, isset($this->urlParams['Action']) ? $this->urlParams['Action'] : null, isset($this->urlParams['ID']) ? $this->urlParams['ID'] : null, isset($this->urlParams['OtherID']) ? $this->urlParams['OtherID'] : null); $response = new HTTPResponse(); $response->redirect($url, 301); return $response; } $child = $this->get404Page(); } if ($child) { $this->mapParams(); if (isset($_REQUEST['debug'])) { Debug::message("Using record #{$child->ID} of type {$child->class} with URL {$this->urlParams['URLSegment']}"); } // set language if ($child->Locale) { Translatable::set_current_locale($child->Locale); } $controllerClass = "{$child->class}_Controller"; if ($this->urlParams['Action'] && ClassInfo::exists($controllerClass . '_' . $this->urlParams['Action'])) { $controllerClass = $controllerClass . '_' . $this->urlParams['Action']; } if (ClassInfo::exists($controllerClass)) { $controller = new $controllerClass($child); } else { $controller = $child; } return $controller; } else { return new HTTPResponse("The requested page couldn't be found.", 404); } } else { user_error("NestedUrlController not geting a URLSegment. It looks like the site isn't redirecting to home", E_USER_ERROR); } }
/** * Process the given URL, creating the appropriate controller and executing it. * * This method will: * - iterate over all of the rules given in {@link Director::addRules()}, and find the first one that matches. * - instantiate the {@link Controller} object required by that rule, and call {@link Controller::setURLParams()} to give the URL paramters to the controller. * - link the Controller's session to PHP's main session, using {@link Controller::setSession()}. * - call {@link Controller::run()} on that controller * - save the Controller's session back into PHP's main session. * - output the response to the browser, using {@link HTTPResponse::output()}. * * @param $url String, the URL the user is visiting, without the querystring. * @uses getControllerForURL() rule-lookup logic is handled by this. * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. */ function direct($url) { if (isset($_GET['debug_profile'])) { Profiler::mark("Director", "direct"); } $controllerObj = Director::getControllerForURL($url); if (is_string($controllerObj) && substr($controllerObj, 0, 9) == 'redirect:') { $response = new HTTPResponse(); $response->redirect(substr($controllerObj, 9)); $response->output(); } else { if ($controllerObj) { // Load the session into the controller $controllerObj->setSession(new Session($_SESSION)); $response = $controllerObj->run(array_merge((array) $_GET, (array) $_POST, (array) $_FILES)); $controllerObj->getSession()->inst_save(); if (isset($_GET['debug_profile'])) { Profiler::mark("Outputting to browser"); } $response->output(); if (isset($_GET['debug_profile'])) { Profiler::unmark("Outputting to browser"); } } } if (isset($_GET['debug_profile'])) { Profiler::unmark("Director", "direct"); } }
/** * Process the given URL, creating the appropriate controller and executing it. * * Request processing is handled as folows: * - Director::direct() creates a new HTTPResponse object and passes this to Director::handleRequest(). * - Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle this * request. * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, and call the rule * handling method. * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a * RequestHandler object. * * In addition to request processing, Director will manage the session, and perform the output of the actual response * to the browser. * * @param $url String, the URL the user is visiting, without the querystring. * @uses handleRequest() rule-lookup logic is handled by this. * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call. */ static function direct($url) { $req = new HTTPRequest( (isset($_SERVER['X-HTTP-Method-Override'])) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'], $url, $_GET, array_merge((array)$_POST, (array)$_FILES), @file_get_contents('php://input') ); // @todo find better way to extract HTTP headers if(isset($_SERVER['HTTP_ACCEPT'])) $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']); if(isset($_SERVER['CONTENT_TYPE'])) $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']); if(isset($_SERVER['HTTP_REFERER'])) $req->addHeader("Referer", $_SERVER['HTTP_REFERER']); // Load the session into the controller $session = new Session($_SESSION); $result = Director::handleRequest($req, $session); $session->inst_save(); // Return code for a redirection request if(is_string($result) && substr($result,0,9) == 'redirect:') { $response = new HTTPResponse(); $response->redirect(substr($result, 9)); $response->output(); // Handle a controller } else if($result) { if($result instanceof HTTPResponse) { $response = $result; } else { $response = new HTTPResponse(); $response->setBody($result); } // ?debug_memory=1 will output the number of bytes of memory used for this request if(isset($_REQUEST['debug_memory']) && $_REQUEST['debug_memory']) { echo number_format(memory_get_peak_usage(),0); } else { $response->output(); } //$controllerObj->getSession()->inst_save(); } }