Пример #1
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()));
 }