<?
// ChatBot v1
// Alex Kovalyov 2003 | hide@address.com
include("config.php");
session_start();
if ($cfg["user"] == $user && $pass == $cfg["pass"])
{
session_register("user", $user);
session_register("pass", $pass);
}
// logout
if ($logout == "now")
{
session_unset();
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
}
// deleting answer
if (!empty($del))
{
delstr($del, $wha);
refr();
}
if (!empty($adda))
{
addask(trim($newask));
refr();
}
if (!empty($addans))
{
addanswer(trim($newanswer), $askk);
refr();
}
function getfilestr($file)
{
$fd = fopen ("$file", "r");
while (!feof ($fd))
{
$buffer .= fgets($fd, 1024);
}
fclose ($fd);
return $buffer;
}
function refr()
{
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
}
// deletes answer
function delstr($del, $wha)
{
global $cfg;
prepreg(&$del);
//die($del);
$fp = fopen($cfg["answersfile"],'a+');
$file = getfilestr($cfg["answersfile"]);
if ($wha == "ask")
$file = preg_replace("/\n(.*?)$del;\n/mi","\n",$file);
else
$file = preg_replace("/;$del;/",";",$file);
flock ($fp,2);
ftruncate($fp,0);
fwrite($fp,$file);
flock($fp,3);
fclose($fp);
}
// adds ask
function addask($ask)
{
global $cfg;
bust($ask);
$fp = fopen($cfg["answersfile"],'a+');
flock ($fp,2);
fwrite($fp,"$ask;\n");
flock($fp,3);
fclose($fp);
}
// adds answer
function addanswer($answer, $askk)
{
global $cfg;
bust(&$answer);
$fp = fopen($cfg["answersfile"],'a+');
$file = getfilestr($cfg["answersfile"]);
$file = preg_replace("/\n$askk(.*?);\n/mi","\n$askk$1;$answer;\n",$file);
$file = stripslashes($file);
flock ($fp,2);
ftruncate($fp,0);
fwrite($fp,$file);
flock($fp,3);
fclose($fp);
}
function bust($str)
{
$str = preg_replace("/;/", ":", $str);
$str = stripslashes(preg_replace("/\"/", "'", $str));
}
function prepreg($str)
{
$str = preg_quote(stripslashes($str), "/");
}
?>
<html>
<head>
<title>ChatBot v1 : Admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=<? echo $cfg["encoding"] ?>">
<link href="bot.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" cellpadding="5" cellspacing="0" bgcolor="#6699CC">
<tr class="bt">
<td width="26%" class="bt">ChatBot v1 : Admin</td>
<td bgcolor="#FFFFFF">To</td>
<td bgcolor="#FFFFFF"> </td>
</tr>
<?
if ($_SESSION["user"] != $cfg["user"] || $_SESSION["pass"] != $cfg["pass"])
{
?>
<tr class="bt">
<td><form name="form3" method="post" action="">
<br>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="32%">Login:
<br>
</td>
<td width="68%"><input name="user" type="text" class="inp" id="user" size="15"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="pass" type="password" class="inp" id="pass" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="login" type="submit" class="btn" id="login" value="Login"></td>
</tr>
</table>
</form></td>
<td bgcolor="#FFFFFF"> </td>
<td bgcolor="#FFFFFF"> </td>
</tr>
<? } else { ?>
<tr class="bt">
<td><ul>
<li>To delete question or answer just click on it</li>
<li>To delete question, delete all it's answers first</li>
</ul>
</td>
<td bgcolor="#FFFFFF">To</td>
<td bgcolor="#FFFFFF"> </td>
</tr>
<tr class="bt">
<td bgcolor="#FFFFFF"> </td>
<td bgcolor="#FFFFFF"> </td>
<td bgcolor="#FFFFFF"> </td>
</tr>
<tr class="bt">
<td class="bt">Questions</td>
<td width="39%" class="bt">Answers</td>
<td width="35%" class="bt"> </td>
</tr>
<?
$f = file($cfg["answersfile"]);
for ($i=1; $i<count($f); $i++)
{
$color = !$color;
$col = split(";", $f[$i]);
?>
<tr bgcolor="<? echo ($color) ? "#75A3D0" : "" ?>">
<td valign="top"><a href="?wha=ask&del=<? echo $col[0]; ?>"><? echo $col[0] ?></a></td>
<td valign="top">
<?
// answers
for ($ii=1; $ii<=count($col); $ii++)
{
?>
<a href="?del=<? echo str_replace("\"", "'",$col[$ii]); ?>"><nobr><? echo $col[$ii]; ?></nobr></a>
<? } ?>
</td>
<td nowrap><form name="form2" method="post" action="">
<input name="askk" type="hidden" value="<? echo $col[0] ?>">
<input name="newanswer" type="text" class="inp" id="newanswer">
<input name="addans" type="submit" class="btn" id="addans" value="Add answer"></form></td>
</tr>
<?
}
?>
<tr>
<td nowrap class="top"><form name="form1" method="post" action=""><input name="newask" type="text" class="inp" id="newask">
<input name="adda" type="submit" class="btn" id="adda" value="Add question">
</form></td>
<td class="top"> </td>
<td class="top"> </td>
</tr>
<? } ?>
</table>
</body>
</html>