/** * {@inheritdoc} */ public function onExecuteAction(ExecuteActionEvent $event, $eventConfig) { $request = $event->getRequest(); try { $userId = $request->getSession()->get('user.id'); $user = $this->cast('Mapper\\User', $userId); $redirectUrl = null; if ($request->getController() == 'Resource') { return; } else { if (!$user->getLocation() && ($request->getController() != 'Authenticate' || $request->getMethod() != 'setLocation')) { $redirectUrl = '/login/location'; } else { if ($user->getLocation() && $request->getController() == 'Authenticate' && $request->getMethod() == 'setLocation') { $redirectUrl = '/'; } else { return; } } } $response = new Response(); $response->redirect($redirectUrl); $event->setResponse($response); } catch (ValueNotFoundException $e) { return; } catch (UserNotFoundExceptio $e) { $request->getSession()->clear(); $response = new Response(); $response->redirect('/login'); $event->setResponse($response); } }
/** * Support Strict-Transport-Security * * @param \Arbor\Provider\Response $response * @param array $config * @see https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security * @since 0.1.0 */ private function supportTimeForceSSL(Response $response, $config) { if (isset($config['forceSSL']) && $config['forceSSL'] == "true") { $maxAge = '3600000'; if (isset($config['timeForceSSL'])) { $maxAge = $config['timeForceSSL']; } $response->setHeader('Strict-Transport-Security', 'max-age=' . $maxAge . '; includeSubDomains'); } }
public function render(RequestConfig $config, Response $response) { if (!$config->isSilent()) { header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage()); } foreach ($response->getHeaders() as $name => $value) { header($name . ': ' . $value); } echo (string) $response->getContent(); }
private function createResponseRedirect($redirect, $request) { $response = new Response(); if ($request->isAjax()) { $response->setStatusCode(401); $response->setHeader('X-Location', $redirect); } else { $response->redirect($redirect); } return $response; }
/** * Saves user entity to database after edit * * @param User $entity * @return Response|array */ public function edit($entity) { $form = $this->createForm($entity); if ($form->isValid()) { $data = $form->getData(); $this->setEntity($entity, $data); $this->flush(); $response = new Response(); $response->redirect('/user'); return $response; } return compact('form'); }
public function render(RequestConfig $config, Response $response) { header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage()); foreach ($response->getHeaders() as $name => $value) { header($name . ': ' . $value); } if ($response->getStatusCode() < 299) { $startRange = 0; $endRange = filesize($response->getContent()) - 1; try { $contentRange = $response->getHeader('Content-Range'); if (preg_match('/^bytes ([0-9]+)-([0-9]+)\\/([0-9]+)$/', $contentRange, $match)) { $startRange = (int) $match[1]; $endRange = (int) $match[2]; } } catch (HeaderNotFoundException $e) { //skipp } $buffer = 1024 * 8; $file = @fopen($response->getContent(), 'rb'); fseek($file, $startRange); while (!feof($file) && ($p = ftell($file)) <= $endRange) { if ($p + $buffer > $endRange) { $buffer = $endRange - $p + 1; } set_time_limit(0); echo fread($file, $buffer); flush(); } fclose($file); } }
private function displayError(Response $response) { $presenterConfig = $this->config->getPresenter(); \Twig_Autoloader::register(); $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../../template'); //TODO przekazywać do presentera config $twig = new \Twig_Environment($loader); $exception = $response->getContent(); $data = array('statusCode' => $response->getStatusCode(), 'message' => $exception->getMessage(), 'exception' => get_class($exception), 'file' => $exception->getFile(), 'line' => $exception->getLine()); if ($this->config->isDebug()) { echo $twig->render('error.twig', $data); } else { echo $twig->render($response->getStatusCode() . '.twig'); } }
private function renderFail(Response $response) { if (!$this->config->isSilent()) { header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage()); } if ($response->getContent() instanceof \Exception) { $exception = $response->getContent(); $message = 'Błąd wewnętrzny!'; if ($exception instanceof \Arbor\Core\Exception) { $message = $exception->getSafeMessage(); } if ($this->config->isDebug()) { $message = $exception->getMessage(); } $data = array('code' => $exception->getCode(), 'message' => $message); if ($this->config->isDebug()) { $data = array_merge($data, array('file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => $exception->getTraceAsString(), 'exception' => get_class($exception))); } echo json_encode($data); } }
/** * Assign device to my location * * @param \Entity\Device $entity * @return Response */ public function assign($entity) { if ($entity->getUser()->getId() != $this->getUser()->getId()) { throw new YouAreNotOwnerException(); } $entity->setUser(null); $this->flush(); $response = new Response(); $response->redirect('/device/my'); return $response; }
/** * Removes location entity * * @param \Entity\Location $entity * @return Response */ public function remove($entity) { if ($entity == $this->getUser()->getLocation()) { throw new UnableToDeleteOwnLocationException(); } $this->getDoctrine()->getEntityManager()->remove($entity); $this->flush(); $response = new Response(); $response->redirect('/location'); return $response; }
/** * Set headers for range data support. * * @param \Arbor\Provider\Response $response * @param long $fileSize * @since 0.1.0 */ private function rangeSupport($response, $fileSize) { try { $request = $this->getRequest(); $range = $request->getHeader('Range'); $invalidRange = false; if (preg_match('/^bytes=([0-9]+)-([0-9]*)$/', $range, $match)) { $startRange = $match[1]; $endRange = $match[2]; if ($endRange == '' || $endRange > $filesize) { $endRange = $fileSize - 1; } if ($startRange > $endRange) { $invalidRange = true; } else { $response->setHeader('Accept-Ranges', '0-' . $fileSize); $response->setHeader('Content-Range', 'bytes ' . $startRange . '-' . $endRange . '/' . $fileSize); $response->setHeader('content-length', $endRange - $startRange + 1); $response->setStatusCode(206); } } else { $invalidRange = true; } if ($invalidRange) { $response->setStatusCode(416); $response->setHeader('Content-Range', 'bytes *-/' . $filesize); } } catch (HeaderNotFoundException $e) { //skipp } }
/** * Form with contact data to current owner * * @return Response|array */ public function addApply() { $data = $this->getRequest()->getSession()->get('order.info'); $device = $this->cast('Mapper\\Device', $data['device']); /* @var $device Device */ $location = $device->getLocation(); /* @var $location Location */ $form = $this->createApplyForm(); if ($form->isValid()) { if ($device->getLocation() == $this->getUser()->getLocation()) { throw new OrderWrongLocationException(); } $entity = new \Entity\Order(); $entity->setOwner($this->getUser()); $entity->setDevice($device); $entity->setState($this->cast('Mapper\\OrderState', 1)); $this->persist($entity); $device->setState($this->cast('Mapper\\DeviceState', 2)); $this->flush(); $response = new Response(); $response->redirect('/order'); $this->getRequest()->getSession()->remove('order.info'); return $response; } return compact('form', 'device', 'location'); }
/** * Removing device type from database * * @param \Entity\DeviceType $entity * @return Response */ public function remove($entity) { $this->getDoctrine()->getEntityManager()->remove($entity); $this->flush(); $response = new Response(); $response->redirect('/devicetype'); return $response; }
/** * Execute controller method * * @throws \Arbor\Exception\ActionNotFoundException * @since 0.1.0 */ protected function callMethod() { $this->resources->registerPresenter($this->getPresenter()); $response = new Response(); $response->setPresenter($this->resources->getPresenter()); $event = new ExecuteActionEvent($this->request); $this->eventManager->fire('executeAction', $event); if (!$event->getResponse()) { $controllerName = $this->request->getClass(); $controller = new $controllerName($this->request, $this->resources); if (!is_callable(array($controller, $this->request->getMethod()))) { throw new ActionNotFoundException($controllerName, $this->request->getMethod()); } $controllerData = call_user_func_array(array($controller, $this->request->getMethod()), $this->request->getArguments()); if ($controllerData instanceof Response) { $response = $controllerData; if (!$response->getPresenter()) { $response->setPresenter($this->resources->getPresenter()); } else { $this->resources->registerPresenter($response->getPresenter()); } } else { $response->setContent($controllerData); } $event = new ExecutedActionEvent($this->request, $response); $this->eventManager->fire('executedAction', $event); } else { $response = $event->getResponse(); } $this->resources->registerResponse($response); $this->prepareView($this->request, $this->resources->getPresenter(), $response); }
/** * Logout method * * @return Response * @throws \Arbor\Exception\ServiceNotFoundException */ public function logout() { $googleService = $this->getService('google'); $client = $googleService->getClient(); $client->revokeToken(); $this->getRequest()->getSession()->clear(); $response = new Response(); $response->redirect('/'); return $response; }
/** * Parse view for exception. * * @param \Exception $exception * @since 0.1.0 */ private function parseView($exception) { if (!$this->resources->getEnviorment()->isSilent()) { error_log($exception->getMessage() . " " . $exception->getFile() . "(" . $exception->getLine() . ")"); } $response = new Response(); $response->setStatusCode(500); $response->setContent($exception); $this->resources->registerResponse($response); $presenter = null; try { $presenter = $this->resources->getPresenter(); } catch (ResourcesNotRegisteredException $e) { $presenter = $this->findPresenter(); } try { $request = $this->resources->getRequest(); } catch (ResourcesNotRegisteredException $e) { $requestConfig = new RequestConfig('', '', $this->resources->getEnviorment(), array('route' => '', 'presenter' => array('class' => ''), 'class' => '')); if ($this->resources->getEnviorment()->isSilent()) { $request = new RequestTest($this->resources->getUrl(), $this->resources->getEnviorment()); $request->setConfig($requestConfig); } else { $session = new Session($this->resources->getEnviorment()); $request = new Request($requestConfig, $this->resources->getUrl(), $session); } $this->resources->registerRequest($request); } if ($presenter) { $event = new ExecutePresenterEvent($this->resources->getRequest(), $response); $this->eventManager->fire('executePresenter', $event); $presenter->render($request->getConfig(), $response); } }
/** * create response with configure redirect action * * @param \Arbor\Core\Container $container * @param string $url - destiny http address * @return \Arbor\Provider\Response */ public function redirect(Container $container, $url) { $response = new Response(); $response->redirect($url); return $response; }
/** * Assign device to me * * @param \Entity\Device $entity * @return Response */ public function assign($entity) { if ($entity->getLocation()->getId() != $this->getUser()->getId()) { throw new DeviceNotFoundException(); } if ($entity->getUser()) { throw new AlreadyHasOwnerException(); } $entity->setUser($this->getUser()); $this->flush(); $response = new Response(); $response->redirect('/device/location'); return $response; }
/** * Save changes on device after edit * * @param \Entity\Device $device * @return Response|array */ public function edit($device) { $form = $this->createForm($device); if ($form->isValid()) { $data = $form->getData(); $conn = $this->getDoctrine()->getEntityManager()->getConnection(); $conn->beginTransaction(); if ($data['photo']) { //save uploaded photo to cache file $data['tmpPhoto'] = $this->saveTmpPhoto($data['photo']); } $this->saveEntity($device, $data, $data['serialNumber']); $this->flush(); $conn->commit(); $response = new Response(); $response->redirect('/device'); return $response; } return compact('form'); }
public function render(RequestConfig $config, Response $response) { $this->setHeaders(); echo json_encode($response->getContent()); }