/** * @return null|string */ public function getErrorPresenter() { $errorPresenter = $this->defaultErrorPresenter; $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { return $errorPresenter; } $name = $request->getPresenterName(); $modules = explode(":", $name); unset($modules[count($modules) - 1]); while (count($modules) != 0) { $catched = false; try { $errorPresenter = implode(":", $modules) . ':Error'; $this->presenterFactory->getPresenterClass($errorPresenter); break; } catch (InvalidPresenterException $e) { $catched = true; } unset($modules[count($modules) - 1]); } if (isset($catched) && $catched) { return $this->defaultErrorPresenter; } return $errorPresenter; }
/** * @param string|NULL */ public function renderIn($backlink) { $httpRequest = $this->getHttpRequest(); $referer = NULL; if ($httpRequest instanceof \Nette\Http\Request) { $referer = $httpRequest->getReferer(); } if (!$backlink && $referer && $referer->getHost() == $httpRequest->getUrl()->getHost()) { $url = new UrlScript($referer); $url->setScriptPath($httpRequest->getUrl()->getScriptPath()); $tmp = new Request($url); $req = $this->router->match($tmp); if (!$req) { return; } if (isset($req->parameters[static::SIGNAL_KEY])) { $params = $req->parameters; unset($params[static::SIGNAL_KEY]); $req->setParameters($params); } if ($req->getPresenterName() != $this->getName()) { $session = $this->getSession('Nette.Application/requests'); do { $key = Strings::random(5); } while (isset($session[$key])); $session[$key] = array($this->getUser()->getId(), $req); $session->setExpiration('+ 10 minutes', $key); $this->params['backlink'] = $key; } } }
/** * @return \Nette\Application\Request */ public function getRequest() { if (!$this->request) { $this->request = $this->router->match($this->httpRequest); } return $this->request; }
/** * @return Request */ public function createInitialRequest() { $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { throw new BadRequestException('No route for HTTP request.'); } elseif (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } try { $name = $request->getPresenterName(); $this->presenterFactory->getPresenterClass($name); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 0, $e); } return $request; }
public function update(Items\Base $item = NULL, $data) { try { $actualUrl = $this->request->getUrl(); $urlScript = new Nette\Http\UrlScript($data); $urlScript->setScriptPath($actualUrl->getScriptPath()); if ($urlScript->getHost() == $actualUrl->getHost()) { $request = new Nette\Http\Request($urlScript); $appRequest = $this->router->match($request); if (!is_null($appRequest)) { $data = $appRequest->getPresenterName(); $data .= ':' . $appRequest->getParameter('action'); $fragment = $urlScript->getFragment(); if ($fragment != '') { $data .= '#' . $fragment; } $parameters = $appRequest->getParameters(); unset($parameters['action']); if (count($parameters) > 0) { $data .= ', ' . Nette\Utils\Json::encode($parameters); } } } } catch (Nette\InvalidArgumentException $e) { } return $data; }
/** * @return Request */ public function createInitialRequest() { $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { throw new BadRequestException('No route for HTTP request.'); } return $request; }
public function __invoke($application, $response) { if ($response instanceof JsonResponse && ($payload = $response->getPayload()) instanceof \stdClass) { if (!$this->forwardHasHappened && isset($payload->redirect)) { $url = new Http\UrlScript($payload->redirect); $url->setScriptPath($this->httpRequest->url->scriptPath); $httpRequest = new Http\Request($url); if ($this->router->match($httpRequest) !== NULL) { $prop = new Property($application, 'httpRequest'); $prop->setAccessible(TRUE); $prop->setValue($application, $httpRequest); $application->run(); exit; } } elseif ($this->forwardHasHappened && !isset($payload->redirect)) { $payload->redirect = $application->getPresenter()->link('this'); } } }
public function __invoke($application, $response) { if ($response instanceof JsonResponse && ($payload = $response->getPayload()) instanceof \stdClass) { if (!$this->forwardHasHappened && isset($payload->redirect)) { if (($fragmentPos = strpos($payload->redirect, '#')) !== FALSE) { $this->fragment = substr($payload->redirect, $fragmentPos); } $url = new Http\UrlScript($payload->redirect); $url->setScriptPath($this->httpRequest->url->scriptPath); $httpRequest = new Http\Request($url); if ($this->router->match($httpRequest) !== NULL) { $prop = new Property('Nette\\Application\\Application', 'httpRequest'); $prop->setAccessible(TRUE); $prop->setValue($application, $httpRequest); $application->run(); exit; } } elseif ($this->forwardHasHappened && !isset($payload->redirect)) { $payload->redirect = $application->getPresenter()->link('this', $application->getPresenter()->getParameters()) . $this->fragment; $this->fragment = ''; } } }
/** * @param string $username * @param string $password * @param array $presenters If array of presenters is empty, accept all * @param Nette\Application\IRouter $router * @param Nette\Http\IRequest $httpRequest * @param Nette\Http\IResponse $httpResponse */ public function __construct($username, $password, $presenters, Nette\Application\IRouter $router, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse, $exit_on_bad_credentials = TRUE) { $this->httpRequest = $httpRequest; $this->httpResponse = $httpResponse; $this->exit_on_bad_credentials = $exit_on_bad_credentials; $request = $router->match($httpRequest); if (!$request) { return; } /** * Eccapt either all presenter or just the specified ones */ if (empty($presenters) || in_array($request->getPresenterName(), $presenters)) { $this->authenticate($username, $password); } }
/** * Gets named option from current route * * @param string $name * * @return string|null */ protected function getRoutingOption($name) { $option = NULL; // Get actual route $request = $this->router->match($this->httpRequest); if ($request instanceof Application\Request) { $params = $request->getParameters(); $option = isset($params[$name]) ? $params[$name] : NULL; } if (!$option) { $option = $this->redirectConf[$name]['action']; } if (in_array($option, array(self::REDIRECT, self::REDIRECT_WITHOUT_PATH, self::NO_REDIRECT))) { return $option; } return null; }
/** * Dispatch a HTTP request to a front controller. * * @return void */ public function run() { $request = null; $repeatedError = false; do { try { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = null; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } } $this->requests[] = $request; $this->onRequest($this, $request); // Instantiate presenter $presenterName = $request->getPresenterName(); try { $this->presenter = $this->presenterFactory->createPresenter($presenterName); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 404, $e); } $this->presenterFactory->getPresenterClass($presenterName); $request->setPresenterName($presenterName); $request->freeze(); // Execute presenter $response = $this->presenter->run($request); if ($response) { $this->onResponse($this, $response); } // Send response if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { // fault barrier $this->onError($this, $e); if (!$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } if ($repeatedError) { $e = new ApplicationException('An error occurred while executing error-presenter', 0, $e); } if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if (!$repeatedError && $this->errorPresenter) { $repeatedError = true; if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } else { // default error handler if ($e instanceof BadRequestException) { $code = $e->getCode(); } else { $code = 500; Nette\Diagnostics\Debugger::log($e, Nette\Diagnostics\Debugger::ERROR); } require __DIR__ . '/templates/error.phtml'; break; } } } while (1); $this->onShutdown($this, isset($e) ? $e : null); }
/** * Dispatch a HTTP request to a front controller. * @return void */ public function run() { $request = NULL; $repeatedError = FALSE; do { try { if (count($this->requests) > self::$maxLoop) { throw new ApplicationException('Too many loops detected in application life cycle.'); } if (!$request) { $this->onStartup($this); $request = $this->router->match($this->httpRequest); if (!$request instanceof Request) { $request = NULL; throw new BadRequestException('No route for HTTP request.'); } if (strcasecmp($request->getPresenterName(), $this->errorPresenter) === 0) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } } $this->requests[] = $request; $this->onRequest($this, $request); // Instantiate presenter $presenterName = $request->getPresenterName(); try { $this->presenter = $this->presenterFactory->createPresenter($presenterName); } catch (InvalidPresenterException $e) { throw new BadRequestException($e->getMessage(), 404, $e); } $this->presenterFactory->getPresenterClass($presenterName); $request->setPresenterName($presenterName); $request->freeze(); // Execute presenter $response = $this->presenter->run($request); if ($response) { $this->onResponse($this, $response); } // Send response if ($response instanceof Responses\ForwardResponse) { $request = $response->getRequest(); continue; } elseif ($response instanceof IResponse) { $response->send($this->httpRequest, $this->httpResponse); } break; } catch (\Exception $e) { $this->onError($this, $e); if ($repeatedError) { $e = new ApplicationException("An error occurred while executing error-presenter '{$this->errorPresenter}'.", 0, $e); } if ($repeatedError || !$this->catchExceptions) { $this->onShutdown($this, $e); throw $e; } $repeatedError = TRUE; $this->errorPresenter = $this->errorPresenter ?: 'Nette:Error'; if (!$this->httpResponse->isSent()) { $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() : 500); } if ($this->presenter instanceof UI\Presenter) { try { $this->presenter->forward(":{$this->errorPresenter}:", array('exception' => $e)); } catch (AbortException $foo) { $request = $this->presenter->getLastCreatedRequest(); } } else { $request = new Request($this->errorPresenter, Request::FORWARD, array('exception' => $e)); } // continue } } while (1); $this->onShutdown($this, isset($e) ? $e : NULL); }