/** * Handles HTTP requests. * * If you are going to overload handleRequest, make sure that you start the method with $this->pushCurrent() * and end the method with $this->popCurrent(). Failure to do this will create weird session errors. * * @param $request The {@link HTTPRequest} object that is responsible for distributing request parsing. */ function handleRequest(HTTPRequest $request) { if(!$request) user_error("Controller::handleRequest() not passed a request!", E_USER_ERROR); $this->pushCurrent(); $this->urlParams = $request->allParams(); $this->request = $request; $this->response = new HTTPResponse(); // Init $this->baseInitCalled = false; $this->init(); if(!$this->baseInitCalled) user_error("init() method on class '$this->class' doesn't call Controller::init(). Make sure that you have parent::init() included.", E_USER_WARNING); // If we had a redirection or something, halt processing. if($this->response->isFinished()) { $this->popCurrent(); return $this->response; } $body = parent::handleRequest($request); if($body instanceof HTTPResponse) { if(isset($_REQUEST['debug_request'])) Debug::message("Request handler returned HTTPResponse object to $this->class controller; returning it without modification."); $this->response = $body; } else { if(is_object($body)) { if(isset($_REQUEST['debug_request'])) Debug::message("Request handler $body->class object to $this->class controller;, rendering with template returned by $body->class::getViewer()"); $body = $body->getViewer($request->latestParam('Action'))->process($body); } $this->response->setBody($body); } ContentNegotiator::process($this->response); HTTP::add_cache_headers($this->response); $this->popCurrent(); return $this->response; }
/** * Throw an HTTP error instead of performing the normal processing * @todo This doesn't work properly right now. :-( */ function httpError($errorCode, $errorMessage = null) { $r = new HTTPResponse(); $r->setBody($errorMessage); $r->setStatuscode($errorCode); return $r; }
function html(HTTPResponse $response) { $response->addHeader("Content-type", "text/html; charset=" . self::$encoding); $response->addHeader("Vary", "Accept"); $content = $response->getBody(); $content = ereg_replace("<\\?xml[^>]+\\?>\n?", '', $content); $content = str_replace(array('/>', 'xml:lang', 'application/xhtml+xml'), array('>', 'lang', 'text/html'), $content); $content = ereg_replace('<!DOCTYPE[^>]+>', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', $content); $content = ereg_replace('<html xmlns="[^"]+"', '<html ', $content); $response->setBody($content); }
/** * @param String $url * @param Array $data * @return String HTTPResponse */ function post($url, $postVars) { $ch = curl_init($url); if (!empty(RecaptchaField::$proxy_server)) { curl_setopt($ch, CURLOPT_PROXY, RecaptchaField::$proxy_server); if (!empty(RecaptchaField::$proxy_auth)) { curl_setopt($ch, CURLOPT_PROXYUSERPWD, RecaptchaField::$proxy_auth); } } curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'reCAPTCHA/PHP'); // we need application/x-www-form-urlencoded curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postVars)); $response = curl_exec($ch); if (class_exists('SS_HTTPResponse')) { $responseObj = new SS_HTTPResponse(); } else { // 2.3 backwards compat $responseObj = new HTTPResponse(); } $responseObj->setBody($response); // 2.2. compat return $responseObj; }
function html(HTTPResponse $response) { $response->addHeader("Content-Type", "text/html; charset=" . self::$encoding); $response->addHeader("Vary", "Accept"); $content = $response->getBody(); $hasXMLHeader = substr($content, 0, 5) == '<' . '?xml'; $content = ereg_replace("<\\?xml[^>]+\\?>\n?", '', $content); $content = str_replace(array('/>', 'xml:lang', 'application/xhtml+xml'), array('>', 'lang', 'text/html'), $content); // Only replace the doctype in templates with the xml header if ($hasXMLHeader) { $content = ereg_replace('<!DOCTYPE[^>]+>', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', $content); } $content = ereg_replace('<html xmlns="[^"]+"', '<html ', $content); $response->setBody($content); }
/** * 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(); } } }
/** * 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(); } }