<?
include_once '../../sys/inc/start.php';
include_once '../../sys/inc/compress.php';
include_once '../../sys/inc/sess.php';
include_once '../../sys/inc/home.php';
include_once '../../sys/inc/settings.php';
include_once '../../sys/inc/db_connect.php';
include_once '../../sys/inc/ipua.php';
include_once '../../sys/inc/fnc.php';
include_once '../../sys/inc/user.php';
only_reg();

// Добавление записи
if (isset($_GET['add']) && isset($_POST['name']) && $_POST['name'] != NULL && isset($_POST['msg']) && $_POST['msg'] != NULL)
{
	$name = esc(stripcslashes(htmlspecialchars($_POST['name'])));
	$msg = my_esc($_POST['msg']);
	$key = $passgen;
	
	mysql_query("INSERT INTO `notebook` (`name`, `msg`, `key`, `time`, `id_user`) VALUES ('$name', '$msg', '$key', '$time', '$user[id]')");
	$_SESSION['message'] = 'Запись успешно добавлена';
	header('Location: /note' . $key);
	exit;
}

// Редактирование записи
if (isset($_GET['edit']) && isset($_POST['name']) && $_POST['name'] != NULL && isset($_POST['msg']) && $_POST['msg'] != NULL)
{
	$notebook = mysql_fetch_assoc(mysql_query("SELECT * FROM `notebook` WHERE `id` = '" . intval($_GET['edit']) . "' LIMIT 1"));
	$name = esc(stripcslashes(htmlspecialchars($_POST['name'])));
	$msg = my_esc($_POST['msg']);
	
	if ($notebook['id_user'] == $user['id'])
	{
		mysql_query("UPDATE `notebook` SET `name` = '$name', `msg` = '$msg' WHERE `id` = '$notebook[id]' LIMIT 1");
		$_SESSION['message'] = 'Запись успешно отредактирована';
		header('Location: /note' . $notebook['key']);
		exit;
	}
}


$set['title'] = 'Блокнот - Управление';
include_once '../../sys/inc/thead.php';
title();
aut();

// Редактирование
if (isset($_GET['edit']))
{
	$notebook = mysql_fetch_assoc(mysql_query("SELECT * FROM `notebook` WHERE `id` = '" . intval($_GET['edit']) . "' LIMIT 1"));
	
	if ($notebook['id_user'] != $user['id'])
	{
		header('Location: index.php');
		exit;
	}
	
	echo '<form method="post" name="message" action="create.php?edit=' . $notebook['id'] . '">';
	echo 'Название:<br /><input name="name" size="16" maxlength="32" value="' . $notebook['name'] . '" type="text" /><br />';
	
	if (is_file(H.'style/themes/' . $set['set_them'] . '/altername_post_form.php'))
	{
		$msg2 = esc(stripcslashes(htmlspecialchars($notebook['msg'])));
		include_once H.'style/themes/' . $set['set_them'] . '/altername_post_form.php';
	}
	else
	{
		echo 'Текст:<br /><textarea name="msg" size="16" maxlength="20000" placeholder="Запишите все что хотите тут.." />' . esc(stripcslashes(htmlspecialchars($notebook['msg']))) . '</textarea><br />';
	}
	

	echo '<input value="Cохранить" type="submit" />';
	echo '</form>';
	
	echo '<div class="foot">';
	echo '<img src="/style/icons/str2.gif" alt="*" /> <a href="/user/notebook/">Блокнот</a> | <b>Редактирование записи</b>';
	echo '</div>';

	include_once '../../sys/inc/tfoot.php';
	exit;
}

echo '<form method="post" name="message" action="create.php?add">';
echo 'Название:<br /><input name="name" size="16" maxlength="128" value="" type="text" placeholder="Название записи.."  /><br />';

if (is_file(H.'style/themes/' . $set['set_them'] . '/altername_post_form.php'))
{
	include_once H.'style/themes/' . $set['set_them'] . '/altername_post_form.php';
}
else
{
	echo 'Текст:<br /><textarea name="msg" size="16" maxlength="20000" placeholder="Запишите все что хотите тут.." /></textarea><br />';
}

echo '<input value="Записать" type="submit" />';
echo '</form>';

echo '<div class="foot">';
echo '<img src="/style/icons/str2.gif" alt="*" /> <a href="/user/notebook/">Блокнот</a> | <b>Новая запись</b>';
echo '</div>';

include_once '../../sys/inc/tfoot.php';
?>