<?php
if(!isset($_POST['action']))
{
echo "Вопрос:<br/>\n";
echo "<input type=\"text\" name=\"question$nocache\" maxlength=\"50\"/><br/>\n";
echo "Ответ:<br/>\n";
echo "<input type=\"text\" name=\"answer$nocache\" maxlength=\"50\"/><br/>\n";
echo "Транслитировать:<br/>\n";
echo "<select multiple=\"true\" name=\"translit$nocache\">\n";
echo "<option value=\"question\">Вопрос</option>\n";
echo "<option value=\"answer\">Ответ</option></select><br/>\n";
echo "<anchor>[Добавить]<go href=\"admin.php?nocache=$nocache&amp;ver=wml&amp;".SID."&amp;pass=$pass&amp;mod=addquestion\" method=\"post\">\n";
echo "<postfield name=\"question\" value=\"$(question$nocache)\"/>\n";
echo "<postfield name=\"answer\" value=\"$(answer$nocache)\"/>\n";
echo "<postfield name=\"translit\" value=\"$(translit$nocache)\"/>\n";
echo "<postfield name=\"action\" value=\"add\"/>\n";
echo "</go></anchor><br/>\n";
}
else
{
$lat = array("J", "j", "Zh", "zh", "Ch", "ch", "Ye", "ye", "Yu", "yu", "Ya", "ya", "Shc", "shc", "Sh", "sh", "Yo", "yo", "Q", "q", "'", "A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "X", "x", "Z", "z");
$rus = array("Й", "й", "Ж", "ж", "Ч", "ч", "Э", "э", "Ю", "ю", "Я", "я", "Щ", "щ", "Ш", "ш", "Ё", "ё", "Ь", "ь", "ъ", "А", "а", "Б", "б", "Ц", "ц", "Д", "д", "Е", "е", "Ф", "ф", "Г", "г", "Х", "х", "И", "и", "К", "к", "Л", "л", "М", "м", "Н", "н", "О", "о", "П", "п", "Р", "р", "С", "с", "Т", "т", "У", "у", "В", "в", "Х", "х", "З", "з");
$question = htmlspecialchars(mysql_escape_string(trim($_POST['question'])));
$answer = htmlspecialchars(mysql_escape_string(trim($_POST['answer'])));
$question = str_replace('$', '$$', $question);
$answer = str_replace('$', '$$', $answer);
$translit = $_POST['translit'];

if(substr_count($translit, "question") != 0)
{
$question = str_replace($lat, $rus, $question);
}

if(substr_count($translit, "answer") != 0)
{
$answer = str_replace($lat, $rus, $answer);
}

if(empty($question))
{
echo "Вы не ввели вопрос!<br/>\n";
break;
}

if(empty($answer))
{
echo "Вы не ввели ответ!<br/>\n";
break;
}

$q = mysql_query("SELECT `id` FROM `chat_questions` WHERE `question` = '".$question."' AND `answer` = '".$answer."';");

if(mysql_num_rows($q) != 0)
{
echo "Такой вопрос уже есть в базе.<br/>\n";
break;
}

$translit = strtolower(str_replace($rus, $lat, $answer));

mysql_query("INSERT INTO `chat_questions` VALUES(0, '".$question."', '".$answer."', '".$translit."');");

$q = mysql_query("SELECT COUNT(*) FROM `chat_questions`;");
$questions = mysql_result($q, 0);

echo "Вопрос успешно добавлен!<br/>\n";
echo "В базе $questions вопросов.<br/>\n";
}
?>