private function processRequest() { if ($this->httpRequest->isPost() && $this->httpRequest->isAjax() && $this->httpRequest->getHeader(self::XHR_HEADER)) { $data = json_decode(file_get_contents('php://input'), true); if ($data && isset($data[self::AJAX_ACTION_KEY])) { switch ($data[self::AJAX_ACTION_KEY]) { case self::AJAX_ACTION_LOAD: $message = $data[self::AJAX_MESSAGE_KEY]; if (!$this->translator->hasTranslation($message)) { throw new \Exception(); } $data = array('translation' => $this->translator->findTranslation($message)); $this->httpResponse->setContentType('application/json'); echo json_encode($data); break; case self::AJAX_ACTION_EDIT: $message = $data[self::AJAX_MESSAGE_KEY]; $translation = $data[self::AJAX_TRANSLATION_KEY]; if (!$this->translator->hasTranslation($message)) { throw new \Exception(); } $info = $this->translator->getMessageInfo($message); $data = $this->translator->_loadCategory($info['path'], $info['category']); $data[$info['name']] = $translation; $this->translator->getLoader()->save($info['path'], $info['category'], $this->translator->getLanguage(), $data); $this->httpResponse->setContentType('application/json'); break; default: throw new \Exception(); break; } } exit; } }
protected function startup() { parent::startup(); if ($this->authentication->getCredential() !== substr($this->httpRequest->getHeader('authorization'), 6)) { //throw new AuthenticationException(); } }
/** * @return Webhooks\Data * @throws Utils\JsonException In case request body is not valid JSON * @throws Utils\AssertionException In case request body is missing required keys */ public function create() { $data = Utils\Json::decode($this->request->getRawBody(), Utils\Json::FORCE_ARRAY); Utils\Validators::assert($data, 'array', 'request body'); Utils\Validators::assertField($data, 'id', 'string', 'item % in request body'); Utils\Validators::assertField($data, 'suspectId', 'string', 'item % in request body'); Utils\Validators::assertField($data, 'externalId', 'string|null', 'item % in request body'); Utils\Validators::assertField($data, 'user', 'array', 'item % in request body'); Utils\Validators::assertField($data['user'], 'id', 'int', 'item % in user hash within request body'); Utils\Validators::assertField($data['user'], 'salescode', 'string', 'item % in user hash within request body'); Utils\Validators::assertField($data['user'], 'name', 'string', 'item % in user hash within request body'); Utils\Validators::assertField($data, 'data', 'array', 'item % in request body'); return new Webhooks\Data($id = $this->request->getHeader(Webhooks\Headers::IDENTIFIER), $interactionId = $data['id'], $suspectId = $data['suspectId'], $externalId = $data['externalId'], $campaignId = $this->request->getHeader(Webhooks\Headers::CAMPAIGN), $signature = $this->request->getHeader(Webhooks\Headers::SIGNATURE), $event = $this->request->getHeader(Webhooks\Headers::EVENT), $user = $data['user'], $suspectData = $data['data']); }
/** * Handles an incomuing request and saves the data if necessary. */ private function processRequest() { if ($this->httpRequest->isPost() && $this->httpRequest->isAjax() && $this->httpRequest->getHeader($this->xhrHeader)) { $data = json_decode(file_get_contents('php://input')); if ($data) { if ($this->debugMode === true && $this->sessionStorage) { $stack = isset($this->sessionStorage['stack']) ? $this->sessionStorage['stack'] : array(); } $this->translator->lang = $data->{$this->languageKey}; $file = $data->{$this->fileKey}; unset($data->{$this->languageKey}, $data->{$this->fileKey}); foreach ($data as $string => $value) { $this->translator->setTranslation($string, $value, $file); if ($this->debugMode === true && $this->sessionStorage && isset($stack[$string])) { unset($stack[$string]); } } $this->translator->save($file); if ($this->debugMode === true && $this->sessionStorage) { $this->sessionStorage['stack'] = $stack; } } exit; } }
/** * @param string $language * @param Basket $basket * @throws AbortException */ public function createOrder($language, Basket $basket) { $customer = array('firstname' => $basket->getName(), 'lastname' => $basket->getSurname(), 'telephone' => $basket->getPhone(), 'email' => $basket->getEmail(), 'address' => $basket->getStreet(), 'city' => $basket->getCity(), 'postcode' => $basket->getPostcode(), 'comment' => $basket->getNote(), 'ip' => $this->request->getRemoteAddress(), 'user_agent' => $this->request->getHeader('User-Agent'), 'accept_language' => ''); $products = array(); foreach ($basket->getItems() as $item) { /** @var $item BasketItem */ $products[$item->getItemId()] = array('id' => $item->getItemId(), 'quantity' => $item->getQuantity()); } $order = array('language' => $language, 'products' => $products, 'customer' => $customer); //$response = $this->caller->call(Caller::GET, 'Fake', 'delay', array('seconds' => 2)); sleep(2); /* if ($response['status'] != "ok") { throw new AbortException; } */ }
/** * @return string|null */ public function getHeaders() { if (isset($this->config['headers'])) { if ($this->config['headers'] === '*') { $headers = array('origin', 'content-type', 'authorization'); /* * Because OPTIONS requests aren't contain declared headers but send list of * headers in Access-Control-Request-Headers header */ $expectedHeaders = $this->httpRequest->getHeader("Access-Control-Request-Headers", []); if (!empty($expectedHeaders)) { $expectedHeaders = array_map('trim', explode(",", $expectedHeaders)); } $this->config['headers'] = array_merge($headers, array_keys((array) $this->httpRequest->getHeaders()), $expectedHeaders); } if (is_array($this->config['headers'])) { $this->config['headers'] = implode(',', $this->config['headers']); } return (string) $this->config['headers']; } }
/** * Detects language from the Accept-Language header. * This method uses the code from Nette\Http\Request::detectLanguage. * @link https://github.com/nette/http/blob/0d9ef49051fba799148ef877dd32928a68731766/src/Http/Request.php#L294-L326 * @author David Grudl * @param Kdyby\Translation\Translator $translator * @return string|NULL */ public function resolve(Kdyby\Translation\Translator $translator) { $header = $this->httpRequest->getHeader('Accept-Language'); if (!$header) { return NULL; } $langs = []; foreach ($translator->getAvailableLocales() as $locale) { $langs[] = $locale; if (strlen($locale) > 2) { $langs[] = substr($locale, 0, 2); } } if (!$langs) { return NULL; } $s = strtolower($header); // case insensitive $s = strtr($s, '_', '-'); // cs_CZ means cs-CZ rsort($langs); // first more specific preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\\s,;=]+)?\\s*(?:;\\s*q=([0-9.]+))?#', $s, $matches); if (!$matches[0]) { return NULL; } $max = 0; $lang = NULL; foreach ($matches[1] as $key => $value) { $q = $matches[2][$key] === '' ? 1.0 : (double) $matches[2][$key]; if ($q > $max) { $max = $q; $lang = $value; } } return $lang; }
/** * @param HttpRequest $request * @return string */ protected function detectMethod(HttpRequest $request) { $requestMethod = $request->getMethod(); if ($requestMethod !== 'POST') { return $request->getMethod(); } $method = $request->getHeader(self::HTTP_HEADER_OVERRIDE); if (isset($method)) { return strtoupper($method); } $method = $request->getQuery(self::QUERY_PARAM_OVERRIDE); if (isset($method)) { return strtoupper($method); } return $requestMethod; }
/** * @param Nette\Http\Request $request * @return array Http parametry hlasujiciho uzivatele */ protected function getRequestInfo($request) { $result = ['ip' => $request->getRemoteAddress(), 'agent' => $request->getHeader('user-agent'), 'cookie' => $request->getCookie('nette-browser')]; return $result; }
/** * @param \Nette\Http\Request */ public function log(\Nette\Http\Request $req) { $this->server->send('access', array('ua' => $req->getHeader('User-Agent'), 'ip' => $req->getRemoteAddress(), 'host' => $req->getRemoteHost(), 'method' => $req->getMethod(), 'url' => (string) $req->getUrl(), 'memory' => function_exists('memory_get_peak_usage') ? number_format(memory_get_peak_usage() / 1000000, 2, '.', ' ') : 'n/a', 'time' => number_format((microtime(TRUE) - \Nette\Diagnostics\Debugger::$time) * 1000, 1, '.', ' '), 'referer' => (string) $req->getHeader('Referer'))); }
/** * @param \Nette\Http\Request $request * @return string */ private function detectFormat(HttpRequest $request) { $header = $request->getHeader('Accept'); // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html foreach ($this->formats as $format => $fullFormatName) { $fullFormatName = Strings::replace($fullFormatName, '/\\//', '\\/'); if (Strings::match($header, "/{$fullFormatName}/")) { return $format; } } // Try retrieve fallback from URL. $path = $request->getUrl()->getPath(); $formats = array_keys($this->formats); $formats = implode('|', $formats); if (Strings::match($path, "/\\.({$formats})\$/")) { list($path, $format) = explode('.', $path); return $format; } return $this->defaultFormat; }
public function getHeader($header, $default = null) { return $this->request->getHeader($header, $default); }
public function isAMFRequest() { return in_array($this->httpRequest->getHeader('Content-type'), AMFRequestFactory::$contentTypes); }
/** * @param \Nette\Http\Request $request * @return string */ private function detectFormat(HttpRequest $request) { // Try retrieve fallback from URL. $path = $request->getUrl()->getPath(); $formats = array_keys($this->formats); $formats = implode('|', $formats); if (Strings::match($path, "/\\.({$formats})\$/")) { list($path, $format) = explode('.', $path); return $format; } $header = $request->getHeader('Content-Type'); foreach ($this->formats as $format => $fullFormatName) { $fullFormatName = Strings::replace($fullFormatName, '/\\//', '\\/'); if (Strings::match($header, "/{$fullFormatName}/")) { return $format; } } }
/** * Helper * * @param string * @return string|NULL */ private function getHeader($name) { return $this->httpRequest->getHeader($name); }