Exemplo n.º 1
0
	/**
	 * Maps HTTP request to a Request object.
	 * @param  IHttpRequest
	 * @return NPresenterRequest|NULL
	 */
	public function match(IHttpRequest $httpRequest)
	{
		if ($httpRequest->getUrl()->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 NPresenterRequest(
			$presenter,
			$httpRequest->getMethod(),
			$params,
			$httpRequest->getPost(),
			$httpRequest->getFiles(),
			array(NPresenterRequest::SECURED => $httpRequest->isSecured())
		);
	}
Exemplo n.º 2
0
 function match(IHttpRequest $httpRequest)
 {
     $url = $httpRequest->getUrl();
     $lang = $this->getDefaultLang();
     $langs = Environment::getVariable("langs");
     $langDomains = Environment::getVariable("langDomains");
     //domains for languages or just directory prefix?
     if (count($langDomains)) {
         $path = '//' . $url->getHost() . $url->getPath();
         foreach ($langDomains as $lang => $pattern) {
             $pattern = preg_quote($pattern, '~');
             $pattern = preg_replace('~^//(www\\.)?~', '//(www\\.)?', $pattern);
             //www not mandatory
             if (preg_match("~^{$pattern}~", $path, $m)) {
                 //matching absolute path
                 $seoname = substr($path, strlen($m[0]));
                 //what follows
                 break;
             }
         }
     } else {
         //just language prefixes
         $path = $url->pathInfo;
         foreach ($langs as $lang => $txt) {
             if (preg_match("~^{$lang}/~", $path, $m)) {
                 //matching relative path
                 $seoname = substr($path, strlen($m[0]));
                 //what follows
                 break;
             }
         }
     }
     if (!isset($seoname)) {
         //default language possible without prefix
         $keys = array_keys($langs);
         $lang = array_shift($keys);
         $seoname = $url->pathInfo;
     }
     if (substr($seoname, -1) == '/') {
         $seoname = substr($seoname, 0, -1);
     }
     $page = PagesModel::getPageBySeoname('/' . $seoname, $lang);
     //just one level
     if (!$page) {
         if (preg_match('~^p([0-9]+)(-|$)~', $seoname, $matches)) {
             $page = PagesModel::getPageById($matches[1], $lang);
             if (!$page) {
                 return NULL;
             }
         } else {
             return NULL;
         }
     }
     $params = array();
     $params += $httpRequest->getQuery();
     $params['id_page'] = $page['id_page'];
     $params['lang'] = $lang;
     return new PresenterRequest(self::PRESENTER, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured()));
 }
Exemplo n.º 3
0
 function match(IHttpRequest $httpRequest)
 {
     $path = $httpRequest->getUrl()->getPath();
     //look for redirect record
     $newurl = RedirectModel::getByOldUrl($path);
     //fix pathinfo bug - strip "/index.php/" from url
     if (!$newurl and preg_match('~^/index\\.php~', $path)) {
         $newurl = preg_replace('~^/index\\.php~', '', $path);
     }
     if ($newurl) {
         RedirectModel::hit($path);
         if ($p = RedirectModel::parseHashIdLang($newurl)) {
             $newurl = Environment::getLinkHelper()->pageLink($p[0], $p[1]);
             if ($newurl == NULL) {
                 return NULL;
             }
         }
         header("Location: {$newurl}", true, 301);
         //TODO (ask) if comented, output twice :/ better?
         echo "Moved permanetly to <a href='{$newurl}'>{$newurl}</a>";
         exit;
     }
     return NULL;
 }
Exemplo n.º 4
0
	/**
	 * Maps HTTP request to a Request object.
	 * @param  IHttpRequest
	 * @return NPresenterRequest|NULL
	 */
	public function match(IHttpRequest $httpRequest)
	{
		// combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults

		// 1) URL MASK
		$url = $httpRequest->getUrl();

		if ($this->type === self::HOST) {
			$path = '//' . $url->getHost() . $url->getPath();

		} elseif ($this->type === self::RELATIVE) {
			$basePath = $url->getBasePath();
			if (strncmp($url->getPath(), $basePath, strlen($basePath)) !== 0) {
				return NULL;
			}
			$path = (string) substr($url->getPath(), strlen($basePath));

		} else {
			$path = $url->getPath();
		}

		if ($path !== '') {
			$path = rtrim($path, '/') . '/';
		}

		if (!$matches = NStrings::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]])) { // applies filterTable only to scalar parameters
					$params[$name] = $meta[self::FILTER_TABLE][$params[$name]];

				} elseif (isset($meta[self::FILTER_TABLE]) && !empty($meta[self::FILTER_STRICT])) {
					return NULL; // rejected by filterTable

				} elseif (isset($meta[self::FILTER_IN])) { // applies 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 Request
		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 NPresenterRequest(
			$presenter,
			$httpRequest->getMethod(),
			$params,
			$httpRequest->getPost(),
			$httpRequest->getFiles(),
			array(NPresenterRequest::SECURED => $httpRequest->isSecured())
		);
	}