<?php
function checkinstall(){
	if (file_exists("inc/install.php")){
		$removeinstall = unlink("inc/install.php");
		if(!$removeinstall){
			echo $l[installnotcomplete];
		}
	}
}
function compare($a,$b) {
	return $b[1]-$a[1];
}
function page($pid,$cid,$gid,$gp){
	if(!isset($pid)){
		$p = "index";
		return "data/".$p.".page.php";
	}else{
		if (isset($pid) and isset($cid)){
			if(!file_exists("data/".$pid.".child.".$cid.".php")){
				return "data/404.inc";
			}else{
				$p = $pid;
				$c = $cid;
				return "data/".$p.".child.".$c.".php";
			}
		}else{
			if(!file_exists("data/".$pid.".page.php")){
				return "data/404.inc";
			}else{
				$p = $pid;
				return "data/".$p.".page.php";
			}
		}
	}
}
function libraries($jquery,$jqueryui,$prototype,$scriptaculous,$mootools,$swfobject,$yui){
	$libraries = array($apikey,$jquery,$jqueryui,$prototype,$scriptaculous,$mootools,$swfobject,$yui);
	if (strlen($apikey) != 0) {
		if (in_array(true,$libraries)){
			echo "<script src=\"http://www.google.com/jsapi?key=".$apikey."\"></script>";
			echo "<script type=\"text/javascript\">";
			if ($jquery) { echo "google.load(\"jquery\", \"1\");"; }
			if ($jqueryui) { echo "google.load(\"jqueryui\", \"1.8.0\");"; }
			if ($prototype) { echo "google.load(\"prototype\", \"1.6.1.0\");"; }
			if ($scriptaculous) { echo "google.load(\"scriptaculous\", \"1.8.3\");"; }
			if ($mootools) { echo "google.load(\"mootools\", \"1.2.4\");"; }
			if ($swfobject) { echo "google.load(\"swfobject\", \"2.2\");"; }
			if ($yui) { echo "google.load(\"yui\", \"2.8.0r4\");"; }
			echo "</script>";
		}
	}
}
function ischild($child){
	if (strlen($child) != 0){
		return false;
	}else{
		return true;
	}
}
function nav(){
	$navs = glob("data/*.page.php");
	$sortedvals = array();
	$sortedkeys = array();
	foreach ($navs as $nav){
		require $nav;
		array_push($sortedvals,$page[id]);
		array_push($sortedkeys,$page[order].$page[id]);
		
	}
	$sortednavs = array_combine($sortedkeys,$sortedvals);
	ksort ($sortednavs);
	foreach ($sortednavs as $sortednav){
		require "data/".$sortednav.".page.php";
		if ($page[id] == "index"){
			echo "<li class=\"mainnav";
			if ($_GET[p] == "index" or !isset($_GET[p])){
				echo " selected";
			}
			echo "\">";
			echo "<a href=\"./\" title=\"".$page[title]."\" rel=\"index\">";
			echo $page[title];
			echo "</a>";
		}else{
		echo "<li class=\"mainnav";
		if ($_GET[p] == $page[id] or $_GET[c] == urlencode($page[id])){
			echo " selected";
		}
		echo "\">";
		
		echo "<a href=\"?p=".urlencode($page[id])."\" title=\"".$page[title]."\">";
		echo $page[title];
		echo "</a>";
		}
		if (count(glob("data/*.child.".$page[id].".php")) != 0){
			$childs = glob("data/*.child.".$page[id].".php");
			$childpage = $page[id];
			$sortedchildvals = array();
			$sortedchildkeys = array();
			foreach ($childs as $child){
				require $child;
				array_push($sortedchildvals,$page[id]);
				array_push($sortedchildkeys,$page[order].$page[id]);
			}
			$sortedchildnavs = array_combine($sortedchildkeys,$sortedchildvals);
			ksort ($sortedchildnavs);
			echo "<ul class=\"subnav\">";
			foreach ($sortedchildnavs as $sortedchildnav){
				require "data/".$sortedchildnav.".child.".$childpage.".php";
				echo "<li>";
				echo "<a href=\"?c=".urlencode($page[ch])."&amp;p=".urlencode($page[id])."\" title=\"".$page[title]."\">";
				echo $page[title];
				echo "</a>";
				echo "</li>";
			}
			echo "</ul>";
		}
		echo "</li>";
	}
}
function maincol($pos){
	if ($pos == "left"){
		return " floatright";
	}else{
		return " floatleft";
	}
}
function sidecol($pos){
	if ($pos == "left"){
		return " floatleft";
	}else{
		return " floatright";
	}
}
function theme($theme){
	if (is_dir("themes/".$theme."/") and file_exists("themes/".$theme."/style.php")){
		return "<link href=\"themes/".$theme."/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
		
	}else{
		return "<link href=\"themes/default/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
	}
}
function pad($theme,$deigned,$powered){
	if (file_exists("themes/".$theme."/style.php")){
		require "themes/".$theme."/style.php";
	}else{
		require "themes/default/style.php";
	}
		if ($css[author] == "AWEN art studio"){
			if (file_exists("inc/files/awenartstudio.png")){
				$dby = "<a href=\"http://www.awenart.eu\" title=\"Theme design by AWEN art studio\"><img src=\"inc/files/awenartstudio.png\" alt=\"Theme design by AWEN art studio\" width=\"87\" height=\"30\" border=\"0\" /></a>";
			}else{
				$dby = "<a href=\"http://www.awenart.eu\" title=\"Theme design by AWEN art studio\">Theme design by AWEN art studio</a>";
			}
			if (file_exists("inc/files/s40.png")){
				$pby = "<a href=\"http://s40.biz\" title=\"Powered by S40 CMS\"><img src=\"inc/files/s40.png\" alt=\"Powered by S40 CMS\" width=\"83\" height=\"30\" border=\"0\" /></a>";
			}else{
				$pby = " | <a href=\"http://s40.biz\" title=\"Powered by S40 CMS\">Powered by S40 CMS</a>";
			}
		}else{
			$dby = false;
			if (file_exists("inc/files/s40powered.png")){
				$pby = "<a href=\"http://s40.biz\" title=\"Powered by S40 CMS\"><img src=\"inc/files/s40powered.png\" alt=\"Powered by S40 CMS\" width=\"97\" height=\"30\" border=\"0\" /></a>";
			}else{
				$pby = " | <a href=\"http://s40.biz\" title=\"Powered by S40 CMS\">Powered by S40 CMS</a>";
			}
		}
	if($dby != false){
		echo "<div id=\"pad\" style=\"float:right;width:auto;font-size:0.7em;\">";
		echo $dby;
		echo $pby;
		echo "</div>";
	}else{
		echo "<div id=\"pad\" style=\"display:block; text-align:right\">";
		echo $pby;
		echo "</div>";
	}
}
function gallery($garray,$src,$child,$childsrc){
	if (!$child){
		require "data/".$src.".child.".$childsrc.".php";
	}else{
		require "data/".$src.".page.php";
	}
		$images = explode(",",$garray);
		require "inc/config.php";
		foreach ($images as $image){
			if (file_exists("media/original/".$image)){
				list($width, $height, $type, $attr) = getimagesize("media/original/".$image);
				if ($width < $s40[imagesize] and $height < $s40[imagesize]){
					echo "<li class=\"image\">";
					echo "<a href=\"media/original/".$image."\" rel=\"s40-gallery\" title=\"".$page[title]."\">";
					echo "<img src=\"media/thumbs/".$image."\" border=\"0\" width=\"".$s40[thumbsize]."\" height=\"".$s40[thumbsize]."\" alt=\"".$page[title]." | ".$image."\" />";
					echo "</a>";
					echo "</li>";
				}else{
					echo "<li class=\"image\">";
					echo "<a href=\"media/images/".$image."\" rel=\"s40-gallery\">";
					echo "<img src=\"media/thumbs/".$image."\" border=\"0\" alt=\" \" />";
					echo "</a>";
					echo "</li>";
				}
			}
		}
}
function render($type,$textpos,$src,$child,$childsrc){
	if (!$child){
		require "data/".$src.".child.".$childsrc.".php";
	}else{
		if ($src == "404S40"){
			require "data/404.inc";
		}else{
			require "data/".$src.".page.php";
		}
	}
	switch ($page[type]) {
		case "gallery":
			if ($textpos == "top"){
				echo "<div class=\"article\">";
				echo "<h1>".$page[title]."</h1>";
				echo $page[content];
				echo "</div>";
				if (strlen($page[media]) != 0){
					echo "<div class=\"gallery\">";
					echo "<ul>";
					echo gallery($page[media],$src,ischild($page[ch]),$page[ch]);
					echo "</ul>";
					echo "</div>";
					echo "<div class=\"clear\"></div>";
				}
			}else{
				if (strlen($page[media]) != 0){
					echo "<div class=\"gallery\">";
					echo "<ul>";
					echo gallery($page[media],$src,ischild($page[ch]),$page[ch]);
					echo "</ul>";
					echo "</div>";
				}
				echo "<div class=\"clear\"></div>";	
				echo "<div class=\"article\">";
				echo "<h1>".$page[title]."</h1>";
				echo $page[content];
				echo "</div>";
				
			}
		break;
		case "mixed":
			if ($textpos == "left"){
				if (strlen($page[media]) != 0){
					echo "<div class=\"gallerycol floatright\">";
					echo "<ul>";
					echo gallery($page[media],$src,ischild($page[ch]),$page[ch]);
					echo "</ul>";
					echo "</div>";
				}	
				echo "<div class=\"article\">";
				echo "<h1>".$page[title]."</h1>";
				echo $page[content];
				echo "</div>";
			}else{
				if (strlen($page[media]) != 0){
					echo "<div class=\"gallerycol floatleft\">";
					echo "<ul>";
					echo gallery($page[media],$src,ischild($page[ch]),$page[ch]);
					echo "</ul>";
					echo "</div>";
				}	
				echo "<div class=\"article\">";
				echo "<h1>".$page[title]."</h1>";
				echo $page[content];
				echo "</div>";
			}
		break;
		case "list":
				echo "<div class=\"article\">";
				echo "<h1>".$page[title]."</h1>";
				echo $page[content];
				echo "</div>";
				if (strlen($page[media]) != 0){
					echo "<div class=\"gallery\">";
					echo "<ul>";
					echo gallery($page[media],$src,ischild($page[ch]),$page[ch]);
					echo "</ul>";
					echo "</div>";
				}
				if (strlen($page[ch]) == 0){
					if (count(glob("data/*.child.".$page[id].".php")) != 0){
						$childs = glob("data/*.child.".$page[id].".php");
						$childpage = $page[id];
						$sortedchildvals = array();
						$sortedchildkeys = array();
						foreach ($childs as $child){
							require $child;
							array_push($sortedchildvals,$page[id]);
							array_push($sortedchildkeys,$page[order].$page[id]);
						}
						$sortedchildnavs = array_combine($sortedchildkeys,$sortedchildvals);
						ksort ($sortedchildnavs);
						echo "<div class=\"article\">";
						foreach ($sortedchildnavs as $sortedchildnav){
							require "data/".$sortedchildnav.".child.".$childpage.".php";
							echo "<div class=\"listsubnavitem\">";
							if (strlen($page[media]) != 0){
								$subitemthumb = array($page[media]);
								if (file_exists("media/thumbs/".reset($subitemthumb))){
									echo "<a class=\"image\" href=\"?c=".urlencode($page[ch])."&amp;p=".urlencode($page[id])."\" title=\"".$page[title]."\">";
									echo "<img src=\"media/thumbs/".reset($subitemthumb)."\" border=\"0\" />";
									echo "</a>";
								}
							}
							echo "<h2><a href=\"?c=".urlencode($page[ch])."&amp;p=".urlencode($page[id])."\" title=\"".$page[title]."\">";
							echo $page[title];
							echo "</a></h2>";
							echo "<div class=\"listsubnavdescription\">".$page[description]."</div>";
							echo "<div class=\"clear\"></div>";
							echo "</div>";
						}
						echo "</div>";
					}
				}
		break;
	}
}
function gadgets() {
	require "sidecol/gadgets.php";
	$gadgets = explode(",",$s40[gadgets]);
	foreach ($gadgets as $gadget){
		if(file_exists("sidecol/".$gadget."/config.php") and file_exists("sidecol/".$gadget."/functions.php")){
			require "sidecol/".$gadget."/config.php";
			require "sidecol/".$gadget."/functions.php";
			echo "<h3>".$g[name]."</h3>";
			echo "<div class=\"gadget\">";
			$g[content]();
			echo "</div>";
		}
	}
//	if (count(glob("sidecol/*/config.php")) != 0){
//		$gadgets = glob("sidecol/*/config.php");
//		foreach ($gadgets as $gadget){
//			require $gadget;
//			if ($g[on]){
//				echo "<h3>".$g[name]."</h3>";
//				echo "<div class=\"gadget\">";
//				echo $g[content];
//				echo "</div>";
//			}
//		}
//	}
}
function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
function canonical() {
	 if(!isset($_SERVER['REQUEST_URI'])){
		 $serverrequri = $_SERVER['PHP_SELF'];
	}else{
		$serverrequri = $_SERVER['REQUEST_URI'];
	}
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
	$canonical = $protocol."://".$_SERVER['SERVER_NAME'].$port.$serverrequri; 
	$canonical = str_replace("&", "&amp;", $canonical);
	return $canonical;
}
?>