View file book.php

File size: 7.39Kb
<?php
// by mides, wmclub.mobi

$title = 'Гостевая';
require_once 'system/sys.php';
require_once 'system/header.php';

switch ($_GET['act']) {
    default:
		echo '<div class="s1">Гостевая книга</div>
		<div class="s2">Сообщение(max250):<br />
		<form action="?act=add" method="post">
		<textarea name="msg" cols="" rows="3"></textarea>';
		if (!$u['id']) {
			echo 'Введите:
			<img src="inc/code.php?'.session_name().'='.session_id().'" alt="" /><br/>
			<input name="code" maxlength="5" size="12" /><br />';
		}
		echo '<input name="" type="submit" value="Добавить">
		</form>
		</div>';
		$count_msg = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `book`"), 0);
		if ($count_msg > 0) {
			$pages = ceil($count_msg/$config['onpage']);
			if (intval($_REQUEST['p'])) {
				$page = intval($_REQUEST['p']);
			} else {
				$page = 1;
			}
			$from = ($page-1)*$config['onpage'];
			
			navig_str($page, '?', $pages);
			
			echo '<div class="list">';
		
			$book_r = mysql_query("SELECT * FROM `book` ORDER BY `time` DESC LIMIT $from, $config[onpage]");
			while ($book = mysql_fetch_assoc($book_r)) {
				if (!isset($num) ) $num = 1;  
				$num++;
				$row_class = (!($num % 2)) ? 'row1' : 'row2';	
				echo '<div class="'.$row_class.'">';
				
				if ($book['id_user'] == 0) {
					echo 'Гость';
				} else {
					echo '<a href="profile.php?id='.$book['id_user'].'">'.login($book['id_user']).'</a> ';
				}
				echo ' '.online(date('d.m.y, H:i', $book['time']), $book['id_user']).' <a href="?act=reply&amp;id='.$book['id'].'">Отв</a>';
				if ($u['access'] > 0 or $u['id'] and $u['id'] == $book['id_user'] and time() - $book['time'] < $config['edit_time']) echo '|<a href="?act=edit&amp;id='.$book['id'].'">Ред</a>';
				if ($u['access'] > 0) echo '|<a href="?act=del&amp;id='.$book['id'].'">Уд</a>';
				echo '<br />'.bb($book['msg']);
				if ($book['edit_by']) echo '<br />_______<br /><span style="font-size: 10px;">отредактировано: '.login_simple($book['edit_by']).' ('.date('d.m.y, H:i', $book['edit_time']).' )</span>';
				echo '</div>';
			}
			navig($page, '?', $pages);
		} else {
			echo 'В гостевой пусто.';
		}
		echo '</div>';
		nav_main();				
	break;
	
	case 'add':
		if ($u['id']) {
			if ($_POST['msg']) {
				if (time() > $_SESSION['antispam']) {
					$msg = check($_POST['msg']);
					mysql_query("INSERT INTO `book` SET `id_user` = '$u[id]', `msg` = '$msg', `time` = '".time()."'");
					mysql_query("UPDATE `users` SET `karma` = (karma+1) WHERE `id` = '$u[id]'");
					$_SESSION['antispam'] = time() + $config['antispam'];
					header('location: ?');
				} else {
					error('Антиспам. Разрешено писать раз в '.$config['antispam'].' сек.');
					nav('?');
				}
			} else {
				header('location: ?');
			}
		} else {
			if ($_POST['msg'] and $_POST['code']) {
				$msg = check($_POST['msg']);
				$code = check($_POST['code']);
				if ($_SESSION['checkcode'] == $code) {
					if (time() > $_SESSION['antispam']) {
						mysql_query("INSERT INTO `book` SET `msg` = '$msg', `time` = '".time()."'");
						$_SESSION['antispam'] = time() + $config['antispam'];
						header('location: ?');
					} else {
						error('Антиспам. Разрешено писать раз в '.$config['antispam'].' сек.');
						nav('?');
					}
				} else {
					error('Неверный код.');
					nav('?');
				}
			} else {
				header('location: ?');
			}
		}
	break;
	
	case 'reply':
		$id = intval($_REQUEST['id']);
		$msg_r = mysql_query("SELECT `id`, `id_user` FROM `book` WHERE `id` = '$id'");
		$msg = mysql_fetch_assoc($msg_r);
		if ($msg['id']) {
			if ($_REQUEST['ok']) {
				if (time() > $_SESSION['antispam']) {
					if ($u['id']) {
						if ($_POST['msg_text']) {
							$msg_text = check($_POST['msg_text']);
							mysql_query("INSERT INTO `book` SET `id_user` = '$u[id]', `msg` = '$msg_text', `time` = '".time()."'");
							mysql_query("UPDATE `users` SET `karma` = (karma+1) WHERE `id` = '$u[id]'");
							$_SESSION['antispam'] = time() + $config['antispam'];
							header('location: ?');
						} else {
							error('Вы не заполнили поле.');
							nav('?act=reply&amp;id='.$id);
						}
					} else {
						if ($_POST['msg'] and $_POST['code']) {
							$msg_text = check($_POST['msg_text']);
							$code = intval($_POST['code']);
							if ($_SESSION['checkcode'] == $code) {
								mysql_query("INSERT INTO `book` SET `msg` = '$msg_text', `time` = '".time()."'");
								$_SESSION['antispam'] = time() + $config['antispam'];
								header('location: ?');
							} else {
								error('Неверный код.');
								nav('?act=reply&amp;id='.$id);
							}
						} else {
							error('Вы не заполнили поле.');
							nav('?act=reply&amp;id='.$id);
						}
					}
				} else {
					error('Антиспам. Разрешено писать раз в '.$config['antispam'].' сек.');
					nav('?');
				}
			} else {
				tp('Ответ на сообщение');
				echo '<form action="?act=reply&amp;id='.$id.'&amp;ok=1" method="post">
				Сообщение(max250):<br /><textarea name="msg_text" cols="" rows="3">[b]'.login_simple($msg['id_user']).'[/b], </textarea><br />';
				if (!$u['id']) {
					echo 'Введите: 
					<img src="inc/code.php?'.session_name().'='.session_id().'" alt="" /><br/>
					<input name="code" maxlength="5" size="12" /><br />';
				}
				echo '<input name="submit" type="submit" value="Ok" />
				</form>';
				nav('?');
			}
		} else {
			error('Сообщение, на которое вы хотите ответить, не существует.');
			nav('?');
		}
	break;
	
	case 'edit':
		if ($u['id']) {
			$id = intval($_REQUEST['id']);
			$book_r = mysql_query("SELECT `id_user`, `msg`, `time` FROM `book` WHERE `id` = '$id'");
			$book = mysql_fetch_assoc($book_r);
			if ($book['msg']) {
				if ($u['access'] > 0 or $u['id'] == $book['id_user'] and time() - $book['time'] < $config['edit_time']) {
					tp('Редактирование сообщения');
					if (empty($_REQUEST['ok'])) {
						echo '<form action="?act=edit&amp;id='.$id.'&amp;ok=1" method="post">
						Сообщение(max250):<br /><textarea name="msg" cols="" rows="3">'.$book['msg'].'</textarea>
						<input name="submit" type="submit" value="Ok" />
						</form>';
						nav('?');
					} else {
						if ($_POST['msg']) {
							$msg = check($_POST['msg']);
							mysql_query("UPDATE `book` SET `msg` = '$msg', `edit_by` = '$u[id]', `edit_time` = '".time()."' WHERE `id` = '$id'");
							header('location: ?');
						} else {
							error('Вы не заполнили поле.');
							nav('?act=edit&amp;id='.$id);
						}
					}
				} else {
					error('Нельзя отредактировать это сообщение.');
					nav('?');
				}
			} else {
				error('Сообщение не существует.');
				nav('?');
			}
		} else {
			header('location: login.php');
		}
	break;
	
	case 'del':
		if ($u['access'] > 0) {
			$id = intval($_REQUEST['id']);
			$msg_r = mysql_query("SELECT `id` FROM `book` WHERE `id` = '$id'");
			if (mysql_num_rows($msg_r)) {
				mysql_query("DELETE FROM `book` WHERE `id` = '$id'");
			}
		}
		header('location: ?');
	break;	
}

require_once 'system/tail.php';
?>