URI = $uri; $this->explodeURI(); $this->getLang(); } /* private function checkDomain($uri) { if (dirname(__FILE__) == $_SERVER['DOCUMENT_ROOT']) { $uri = substr($uri, 1, strlen($uri)); //first slash } else { $tmpURI = substr(dirname(__FILE__), -strpos(strrev(dirname(__FILE__)), strrev($_SERVER['DOCUMENT_ROOT']))); $uri = substr($uri, strlen($tmpURI)); if ($uri[0] == '/') { $uri = substr($uri, 1); //first slash } } if ($uri[strlen($uri)-1] == '/') { $uri = substr($uri, 0, strlen($uri)-1); //last slash } return $uri; } */ private function explodeURI() { $this->SEGMENTS = explode('/', $this->URI); } public function URISegment($segment) { return $this->SEGMENTS[(int)$segment]; } private function getLang() { if (strlen($this->SEGMENTS[0]) == 2 && in_array($this->SEGMENTS[0], Wordpress::$langArr)) { define('CURRENT_LANG',$this->SEGMENTS[0]); $this->SEGMENTS = array_shift($this->SEGMENTS); } else { define('CURRENT_LANG', 'ru'); } } } class Router extends URL_parser { private $mainScheme; private $usageFlag; private $host; private $articleID; function __construct($usageFlag = false, $customURI='') { $this->usageFlag = $usageFlag; parent::__construct($customURI); } public function init($currsite) { $this->host = $currsite['domain']; define('SECTION_PREF', $currsite['main_section_pref']); define('THEME_PREF', $currsite['main_theme_pref']); define('MAIN_MODE', $currsite['main_mode']); define('ARCHIVE_PREF', $currsite['main_archive_pref']); define('ARCHIVE_MODE', $currsite['archive_mode']); define('ARCHIVE_DELIM', $currsite['archive_delim']); define('ORDER_DATE_PREF', $currsite['main_orderbydate']); define('ORDER_RATING_PREF', $currsite['main_orderbyrating']); define('SINGLE_ART_PREF', $currsite['main_articles_pref']); define('SINGLE_ART_MODE', $currsite['main_singleart_mode']); define('PAGE_TITLE_MODE', $currsite['page_title_mode']); define('PAGE_EXTENSION', $currsite['page_extension']); define('PAGE_PREF', $currsite['page_pref']); define('PAGE_RANDOM_LEFT', $currsite['page_random_left']); define('RSS_PREF', 'rss'); define('SITEMAP_PREF', 'sitemap.xml'); define('SEARCH_PREF', 'search'); define('ADDCOMMENT_PREF', 'comment'); define('TAGS_PREF', 'tags'); define('BLOCK_PREF', 'block'); define('PAGER_PREF', $currsite['main_pager_pref']); define('WIKI_ART_ANCHOR', $currsite['wiki_art_anchor']); define('CUSTOM_PREF', 'custom'); $this->mainScheme = $this->setMainScheme(); $continue = 0; switch ($this->SEGMENTS[0]) { case '': $continue = 1; break; case SECTION_PREF: $continue = 1; break; case CUSTOM_PREF: $continue = 1; break; case THEME_PREF: $continue = 1; break; case RSS_PREF: $continue = 1; break; case BLOCK_PREF: $continue = 1; break; case SEARCH_PREF: $continue = 1; break; case ADDCOMMENT_PREF: $continue = 1; break; case TAGS_PREF: $continue = 1; break; case ARCHIVE_PREF: $continue = 1; break; case ORDER_DATE_PREF: $continue = 1; break; case ORDER_RATING_PREF: $continue = 1; break; case 'wiki': $continue = 1; break; case 'sitemap.xml': $continue = 1; break; } if (!strstr ($this->URI, PAGE_EXTENSION) && $continue == 0) { $this->_404(); } define('CURRENT_PAGER', $this->getPager()); if ($this->getMainScheme() == 'ARCHIVES') { if (ARCHIVE_MODE == 1) { preg_match('/(\d{4})/', $this->SEGMENTS[1], $year); preg_match('/(\d{2})/', $this->SEGMENTS[2], $month); preg_match('/(\d{2})/', $this->SEGMENTS[3], $day); define('ARCHIVE_YEAR', (int)$year[0]); define('ARCHIVE_MONTH', (int)$month[0]); define('ARCHIVE_DAY', (int)$day[0]); } else { $archive_uri = $this->SEGMENTS[1]; preg_match_all('/(\d{4})\D*(\d{0,2})\D*(\d{0,2})/', $archive_uri, $archive); // echo var_dump($archive); // echo '
year: '.(int)$archive[1][0].' month: '.(int)$archive[2][0]. ' day: '.(int)$archive[3][0].'
'; define('ARCHIVE_YEAR', (int)$archive[1][0]); define('ARCHIVE_MONTH', (int)$archive[2][0]); define('ARCHIVE_DAY', (int)$archive[3][0]); } return; } if ($this->getMainScheme() == THEME_PREF) { if (MAIN_MODE == 1) { define('CURRENT_ORDER', $this->SEGMENTS[2]); define('THEME_URI', $this->SEGMENTS[1]); } elseif (MAIN_MODE == 2) { define('CURRENT_ORDER', $this->SEGMENTS[1]); define('THEME_URI', $this->SEGMENTS[2]); } elseif(MAIN_MODE == 3) { define('CURRENT_ORDER', $this->SEGMENTS[0]); define('THEME_URI', $this->SEGMENTS[2]); } return; } if ($this->getMainScheme() == SECTION_PREF) { if (MAIN_MODE == 1) { define('CURRENT_ORDER', $this->SEGMENTS[2]); define('SECTION_URI', $this->SEGMENTS[1]); } elseif (MAIN_MODE == 2) { define('CURRENT_ORDER', $this->SEGMENTS[1]); define('SECTION_URI', $this->SEGMENTS[2]); } elseif(MAIN_MODE == 3) { define('CURRENT_ORDER', $this->SEGMENTS[0]); define('SECTION_URI', $this->SEGMENTS[2]); } return; } } public function getURI() { return $this->URI; } public function getMainScheme() { return $this->mainScheme; } private function setMainScheme() { if ($this->SEGMENTS[0] == RSS_PREF) { return 'RSS'; } elseif ($this->SEGMENTS[0] == SEARCH_PREF) { return 'SEARCH'; } elseif ($this->SEGMENTS[0] == CUSTOM_PREF) { return 'CUSTOM'; } elseif ($this->SEGMENTS[0] == ADDCOMMENT_PREF) { return 'COMMENT'; } elseif ($this->SEGMENTS[0] == BLOCK_PREF) { return 'BLOCK'; } elseif ($this->SEGMENTS[0] == TAGS_PREF) { return 'TAGS'; } elseif ($this->SEGMENTS[0] == SITEMAP_PREF) { return 'SITEMAP'; } elseif ($this->SEGMENTS[0] == ARCHIVE_PREF) { return 'ARCHIVES'; } elseif ($this->SEGMENTS[0] == WIKI_ART_ANCHOR.'.'.PAGE_EXTENSION) { return 'WIKI'; } elseif (strstr($this->SEGMENTS[count($this->SEGMENTS)-1], PAGE_EXTENSION)) { (PAGE_TITLE_MODE == 1) ? ($sc = 2) : ($sc = 1); $lastSeg = $this->SEGMENTS[count($this->SEGMENTS)-$sc]; preg_match('/'.PAGE_PREF.PAGE_RANDOM_LEFT.'(\d+)'.'/', $lastSeg, $aID); if ((int)$aID[1] != 0) { define('ARTICLE_ID', (int)$aID[1]); $this->articleID = (int)$aID[1]; } else { $this->_404(); } return 'ARTICLE'; } if (MAIN_MODE == 1 || MAIN_MODE == 2) { return $this->SEGMENTS[0]; } elseif(MAIN_MODE == 3 && ($this->SEGMENTS[0] == ORDER_DATE_PREF || $this->SEGMENTS[0] == ORDER_RATING_PREF)) { return $this->SEGMENTS[1]; } } private function getPager() { $lastSeg = $this->SEGMENTS[count($this->SEGMENTS)-1]; if (mb_strstr($lastSeg, PAGER_PREF)) { $pager = (int)mb_substr($lastSeg, mb_strlen(PAGER_PREF, BASE_CHARSET)); } else { $pager = 1; } if (defined('ARTICLE_ID')) { $pager = 1; } if ($pager == 0) { $pager = 1; } return (int)$pager; } public function getPageID(){ return ($this->getMainScheme() == 'ARTICLE') ? $this->articleID : false; } public function getThemeAnchor($themeURIName, $orderType = ORDER_DATE_PREF, $pager = 0) { if (MAIN_MODE == 1) { $themeAnchor = 'http://'.$this->host.'/'.THEME_PREF.'/'.$themeURIName.'/'.$orderType.'/'; } elseif(MAIN_MODE == 2) { $themeAnchor = 'http://'.$this->host.'/'.THEME_PREF.'/'.$orderType.'/'.$themeURIName.'/'; } elseif(MAIN_MODE == 3) { $themeAnchor = 'http://'.$this->host.'/'.$orderType.'/'.THEME_PREF.'/'.$themeURIName.'/'; } if ($pager > 0) { $themeAnchor .= PAGER_PREF.$pager.'/'; } return $themeAnchor; } public function getSectionAnchor($sectionURIName, $orderType = ORDER_DATE_PREF, $pager = 0) { if (MAIN_MODE == 1) { $sectionAnchor = 'http://'.$this->host.'/'.SECTION_PREF.'/'.$sectionURIName.'/'.$orderType.'/'; } elseif(MAIN_MODE == 2) { $sectionAnchor = 'http://'.$this->host.'/'.SECTION_PREF.'/'.$orderType.'/'.$sectionURIName.'/'; } elseif(MAIN_MODE == 3) { $sectionAnchor = 'http://'.$this->host.'/'.$orderType.'/'.SECTION_PREF.'/'.$sectionURIName.'/'; } if ($pager > 0) { $sectionAnchor .= PAGER_PREF.$pager.'/'; } return $sectionAnchor; } public function getArchiveAnchor($year, $month, $day, $pager=0) { (0 < (int)$day && (int)$day < 10) ? ($dayview = '0'.$day) : ($dayview = $day); (0 < (int)$month && (int)$month < 10) ? ($monthview = '0'.$month) : ($monthview = $month); if (ARCHIVE_MODE == 1) { ($day == 0) ? $dayview = '' : $dayview = '/'.$dayview; ($month == 0) ? $monthview = '' : $monthview = '/'.$monthview; $archiveAnchor = 'http://'.$this->host.'/'.ARCHIVE_PREF.'/'.$year.$monthview.$dayview.'/'; } else { ($day == 0) ? $dayview = '' : $dayview = ARCHIVE_DELIM.$dayview; ($month == 0) ? $monthview = '' : $monthview = ARCHIVE_DELIM.$monthview; $archiveAnchor = 'http://'.$this->host.'/'.ARCHIVE_PREF.'/'.$year.$monthview.$dayview.'/'; } if ($pager > 0) { $archiveAnchor .= PAGER_PREF.$pager.'/'; } return $archiveAnchor; } public function getSingleArtAnchor($themeURIName, $sectionURIName, $artID, $lang = '', $artTitle = '') { (PAGE_TITLE_MODE == 1) ? ($titleURI = '/'.$artTitle) : ($titleURI = ''); if (SINGLE_ART_MODE == 1) { $uri = PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 2) { $uri = SINGLE_ART_PREF.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 3) { $uri = $themeURIName.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 4) { $uri = $sectionURIName.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 5) { $uri = $sectionURIName.'-'.$themeURIName.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 6) { $uri = $themeURIName.'-'.$sectionURIName.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 7) { $uri = $sectionURIName.'/'.$themeURIName.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } elseif(SINGLE_ART_MODE == 8) { $uri = $themeURIName.'/'.$sectionURIName.'/'.PAGE_PREF.PAGE_RANDOM_LEFT.$artID.$titleURI.'.'.PAGE_EXTENSION; } if ($lang) { return 'http://'.$this->host.'/'.$lang.'/'.$uri; } else { return 'http://'.$this->host.'/'.$uri; } } public function getSearchQuery() { if ($this->mainScheme == 'SEARCH') { return $this->SEGMENTS[1]; } else { return ''; } } public function getTagQuery() { if ($this->mainScheme == 'TAGS') { return $this->SEGMENTS[1]; } else { return ''; } } public function getCommentAnchor() { return 'http://'.$this->host.'/'.ADDCOMMENT_PREF; } public function getTagAnchor($tag) { return 'http://'.$this->host.'/'.TAGS_PREF.'/'.$tag; } public function getCustomAnchor() { return 'http://'.$this->host.'/'.CUSTOM_PREF.'/'; } public function getBlockAnchor($blockTitle, $pager = '') { ($pager) ? ($pgURI = PAGER_PREF.$pager.'/') : ($pgURI = ''); return 'http://'.$this->host.'/'.BLOCK_PREF.'/'.$blockTitle.'/'.$pgURI; } public function getBlockTitle() { if ($this->mainScheme == 'BLOCK') { return $this->SEGMENTS[1]; } else { return ''; } } private function _404() { if ($this->usageFlag == true){ return false; } else { header('HTTP/1.1 404 Not Found'); exit; } } public function printInfo() { echo 'mainscheme: '.$this->getMainScheme().'
'; echo 'URI: '.$this->URI.'
'; echo 'SEGMENTS:
'; $cc=0; foreach ($this->SEGMENTS as $item) { echo $cc.': '.$item.'
'; $cc++; } echo 'artID: '.ARTICLE_ID.'
'; echo 'LANG: '.CURRENT_LANG.'
'; echo 'PAGER: '.CURRENT_PAGER.'
'; echo 'ORDER: '.CURRENT_ORDER.'
'; echo 'THEME_URI: '.THEME_URI.'
'; echo 'SECTION_URI: '.SECTION_URI.'
'; echo 'ARCHIVE_URI: '.ARCHIVE_PREF.'
'; echo 'DATE: '.ARCHIVE_YEAR.'::'.ARCHIVE_MONTH.'::'.ARCHIVE_DAY.'
'; } } ?>