<?php
function notes($note,$lang,$action){
	if (isset($note)){
		require "../inc/langs/".$lang.".php";
		if (isset($l[$note])){
			echo "<div id=\"note\" class=\"roundall\">";
			echo "<div id=\"close\"><a href=\"".$_SERVER['PHP_SELF'];
			if (isset($action)){
				echo "?a=";
				echo $action;
			}
			echo "\">X</a></div>";
			echo $l[$note];
			echo "</div>";
		}
			
	}
}
function nav($nav){
	if(!isset($nav) or (!file_exists("inc/".$nav.".php"))){
		return "inc/pages.php";
	}else{
		return "inc/".$nav.".php";
	}
}
function editpage($id,$parent) {
	if (isset($parent)){
		return $id.".child.".$parent.".php";
	}else{
		return $id.".page.php";
	}
}
function pimages($i1,$i2,$i3,$i4,$i5,$i6){
	if(strlen($i1)>3){
		$pimages = $i1.",";
	}
	if(strlen($i2)>3){
		$pimages .= $i2.",";
	}
	if(strlen($i3)>3){
		$pimages .= $i3.",";
	}
	if(strlen($i4)>3){
		$pimages .= $i4.",";
	}
	if(strlen($i5)>3){
		$pimages .= $i5.",";
	}
	if(strlen($i6)>3){
		$pimages .= $i6.",";
	}
	if(strlen(str_replace(",","",$pimages))>3){
		return substr($pimages, 0, -1);
	}
}
function subpagelist($subpage,$lang){
	require "../inc/langs/".$lang.".php";
	$childs = glob("../data/*.child.".$subpage.".php");
	$childpage = $subpage;
	$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);
	foreach ($sortedchildnavs as $sortedchildnav){
		require "../data/".$sortedchildnav.".child.".$childpage.".php";
		echo "<tr>";
		echo "<td align=\"left\" valign=\"top\" class=\"std\">";
		echo "<span class=\"l\">L</span> ".$page[title];
		echo "</td>";
		echo "<td width=\"180\" align=\"left\" valign=\"top\">";
		echo "<a class=\"actionbutton\" href=\"?a=editpage&amp;c=".$page[ch]."&amp;p=".$page[id]."\"><img src=\"style/edit.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
		echo $l[edit];
		echo "</span></a>";
		echo "<a class=\"actionbutton pagepreview\" href=\"../?c=".$page[ch]."&amp;p=".$page[id]."\" title=\"".$l[preview].": ".$page[title]."\"><img src=\"style/preview.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
		echo $l[preview];
		echo "</span></a>";
		echo "<a class=\"actionbutton\" href=\"?a=delete&amp;c=".$page[ch]."&amp;p=".$page[id]."\" onclick=\"if(! confirm('".$l[deleteconfirm]."')) return false;\"><img src=\"style/delete.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
		echo $l[delete];
		echo "</span></a>";
		echo "</td>";
		echo "</tr>";;
	}
}
function pagelist($lang){
	require "../inc/langs/".$lang.".php";
	$mpages = glob("../data/*.page.php");
	foreach ($mpages as $mpage){
		require $mpage;
		echo "<tr>";
		echo "<td width=\"60\" align=\"right\" valign=\"middle\" >";
		echo $page[order];
		echo "</td>";
		echo "<td align=\"left\" valign=\"top\" class=\"mtd\">";
		echo "<table class=\"pages\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
		echo "<tr>";
		echo "<td align=\"left\" valign=\"top\">";
		echo "<strong>".$page[title]."</strong>";
		echo "</td>";
		echo "<td width=\"180\" align=\"left\" valign=\"top\">";
		echo "<a class=\"actionbutton\" href=\"?a=editpage&amp;p=".$page[id]."\"><img src=\"style/edit.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
		echo $l[edit];
		echo "</span></a>";
		echo "<a class=\"actionbutton pagepreview\" href=\"../?p=".$page[id]."\" title=\"".$l[preview].": ".$page[title]."\"><img src=\"style/preview.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
		echo $l[preview];
		echo "</span></a>";
		if($page[id] == "index"){
			echo "<a class=\"actionbutton\" href=\"javascript:void(0);\" onclick=\"alert('".$l[deletenoteindex]."')\"><img src=\"style/deletedisabled.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
			echo $l[delete];
			echo "</span></a>";
		}else{
			if (count(glob("../data/*.child.".$page[id].".php")) != 0){
			echo "<a class=\"actionbutton\" href=\"javascript:void(0);\" onclick=\"alert('".$l[deletenotechilds]."')\"><img src=\"style/deletedisabled.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
			echo $l[delete];
			echo "</span></a>";
			}else{
				echo "<a class=\"actionbutton\" href=\"?a=delete&amp;p=".$page[id]."\" onclick=\"if(! confirm('".$l[deleteconfirm]."')) return false;\"><img src=\"style/delete.png\" border=\"0\" alt=\"\" width=\"24\" height=\"24\" /><span>";
				echo $l[delete];
				echo "</span></a>";
			}
		}
		echo "</td>";
		echo "</tr>";
		if (count(glob("../data/*.child.".$page[id].".php")) != 0){
			echo subpagelist($page[id],$lang);
		}
		echo "</table>";
		echo "</td>";
		echo "</tr>";
		
	}
}
function parentselected($parentselected, $parentself){
	$parents = glob("../data/*.page.php");
	sort ($parents);
	foreach ($parents as $parent){
		require $parent;
		if($page[id] != $parentself){
			echo "<option value=\"".$page[id]."\"";
			if ($parentselected == $page[id]){ echo " selected";}
			echo ">".$page[title]."</option>";
		}
	}
}
function pageid(){
	$countlog = file_get_contents("inc/pagelog.log");
	$countlog = explode("|",$countlog);
	$countlog = (count($countlog)+1);
	echo $countlog;
}
function cropimage($nw, $nh, $source, $stype, $dest) {
	$size = getimagesize($source);
	$w = $size[0];
	$h = $size[1];
	switch($stype) {
		case 'gif':
		$simg = imagecreatefromgif($source);
		break;
		case 'jpg':
		$simg = imagecreatefromjpeg($source);
		break;
		case 'png':
		$simg = imagecreatefrompng($source);
		break;
	}
	$dimg = imagecreatetruecolor($nw, $nh);
	$wm = $w/$nw;
	$hm = $h/$nh;
	$h_height = $nh/2;
	$w_height = $nw/2;
	if($w> $h) {
		$adjusted_width = $w / $hm;
		$half_width = $adjusted_width / 2;
		$int_width = $half_width - $w_height;
		imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
	} elseif(($w <$h) || ($w == $h)) {
		$adjusted_height = $h / $wm;
		$half_height = $adjusted_height / 2;
		$int_height = $half_height - $h_height;
		imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
	} else {
		imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
	}
	imagejpeg($dimg,$dest,90);
}
function findext($sname){
	if(preg_match("/.jpg/i", "$sname") or preg_match("/.jpeg/i", "$sname"))
	{
	return "jpg";
	}
	if (preg_match("/.gif/i", "$sname"))
	{
		return "gif";
	}
	if(preg_match("/.png/i", "$sname"))
	{
		return "png";
	}
}
function rss(){
	require "../inc/config.php";
	$rss = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" . "\r\n";
	$rss .= "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">" . "\r\n";
	$rss .= "\t" . "<channel>" . "\r\n";
	$rss .= "\t\t" . "<atom:link href=\"".$s40[home]."rss.xml\" rel=\"self\" type=\"application/rss+xml\" />" . "\r\n";
	$rss .= "\t\t" . "<title>".$s40[title]."</title>" . "\r\n";
	$rss .= "\t\t" . "<link>".$s40[home]."</link>" . "\r\n";
	$rss .= "\t\t" . "<description>".$s40[description]."</description>" . "\r\n";
	$rss .= "\t\t" . "<language>".$s40[lang]."</language>" . "\r\n";
	$rss .= "\t\t" . "<generator>S40 CMS (s40.biz)) by AWEN art studio (www.awenart.eu)</generator>" . "\r\n";
	$dir="../data/";
	$max=15;
	$files=Array();
	$f=opendir($dir);
	while (false !== ($file = readdir($f))) {
		if (strpos($file, ".php", 1)) {
    		$files[]=Array($file,filectime("$dir/$file"));
   		}
	}
	closedir($f);
	usort($files,"compare");
	$m=min($max,count($files));
	for ($i=0;$i<$m;$i++) {
		$filename = $files[$i][0];
		require "../data/".$filename;
			$rss .= "\t\t" . "<item>" . "\r\n";
			$rss .= "\t\t\t" . "<title>".$page[title]."</title>" . "\r\n";
			if (strlen($page[ch]) == 0){
				$rss .= "\t\t\t" . "<link>".$s40[home]."?p=".urlencode($page[id])."</link>" . "\r\n";
			}else{
				$rss .= "\t\t\t" . "<link>".$s40[home]."?c=".urlencode($page[ch])."&amp;p=".urlencode($page[id])."</link>" . "\r\n";
			}
			$rss .= "\t\t\t" . "<description>".$page[description]."</description>" . "\r\n";
			$rss .= "\t\t\t" . "<pubDate>".$page[pagelog]."</pubDate>" . "\r\n";
			$rss .= "\t\t\t" . "<guid>".$s40[home]."?c=".urlencode($page[ch])."&amp;p=".urlencode($page[id])."#article</guid>" . "\r\n";
			$rss .= "\t\t" . "</item>" . "\r\n";
		}
	
	$rss .= "\t" . "</channel>" . "\r\n";
	$rss .= "</rss>";
	file_put_contents("../rss.xml", $rss);
}
function sitemap(){
	require "../inc/config.php";
	$sm = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" . "\r\n";
	$sm .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" . "\r\n";
	$sm .= "<url>" . "\r\n";
	$sm .= "\t" . "<loc>".$s40[home]."</loc>" . "\r\n";
	$sm .= "\t" ."<lastmod>".date("Y-m-d", filemtime("../data/index.page.php"))."</lastmod>" . "\r\n";
	$sm .= "\t" . "<changefreq>monthly</changefreq>" . "\r\n";
	$sm .= "\t" . "<priority>1</priority>" . "\r\n";
	$sm .= "</url>" . "\r\n";
	$smaps = glob("../data/*.php");
	foreach ($smaps as $smap){
		require $smap;
		$sm .= "<url>" . "\r\n";
		if (strlen($page[ch]) == 0){
			$sm .= "\t" . "<loc>".$s40[home]."?p=".$page[id]."</loc>" . "\r\n";
			$sm .= "\t" ."<lastmod>".$page[pagelog]."</lastmod>" . "\r\n";
			$sm .= "\t" . "<changefreq>monthly</changefreq>" . "\r\n";
			$sm .= "\t" . "<priority>0.8</priority>" . "\r\n";
			$sm .= "</url>" . "\r\n";
		}else{
			$sm .= "\t" . "<loc>".$s40[home]."?c=".$page[ch]."&amp;p=".$page[id]."</loc>" . "\r\n";
			$sm .= "\t" ."<lastmod>".$page[pagelog]."</lastmod>" . "\r\n";
			$sm .= "\t" . "<changefreq>monthly</changefreq>" . "\r\n";
			$sm .= "\t" . "<priority>0.6</priority>" . "\r\n";
			$sm .= "</url>" . "\r\n";
		}
	}
	$sm .= "</urlset>";
	file_put_contents("../sitemap.xml", $sm);
}
function settingstheme($current){
	$themes = glob("../themes/*/style.php");
	foreach ($themes as $theme){
		require $theme;
		if($current == $css[name]){
			echo "<option value=\"".$css[name]."\" selected=\"selected\">".$css[name]."</option>";
		}else{
			echo "<option value=\"".$css[name]."\">".$css[name]."</option>";
		}
	}
}
function settingslang($current){
	$langs = glob("../inc/langs/*.php");
	foreach ($langs as $lang){
		$lang = str_replace("../inc/langs/","",$lang);
		$lang = str_replace(".php","",$lang);
		if($current == $lang){
			echo "<option value=\"".$lang."\" selected=\"selected\">".$lang."</option>";
		}else{
			echo "<option value=\"".$lang."\">".$lang."</option>";
		}
	}
}
function settingslangdir($current){
	if($current == "ltr"){
		echo "<option value=\"ltr\" selected=\"selected\">ltr</option>";
		echo "<option value=\"rtl\">rtl</option>";
	}else{
		echo "<option value=\"ltr\">ltr</option>";
		echo "<option value=\"rtl\" selected=\"selected\" >rtl</option>";
	}
}
function settingssidecol($current,$right,$left){
	if($current == "right"){
		echo "<option value=\"right\" selected=\"selected\">".$right."</option>";
		echo "<option value=\"left\">".$left."</option>";
	}else{
		echo "<option value=\"right\">".$right."</option>";
		echo "<option value=\"left\" selected=\"selected\" >".$left."</option>";
	}
}
function settingsheader($current,$display,$hide){
	if($current == true){
		echo "<option value=\"true\" selected=\"selected\">".$display."</option>";
		echo "<option value=\"false\">".$hide."</option>";
	}else{
		echo "<option value=\"true\">".$display."</option>";
		echo "<option value=\"false\" selected=\"selected\" >".$hide."</option>";
	}
}
function languages($current,$alertforbidden,$alertconfirm) {
	$langs = glob("../inc/langs/*.php");
	foreach ($langs as $lang){
		require $lang;
		if (isset($l)){
			echo "<tr><td width=\"70%\">";
			echo $l[s40language];
			echo " (v. ".$l[s40version].") - <a href=\"".$l[s40authorurl]."\" target=\"_blank\">".$l[s40author]."</a>";
			echo "</td><td width=\"10%\">";
			echo $l[s40langpack];
			echo "</td><td width=\"20%\">";
			echo "<a href=\"main.php?a=editor&amp;t=languages&amp;i=".$l[s40langpack]."\"><img src=\"style/edit.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			if ($current == $l[s40langpack]){
				echo "<a href=\"javascript:void(0);\" onclick=\"alert('".$alertforbidden."');\"><img src=\"style/deletedisabled.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			}else{
				echo "<a href=\"main.php?a=delete&amp;l=".$l[s40langpack]."\" onclick=\"if(! confirm('".$alertconfirm."')) return false;\"><img src=\"style/delete.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			}
			echo "</td></tr>";
		}
	}
	
}
function listgadgets(){
	require "../sidecol/gadgets.php";
	$gadgets = glob("../sidecol/*", GLOB_ONLYDIR);
	foreach ($gadgets as $gadget){
		$gadget = str_replace("../sidecol/", "", $gadget);
		$pos = strpos($gadget, $s40[gadgets]);
		if ($pos === false){
			echo "<option value='".$gadget."'>".$gadget."</option>";
		}
	}
}
function gadgets($alertconfirm) {
	$gid = "";
	require "../sidecol/gadgets.php";
	$gadgets = explode(",",$s40[gadgets]);
	foreach ($gadgets as $gadget){
		if (file_exists("../sidecol/".$gadget."/config.php")){
			$gid++;
			require "../sidecol/".$gadget."/config.php";
			echo "<tr><td width=\"5%\" class=\"dndr\">";
			echo $gid;
			echo "</td>";
			echo "<td width=\"65%\">";
			echo "<input name=\"gadgetid[]\" type=\"hidden\" id=\"gadgetid".$gid."\" value=\"".$g[id]."\" />";
			echo $g[name];
			echo " (v. ".$g[version].") - <a href=\"".$g[authorurl]."\" target=\"_blank\">".$g[author]."</a>";
			echo "</td><td width=\"10%\">";
			echo "<img src=\"style/on.png\" width=\"24\" height=\"24\" border=\"0\" />";
			echo "</td><td width=\"20%\">";
			echo "<a href=\"main.php?a=editor&amp;t=gadgets&amp;i=".$g[id]."\"><img src=\"style/edit.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			echo "<a href=\"javascript:void(0);\" onclick=\"if(confirm('".$alertconfirm."')) document.gadget.gadgetid".$gid.".value='';\"><img src=\"style/remove.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			echo "</td></tr>";
			
		}
		
	}
	echo "<tr><td width=\"5%\" class=\"dndr\"> +";
	echo "</td><td colspan=\"3\">";
	echo "<select name=\"gadgetid[]\" id=\"gadgetid0\"><option value=\"\">- - - -</option>";
	echo listgadgets();
    

	echo "</select>";
	echo "</td></tr>";
}
function allgadgets($alertconfirm){
	require "../sidecol/gadgets.php";
	$allgadgets = glob("../sidecol/*/config.php");
	foreach ($allgadgets as $allgadget){
		require $allgadget;
		echo "<tr><td width=\"70%\">";
		echo $g[name];
		echo " (v. ".$g[version].") - <a href=\"".$g[authorurl]."\" target=\"_blank\">".$g[author]."</a>";
		echo "</td><td width=\"10%\">";
		$pos = strpos($g[id], $s40[gadgets]);
		if ($pos === false){
			echo "<img src=\"style/off.png\" width=\"24\" height=\"24\" border=\"0\" />";
		}else{
			echo "<img src=\"style/on.png\" width=\"24\" height=\"24\" border=\"0\" />";
		}
		
		echo "</td><td width=\"20%\">";
		echo "<a href=\"main.php?a=editor&amp;t=gadgets&amp;i=".$g[id]."\"><img src=\"style/edit.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
		echo "<a href=\"main.php?a=delete&amp;g=".$g[id]."\" onclick=\"if(! confirm('".$alertconfirm."')) return false;\"><img src=\"style/delete.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
		echo "</td></tr>";
	}
}
function themes($current,$alertforbidden,$alertconfirm){
	$themes = glob("../themes/*/style.php");
	foreach ($themes as $theme){
		require $theme;
		echo "<tr><td width=\"70%\">";
		echo $css[name];
		echo " (v. ".$css[version].") - <a href=\"".$css[authorurl]."\" target=\"_blank\">".$css[author]."</a>";
		echo "</td><td width=\"10%\">";
		 if (file_exists("../themes/".$css[name]."/thumbnail.png")) {
			echo "<a href=\"../themes/".$css[name]."/thumbnail.png\" rel=\"image\" title=\"".$css[name]."\"><img src=\"style/preview.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
		 }else{
			 echo "<img src=\"style/previewdisabled.png\" width=\"24\" height=\"24\" border=\"0\" />";
		 }
		echo "</td><td width=\"20%\">";
		echo "<a href=\"main.php?a=editor&amp;t=themes&amp;i=".$css[name]."\"><img src=\"style/edit.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			if ($current == $css[name] or $css[name == "default"]){
				echo "<a href=\"javascript:void(0);\" onclick=\"alert('".$alertforbidden."');\"><img src=\"style/deletedisabled.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			}else{
				echo "<a href=\"main.php?a=delete&amp;t=".$css[name]."\" onclick=\"if(! confirm('".$alertconfirm."')) return false;\"><img src=\"style/delete.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
			}
		echo "</td></tr>";
	}
}
function codeeditor($type, $src, $srctree, $area) {
	switch ($type){
		case "languages":
		$filepath = "../inc/langs/";
		$fileload = $filepath.$src.".php";
		$filename = "<li>".$src.".php</li>";
		if ($area == "code"){
			return $fileload;
		}else{
			echo $filename;
		}
		break;
		case "gadgets":
			$filepath = "../sidecol/";
			$files = glob($filepath.$src."/*.php");
			if ($area == "tree"){
				foreach ($files as $file){
					$filename = str_replace($filepath.$src."/","",$file);
					$filename = "<li><a href=\"main.php?a=editor&amp;t=gadgets&amp;i=".$src."&amp;s=".$filename."\">".$filename."</a></li>";
					echo $filename;
				}
			}else{
				if ($srctree != ""){
					$fileload = $filepath.$src."/".$srctree;
					return $fileload;
				}else{
					$fileload = $filepath.$src."/config.php";
					return $fileload;
				}
			}
		break;
		case "themes":
		$filepath = "../themes/";
		$files = glob($filepath.$src."/{*.php,*.css}", GLOB_BRACE);
		if ($area == "tree"){
			foreach ($files as $file){
				$filename = str_replace($filepath.$src."/","",$file);
				$filename = "<li><a href=\"main.php?a=editor&amp;t=themes&amp;i=".$src."&amp;s=".$filename."\">".$filename."</a></li>";
				echo $filename;
			}
		}else{
			if ($srctree != ""){
				$fileload = $filepath.$src."/".$srctree;
				return $fileload;
			}else{
				$fileload = $filepath.$src."/style.css";
				return $fileload;
			}
		}
		break;
	}
}
function backups($nobackup,$deleteconfirm,$restoreconfirm) {
	$archivepath = "../data/";
	$archives = glob($archivepath."*.zip");
	if (count($archives) != 0){
		rsort ($archives);
		foreach ($archives as $archive){
			$archivesize = (filesize($archive)/ 1024);
			echo "<tr><td width=\"50%\" align=\"left\" valign=\"top\">";
			echo str_replace($archivepath,"",$archive);
			echo "</td><td width=\"15%\" align=\"left\" valign=\"top\">";
			echo round($archivesize,2);
			echo "</td><td width=\"35%\" align=\"left\" valign=\"top\">";
			echo "<a href=\"".$archive."\" target=\"_blank\"><img src=\"style/download.png\" width=\"24\" height=\"24\" border=\"0\" /></a> ";
			echo "<a href=\"main.php?a=delete&amp;b=".str_replace($archivepath,"",$archive)."\" onclick=\"if(! confirm('".$deleteconfirm."')) return false;\" ><img src=\"style/delete.png\" width=\"24\" height=\"24\" border=\"0\" /></a> ";
			echo "<a href=\"main.php?a=backup&amp;r=".str_replace($archivepath,"",$archive)."\" onclick=\"if(! confirm('".$restoreconfirm."')) return false;\" ><img src=\"style/restore.png\" width=\"24\" height=\"24\" border=\"0\" /></a> ";
			echo "</td></tr>";
		}
	}else{
		echo "<tr><td colspan=\"3\">";
		echo $nobackup;
		echo "</td></tr>";
	}
}
function maxsize () {
	$max_upload = (int)(ini_get('upload_max_filesize'));
	$max_post = (int)(ini_get('post_max_size'));
	$max_memory = (int)(ini_get('memory_limit'));
	$upload = min($max_upload, $max_post, $max_memory);
	return " ".$upload. " mb max";
}
?>