public function match(IHttpRequest $httpRequest) { $path = substr($httpRequest->getUri()->getPath(), strlen($httpRequest->getUri()->getBasePath())); foreach ($this->getData() as $id => $url) { if ($this->prefix . $url == $path || $this->prefix . $url . "/" == $path) { $params = $httpRequest->getQuery(); $params["id"] = $id; $params["action"] = $this->action; return new PresenterRequest($this->presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array('secured' => $httpRequest->isSecured())); } } return null; }
/** * Maps HTTP request to a PresenterRequest object. * @param Nette\Web\IHttpRequest * @return PresenterRequest|NULL */ public function match(Nette\Web\IHttpRequest $httpRequest) { if ($httpRequest->getUri()->getPathInfo() !== '') { return NULL; } // combine with precedence: get, (post,) defaults $params = $httpRequest->getQuery(); $params += $this->defaults; if (!isset($params[self::PRESENTER_KEY])) { throw new \InvalidStateException('Missing presenter.'); } $presenter = $this->module . $params[self::PRESENTER_KEY]; unset($params[self::PRESENTER_KEY]); return new PresenterRequest( $presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured()) ); }
/** * Constructs absolute URL from PresenterRequest object. * @param Nette\Web\IHttpRequest * @param PresenterRequest * @return string|NULL */ public function constructUrl(PresenterRequest $appRequest, Nette\Web\IHttpRequest $httpRequest) { $params = $appRequest->getParams(); // presenter name $presenter = $appRequest->getPresenterName(); if (strncasecmp($presenter, $this->module, strlen($this->module)) === 0) { $params[self::PRESENTER_KEY] = substr($presenter, strlen($this->module)); } else { return NULL; } // remove default values; NULL values are retain foreach ($this->defaults as $key => $value) { if (isset($params[$key]) && $params[$key] == $value) { // intentionally == unset($params[$key]); } } $uri = $httpRequest->getUri(); $uri = ($this->flags & self::SECURED ? 'https://' : 'http://') . $uri->getAuthority() . $uri->getScriptPath(); $sep = ini_get('arg_separator.input'); $query = http_build_query($params, '', $sep ? $sep[0] : '&'); if ($query != '') { // intentionally == $uri .= '?' . $query; } return $uri; }
/** * Maps HTTP request to a PresenterRequest object. * * @author Jan Tvrdík * @param IHttpRequest * @return PresenterRequest|NULL */ public function match(IHttpRequest $httpRequest) { $this->httpRequest = $httpRequest; $path = rtrim($httpRequest->getUri()->getRelativeUri(), '/'); $tmp = $this->resolvePath($path); if ($tmp === FALSE) return NULL; list($page, $lang) = $tmp; return new PresenterRequest( $this->presenter, $httpRequest->getMethod(), array('page' => $page, 'lang' => $lang), $httpRequest->getPost(), $httpRequest->getFiles(), array('secured' => $httpRequest->isSecured()) ); }
/** * Maps HTTP request to a PresenterRequest object. * * @author Jan Tvrdík * @param IHttpRequest * @return PresenterRequest|NULL */ public function match(IHttpRequest $httpRequest) { $path = rtrim($httpRequest->getUri()->getRelativeUri(), '/'); $page = ($path === '' ? $this->homepage : $path); $templateLocator = Env::getService('TemplateLocator'); if (!$templateLocator->existsPage($page)) { $page .= '/' . $this->defaultPage; if (!$templateLocator->existsPage($page)) { return NULL; } } return new PresenterRequest( $this->presenter, $httpRequest->getMethod(), array('page' => $page), $httpRequest->getPost(), $httpRequest->getFiles(), array('secured' => $httpRequest->isSecured()) ); }
/** * Constructs absolute URL from PresenterRequest object. * @param Nette\Web\IHttpRequest * @param PresenterRequest * @return string|NULL */ public function constructUrl(PresenterRequest $appRequest, Nette\Web\IHttpRequest $httpRequest) { if ($this->flags & self::ONE_WAY) { return NULL; } $params = $appRequest->getParams(); $metadata = $this->metadata; $presenter = $appRequest->getPresenterName(); $params[self::PRESENTER_KEY] = $presenter; if (isset($metadata[self::MODULE_KEY])) { // try split into module and [submodule:]presenter parts $module = $metadata[self::MODULE_KEY]; if (isset($module['fixity']) && strncasecmp($presenter, $module[self::VALUE] . ':', strlen($module[self::VALUE]) + 1) === 0) { $a = strlen($module[self::VALUE]); } else { $a = strrpos($presenter, ':'); } if ($a === FALSE) { $params[self::MODULE_KEY] = ''; } else { $params[self::MODULE_KEY] = substr($presenter, 0, $a); $params[self::PRESENTER_KEY] = substr($presenter, $a + 1); } } foreach ($metadata as $name => $meta) { if (!isset($params[$name])) { continue; } // retains NULL values if (isset($meta['fixity'])) { if (is_scalar($params[$name]) && strcasecmp($params[$name], $meta[self::VALUE]) === 0) { // remove default values; NULL values are retain unset($params[$name]); continue; } elseif ($meta['fixity'] === self::CONSTANT) { return NULL; // missing or wrong parameter '$name' } } if (!is_scalar($params[$name])) { } elseif (isset($meta['filterTable2'][$params[$name]])) { $params[$name] = $meta['filterTable2'][$params[$name]]; } elseif (isset($meta[self::FILTER_OUT])) { $params[$name] = call_user_func($meta[self::FILTER_OUT], $params[$name]); } if (isset($meta[self::PATTERN]) && !preg_match($meta[self::PATTERN], rawurldecode($params[$name]))) { return NULL; // pattern not match } } // compositing path $sequence = $this->sequence; $brackets = array(); $required = 0; $uri = ''; $i = count($sequence) - 1; do { $uri = $sequence[$i] . $uri; if ($i === 0) { break; } $i--; $name = $sequence[$i]; $i--; // parameter name if ($name === ']') { // opening optional part $brackets[] = $uri; } elseif ($name[0] === '[') { // closing optional part $tmp = array_pop($brackets); if ($required < count($brackets) + 1) { // is this level optional? if ($name !== '[!') { // and not "required"-optional $uri = $tmp; } } else { $required = count($brackets); } } elseif ($name[0] === '?') { // "foo" parameter continue; } elseif (isset($params[$name]) && $params[$name] != '') { // intentionally == $required = count($brackets); // make this level required $uri = $params[$name] . $uri; unset($params[$name]); } elseif (isset($metadata[$name]['fixity'])) { // has default value? $uri = $metadata[$name]['defOut'] . $uri; } else { return NULL; // missing parameter '$name' } } while (TRUE); // build query string if ($this->xlat) { $params = self::renameKeys($params, $this->xlat); } $sep = ini_get('arg_separator.input'); $query = http_build_query($params, '', $sep ? $sep[0] : '&'); if ($query != '') { $uri .= '?' . $query; } // intentionally == // absolutize path if ($this->type === self::RELATIVE) { $uri = '//' . $httpRequest->getUri()->getAuthority() . $httpRequest->getUri()->getBasePath() . $uri; } elseif ($this->type === self::PATH) { $uri = '//' . $httpRequest->getUri()->getAuthority() . $uri; } if (strpos($uri, '//', 2) !== FALSE) { return NULL; // TODO: implement counterpart in match() ? } $uri = ($this->flags & self::SECURED ? 'https:' : 'http:') . $uri; return $uri; }
/** * Maps HTTP request to a PresenterRequest object. * @param Nette\Web\IHttpRequest * @return PresenterRequest|NULL */ public function match(Nette\Web\IHttpRequest $httpRequest) { // combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults // 1) URL MASK $uri = $httpRequest->getUri(); if ($this->type === self::HOST) { $path = '//' . $uri->getHost() . $uri->getPath(); } elseif ($this->type === self::RELATIVE) { $basePath = $uri->getBasePath(); if (strncmp($uri->getPath(), $basePath, strlen($basePath)) !== 0) { return NULL; } $path = (string) substr($uri->getPath(), strlen($basePath)); } else { $path = $uri->getPath(); } if ($path !== '') { $path = rtrim($path, '/') . '/'; } if (!$matches = String::match($path, $this->re)) { // stop, not matched return NULL; } // deletes numeric keys, restore '-' chars $params = array(); foreach ($matches as $k => $v) { if (is_string($k) && $v !== '') { $params[str_replace('___', '-', $k)] = $v; // trick } } // 2) CONSTANT FIXITY foreach ($this->metadata as $name => $meta) { if (isset($params[$name])) { //$params[$name] = $this->flags & self::CASE_SENSITIVE === 0 ? strtolower($params[$name]) : */$params[$name]; // strtolower damages UTF-8 } elseif (isset($meta['fixity']) && $meta['fixity'] !== self::OPTIONAL) { $params[$name] = NULL; // cannot be overwriten in 3) and detected by isset() in 4) } } // 3) QUERY if ($this->xlat) { $params += self::renameKeys($httpRequest->getQuery(), array_flip($this->xlat)); } else { $params += $httpRequest->getQuery(); } // 4) APPLY FILTERS & FIXITY foreach ($this->metadata as $name => $meta) { if (isset($params[$name])) { if (!is_scalar($params[$name])) { } elseif (isset($meta[self::FILTER_TABLE][$params[$name]])) { // applyies filterTable only to scalar parameters $params[$name] = $meta[self::FILTER_TABLE][$params[$name]]; } elseif (isset($meta[self::FILTER_IN])) { // applyies filterIn only to scalar parameters $params[$name] = call_user_func($meta[self::FILTER_IN], (string) $params[$name]); if ($params[$name] === NULL && !isset($meta['fixity'])) { return NULL; // rejected by filter } } } elseif (isset($meta['fixity'])) { $params[$name] = $meta[self::VALUE]; } } // 5) BUILD PresenterRequest if (!isset($params[self::PRESENTER_KEY])) { throw new \InvalidStateException('Missing presenter in route definition.'); } if (isset($this->metadata[self::MODULE_KEY])) { if (!isset($params[self::MODULE_KEY])) { throw new \InvalidStateException('Missing module in route definition.'); } $presenter = $params[self::MODULE_KEY] . ':' . $params[self::PRESENTER_KEY]; unset($params[self::MODULE_KEY], $params[self::PRESENTER_KEY]); } else { $presenter = $params[self::PRESENTER_KEY]; unset($params[self::PRESENTER_KEY]); } return new PresenterRequest( $presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured()) ); }