<?php

/**
* SmartyCMS(R) v.3.0
* @copyright (c)2006-2010 Aleksandr Datsko <www.smartycms.com>
* @author Aleksandr Datsko <alex@datsko.net> <www.datsko.net>
* @version 04.01.2010 21:44
*/

// error_reporting(0);
error_reporting( E_ALL & ~E_NOTICE );
session_start( session_name( "SmartyCMS" ) );

//print_r( $_GET );
/*
if( isset( $_GET["stopredirect"] ) || false !== strpos( $_SERVER["HTTP_USER_AGENT"], "yandex.com/bots" ) ){
	$_SESSION["stopredirect"] = "y";
}
if( "y" != $_SESSION["stopredirect"] ){
	include "geoip/geoip.inc";
	$gi = geoip_open( "geoip/GeoIP.dat", GEOIP_STANDARD );

	if( "RU" == geoip_country_code_by_addr( $gi, $_SERVER["REMOTE_ADDR"] ) && "" == $_SESSION["UserId"] && false === strpos( $_SERVER["HTTP_USER_AGENT"], "yandex.com/bots" ) ){
		header( "HTTP/1.1 303 See Other" );
		header( "Location: http://mv-furnitura.ru/" );
		exit();
	}
}
*/
$OK = TRUE;

require( "_inc/000.smarty.class.inc" );
require( "_inc/001.class.inc" );

function Core301( $url = "" ){ header( "HTTP/1.1 301 Moved Permanently" ); header( "Location: " . CONFIG_HOSTNAME . $url ); }
function Core404(){ global $id_page; header( "HTTP/1.1 404 Not Found" ); $id_page = "404"; }

if( "" != $_GET["id"] ){ 
	if( is_numeric( $_GET["id"] ) && (int)$_GET["id"] > 0 ){
		Core301( $_GET["id"] . "/" );
	}
}
if( "" != $_GET["id_resource"] ){ if( is_numeric( $_GET["id_resource"] ) && (int)$_GET["id_resource"] > 0 ){ $_GET["id_res"] = (int)$_GET["id_resource"]; } }
if( "" != $id_res_stat ){ if( is_numeric( $id_res_stat ) && (int)$id_res_stat > 0 ){ $_GET["id_res"] = (int)$id_res_stat; } }
if( "" != $_GET["section"] ){ if( is_numeric( $_GET["section"] ) && (int)$_GET["section"] > 0 ){ $_GET["id_serv"] = (int)$_GET["section"]; } }
if( "" != $id_serv_stat ){ if( is_numeric( $id_serv_stat ) && (int)$id_serv_stat ){ $_GET["id_serv"] = (int)$id_serv_stat; } }

if( "" != $_GET["static_link"] ){
	
	$CHECK_STATIC_LINK = FALSE;

	$z = $SQL->GetRecord( "SELECT a.id FROM " . PTABLE . "resources a WHERE a.static_link = '" . (string)$SQL->PrepareSQL( $_GET["static_link"] ) . "'" . SmartyCMS_ResourcePublic . " LIMIT 1" );
	if( "" != $z["id"] ){
		$_GET["id_res"] = $z["id"];
		$CHECK_STATIC_LINK = TRUE;
	}

	if( !$CHECK_STATIC_LINK ){
		$z = $SQL->GetRecord( "SELECT a.id, a.static_link FROM " . PTABLE . "resources a WHERE a.static_link = '" . (string)$SQL->PrepareSQL( $_GET["static_link"] ) . "/'" . SmartyCMS_ResourcePublic . " LIMIT 1" );
		if( "" != $z["id"] ){
			Core301( $z["static_link"] );
			exit();
		}
	}

	if( !$CHECK_STATIC_LINK ){
		$z = $SQL->GetRecord( "SELECT id FROM " . PTABLE . "services_vid WHERE static_link = '" . (string)$SQL->PrepareSQL( $_GET["static_link"] ) . "' AND id_public = 1 LIMIT 1" );
		if( "" != $z["id"] ){
			$_GET["id_serv"] = $z["id"];
			$CHECK_STATIC_LINK = TRUE;
		}
	}

	if( !$CHECK_STATIC_LINK ){
		$z = $SQL->GetRecord( "SELECT id, static_link FROM " . PTABLE . "services_vid WHERE static_link = '" . (string)$SQL->PrepareSQL( $_GET["static_link"] ) . "/' AND id_public = 1 LIMIT 1" );
		if( "" != $z["id"] ){
			Core301( $z["static_link"] );
			exit();
		}
	}

	if( !$CHECK_STATIC_LINK ){
		Core404();
		$OK = FALSE;
	}
}

if( "" != $subdomen ){
	$z = $SQL->GetRecord( "SELECT a.id FROM " . PTABLE . "resources a WHERE a.subdomen = '" . (string)$SQL->PrepareSQL( $subdomen ) . "' AND a.static_link IS NULL" . SmartyCMS_ResourcePublic . " LIMIT 1" );
	if( "" != $z["id"] ){
		$_GET["id_res"] = $z["id"];
	}else{
		$z2 = $SQL->GetRecord( "SELECT id FROM " . PTABLE . "services_vid WHERE static_link = '" . (string)$SQL->PrepareSQL( $subdomen ) . "' AND id_public = 1 LIMIT 1" );
		if( "" != $z2["id"] ){
			$_GET["id_serv"] = $z2["id"];
		}else{
			
			Core404();
			$OK = FALSE;
		}
	}
}
if( $OK ){

	$id_page = "index";

	if( "" != $_GET["id_res"] || "" != $_GET["id_serv"] ){
		$id_page = "resource";
	}

	$TitlePage = FALSE;
	$DescriptionPage = FALSE;
	$KeywordsPage = FALSE;

	require( "_inc/prepare.php" );

	$alias_page = FALSE;

	if( "resource" == $id_page ){

		$id_res = FALSE;
		if( "" != $_GET["id_res"] && is_numeric( $_GET["id_res"] ) && (int)$_GET["id_res"] > 0 ){
			$id_res = (int)$_GET["id_res"];
			$RES = SmartyCMS_GetResourceData( $id_res, 0, array( "basic" ) );
			if( $RES ){
				if( 0 == $RES["id_public"] && "" == $_SESSION["UserId"] ){
					Core404();
					$OK = FALSE;
				}
	
				if( $RES["static_link"] && $_GET["id_res"] && !$_GET["static_link"] ){
					/*
					Core301( $RES["static_link"] );
					exit();
					*/
					header ( "HTTP/1.1 301 Moved Permanently" );
					header ( "Location: " . CONFIG_HOSTNAME . $RES["static_link"] );
					exit();
				}
				
				$smarty->assign( "id_res", $id_res );
				$smarty->assign( "RES", $RES );
				$alias_page = "resource" . $id_res;
				$_GET["id_serv"] = (int)$RES["service_id"];
			}else{
				Core404();
				$OK = FALSE;
			}
		}

		if( $OK ){

			$id_serv = FALSE;
			if( "" != $_GET["id_serv"] && is_numeric( $_GET["id_serv"] ) && (int)$_GET["id_serv"] > 0 ){
				$id_serv = (int)$_GET["id_serv"];
				$SERV = SmartyCMS_GetServiceData( $id_serv );
				if( $SERV ){
					
					if( 0 == $SERV["id_public"] && "" == $_SESSION["UserId"] ){
						Core404();
						$OK = FALSE;
					}
					$smarty->assign( "id_serv", $id_serv );
					$smarty->assign( "SERV", $SERV );
					$smarty->assign( "SERV_KF", $SERV["xml"]["ALIAS"] );
					if( !$id_res ){
						$alias_page = "service" . $id_serv;
					}
				}else{
					Core404();
					$OK = FALSE;
				}
			}

			if( !$id_res && !$id_serv ){
				Core404();
				$OK = FALSE;
			}

			if( $id_serv ){

				$TitlePage = $SERV["name"];
				$DescriptionPage = $SERV["name"];
				$KeywordsPage = $SERV["name"];

				if( $id_res ){

					if( "" != $RES["meta_title"] && " " != $RES["meta_title"] ){
						$TitlePage = $RES["meta_title"];
					}else{
						$TitlePage = $RES["name"];
					}
					if( "" != $RES["meta_desc"] ){
						$DescriptionPage = $RES["meta_desc"];
					}else{
						$DescriptionPage = $TitlePage . ". " . $SERV["name"];
					}
					if( "" != $RES["kf"] ){
						$KeywordsPage = $RES["kf"];
					}else{
						$KeywordsPage = $TitlePage . ". " . $SERV["name"];
					}

					$VIEW = "res";

				}else{

					$VIEW = "serv";

				}

				require( "_inc/postpare.php" );

				if( $id_serv && !$id_res ){
					$tpl = "";
					if( "" != $SERV["xml"]["TPL"] ){
						$tpl = $SERV["xml"]["TPL"];
					}else{
						$tpl = $SERV["xml"]["ALIAS"];
					}
					if( file_exists( CONFIG_DOCUMENTROOT . CONFIG_TEMPLATES . "resource_" . $tpl . ".tpl" ) ){
						$VIEW = $tpl;
					}
				}

				if( $id_res ){
					$tpl = "";
					if( "" != $RES["xml"]["TPL"] ){
						$tpl = $RES["xml"]["TPL"];
					}else{
						$tpl = $RES["xml"]["ALIAS"];
					}
					if( file_exists( CONFIG_DOCUMENTROOT . CONFIG_TEMPLATES . "resource_" . $tpl . ".tpl" ) ){
						$VIEW = $tpl;
					}
				}

				if( "y" == $_GET["4print"] ){
					$VIEW = "print";
				}

			}

			$smarty->assign( "VIEW", "resource_" . $VIEW . ".tpl" );

		}

	}else{

		require( "_inc/postpare.php" );

	}
}

if( 404 == $id_page ){
	require( "_inc/postpare.php" );
}

require( "_inc/end.inc" );

?>