Exemplo n.º 1
0
 /**
  * Sends response to output.
  * @return void
  */
 public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setContentType($this->contentType);
     $httpResponse->setHeader('Content-Disposition', ($this->forceDownload ? 'attachment' : 'inline') . '; filename="' . $this->name . '"' . '; filename*=utf-8\'\'' . rawurlencode($this->name));
     $length = strlen($this->data);
     /*
     		if ($this->resuming) {
     			$httpResponse->setHeader('Accept-Ranges', 'bytes');
     			if (preg_match('#^bytes=(\d*)-(\d*)\z#', $httpRequest->getHeader('Range'), $matches)) {
     				list(, $start, $end) = $matches;
     				if ($start === '') {
     					$start = max(0, $filesize - $end);
     					$end = $filesize - 1;
     
     				} elseif ($end === '' || $end > $filesize - 1) {
     					$end = $filesize - 1;
     				}
     				if ($end < $start) {
     					$httpResponse->setCode(416); // requested range not satisfiable
     					return;
     				}
     
     				$httpResponse->setCode(206);
     				$httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
     				$length = $end - $start + 1;
     				fseek($handle, $start);
     
     			} else {
     				$httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
     			}
     		}*/
     $httpResponse->setHeader('Content-Length', $length);
     echo $this->data;
 }
Exemplo n.º 2
0
 /**
  * {inheritDoc}
  */
 public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setContentType($this->getContentType(), 'utf-8');
     $httpResponse->setExpiration(FALSE);
     $httpResponse->setCode($this->code);
     echo Nette\Utils\Json::encode($this->getPayload(), Nette\Utils\Json::PRETTY);
 }
Exemplo n.º 3
0
 public function generateImage(ImageRequest $request)
 {
     $width = $request->getWidth();
     $height = $request->getHeight();
     $format = $request->getFormat();
     if (!$this->validator->validate($width, $height)) {
         throw new Application\BadRequestException();
     }
     $image = NULL;
     foreach ($this->providers as $provider) {
         $image = $provider->getImage($request);
         if ($image) {
             break;
         }
     }
     if (!$image) {
         $this->httpResponse->setHeader('Content-Type', 'image/jpeg');
         $this->httpResponse->setCode(Http\IResponse::S404_NOT_FOUND);
         exit;
     }
     $destination = $this->wwwDir . '/' . $this->httpRequest->getUrl()->getRelativeUrl();
     $dirname = dirname($destination);
     if (!is_dir($dirname)) {
         $success = @mkdir($dirname, 0777, TRUE);
         if (!$success) {
             throw new Application\BadRequestException();
         }
     }
     $success = $image->save($destination, 90, $format);
     if (!$success) {
         throw new Application\BadRequestException();
     }
     $image->send();
     exit;
 }
Exemplo n.º 4
0
 /**
  * @param IRequest $httpRequest
  * @param IResponse $httpResponse
  * @return void
  */
 public function send(IRequest $httpRequest, IResponse $httpResponse)
 {
     $httpResponse->setCode($this->code);
     foreach ($this->headers as $name => $value) {
         $httpResponse->setHeader($name, $value);
     }
     echo (string) $this->getBody();
 }
Exemplo n.º 5
0
 /**
  * @param Nette\Http\IRequest $httpRequest
  * @param Nette\Http\IResponse $httpResponse
  */
 function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $response = $this->getResponse();
     $httpResponse->setCode(200);
     $httpResponse->setContentType('text/plain');
     $httpResponse->setHeader('Content-Length', strlen($response));
     echo $response;
 }
Exemplo n.º 6
0
 /**
  * @param Http\IRequest $request
  * @param Http\IResponse $response
  */
 public function send(Http\IRequest $request, Http\IResponse $response)
 {
     $response->setContentType($this->contentType);
     $response->setCode($this->code ?: Http\IResponse::S200_OK);
     $response->setExpiration($this->expiration);
     $response->setHeader('Pragma', $this->expiration ? 'cache' : 'no-cache');
     echo Json::encode($this->data);
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function send(IRequest $httpRequest, IResponse $httpResponse)
 {
     $httpResponse->setContentType('text/xml');
     $httpResponse->setExpiration(false);
     $httpResponse->setCode($this->getCode());
     $httpResponse->setHeader('Content-Length', strlen($this->response));
     echo $this->response;
 }
Exemplo n.º 8
0
 public function __invoke($application, $response)
 {
     if ($response instanceof JsonResponse && ($payload = $response->getPayload()) instanceof \stdClass) {
         if ($this->forwardHasHappened && !isset($payload->redirect)) {
             $payload->url = $application->getPresenter()->link('this');
         }
         $this->httpResponse->addHeader('Vary', 'X-Requested-With');
     }
 }
Exemplo n.º 9
0
 /**
  * Send JSONP response to output
  * @param IRequest $httpRequest
  * @param IResponse $httpResponse
  * @throws \Drahak\Restful\InvalidArgumentException
  */
 public function send(IRequest $httpRequest, IResponse $httpResponse)
 {
     $httpResponse->setContentType($this->contentType ? $this->contentType : 'application/javascript', 'UTF-8');
     $data = array();
     $data['response'] = $this->data;
     $data['status'] = $httpResponse->getCode();
     $data['headers'] = $httpResponse->getHeaders();
     $callback = $httpRequest->getQuery('jsonp') ? Strings::webalize($httpRequest->getQuery('jsonp'), NULL, FALSE) : '';
     echo $callback . '(' . $this->mapper->stringify($data, $this->isPrettyPrint()) . ');';
 }
Exemplo n.º 10
0
 /**
  * Check allowed methods
  *
  * @throws BadRequestException If method is not supported but another one can be used
  */
 protected function checkAllowedMethods()
 {
     $availableMethods = $this->methods->getOptions($this->request->getUrl());
     if (!$availableMethods || in_array($this->request->method, $availableMethods)) {
         return;
     }
     $allow = implode(', ', $availableMethods);
     $this->response->setHeader('Allow', $allow);
     throw BadRequestException::methodNotSupported('Method not supported. Available methods: ' . $allow);
 }
Exemplo n.º 11
0
 /**
  * @param Nette\Http\IRequest $httpRequest
  * @param Nette\Http\IResponse $httpResponse
  * @throws \Nette\Application\BadRequestException
  */
 public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setContentType($this->contentType);
     $httpResponse->setExpiration(FALSE);
     $callback = $httpRequest->getQuery(self::$callbackName);
     if (is_null($callback)) {
         throw new \Nette\Application\BadRequestException("Invalid JSONP request.");
     }
     echo $callback . "(" . Nette\Utils\Json::encode($this->payload) . ")";
 }
Exemplo n.º 12
0
 /**
  * Sends response to output.
  * @param IRequest   $request
  * @param IResponse  $response
  */
 public function send(IRequest $request, IResponse $response)
 {
     // Set response headers for the file download
     $response->setHeader('Content-Length', $this->stream->getSize());
     $response->setHeader('Content-Type', $this->contentType);
     $response->setHeader('Content-Disposition', 'attachment; filename="' . $this->name . '";');
     while (!$this->stream->eof()) {
         echo $this->stream->read(4000000.0);
     }
     $this->stream->close();
 }
Exemplo n.º 13
0
 /**
  * Sends response to output.
  * @return void
  */
 public function send(IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
 {
     if ($this->error) {
         $this->payload = ['error' => TRUE, 'message' => $this->errorMessage];
     } else {
         $this->payload['error'] = FALSE;
     }
     $httpResponse->setContentType($this->contentType);
     $httpResponse->setExpiration(FALSE);
     echo Json::encode($this->payload);
 }
Exemplo n.º 14
0
 /**
  * Sends response to output.
  *
  * @param \Nette\Http\IRequest $httpRequest
  * @param \Nette\Http\IResponse $httpResponse
  */
 public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
 {
     if (strlen($this->etag)) {
         $httpResponse->setHeader('Etag', $this->etag);
     }
     $httpResponse->setExpiration(\Nette\Http\IResponse::PERMANENT);
     if (($inm = $httpRequest->getHeader('if-none-match')) && $inm == $this->etag) {
         $httpResponse->setCode(\Nette\Http\IResponse::S304_NOT_MODIFIED);
         return;
     }
     $httpResponse->setContentType($this->contentType);
     echo $this->content;
 }
Exemplo n.º 15
0
 function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     if ($this->isAmfRequest($httpRequest)) {
         $httpResponse->setContentType('application/x-amf');
         $this->manager->sendResponse();
     } else {
         $responses = $this->manager->getResponses();
         if (count($responses) === 1) {
             $responses = array_shift($responses);
         }
         $response = new Nette\Application\Responses\JsonResponse($responses);
         $response->send($httpRequest, $httpResponse);
     }
 }
Exemplo n.º 16
0
 /**
  * Sends chunked response to output.
  *
  * @param \MouseOver\Storage\IStorageFile $storageFile  Storage file
  * @param \Nette\Http\IRequest            $httpRequest  HTTP request
  * @param \Nette\Http\IResponse           $httpResponse HTTP response
  *
  * @return void
  */
 protected function sendStorageFile($storageFile, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setHeader('Accept-Ranges', 'bytes');
     $reader = $storageFile->getReader();
     if (preg_match('#^bytes=(\\d*)-(\\d*)\\z#', $httpRequest->getHeader('Range'), $matches)) {
         list(, $start, $end) = $matches;
         if ($start === '') {
             $start = null;
         }
         if ($end === '') {
             $end = null;
         }
         try {
             $reader->setRange($start, $end);
         } catch (\InvalidArgumentException $invalidArgumentException) {
             $httpResponse->setCode(416);
             // requested range not satisfiable
             return;
         }
         $httpResponse->setCode(206);
         $httpResponse->setHeader('Content-Range', 'bytes ' . $reader->getRangeStart() . '-' . $reader->getRangeEnd() . '/' . $reader->getFileSize());
         $reader->setRange($start, $end);
         $httpResponse->setHeader('Content-Length', $reader->getLength());
     } else {
         $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($reader->getFileSize() - 1) . '/' . $reader->getFileSize());
         $httpResponse->setHeader('Content-Length', $reader->getLength());
     }
     while ($reader->hasContent()) {
         echo $reader->read();
     }
 }
Exemplo n.º 17
0
 /**
  * Sends response to output.
  * @param Nette\Http\IRequest  $httpRequest
  * @param Nette\Http\IResponse $httpResponse
  */
 function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setContentType('application/pdf');
     $httpResponse->setHeader('Content-Disposition', 'inline; filename="' . basename($this->filePath) . '"');
     $httpResponse->setHeader('Content-Description', 'File Transfer');
     $httpResponse->setHeader('Connection', 'Keep-Alive');
     $httpResponse->setHeader('Expires', '0');
     $httpResponse->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
     $httpResponse->setHeader('Pragma', 'public');
     $httpResponse->setHeader('Content-Length', filesize($this->filePath));
     readfile($this->filePath);
 }
Exemplo n.º 18
0
 /**
  * Performs a temporarily HTTP header 302 redirect
  *
  * @param string $path Page location path
  *
  * @return void
  *
  * @throws \InvalidArgumentException if location path is invalid
  *
  * @access public
  */
 public function redirectSoft($path)
 {
     if (!empty($path) && is_string($path)) {
         $this->response->redirect($path, 302);
     } else {
         throw new \InvalidArgumentException('A temporarily HTTP header 302 redirect requires a valid location path.');
     }
 }
Exemplo n.º 19
0
 /**
  * Gets the cookie
  *
  * @param string $cookieValue
  */
 protected function createCookie($cookieValue)
 {
     $currentDate = new \DateTime('+1 month');
     // Create cookie object
     $cookie = new Cookie(self::COOKIE_KEY, $cookieValue, $currentDate->format('Y-m-d'));
     // Store cookie in response
     $this->httpResponse->setCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
 }
Exemplo n.º 20
0
 /**
  * @param Translator $translator
  * @return string|NULL
  */
 public function resolve(Translator $translator)
 {
     if (!$this->session->isStarted() && $this->httpResponse->isSent()) {
         trigger_error("The advice of session locale resolver is required but the session has not been started and headers had been already sent. " . "Either start your sessions earlier or disabled the SessionResolver.", E_USER_WARNING);
         return NULL;
     }
     if (empty($this->localeSession->locale)) {
         return NULL;
     }
     $short = array_map(function ($locale) {
         return substr($locale, 0, 2);
     }, $translator->getAvailableLocales());
     if (!in_array(substr($this->localeSession->locale, 0, 2), $short, TRUE)) {
         return NULL;
     }
     return $this->localeSession->locale;
 }
Exemplo n.º 21
0
 /**
  * Sends response to output.
  * @param Nette\Http\IRequest $httpRequest
  * @param Nette\Http\IResponse $httpResponse
  * @return void
  */
 public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setContentType($this->contentType);
     $httpResponse->setExpiration(FALSE);
     $response = array();
     $actual_url = $httpRequest->getUrl()->getAbsoluteUrl();
     date_default_timezone_set('UTC');
     //TODO: -1min
     $date = date('YmdHi');
     $hmac = hash_hmac('sha512', $actual_url . $date, 'PRIVATE_KEY');
     //TODO: na základě public key ($api_key)
     if ($this->payload['api_hash'] !== $hmac) {
         $response['err_code'] = 10;
         $response['err_info'] = $this->errors[10];
     }
     echo Nette\Utils\Json::encode($this->payload);
 }
Exemplo n.º 22
0
 /**
  * Sends the session cookies.
  * @return void
  */
 private function sendCookie()
 {
     if (!headers_sent() && ob_get_level() && ob_get_length()) {
         trigger_error('Possible problem: you are starting session while already having some data in output buffer. This may not work if the outputted data grows. Try starting the session earlier.', E_USER_NOTICE);
     }
     $cookie = $this->getCookieParameters();
     $this->response->setCookie(session_name(), session_id(), $cookie['lifetime'] ? $cookie['lifetime'] + time() : 0, $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly'])->setCookie('nette-browser', $_SESSION['__NF']['B'], Response::BROWSER, $cookie['path'], $cookie['domain']);
 }
Exemplo n.º 23
0
 /**
  * Sends response to output.
  * @param  Nette\Http\IRequest  $httpRequest
  * @param  Nette\Http\IResponse $httpResponse
  * @return void
  */
 public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     /**
      * Disable tracy bar
      */
     if (class_exists('\\Tracy\\Debugger') && property_exists('\\Tracy\\Debugger', 'productionMode')) {
         \Tracy\Debugger::$productionMode = TRUE;
     }
     /**
      * Set Content-Type header
      */
     $httpResponse->setContentType($this->contentType, $this->output_encoding);
     /**
      * Set Content-Disposition header
      */
     $httpResponse->setHeader('Content-Disposition', 'attachment' . '; filename="' . $this->name . '"');
     /*. '; filename*=' . $this->output_encoding . '\'\'' . rawurlencode($this->name));*/
     /**
      * Set other headers
      */
     foreach ($this->headers as $key => $value) {
         $httpResponse->setHeader($key, $value);
     }
     if (function_exists('ob_start')) {
         ob_start();
     }
     /**
      * Output data
      */
     if ($this->include_bom && strtolower($this->output_encoding) == 'utf-8') {
         echo "";
     }
     $delimiter = '"' . $this->delimiter . '"';
     foreach ($this->data as $row) {
         if (strtolower($this->output_encoding) == 'utf-8') {
             echo '"' . implode($delimiter, (array) $row) . '"';
         } else {
             echo iconv('UTF-8', $this->output_encoding, '"' . implode($delimiter, (array) $row) . '"');
         }
         echo "\r\n";
     }
     if (function_exists('ob_end_flush')) {
         ob_end_flush();
     }
 }
Exemplo n.º 24
0
 /**
  * Attempts to cache the sent entity by its last modification date.
  * @param  string|int|DateTime  last modified time
  * @param  string strong entity tag validator
  * @param  mixed  optional expiration time
  * @return void
  * @throws Nette\Application\AbortException
  */
 public function lastModified($lastModified, $etag = NULL, $expire = NULL)
 {
     if ($expire !== NULL) {
         $this->httpResponse->setExpiration($expire);
     }
     if (!$this->httpContext->isModified($lastModified, $etag)) {
         $this->terminate();
     }
 }
Exemplo n.º 25
0
 /**
  * Sends response to output.
  * @return void
  */
 public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
 {
     $httpResponse->setContentType($this->contentType);
     $httpResponse->setHeader('Content-Disposition', ($this->forceDownload ? 'attachment' : 'inline') . '; filename="' . $this->name . '"');
     $output = NULL;
     if ($this->precalculateFileSize) {
         ob_start();
         Nette\Utils\Callback::invokeArgs($this->outputGenerator);
         $output = ob_get_clean();
         $filesize = $length = strlen($output);
     }
     if ($this->resuming && $this->precalculateFileSize) {
         $httpResponse->setHeader('Accept-Ranges', 'bytes');
         if (preg_match('#^bytes=(\\d*)-(\\d*)\\z#', $httpRequest->getHeader('Range'), $matches)) {
             list(, $start, $end) = $matches;
             if ($start === '') {
                 $start = max(0, $filesize - $end);
                 $end = $filesize - 1;
             } elseif ($end === '' || $end > $filesize - 1) {
                 $end = $filesize - 1;
             }
             if ($end < $start) {
                 $httpResponse->setCode(416);
                 // requested range not satisfiable
                 return;
             }
             $httpResponse->setCode(206);
             $httpResponse->setHeader('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $filesize);
             $length = $end - $start + 1;
         } else {
             $httpResponse->setHeader('Content-Range', 'bytes 0-' . ($filesize - 1) . '/' . $filesize);
         }
     }
     if ($this->precalculateFileSize) {
         $httpResponse->setHeader('Content-Length', $length);
     }
     if (isset($start)) {
         echo substr($output, $start, $length);
     } elseif (isset($output)) {
         echo $output;
     } else {
         Nette\Utils\Callback::invoke($this->outputGenerator);
     }
 }
Exemplo n.º 26
0
 /**
  * Terminates current request and sends HTTP response with
  * code and optionaly payload formatted for requested output content type
  *
  * @throws AbortException
  */
 public function terminateWithCode($code, $payload = NULL)
 {
     $this->httpResponse->setCode($code);
     if (func_num_args() == 1) {
         $this->response = $this->createResponse();
     } else {
         $this->response = $this->createResponse($payload);
     }
     throw new AbortException();
 }
Exemplo n.º 27
0
 /**
  * Attempts to cache the sent entity by its last modification date.
  * @param  string|int|\DateTimeInterface  last modified time
  * @param  string strong entity tag validator
  * @param  mixed  optional expiration time
  * @return void
  * @throws Nette\Application\AbortException
  */
 public function lastModified($lastModified, $etag = NULL, $expire = NULL)
 {
     if ($expire !== NULL) {
         $this->httpResponse->setExpiration($expire);
     }
     $helper = new Http\Context($this->httpRequest, $this->httpResponse);
     if (!$helper->isModified($lastModified, $etag)) {
         $this->terminate();
     }
 }
Exemplo n.º 28
0
 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');
         }
         $this->httpResponse->addHeader('Vary', 'X-Requested-With');
     }
 }
Exemplo n.º 29
0
 public function doProcessException($e)
 {
     if (!$e instanceof BadRequestException && $this->httpResponse instanceof Response) {
         $this->httpResponse->warnOnBuffer = false;
     }
     if (!$this->httpResponse->isSent()) {
         $this->httpResponse->setCode($e instanceof BadRequestException ? $e->getCode() ?: 404 : 500);
     }
     $requests = $this->getRequests();
     $request = end($requests) ?: $this->initialRequest;
     $args = ['exception' => $e, 'request' => $request];
     $errorPresenter = $request ? $this->findErrorPresenter($request->getPresenterName()) : $this->errorPresenter;
     if ($this->getPresenter() instanceof Presenter) {
         try {
             $this->getPresenter()->forward(":{$errorPresenter}:", $args);
         } catch (AbortException $_) {
             $this->processRequest($this->getPresenter()->getLastCreatedRequest());
         }
     } else {
         $this->processRequest(new Request($errorPresenter, Request::FORWARD, $args));
     }
 }
Exemplo n.º 30
0
 /**
  * Destroy the current session
  */
 public function destroySession()
 {
     $this->accessToken = NULL;
     $this->signedRequest = NULL;
     $this->user = NULL;
     $this->session->clearAll();
     // Javascript sets a cookie that will be used in getSignedRequest that we need to clear if we can
     $cookieName = $this->config->getSignedRequestCookieName();
     if (array_key_exists($cookieName, $this->httpRequest->getCookies())) {
         $this->httpResponse->deleteCookie($cookieName, '/', $this->getBaseDomain());
         unset($_COOKIE[$cookieName]);
     }
 }