<?php
session_start();
$w=file("ac/sess");
$id="";
foreach($w as $line)
 {
   $expl=explode("*",$line);
   $line=trim($line);
   if(session_id()==$expl[1])
      {
        $id=$expl[2];
        $name=$expl[3];
        break;
      }
 }
if($id=="")
  {
    echo "<meta http-equiv=refresh content='0; url=index.php'>";
    exit();
  }
if(isset($_GET['q']))
   {
    if(!file_exists("quest/$_GET[q]") || !is_numeric($_GET['q']))
       {
        echo "<meta http-equiv=refresh content='0; url=admin1.php?sel1=selected'>";
        exit();
       }
   }
else
   {
        echo "<meta http-equiv=refresh content='0; url=admin1.php?sel1=selected'>";
        exit();
   }
 $w=file("quest/$_GET[q]");
 $expl=explode("*",$w[0]);

 if(isset($_POST['exit']))
  {
  	echo "<meta http-equiv=refresh content='0; url=admin1.php?sel1=selected'>";
    exit();
  }
 
 
//почта 
$mset=file('../../../sg_cm/conf/set.php');
for($i=0; $i<count($mset); $i++) $mset[$i]=trim($mset[$i]);
if ($mset[4]=="X5" && is_file("../../../res/imemail.inc.php")) {$incl="../../../res/imemail.inc.php";}
elseif (is_file("../../../sg_cm/include/email_libM.php")) {$incl="../../../sg_cm/include/email_libM.php"; $mset[4]=="libM";}
elseif (is_file("../../../res/imemail.inc.php")) {$incl="../../../res/imemail.inc.php"; $mset[4]=="X5";}
else exit("Не найден скрипт отправки почты");
require($incl); 
require ('../../../sg_cm/include/mailtemplater.php');
require ('../../../sg_cm/root.php');

$server=rtrim($_SERVER['SERVER_NAME']."/".$sg_root,'/');

if(isset($_POST['go']))
  {
    $_POST['text']=trim($_POST['text']);
    if($_POST['text']!="")
       {
         $_POST['text']=str_replace("\r\n","<br>",$_POST['text']);
         $_POST['text']=stripslashes($_POST['text']);
         $w[0]=trim($w[0]);
         $expl=explode("*",$w[0]);
         if($expl[4]=="")$w[0].="$id";
         $w[0].="\r\n";
         $f=fopen("db/$id/$_GET[q]","w+");

         fwrite($f,$w[0].$w[1].$_POST['text']);
         fclose($f);
         unlink("quest/$_GET[q]");

         //Письмо спросившему
         $subj= "На Ваш вопрос ответили.";
         $mess=
"Здравствуйте, $expl[1]! Вы задавали вопрос на сайте http://".$server." 
 На ваш вопрос ответили.
C уважением. Администратор.";
         $mess=str_replace("\r\n\r\n","\r\n",$mess);
	$nnews=array (0=>'');
	$t=array (0=>'',1=>'',2=>'',3=>'');
	$t[0]=ucfirst($server);
	$nnews[0]=$mess;
	if ($mset[3]!='TXT') $mess=mailtemplater($nnews,$t);
	else $mess='';
	if ($mset[3]!='HTML')$mess_txt=mailtemplater_txt($nnews,$t);
	else $mess_txt='';		 
         $to=$expl[2]; require("../../../sg_cm/conf/email_set.php");
         echo "<meta http-equiv=refresh content='0; url=admin1.php?sel1=selected'>";
         exit();
       }
  }
?>
<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8" />
  <title>Ответ на вопросы</title>
<style>
.consTable {width:85%; max-width:1200px;}	
  #cit
         {
          color:#808080;
          font-size:11pt;
          font-family: Arial, sans-serif;
         }
  h2
    {
       color:#408080;
       font-size:14pt;
       font-family: Arial, sans-serif;
    }
   #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>
<h2>Ответ на вопрос</h2>
<table align=center class=consTable bgcolor=#ffffff  CELLPADDING=5>
                  <tr>
                      <td id=cit>
                         <font color=#408080><b><?php echo $expl[0]?>&nbsp;&nbsp;<?php echo $expl[1]?></b></font><br />
                         <?php echo $w[1]?>
                      </td>
                 </tr>


                 <tr><td><form  action=<?php echo "repl.php?q=$_GET[q]" ?> method="post">
                    <textarea name="text" rows=20 cols=80 ></textarea><br />
                    <input type="submit" value="Ответить" name=go id=button>&nbsp;
                    <input type="submit" value="Выход" name=exit id=button>
                         </form></td></tr>
               </table>


</body>

</html>