<?php
function quotes($quote){
	$fq = array("\""); 
    $rq = array("&quot;"); 
    return stripslashes(str_replace($fq, $rq, $quote));
}
function squotes($squote){
	$fq = array("\'"); 
    $rq = array("&rsquo;"); 
    return stripslashes(str_replace($fq, $rq, $squote));
}
function htmlquotes($hquote){
	$fq = array("\""); 
    $rq = array("\'"); 
    return stripslashes(str_replace($fq, $rq, $hquote));
}
function rettype($type){
	$ptype = array($type);
	$ptype = explode("|", $type);
	return $ptype[0];
}
function retpos($pos){
	$ppos = array($pos);
	$ppos = explode("|", $pos);
	return $ppos[1];
}
function retdescr($descr,$cont){
	if (strlen($descr) == 0){
		$description = strip_tags($cont);
		$description = substr($description,0,200);
		$description = quotes($description);
		return $description;
	}else{
		return $descr;
	}
}
function pagelog($id,$log){
	$file = "inc/pagelog.log";
	$fh = fopen($file, "a") or die("can't open file");
	$confData = $id."-".$log."|";
	fwrite($fh, $confData);
	fclose($fh);

}
function subnotes($note){
	echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=".$note."&a=pages\";</script>";
}
function settings($installed,$title,$version,$slogan,$keywords,$description,$theme,$lang,$langdir,$home,$name,$passkey,$user,$pass,$sidecol,$imagesize,$thumbsize,$headerbar){
	$file = "../inc/config.php";
	$fh = fopen($file, "w") or die("can't open file");
	$confData = "<?php" . " \r\n";
	$confData .= "// S40 CMS by AWEN art studio" . " \r\n";
	$confData .= "\$s40[installed] 			= 	$installed;" . " \r\n";
	$confData .= "\$s40[title] 				= 	\"".$title."\";" . " \r\n";
	$confData .= "\$s40[version] 				= 	\"".$version."\";" . " \r\n";
	$confData .= "\$s40[slogan] 			= 	\"".$slogan."\";" . " \r\n";
	$confData .= "\$s40[keywords] 			= 	\"".$keywords."\";" . " \r\n";
	$confData .= "\$s40[description] 		= 	\"".$description."\";" . " \r\n";
	$confData .= "\$s40[theme] 				= 	\"".$theme."\";" . " \r\n";
	$confData .= "\$s40[lang] 				= 	\"".$lang."\";" . " \r\n";
	$confData .= "\$s40[langdir] 				= 	\"".$langdir."\";" . " \r\n";
	$confData .= "\$s40[home] 				= 	\"".$home."\";" . " \r\n";
	$confData .= "\$s40[name] 				= 	\"".$name."\";" . " \r\n";
	$confData .= "\$s40[passkey] 			= 	\"".$passkey."\";" . " \r\n";
	$confData .= "\$s40[user] 				= 	\"".$user."\";" . " \r\n";
	$confData .= "\$s40[pass] 				= 	\"".base64_encode($pass.$passkey)."\";" . " \r\n";
	$confData .= "\$s40[sidecol] 			= 	\"".$sidecol."\";" . " \r\n";
	$confData .= "\$s40[imagesize] 			= 	\"".$imagesize."\";" . " \r\n";
	$confData .= "\$s40[thumbsize] 			= 	\"".$thumbsize."\";" . " \r\n";
	$confData .= "\$s40[headerbar] 			= 	".$headerbar.";" . " \r\n";
	$confData .= "?>";	
	fwrite($fh, $confData);
	fclose($fh);
	echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=602&a=settings\";</script>";
}


function newpage($pagetitle,$pageid,$pagetype,$pageorder,$pageparent,$pagekeywords,$pagedescription,$pagecontent,$pageimages,$pagelog){
	if($pageparent == ""){
		$file = "../data/".urlencode($pageid).".page.php";
	}else{
		$file = "../data/".urlencode($pageid).".child.".urlencode($pageparent).".php";
	}
		$fh = fopen($file, "w") or die("can't open file");
		$stringData = "<?php" . " \r\n";
		$stringData .= "\$page[order]					= 	\"".$pageorder."\";" . " \r\n";
		$stringData .= "\$page[id]						= 	\"".$pageid."\";" . " \r\n";
		$stringData .= "\$page[ch]						= 	\"".$pageparent."\";" . " \r\n";
		$stringData .= "\$page[type]					= 	\"".rettype($pagetype)."\";" . " \r\n";
		$stringData .= "\$page[textpos]					= 	\"".retpos($pagetype)."\";" . " \r\n";
		$stringData .= "\$page[title]					= 	\"".$pagetitle."\";" . " \r\n";
		$stringData .= "\$page[description]				= 	\"".retdescr($pagedescription,$pagecontent)."\";" . " \r\n";
		$stringData .= "\$page[keywords]				= 	\"".$pagekeywords."\";" . " \r\n";
		$stringData .= "\$page[content]					= 	\"".htmlquotes(squotes($pagecontent))."\";" . " \r\n";
		$stringData .= "\$page[media]					= 	\"".$pageimages."\";" . " \r\n";
		$stringData .= "\$page[pagelog]					= 	\"".$pagelog."\";" . " \r\n";
		$stringData .= "?>";	
		fwrite($fh, $stringData);
		fclose($fh);
	pagelog($pageid, $pagelog);	
	subnotes(600);
}


function editpages($pagetitle,$pageid,$pagetype,$pageorder,$pageparent,$pagekeywords,$pagedescription,$pagecontent,$expageimages,$pageimages,$pagelog,$expid){
	
	if(strlen($expageimages) != 0 and strlen($pageimages) != 0){
		$newpageimages = $expageimages.",".$pageimages;
	}else{
		$newpageimages = $expageimages.$pageimages;
	}
	unlink ("../data/".$expid);
	if($pageparent == ""){
		$file = "../data/".$pageid.".page.php";
	}else{
		$file = "../data/".$pageid.".child.".$pageparent.".php";
	}
		$fh = fopen($file, "w") or die("can't open file");
		$stringData = "<?php" . " \r\n";
		$stringData .= "\$page[order]					= 	\"".$pageorder."\";" . " \r\n";
		$stringData .= "\$page[id]						= 	\"".$pageid."\";" . " \r\n";
		$stringData .= "\$page[ch]						= 	\"".$pageparent."\";" . " \r\n";
		$stringData .= "\$page[type]					= 	\"".rettype($pagetype)."\";" . " \r\n";
		$stringData .= "\$page[textpos]					= 	\"".retpos($pagetype)."\";" . " \r\n";
		$stringData .= "\$page[title]					= 	\"".$pagetitle."\";" . " \r\n";
		$stringData .= "\$page[description]				= 	\"".retdescr($pagedescription,$pagecontent)."\";" . " \r\n";
		$stringData .= "\$page[keywords]				= 	\"".$pagekeywords."\";" . " \r\n";
		$stringData .= "\$page[content]					= 	\"".htmlquotes(squotes($pagecontent))."\";" . " \r\n";
		$stringData .= "\$page[media]					= 	\"".$newpageimages."\";" . " \r\n";
		$stringData .= "\$page[pagelog]					= 	\"".$pagelog."\";" . " \r\n";
		$stringData .= "?>";	
		fwrite($fh, $stringData);
		fclose($fh);
	subnotes(601);
}


function delete($page, $child){
	if (isset($child)){
		if(file_exists("../data/".$page.".child.".$child.".php")){
			if(unlink("../data/".$page.".child.".$child.".php")){
				subnotes(606);
			}else{
				subnotes(607);
			}
		}else{
			subnotes(607);
		}
	}else{
		if(file_exists("../data/".$page.".page.php")){
			if(unlink("../data/".$page.".page.php")){
				subnotes(606);
			}else{
				subnotes(607);
			}
		}else{
			subnotes(607);
		}
	}
}
function deleteimage($src){
	if (file_exists("../media/thumbs/".$src."")){
		unlink("../media/thumbs/".$src."");
		unlink("../media/images/".$src."");
		unlink("../media/original/".$src."");
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=605&a=gallery\";</script>";
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=604&a=gallery\";</script>";
	}
}
function deletelang($src) {
	if(file_exists("../inc/langs/".$src.".php")){
		unlink("../inc/langs/".$src.".php");
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=603&a=addons\";</script>";
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=610&a=addons\";</script>";
	}
}
function deletegadget($src){
	if(is_dir("../sidecol/".$src."/")){
		$deletes = glob("../sidecol/".$src."/*");
		foreach ($deletes as $delete){
			unlink ($delete);
		}
		rmdir ("../sidecol/".$src."/");
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=603&a=addons\";</script>";
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=610&a=addons\";</script>";
	}
}
function deletetheme($src){
	if(is_dir("../themes/".$src."/")){
		$folders = glob("../themes/".$src."/*", GLOB_ONLYDIR);
		foreach ($folders as $folder){
			$ffiles = glob($folder."/*");
			foreach($ffiles as $ffile){
				unlink($ffile);
			}
			rmdir($folder);
		}
		$files = glob("../themes/".$src."/*");
		foreach ($files as $file){
			unlink ($file);
		}
		rmdir ("../themes/".$src."/");
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=603&a=addons\";</script>";
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=610&a=addons\";</script>";
	}
}
function deletebackup($src){
	if(file_exists("../data/".$src)){
		unlink("../data/".$src);
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=603&a=backup\";</script>";
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=610&a=backup\";</script>";
	}
}
function ezip ($zipsrc, $zipdest){
	$zip = new ZipArchive();   
	$zip->open($zipsrc);
	$zip->extractTo($zipdest);
	$zip->close();
	unlink($zipsrc);
}

function langup(){
	if (!$_FILES['langzip']['name'] or !preg_match("/\.zip\b/i", $_FILES['langzip']['name'])){
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=700&a=addons\";</script>";
	}else{
		$tmp = "../tmp/";
		$tmp = $tmp .  basename($_FILES['langzip']['name']);
		if(move_uploaded_file($_FILES['langzip']['tmp_name'], $tmp)) {
			ezip("../tmp/".$_FILES['langzip']['name'],"../inc/langs/");
			echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=702&a=addons\";</script>";
		}else{
			echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=701&a=addons\";</script>";
		}

	}

}
function gadgetsupdate(){
	$activegadgets = "";
	for ($n="0"; $n<(count($_POST[gadgetid]));$n++){
		if(strlen($_POST[gadgetid][$n]) != 0){
			$activegadgets .= $_POST[gadgetid][$n].",";
		}
	}
	if (strlen($activegadgets) != 0){
		$activegadgets = substr($activegadgets, 0, -1);
		$file = "../sidecol/gadgets.php";
		$fh = fopen($file, "w") or die("can't open file");
		$stringData = "<?php" . " \r\n";
		$stringData .= "\$s40[gadgets]					= 	\"".$activegadgets."\";" . " \r\n";
		$stringData .= "?>";	
		fwrite($fh, $stringData);
		fclose($fh);
	}
	echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=704&a=addons\";</script>";

	
}
function gadgetup(){
	if (!$_FILES['gadgetzip']['name'] or !preg_match("/\.zip\b/i", $_FILES['gadgetzip']['name'])){
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=700&a=addons\";</script>";
	}else{
		$tmp = "../tmp/";
		$tmp = $tmp .  basename($_FILES['gadgetzip']['name']);
		if(move_uploaded_file($_FILES['gadgetzip']['tmp_name'], $tmp)) {
			if (is_dir("../sidecol/".str_replace(".zip","",basename($_FILES['gadgetzip']['name'])))){
				unlink("../tmp/".$_FILES['gadgetzip']['name']);
				echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=703&a=addons\";</script>";
			}else{
				ezip("../tmp/".$_FILES['gadgetzip']['name'],"../sidecol/");
				echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=702&a=addons\";</script>";
			}
		}else{
			echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=701&a=addons\";</script>";
		}

	}

}
function themeup(){
	if (!$_FILES['themezip']['name'] or !preg_match("/\.zip\b/i", $_FILES['themezip']['name'])){
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=700&a=addons\";</script>";
	}else{
		$tmp = "../tmp/";
		$tmp = $tmp .  basename($_FILES['themezip']['name']);
		if(move_uploaded_file($_FILES['themezip']['tmp_name'], $tmp)) {
			if (is_dir("../themes/".str_replace(".zip","",basename($_FILES['themezip']['name'])))){
				unlink("../tmp/".$_FILES['themezip']['name']);
				echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=703&a=addons\";</script>";
			}else{
				ezip("../tmp/".$_FILES['themezip']['name'],"../themes/");
				echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=702&a=addons\";</script>";
			}
		}else{
			echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=701&a=addons\";</script>";
		}

	}

}
function backup() {
	$filepath = "../data/";
	$archive = $filepath.date("YmdHis").".zip";
	$zip = new ZipArchive;
	$filesarchived = "";
	if ($zip -> open($archive, ZipArchive::CREATE) === true){
		$files = glob($filepath."*.php");
		foreach ($files as $file){
			$zip->addFile($file, str_replace($filepath,"",$file));
			$filesarchived++;
		}
		$zip->close();
		if ($_POST[download]){
			echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=800&a=backup&archived=".$filesarchived."&download=true&archive=".$archive."\";</script>";
		}else{
			echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=800&a=backup&archived=".$filesarchived."\";</script>";
		}
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=801&a=backup\";</script>";
	}
	
}
function backuprestore($src){
	$deletes = glob("../data/*.php");
	foreach ($deletes as $delete){
		unlink ($delete);
	}
	$zip = new ZipArchive();   
	if ($zip->open("../data/".$src) === true){
		$zip->extractTo("../data/");
		$zip->close();
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=802&a=backup\";</script>";
	}else{
		copy("inc/index.page.php","../data/index.page.php");
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=803&a=backup\";</script>";
	}
}
?>