#native_company# #native_desc#
#native_cta#

Chat Class

By Vyacheslav Yanson
on April 3, 2007

Version: 1.0

Type: Class

Category: BBS/Discussion

License: GNU General Public License

Description: supports multiple chat rooms

<?php

// error messages (just so its easy to change or translate)
define('RS_WRONG_USERNAME_OR_PASSWORD', 'Wrong username or password');
define('RS_LOGIN_BEFORE_POSTING', 'You need to login before posting');
define('RS_NO_ROOMS', 'No chat rooms are available');
define('RS_NO_ROOM', 'This room is not available');

class rschat
{
	private $db; // database object. class type is mysql
	protected $user_conf = array(); // user configuration. fetched after authentication
	
	private $logged_in = false; // are we logged in or not?
	
	protected $rooms = array(); // room list
	
	// constructor. (mysql) $db
	public function __construct(mysql $db)
	{
		$this->db = $db;
	}
	
	// authentication function. (string) $user, (string) $pass
	public function login($user, $pass)
	{
		// log user out in case already logged in
		$this->user_conf = array();
		$this->logged_in = false;
		
		// try to authenticate
		$auth = $this->db->query("select * from users where username='" . mysql_real_escape_string($user) . "' and password=PASSWORD('" . mysql_real_escape_string($pass) . "') limit 1");
		if (count($auth) > 0)
		{ // good. username and password are correct
			// set variables
			$this->user_conf['username'] = $auth[0]['username'];
			$this->user_conf['id'] = $auth[0]['id'];
			$this->user_conf['msg_format'] = array // message formatting
			(
				'color'	=>	$auth[0]['msg_color'],
				'italic'	=>	$auth[0]['msg_italic'] === 'y' ? true : false,
				'bold'	=>	$auth[0]['msg_bold'] === 'y' ? true : false
			);
			
			$this->logged_in = true;
			return true;
		} else { // bad. something is incorrect in user input
			$this->logged_in = false;
			return RS_WRONG_USERNAME_OR_PASSWORD;
		}
	}
	
	// get rooms list. if $force is set to true then we will grab it from database
	// even if we did that already. (bool) $force
	public function get_rooms($force = false)
	{
		if (count($this->rooms) < 1 || $force)
		{
			$rooms = $this->db->query("select * from rooms order by name");
			if (count($rooms) > 0)
			{
				$this->rooms = $rooms;
				return $rooms;
			} else {
				return RS_NO_ROOMS;
			}
		}
	}
	
	// check if room exists. (int) $room
	public function check_room($room)
	{
		$rooms = $this->get_rooms(true);
		if (is_array($rooms) && count($rooms) > 0)
		{
			foreach($rooms as $n => $v)
			{
				if ($room === $v['id'])
				{
					return true;
				}
			}
			
			return RS_NO_ROOM;
		} else {
			return RS_NO_ROOMS;
		}
	}
	
	// this function sends message to chat room. (int) $room, (string) $message
	public function say($room, $message)
	{
		// are we logged in?
		if ($this->logged_in)
		{
			// check if room actually exists
			$room_exists = $this->check_room($room);
			if ($room_exists === true)
			{
				// format and post message
				$bold_start = $this->user_conf['msg_format']['bold'] ? '<strong>' : '';
				$bold_end = $this->user_conf['msg_format']['bold'] ? '</strong>' : '';
				$italic_start = $this->user_conf['msg_format']['italic'] ? '<em>' : '';
				$italic_end = $this->user_conf['msg_format']['italic'] ? '</em>' : '';
				
				$message = '<font color="' . $this->user_conf['msg_format']['color'] . '">' . $bold_start . $italic_start . $message . $italic_end . $bold_end . '</font>';
				
				$this->db->query("insert into messages (room, user, message) values (" . (int) $room . ", " . (int) $this->user_conf['id'] . ", '" . mysql_real_escape_string($message) . "')");
			} else {
				return $room_exists;
			}
		} else {
			return RS_LOGIN_BEFORE_POSTING;
		}
	}
	
	// get messages from specific room. (int) $room, (int) $last_message_id
	public function get_messages($room, $last_message_id)
	{
		$room_exists = $this->check_room($room);
		if ($room_exists === true)
		{
			$messages = $this->db->query("select m.id, m.message, r.name, u.username from messages as m, users as u, rooms as r where m.room=r.id and m.user=u.id and m.id>" . (int) $last_message_id . " and m.room=" . (int) $room . " order by m.id");
			return array
			(
				'last_message_id'	=>	$messages[count($messages) - 1]['id'],
				'messages'	=>	$messages
			);
		} else {
			return $room_exists;
		}
	}
}

?>