<?php
session_start();
$content=file("../../sg_cm/conf/conf/conf.php");
if (session_id()!=$content[2])
 {
  $url=urlencode("Пройдите_авторизацию!");
  echo "<meta http-equiv=refresh content='0; url=../../sg_cm/admin/index.php?acc=$url'>";
  exit();
 }

if(!isset($_GET['id']) || !isset($_GET['q']) || !isset($_GET['op']))
   {
   	 echo "<meta http-equiv=refresh content='0; url=admin3.php?sel3=selected'>";
     exit();
   }

 if(!file_exists("cons/db/$_GET[id]/$_GET[q]"))
   {
   	 echo "<meta http-equiv=refresh content='0; url=admin3.php?sel3=selected'>";
     exit();
   }

 if(isset($_POST['exit']))
   {
   	 echo "<meta http-equiv=refresh content='0; url=view.php?id=$_GET[id]'>";
     exit();
   }


 if(isset($_POST['save']))
   {
      $_POST['text1']=trim($_POST['text1']);
      $_POST['text2']=trim($_POST['text2']);

      if($_POST['text1']!="" && $_POST['text2']!="")
        {
          $_POST['text1']=stripslashes($_POST['text1']);
          $_POST['text2']=stripslashes($_POST['text2']);
          $_POST['text1']=str_replace("\r\n","<br>",$_POST['text1']);
          $_POST['text2']=str_replace("\r\n","<br>",$_POST['text2']);
          $file=file("cons/db/$_GET[id]/$_GET[q]");
          $f=fopen("cons/db/$_GET[id]/$_GET[q]","w");
          fwrite($f,$file[0].$_POST['text1']."\r\n".$_POST['text2']);
          fclose($f);
        }
   }

 if($_GET['op']==1)
   {
     $file=file("cons/db/$_GET[id]/$_GET[q]");
     $expl=explode("*",$file[0]);
     $file[1]=str_replace("<br>","\r\n",$file[1]);
     $file[2]=str_replace("<br>","\r\n",$file[2]);
   }

if($_GET['op']==2)
   {
     unlink("cons/db/$_GET[id]/$_GET[q]");
     echo "<meta http-equiv=refresh content='0; url=view.php?id=$_GET[id]'>";
     exit();
   }
if($_GET['op']==3)
   {
     $file=file("cons/db/$_GET[id]/$_GET[q]");
     $expl=explode("*",$file[0]);
     $bl=file("conf/bl.txt");
     $search=false;
     foreach($bl as $line)
      {
        $line=trim($line);
        $expl1=explode("*",$line);
        if($expl1[2]==$expl[3])
           {
             $search=true;
             break;
           }
      }
     if(!$search)
        {
          $f=fopen("conf/bl.txt","a+");
          fwrite($f,$expl[1]."*".$expl[2]."*".$expl[3]."\r\n");
          fclose($f);
        }

     echo "<meta http-equiv=refresh content='0; url=view.php?id=$_GET[id]'>";
     exit();
   }


?>
<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8" />
  <title>Редактирование</title>

<style>
  #head
         {
          font-family: Arial, sans-serif;
          font-size:14pt;
          color:#808080;

         }
    #inf
         {
          font-family: Arial, sans-serif;
          font-size:9pt;
          color:#808080;
           font-weight:600;
         }
  #button {

       font-family:"Times New Roman", "serif";
       color:#408080;
       font-size:9pt;
       background-color:#C0C0C0;
      font-weight:600;
      text-align:center;
      width:120px;
         }

</style>
</head>

<body><div id=head>Редактирование</div>
<form  action=red.php?<?php echo "id=$_GET[id]&q=$_GET[q]&op=$_GET[op]"?> method="post">
<?php echo "<div id=inf>$expl[0]&nbsp;&nbsp;&nbsp;$expl[1]&nbsp;&nbsp;&nbsp;$expl[2]&nbsp;&nbsp;&nbsp;$expl[3]</div>" ?>
<textarea name="text1" rows=15 cols=90 ><?php echo $file[1]?></textarea><br />
<textarea name="text2" rows=15 cols=90 ><?php echo $file[2]?></textarea><br />
<input type="submit" value="Сохранить" name=save id=button>
&nbsp;&nbsp;<input type="submit" value="Выход" name=exit id=button>
</form>





</body>

</html>