<?php 

/* ---------------------------------------------------------------------------
--- © Elmar Eigner, open-eye, 2005
--- $Id: index.php 1837 2005-10-14 14:21:29Z ee $
--------------------------------------------------------------------------- */

require_once('class/config.php');
require_once('class/funx.php');
require_once('class/mysqldb.class.php');
require_once('class/templ.class.php');
require_once('lang/lang.vars.php');

define('PRES_WHITELIST_IP', '65.36.241.81' );
define('PRES_WHITELIST_UA', 'InternetSeer.com' );
#require_once('page.restrictor.php');

require_once('eu-cookie/euc.php');

$lang = 'de';
if ( isset ( $_COOKIE['lang'] ) && preg_match("/^[a-z]{2}$/", $_COOKIE['lang'] ) ) {
	$lang = $_COOKIE['lang'];
}
$euc_options = array (
	"print" => false,
	"css_file" => 'eu-cookie/euc_style.css',
	"txt_notice_file" => 'eu-cookie/euc_notice_'.$lang.'.txt'
);
$euc = new euc ( $euc_options );


if ( !isset ( $_GET['pid'] ) ) {
	  $mainkey = 'www';
}
else if ( isset ( $_GET['pid'] ) && preg_match("/^[a-z0-9_\-]{3,48}$/", $_GET['pid'] ) ) {
	$mainkey = $_GET['pid'];
}
else {
	jumpAway ( );
}

$isadmin = 0;
if ( isset ( $_SERVER['HTTP_REFERER'] ) && preg_match("/^http:\/\/".preg_quote($_SERVER['HTTP_HOST'])."\/admin\//", $_SERVER['HTTP_REFERER'] ) ) {
	$isadmin = 1;
}

$mypage = false;
$dbh = new mysqldb;
$dbh->connect();
$getpageq = $dbh->prepare("SELECT *,unix_timestamp(slastmod) AS unixts FROM ".oe_dbpre."pages WHERE skey='{$mainkey}'");
if ( $getpageq->exec() && $getpageq->num_rows() == 1 ) {
	$mypage = $getpageq->get_row();
	$lang = $mypage->slang;
	setcookie('lang', $mypage->slang);
	if ( $mypage->sstatus == 'on' OR $isadmin == 1 ) {
		if ( !$isadmin ) {
			$updviews = $dbh->prepare("UPDATE ".oe_dbpre."pages SET sviewcount=sviewcount+1 WHERE sid={$mypage->sid} LIMIT 1");
			$updviews->exec();
			if ( $mypage->sconttype == 'static' ) {
				createHTTPHeader ( $mypage );
			} else {
				#header ( "Vary: Accept" ); - this breaks adsense, but in fact needed for valid content type headers, sad -.-
				#if ( stristr ( $_SERVER["HTTP_ACCEPT"], "application/xhtml+xml" ) ) {
				#    header( "Content-Type: application/xhtml+xml" );
				#} else {
				    header ( "Content-Type: text/html" );
				#}
			}
		}
		$content = array ();
		$content['title']    = stripslashes($mypage->stitle);
		$content['metakeys'] = stripslashes($mypage->skeywords);
		$content['metadesc'] = stripslashes($mypage->sdesc);
		$content['topmenue'] = getTopMenue( $dbh, $mypage, $mainkey );	
		$content['menue']    = getMenue( $dbh, $mypage, $mainkey );	
		$content['headline'] = stripslashes($mypage->sheadline);
		if ( $mypage->sstatus != 'on' ) {
			$content['headline'] = 'VORSCHAU: '.$content['headline'];
		}
		if ( $mypage->sconttype == 'script' ) {
			$scriptfile = 'extensions/'.$mainkey.'.php';
			if ( file_exists($scriptfile) ) {
				include($scriptfile);
			} else {
				$content['main'] = '<h2>'.stripslashes ( $ltxt[$lang]['err_header_all'] ).'.</h2>';
				$content['main'] .= preg_replace_callback("/\*\*(GLOBALS|SERVER)_([^*]+)\*\*/", 'php_var_replacer', stripslashes ( $ltxt[$lang]['err_scriptnotfound'] ) );
			}
		} else {
			$content['main'] = preg_replace_callback("/\*\*(GLOBALS|SERVER)_([^*]+)\*\*/", 'php_var_replacer', stripslashes ( $mypage->scontent ) );
		}
		$content['lastmod']  = date("d.m.Y - H:i", $mypage->unixts);
		if ( ! empty ( $mypage->sfooter ) ) {
			$content['footer']   = stripslashes($mypage->sfooter);
		}
		$content['viewcnt']  = number_format ( stripslashes($mypage->sviewcount), 0, ',', '.' );	
		$content['infotext'] = '';
		if ( ! empty ( $ltxt[$lang]['global_footerbox'] ) ) {
			$content['adbox'] = stripslashes($ltxt[$lang]['global_footerbox']);
		}
		/* search extensions _ highlight of search words */
		if ( !empty ( $_GET['sqhl'] )   ) {
			$_GET['sqhl'] = strip_tags ( $_GET['sqhl'] );
			$sqhl = explode(' ', $_GET['sqhl'] );
			foreach ( $sqhl AS $v) {
			    if ( strlen ($v) < 4 ) continue;
				$v = preg_quote ( $v, '/' );
				$content['headline'] = preg_replace("/(^|\W)(".$v.")(\W|$)/i", '\\1<span class="search_hl">\\2</span>\\3', $content['headline']);
				$content['main'] = preg_replace("/(^|\W)(".$v.")(\W|$)/i", '\\1<span class="search_hl">\\2</span>\\3', $content['main']);
				while ( preg_match("/(\<)([^>]+)\<span class=\"search_hl\"\>(".$v.")\<\/span\>([^>]+)(\>)/isU", $content['main']) ) {
					$content['main'] = preg_replace("/(\<)([^>]+)\<span class=\"search_hl\"\>(".$v.")\<\/span\>([^>]+)(\>)/isU", "\\1\\2\\3\\4\\5", $content['main']);
				}
			}
		}
		$content['selfuri']  = $_SERVER['REQUEST_URI'];
		$content['eucookienotice']  = $euc->buff;
		$maintmpl = "./tmpl/main-{$lang}.html";
		$pagetmpl = "./tmpl/{$mypage->stemplate}-{$lang}.html";
		if ( file_exists ( $pagetmpl ) ) {
			$maintmpl = $pagetmpl;
		}
		$TF = new displaytmpl($maintmpl);
		#$TF = new displaytmpl("./tmpl/main-{$lang}.html");
		$TF->readTmpl();
		$TF->ParseTmpl ( $content );
		$OUT = $TF->Output();
	} else {
		$content = array ();
		$content['title']    = nicerMainkey($mainkey);
		$content['menue']    = getMenue( $dbh, $mypage, $mainkey );
		$content['errortxt'] = "<h2>".stripslashes ( $ltxt[$lang]['err_header_all'] )."</h2>\n";
		$content['errortxt'] .= preg_replace_callback("/\*\*(GLOBALS|SERVER)_([^*]+)\*\*/", 'php_var_replacer', stripslashes ( $ltxt[$lang]['err_pagenotfound'] ) );
		$content['errortxt'] .= getSiteSearchBox( $ltxt[$lang]['search_form_title'], $ltxt[$lang]['search_form_button'] );
		$content['selfuri']  = $_SERVER['REQUEST_URI'];
		$content['eucookienotice']  = $euc->buff;
		$TF = new displaytmpl("./tmpl/error-{$lang}.html");
		$TF->readTmpl();
		$TF->ParseTmpl ( $content );
		$OUT = $TF->Output();
		header ( "HTTP/1.0 404 Not Found" );
	}
} 
else {	
	$content = array ();
	$content['title'] = nicerMainkey($mainkey);
	$content['menue']    = getMenue( $dbh, $mypage, $mainkey );
	$content['errortxt'] = "<h2>".stripslashes ( $ltxt[$lang]['err_header_all'] )."</h2>\n";
	$content['errortxt'] .= preg_replace_callback("/\*\*(GLOBALS|SERVER)_([^*]+)\*\*/", 'php_var_replacer', stripslashes ( $ltxt[$lang]['err_pagenotfound'] ) );
	$content['errortxt'] .= getSiteSearchBox( $ltxt[$lang]['search_form_title'], $ltxt[$lang]['search_form_button'] );
	$content['selfuri']  = $_SERVER['REQUEST_URI'];
	$content['eucookienotice']  = $euc->buff;
	$TF = new displaytmpl("./tmpl/error-{$lang}.html");
	$TF->readTmpl();
	$TF->ParseTmpl ( $content );
	$OUT = $TF->Output();
	header ( "HTTP/1.0 404 Not Found" );
}

/* close mysql connection */

$dbh->disconnect();

ob_start();
print $OUT;

ob_end_flush();


?>
