<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
	<meta http-equiv="Content-Language" content="ru">
	<title>Landgraph Show Administration</title>
</head>
<body bgcolor="#808080" text="#FFFFFF" link="#d8d8d8" vlink="#d8d8d8" alink="#d8d8d8">

<?php


include('lib.php');

function ShowAll($log, $pas)
{
$datafile = "showdata.inc";
$df = file($datafile);
$cnt = count($df);
 echo "<a href=\"javascript: window.open('adminshow.php?action=menu&login=$log&pass=$pas','','toolbar=0,width=300,height=250');void(0);\">Меню</a>
 &nbsp;&nbsp;<a href=\"adminshow.php?action=view&login=$log&pass=$pas\">Обновить</a>";
 echo '<br><br>
 <table width="300" cellspacing="0" cellpadding="0" border="1">
	<tr>
    	<th>Запрос</th>
		<th>Заголовок</th>
    	<th colspan="3" align="250">Файл(ы)</th>
	</tr>';

 for($i=0; $i<$cnt; $i++)
 {
  $data=explode("|", $df[$i]);
  echo "<tr>
  		<td align=\"center\">$data[0]</td>
		<td align=\"center\">$data[2]</td>
		<td align=\"center\">$data[1]</td>
		<td align=\"center\"><a href=\"adminshow.php?action=del&login=$log&pass=$pas&num=$i\">
		<img src=\"dellink.GIF\" width=\"24\" height=\"24\" border=\"0\" alt=\"Редактировать\"></a>
		</td><td>
		<a href=\"javascript: window.open('adminshow.php?action=edit&login=$log&pass=$pas&id=$i','','toolbar=0,width=300,height=150');void(0);\">
		<img src=\"edit.jpg\" width=\"20\" height=\"20\" border=\"0\" alt=\"Изменить\"></a></td>
		</tr>";
 }
echo "</table>";
}

function ShowForm($log, $pas)
{
echo '<form action="adminshow.php" method="get">
<input type="hidden" name="action" value="add">';
echo "<input type=\"hidden\" name=\"login\" value=\"$log\">";
echo "<input type=\"hidden\" name=\"pass\" value=\"$pas\">";
echo '<table border="0">
<tr><td>Запрос: </td><td><input type="text" name="name"></td></tr>
<tr><td>Заголовок: </td><td><input type="text" name="title"</td></tr>
<tr><td>Файл: </td><td><input type="text" name="fl"></td></tr>
<tr><td colspan="2"><input type="Submit" value="Добавить"></td></tr>
</table>
</form>';
}

function ShowLoginForm()
{
  echo '
  <form action="adminshow.php" method="get">
  <input type="hidden" name="action" value="view">
  <input name="login">
  <input name="pass">
  <input type="Submit" value="Logon">
  </form>';
}

 if(!$login || $login=="" || $login!=$adminname)
 {
  echo "Ошибка аутентификации!!!<br>";
  ShowLoginForm();
  exit;
 }
 if(!$pass || $pass=="" || $pass!=$adminpass)
 {
  echo "Ошибка аутентификации!!!<br>";
  ShowLoginForm();
  exit;
 }

switch($action)
{
 case "add";
 $fp=fopen("showdata.inc", "a+");
 fwrite($fp, $name."|".$fl."|".$title."\n");
 fclose($fp);
 ShowAll($login, $pass);
 ShowForm($login,$pass);
 break;

 case "view";
 ShowAll($login, $pass);
 ShowForm($login,$pass);
 break;

 case "del";
 $fl=fopen($datafile, "r");
 $olddata=fread($fl, filesize($datafile));
 fclose($fl);
 $olddata=explode("\n", $olddata);
 $cnt=count($olddata);
 if($num<0 || $num>=$cnt)
  echo "Ошибка, такой записи нет!!!";
 else
 {
 for($i=0; $i<$num; $i++)
 {
   $newdata[$i]=$olddata[$i];
 }
 for($i=$num+1; $i<$cnt; $i++)
 {
   $newdata[$i]=$olddata[$i];
 }
 $newdata=implode("\n", $newdata);
 $fl=fopen($datafile, "w");
 fwrite($fl, $newdata);
 fclose($fl);
 }
 ShowAll($login, $pass);
 ShowForm($login,$pass);
 break;

 case "stat";
 $fp=fopen($counterfile, "r");
 $arr=fread($fp, filesize($counterfile));
 $arr=explode("\n", $arr);
 $cnt=count($arr);
 echo '<table cellspacing="0" cellpadding="0" border="1" width="100%">
 		<tr><th>Файл</th><th>Кол-во запросов</th></tr>';

 for($i=0; $i<$cnt; $i++)
 {
  $data=explode("|", $arr[$i]);
  if(trim($data[0])=="")
   continue;
  echo "<tr><td align=\"center\">$data[0]</td>";
  echo "<td align=\"center\">$data[1]</td></tr>";  
 }
 echo "</table>";
 fclose($fp);
 break;
 
 case "settop";
  if(trim($text)!="")
  {
   $fp=fopen($top, "w");
   $text=str_replace('\"', '"', $text);
   fwrite($fp, $text);
   fclose($fp);
  }
  else
   echo "Не было получено параметров!!!";
// ShowAll($login, $pass);
// ShowForm($login,$pass);
 echo "<a href=\"javascript: window.close()\">Закрыть</a>";
 echo "<script language=\"Javascript\">window.close()</script>";
 break;

 case "setbottom";
  if(trim($text)!="")
  {
   $fp=fopen($bottom, "w");
   $text=str_replace('\"', '"', $text);
   fwrite($fp, $text);
   fclose($fp);
  }
  else
   echo "Не было получено параметров!!!";
// ShowAll($login, $pass);
// ShowForm($login,$pass);
 echo "<a href=\"javascript: window.close()\">Закрыть</a>";
 echo "<script language=\"Javascript\">window.close()</script>";
 break;
 
 case "viewtop";
  echo "Файл $top:<br>";
  echo "<form action=\"adminshow.php?action=settop&login=$login&pass=$pass\" method=\"post\">";
  echo "<textarea cols=\"70\" rows=\"20\" name=\"text\">";
  $fp=fopen($top, "r");
  $data=fread($fp, filesize($top));
  fclose($fp);
  echo "$data</textarea><br>";
  echo "<input type=\"Submit\" value=\"Сохранить\"></form>";
 break;
 
 case "viewbottom";
  echo "Файл $bottom:<br>";
  echo "<form action=\"adminshow.php?action=setbottom&login=$login&pass=$pass\" method=\"post\">";
  echo "<textarea cols=\"70\" rows=\"20\" name=\"text\">";
  $fp=fopen($bottom, "r");
  $data=fread($fp, filesize($bottom));
  fclose($fp);
  echo "$data</textarea><br>";
  echo "<input type=\"Submit\" value=\"Сохранить\"></form>";
 break;
  
 case "edit";
  $fp=file($datafile);
  if(!$id || $id>=count($fp))
   $id=0;
  $data=explode("|",$fp[$id]);
  echo "
  <form action=\"adminshow.php?action=replace&login=$login&pass=$pass&id=$id\" method=\"post\">
  <table align=\"center\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
<tr>
    <td>Запрос: </td>
    <td><input type=\"text\" value=\"$data[0]\" name=\"name\"></td>
</tr>
<tr>
    <td>Заголовок: </td>
    <td><input type=\"text\" value=\"$data[2]\" name=\"title\"</td>
</tr>
<tr>
    <td>Файл(ы): </td>
    <td><input type=\"text\" value=\"$data[1]\" name=\"fl\"</td>
</tr>
<tr>
    <td colspan=\"2\" align=\"center\"><input type=\"Submit\" value=\"Изменить\"></td>
</tr>
</table>
</form>
<p align=\"center\"><a href=\"javascript: window.close()\">Закрыть</a></p>
";
 break;
 
 case "replace";
  $fp=file($datafile);
  if(!$id || $id>=count($fp))
   $id=0;
  $fs=fopen($datafile, "w");
  for($i=0; $i<count($fp); $i++)
  {
   $data=str_replace("\n","",$fp[$i]);
   if($i!=$id)
    fwrite($fs, $data."\n");
   else
    fwrite($fs, $name."|".$fl."|".$title."\n");
  }
  fclose($fs);
  echo "<p align=\"center\" valign=\"middle\">Изменения приняты<br><a href=\"javascript: window.close()\">Закрыть</a></p>";
 break;
 
 case "menu";
  echo "<h3 align=\"center\">МЕНЮ</h3>
   <ul><li>
<a href=\"javascript: window.open('adminshow.php?action=stat&login=$login&pass=$pass', '', 'toolbar=0,width=400,height=300');void(0);\">Статистика</a>
</li>
<li>
<a href=\"javascript: window.open('adminshow.php?action=viewtop&login=$login&pass=$pass','','toolbar=0,width=600,height=450');void(0);\">Изменение $top</a>
</li>
<li>
<a href=\"javascript: window.open('adminshow.php?action=viewbottom&login=$login&pass=$pass','','toolbar=0,width=600,height=450');void(0);\">Изменение $bottom</a>
</li>
<li>
<a href=\"javascript: window.open('help/index.html','','toolbar=0,scrollbars=1,width=620,height=450');void(0);\">Помощь</a>
</li>
<li>
<a href=\"javascript: window.close()\">Закрыть</a>
</li></ul>
<h2 align=\"center\">PHPShow</h2>";
 break;
 
 default;
  ShowLoginForm();
 break;
}
?>
</body></html>
