Пример #1
0
 /**
  *	Handle Request
  *	@param HTTPRequest &$request
  *	@param HTTPResponse &$response
  *	@return string
  */
 public function handleRequest(HTTPRequest &$request, HTTPResponse &$response)
 {
     //Pass through to Controller
     try {
         $response = new WebpageResponse($this);
         if ($request->isPost()) {
             $validation = Validation::create();
             $this->validatePostRequest($request, $response, $validation);
         }
         $body = parent::handleRequest($request, $response);
     } catch (\Exception $e) {
         if (!$e instanceof HTTPResponseException) {
             error_log(print_r($e, true));
             $e = new HTTPResponseException($e->getMessage(), 500);
             $e->response()->setHeader('Content-Type', 'text/plain');
         }
         $body = $this->handleException($e);
         //If handleException returns a redirect. Follow it.
         if ($body instanceof \Touchbase\Control\HTTPResponse) {
             //TODO: Should we always return the response?
             if ($body->hasFinished()) {
                 return $response = $body;
             }
         }
         $response->setBody($body);
         $response->setStatusCode($e->getCode(), $e->getMessage());
     }
     if ($this->request()->isMainRequest()) {
         $applicationTitles = [];
         $application = $this->application;
         while ($application != null) {
             if (isset($application::$name)) {
                 $applicationTitles[] = $application::$name;
             }
             $application = $application->application;
         }
         $this->response()->assets()->pushTitle(array_reverse($applicationTitles));
         if (isset(static::$name)) {
             $this->response()->assets()->pushTitle(static::$name);
         }
     }
     return $body;
 }
Пример #2
0
 /**
  *	Route
  *	@param HTTPRequest | string $request
  *	@param HTTPResponse $response
  *	@return VOID
  */
 public static function route($request, HTTPResponse &$response = null)
 {
     //GET / POST etc.
     $requestMethod = isset($_SERVER['X-HTTP-Method-Override']) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'];
     // IIS will sometimes generate this.
     if (!empty($_SERVER['HTTP_X_ORIGINAL_URL'])) {
         $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
     }
     $url = is_string($request) ? $request : preg_replace("/^\\/" . str_replace("/", "\\/?", WORKING_DIR) . "/", '', $_SERVER["REQUEST_URI"]);
     if (strpos($url, '?') !== false) {
         list($url, $query) = explode('?', $url, 2);
         parse_str($query, $_GET);
         if ($_GET) {
             $_REQUEST = array_merge((array) $_REQUEST, (array) $_GET);
         }
     }
     // Pass back to the webserver for files that exist
     if (php_sapi_name() == 'cli-server') {
         $realFile = File::create([BASE_PATH, 'public_html', $url]);
         if ($realFile->isFile() && $realFile->exists()) {
             //Can't return false this far up, read file instead.
             readfile($realFile->path);
             exit;
         }
     }
     //Remove base folders from the URL if webroot is hosted in a subfolder
     if (substr(strtolower($url), 0, strlen(BASE_PATH)) == strtolower(BASE_PATH)) {
         $url = substr($url, strlen(BASE_PATH));
     }
     $response = $response ?: HTTPResponse::create();
     if (!$request instanceof HTTPRequest) {
         $request = HTTPRequest::create($requestMethod, $url)->setMainRequest(true);
     }
     SessionStore::ageFlash();
     if (!static::handleRequest($request, $response)) {
         try {
             //Get Dispatchable
             if (!static::$dispatch) {
                 $dispatchNamespace = static::config()->get("project")->get("namespace", "");
                 $dispatch = $dispatchNamespace . '\\' . $dispatchNamespace . 'App';
                 if (substr($dispatch, 0, 1) != '\\') {
                     $dispatch = '\\' . $dispatch;
                 }
                 if (class_exists($dispatch)) {
                     if (!static::$dispatch) {
                         static::$dispatch = $dispatch::create();
                         static::$dispatch->setConfig(static::config())->init();
                     }
                 } else {
                     $e = new HTTPResponseException("Could not load project", 404);
                     //Error responses should be considered plain text for security reasons.
                     $e->response()->setHeader('Content-Type', 'text/plain');
                     throw $e;
                 }
             }
             static::$dispatch->handleRequest($request, $response);
         } catch (HTTPResponseException $e) {
             $response->setStatusCode($e->getCode(), $e->getMessage());
             $response->setBody(static::$dispatch ? static::$dispatch->handleException($e) : $e->getMessage());
         }
     }
     //TODO: It would be good for this to reside in $response->render() - But would need access to $request.
     if ((!$response->isError() && !$response->hasFinished() || $response->statusCode() === 401) && $request->isMainRequest() && !$request->isAjax()) {
         if (strpos($response->getHeader("Content-Type"), "text/html") === 0) {
             static::setRouteHistory(static::buildParams($request->url(), $_GET));
         }
     }
     if (static::isDev()) {
         error_log(sprintf("%s [%d]: %s - %s", $requestMethod, $response->statusCode(), $url, $response->statusDescription()), 4);
     }
     return $response;
 }
Пример #3
0
 /**
  *	Throw HTTP Error
  *	@param int $erroCode
  *	@param string $errorMessage
  *	@throws \Touchbase\Control\Exception\HTTPResponseException
  *	@return VOID
  */
 protected function throwHTTPError($errorCode, $errorMessage = null)
 {
     $e = new HTTPResponseException($errorMessage, $errorCode);
     //Error responses should be considered plain text for security reasons.
     $e->response()->setHeader('Content-Type', 'text/plain');
     throw $e;
 }