<?php

include "../admin/adminses.php";
include "../engine/functions.php";

$template=adminmyconfig("template");

$updateornot = @$_POST["Submit"];
$content = @$_POST["article"];
$sitetitle="Редакция шаблона";
if (empty($updateornot)) {
 				$myFile="../templates/$template/template.php";

				$fh = fopen($myFile, 'r');
				$data = fread($fh, filesize($myFile));
				fclose($fh);
				
$url=$_SERVER['PHP_SELF'];			 
@$contentcenter .=<<<EOT
<form action="$url" method="post" name="my_form">
<h3>Редакция шаблона:</h3>Текущий шаблон: <b>$template</b><br /><br />
<textarea name="article" id="article" cols=87 rows=20>$data</textarea> 
<br />
<input type="submit" class="submit-button" id="Submit" name="Submit" value="Изменить шаблон" /><br />
</form>
EOT;
$contentcenter = stripslashes($contentcenter);
}else{
$myFile = "../templates/$template/template.php";

unlink($myFile);

	@chmod("../templates/$template/template.php", 0777);
	$ourFileName = "../templates/$template/template.php";
	$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
	fclose($ourFileHandle);
	
$somecontent .= $content;

$filename=$ourFileName;
if (is_writable($filename)) {

	 $somecontent=stripslashes($somecontent);
   if (!$handle = fopen($filename, 'a')) {
         echo "Невозможно открыть ($filename)";
         exit;
   }

   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Невозможна запись в ($filename)";
       exit;
   }
  
   fclose($handle);
    @chmod("../templates/$template/template.php", 0604);
$contentcenter.="<h1>Обновлено!</h1>";

}


} 

include "admintemplate.php";

?>
