<?php
function isPage(){
	global $DB,$TPL;		
	$DB->dbRow("SELECT * FROM ".$DB->pre."page WHERE seoUri = '".$TPL->urlBase."' AND status='1'");
	if($DB->numRows){		
		$TPL->data = $DB->row;
		$TPL->modPath = ABSPATH."/mod/page";
		$TPL->modUrl  = SITEURL."/mod/page";
		$TPL->pageType = "page";
		$TPL->tplInc = $TPL->modPath."/x-page.inc.php";	
		if($DB->row["templateFile"] && file_exists($TPL->modPath.'/x-'.$DB->row["templateFile"].'.php')){
			$TPL->tplFile = $TPL->modPath.'/x-'.$DB->row["templateFile"].'.php';
		} else {
			$TPL->tplFile = $TPL->modPath.'/x-page.php';
		}
		$TPL->metaKeyword = $DB->row["metaKeyword"];
		$TPL->metaDesc = $DB->row["metaDesc"];
		$TPL->pageTitle = $DB->row['pageTitle'];							
	}
}

function isCategory(){
	global $TPL,$DB;
	$catUri = $TPL->pageUri;
	if(is_numeric(end($TPL->uriArr))){
		$catUri = "/".implode("/",array_slice($TPL->uriArr, 0, count($TPL->uriArr)-2));
	}
	
	$DB->dbRow("SELECT * FROM ".$DB->pre."category WHERE seoUri = '".$catUri."' AND status='1'");
	if($DB->numRows){
		$TPL->data = $DB->row;
		$TPL->modPath = ABSPATH."/mod/category";
		$TPL->modUrl  = SITEURL."/mod/category";
		$TPL->tplInc = $TPL->modPath."/x-category.inc.php";
		if($catUri == $TPL->pageUri) {
			if($DB->row["templateFile"] && file_exists($TPL->modPath.'/'.$DB->row["templateFile"])){
				$TPL->tplFile = $TPL->modPath.'/'.$DB->row["templateFile"];
			} else {
				$TPL->tplFile = $TPL->modPath.'/x-category.php';
			}
			$TPL->tplTitle   = $DB->row["categoryTitle"];
			if(trim($DB->row['metaTitle']))
				$TPL->metaTitle = $DB->row['metaTitle'];
			else
				$TPL->metaTitle = $DB->row['categoryTitle'];
				
			$TPL->metaKeyword = $DB->row['metaKeyword'];
			$TPL->metaDesc = $DB->row['metaDesc'];
			$TPL->pageType   = "category";
		} else {
			$TPL->modID = end($TPL->uriArr);
			$TPL->tplFile = $TPL->modPath.'/x-detail.php';
		}
		return true;						
	} else {
		return false;
	}
}

if($TPL->pageType=='404'){
	if($TPL->currMenu) {
		$funcName = "is".ucfirst($TPL->currMenu["menuType"]);
		if(function_exists($funcName)){				
			call_user_func($funcName);
			$TPL->tplTitle = $TPL->currMenu["menuTitle"];
		}
	} else {
		if(!isCategory() && !isPage()) {	
			$postVar = end($POSTARR);
			if($postVar) {
				$DB->dbRow("SELECT * FROM ".$DB->pre."post WHERE $postVar = '".$TPL->urlBase."' AND status='1'");
				if($DB->numRows){
					$TPL->data = $DB->row;
					$TPL->modPath = ABSPATH."/mod/post";
					$TPL->modUrl  = SITEURL."/mod/post";
					$TPL->tplInc = $TPL->modPath."/x-post.inc.php";				
					$TPL->tplFile = $TPL->modPath.'/x-post.php';
					$TPL->tplTitle = $DB->row["postTitle"];		
					$TPL->metaKeyword = $DB->row["metaKeyword"];
					$TPL->metaDesc = $DB->row["metaDesc"];		
					$TPL->pageType   = "post";										
				}						
			}
		}
	}
}
?>