View file wap-meet/chat.php

File size: 2.58Kb
<?php
require_once('system/config.php');
require_once('system/functions.php');

if ( ! $is_logged ) {
	header('location: ' . $config['site']['home'] .'auth.php', true, 302);
}
// TODO: Удаление сообщений
$action = ! empty($_GET['action']) ? $_GET['action'] : '';
switch($action) {
default:
	echo head();
	$all = mysql_result(mysql_query("SELECT COUNT(*) FROM `chat`"), 0);
		if( $all < 1 ) {
			echo '<div class="menu">Сообщений пока нет.</div>';
		} else {
			// Очистка чата
			if ( $all > 100 ) {
				mysql_query("DELETE FROM `chat` ORDER BY `id` DESC LIMIT 100");
			}
			$pages = ceil($all / $config['site']['onpage']);
			if ($p > $pages) $p = $pages;
			if ($p < 1) $p = 1;
			$start = ($p - 1) * $config['site']['onpage'];
			$q = mysql_query("SELECT `chat`.*, `users`.`login` FROM `chat`, `users` WHERE `chat`.`user_id` = `users`.`user_id` ORDER BY `chat`.`id` DESC LIMIT $start, " . intval($config['site']['onpage']) . " ");
			while( $message = mysql_fetch_assoc($q) ) {
				echo '<div class="menu">
				<b>' . htmlentities($message['login']) . ':</b> ' . htmlentities($message['message']) . '
				</div>';
			}
			echo navigation($p, $pages, $_SERVER['PHP_SELF'] . '?');
		}
	echo '<div class="menu">
<form action="' . $_SERVER['PHP_SELF'] . '?action=add" method="post" />
<input type="text" name="message" maxlength="250" /><br />
<input type="submit" value="Ок" />
</form>
</div>';
echo foot();
break;

case 'add':
echo head();
	$errors = array();
		if ( $user_data['money'] < $config['chat']['cost'] ) {
			echo '<div class="menu">У Вас не достаточно денег для добавления сообщения.</div>';
		} else {
				if ( isset($_SESSION['chat_timeout']) && ($_SESSION['chat_timeout'] + 3 > $_SERVER['REQUEST_TIME']) ) {
					$errors[] = 'Сообщения отправляются раз в 3 секунды';
				}
				
				if ( empty($_POST['message']) ) $errors[] = 'Вы не ввели сообщение';

				if ( empty($errors) ) {
					$_SESSION['chat_timeout'] = $_SERVER['REQUEST_TIME'];
					mysql_query("INSERT INTO `chat` SET
					`user_id` = '" . $user_data['user_id'] . "',
					`message` = '" . mysql_real_escape_string(mb_substr($_POST['message'], 0, 250)) . "',
					`time` = '" . $_SERVER['REQUEST_TIME'] . "' ");
					mysql_query("UPDATE `users` SET `money` = `money` - " . intval($config['chat']['cost']) . " WHERE `user_id` = " . $user_data['user_id'] . " ");
					header('location: ' . $_SERVER['PHP_SELF'], true, 302);
				} else {
					echo '<div>' . implode($errors, '<br />') . '</div>';
				}
		}
	echo foot();
break;
}