<?php
include('cap.php');
if(!isset($_GET['id']) ||  !file_exists("db/news/$_GET[id]") )
  {
    echo "<meta http-equiv=refresh content='0; url=admin2.php'>";
    exit();
  }

if(!isset($_GET['c']) || !is_int(intval($_GET['c']))) $_GET['c']=1;
if(!isset($_GET['ind1'])) $_GET['ind1']=1;
if(!isset($_GET['page1'])) $_GET['page1']=1;

if(!isset($_GET['op'])) $_GET['op']=4;

if(isset($_POST['exit']))
  {
     echo "<meta http-equiv=refresh content='0; url=admin2.php?c=$_GET[c]&page=$_GET[page1]&ind=$_GET[ind1]'>";
    exit();
  }



  $comm=array();
   $d=opendir("db/comm/$_GET[id]");
           while(($e=readdir($d))!=false)
            {
             if($e =="." || $e ==".." || $e==".htaccess") continue;
             $comm[]=$e;
            }
           closedir($d);
           rsort($comm,SORT_NUMERIC);

  //Постраничная навигация
          $topic_count_page=20;
         if(!isset($_GET['page']) || !is_numeric(@$_GET['page']) || @$_GET['page']<1 )$page=1;
         else $page=$_GET['page'];
         if($page > ceil(count($comm)/ $topic_count_page))$page=1;
         $start=$page * $topic_count_page-$topic_count_page;
         $pages=ceil(count($comm)/ $topic_count_page);
        if(empty($_GET['ind']) || !is_numeric(@$_GET['ind']) || @$_GET['ind']>ceil($pages/$topic_count_page)  || @$_GET['ind']<1)$index=1;
        else $index= $_GET['ind'];


if(isset($_POST['red']) && isset($_GET['comm_id'])&& file_exists("db/comm/$_GET[id]/$_GET[comm_id]"))
  {
    $_POST['comm_name']=trim($_POST['comm_name']);
    $_POST['comm_text']=trim($_POST['comm_text']);
    $_POST['comm_admin']=trim($_POST['comm_admin']); /* ответ админа */
    $_POST['comm_name']=stripcslashes($_POST['comm_name']);
    $_POST['comm_text']=stripcslashes($_POST['comm_text']);
    $_POST['comm_text']=str_replace("\r\n","<br>",$_POST['comm_text']);
    $_POST['comm_admin']=stripcslashes($_POST['comm_admin']);
    $_POST['comm_admin']=str_replace("\r\n","<br>",$_POST['comm_admin']);
if ($_POST['comm_admin']!="")	
{	$_POST['comm_admin']="**:***:*: ADMIN: ".$_POST['comm_admin'];}
	
	
	
    if($_POST['comm_name']!="" && $_POST['comm_text']!="")
      {
	  $_POST['comm_text'].=$_POST['comm_admin'];
         $file=file("db/comm/$_GET[id]/$_GET[comm_id]");
         for($n=0;$n<count($file);$n++)$file[$n]=trim($file[$n]);
         $f=fopen("db/comm/$_GET[id]/$_GET[comm_id]","w");
         fwrite($f,"$file[0]\r\n$file[1]\r\n$_POST[comm_name]\r\n$_POST[comm_text]");
         fclose($f);

          echo "<meta http-equiv=refresh content='0; url=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$page&ind=$index'>";
          exit();
      }
  }

if($_GET['op']==2 && isset($_GET['comm_id'])&& file_exists("db/comm/$_GET[id]/$_GET[comm_id]"))
  {
  	 unlink("db/comm/$_GET[id]/$_GET[comm_id]");
     echo "<meta http-equiv=refresh content='0; url=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$page&ind=$index'>";
     exit();
  }

 if($_GET['op']==3 && isset($_GET['comm_id'])&& file_exists("db/comm/$_GET[id]/$_GET[comm_id]"))
  {
  	 $file=file("db/comm/$_GET[id]/$_GET[comm_id]");
     $ip=trim($file[0]);

     $file=file("db/bl.txt");
     for($n=0;$n<count($file);$n++)$file[$n]=trim($file[$n]);

     if(!in_array($ip,$file))
       {
       	 $f=fopen("db/bl.txt","a+");
         fwrite($f,"$ip\r\n");
         fclose($f);
       }

     echo "<meta http-equiv=refresh content='0; url=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$page&ind=$index'>";
     exit();
  }

if(count($comm)> $topic_count_page)
    {
      //Постраничная навигаця
         echo "<table width=80%  align=center><tr><td>";

          if($index>1)echo "
         <a class='navig_passiv' href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=".(($index-1)*10)."&ind=".($index-1)." title=назад><<</a>";

         for($i=$index*10-9,$p=1; $i < $pages+1; $i++,$p++)
          {

            if($p>10 )
          	  {
          	  	echo "<a class='navig_passiv'  href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=".($i)."&ind=".($index+1)." title=далее>>></a>";
          	  	break;
          	  }
          	if($page==$i)
            echo "<a class='navig_activ' href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$i&ind=$index>$i</a>";
            else
            echo "<a class='navig_passiv'href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$i&ind=$index>$i</a>";
          }


       echo "</td></tr></table><br />";
    }

echo"<table  class=tab ><tr >
  <td  valign=top>  
<form method=post><input type=submit value=Назад id=button name=exit>";

 for($x=$start,$y=0; $x<count($comm); $x++,$y++)
     {
     	if($y==$topic_count_page)break;

     	$text=file("db/comm/$_GET[id]/$comm[$x]");
     	for($n=0;$n<count($text);$n++)$text[$n]=trim($text[$n]);

        echo "<br><table  class=tab1  align=center width=100% border=0>
        <tr><td valign=top><font color=#408080>$text[1]</font>";
       if($_GET['op']==1 && @$_GET['comm_id']==$comm[$x])
         {
          $text[3]=str_replace("<br>","\r\n",$text[3]);
		  $comm_text_adm = explode("**:***:*: ADMIN: ", $text[3]);
		  $text_text=$text_text=$comm_text_adm[0];
		  $text_adm="";
			if (count($comm_text_adm)>1) { $text_adm=$comm_text_adm[1];}
			
           echo"<br /><input id=\"aforma\"  name=comm_name type=text value=$text[2] size=20><br /><textarea name=comm_text rows=10 cols=100 >$text_text</textarea><br /><br />
		   Ответ админа:<br />
		   <textarea name=comm_admin rows=10 cols=100 >$text_adm</textarea><br />
            <input type=submit value=Сохранить id=button name=red>
          </td></tr><br />";
          }
       else
	   {  $text[3]=str_replace("**:***:*: ADMIN: ","<br>****<br>ADMIN:<br>",$text[3]);
          echo"&nbsp;&nbsp;$text[2]<br /><br />$text[3]
             </td></tr><tr><td  align=right class=tab>
             <a href=comm.php?comm_id=$comm[$x]&op=1&id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$page&ind=$index#aforma>редактировать/ответить</a>&nbsp;&nbsp;&nbsp;&nbsp;
             <a href=#  onclick=\"del('comm.php?comm_id=$comm[$x]&op=2&id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$page&ind=$index','комментарий');return false;\" >удалить</a>&nbsp;&nbsp;&nbsp;&nbsp;
             <a href=comm.php?comm_id=$comm[$x]&op=3&id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$page&ind=$index>в чёрный список</a></td></tr></table><br />";}
     }



if(count($comm)> $topic_count_page)
    {
      //Постраничная навигаця
         echo "<table width=80%  align=center><tr><td>";

          if($index>1)echo "
         <a class='navig_passiv' href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=".(($index-1)*10)."&ind=".($index-1)." title=назад><<</a>";

         for($i=$index*10-9,$p=1; $i < $pages+1; $i++,$p++)
          {

            if($p>10 )
          	  {
          	  	echo "<a class='navig_passiv'  href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=".($i)."&ind=".($index+1)." title=далее>>></a>";
          	  	break;
          	  }
          	if($page==$i)
            echo "<a class='navig_activ' href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$i&ind=$index>$i</a>";
            else
            echo "<a class='navig_passiv'href=comm.php?id=$_GET[id]&c=$_GET[c]&page1=$_GET[page1]&ind1=$_GET[ind1]&page=$i&ind=$index>$i</a>";
          }


       echo "</td></tr></table><br />";
      }

echo"</form>
</td></tr>
 </table> ";
?>

</div>
</body>
</html>