Paste: OSPHPChat Ajax.php

Author: OSPHPChat
Mode: php
Date: Sat, 19 Mar 2011 06:49:43
Plain Text |
<?php
	/**
	/*		OSPHPChat Alpha 1 - 2011
	/*
	/*		File: /modules/chat/ajax.php
	/*
	/*		Website: www.OSPHPChat.com
	/*
	/*		Author: Daniel Hood <danny@osphpchat.com>
	/*
	/*
	/*
	**/

	if ($mode == 'send')
	{
		$message = $OSPHPChat->Purify->get('message', 'POST', 'string');
		if (isset($message) && !empty($message))
		{
			if ($OSPHPChat->User->auth->check_auth('main_talk'))
			{
				if ($message{0} == '/')
				{
					$message = substr($message, 1);
					$cmd = explode(' ', $message, 2);
					if (file_exists('modules/chat/commands/' . $cmd[0] . '.php'))
					{
						include('commands/' . $cmd[0] . '.php');
						$command = $cmd[0];
						$command = new $command(&$OSPHPChat, $message);
						if ($command->allowed())
						{
							if (stristr($message, '-help'))
							{
								$data = array(
									'user_id' => $OSPHPChat->User->user_info['id'],
									'to' => 'room:' . $OSPHPChat->Messages->room . ';',
									'message' => $command->help(),
									'date' => $OSPHPChat->data,
									'time' => $OSPHPChat->time,
									'color' => $OSPHPChat->User->user_info['message_color'],
									'user_color' => $OSPHPChat->User->user_info['user_color'],
									'username' => $OSPHPChat->User->user_info['username']
								);
								$OSPHPChat->Messages->send_message($data);
							}
							else
							{
								$command->execute();
							}
						}
						else
						{
							$error = "You are not permitted to use this command.";
						}
					}
				}
				else
				{
					$data = array(
						'user_id' => $OSPHPChat->User->user_info['id'],
						'to' => 'room:' . $OSPHPChat->Messages->room . ';',
						'message' => $message,
						'date' => $OSPHPChat->data,
						'time' => $OSPHPChat->time,
						'color' => $OSPHPChat->User->user_info['message_color'],
						'user_color' => $OSPHPChat->User->user_info['user_color'],
						'username' => $OSPHPChat->User->user_info['username']
					);
					$OSPHPChat->Messages->send_message($data);
				}
			}
			else
			{
				$error = "You are not permitted to talk.";
			}
		}
		else
		{
			$error = "You cannot send blank messages.";
		}
		if ($error)
		{
			echo json_encode($error);
		}
	}
	elseif ($mode == 'load')
	{
		$OSPHPChat->Messages->starting_id = $_SESSION['last_message'];
		$OSPHPChat->Messages->get_messages();
		$Messages = array();
		foreach ($OSPHPChat->Messages->get_messages() as $message)
		{
			// Establishes User Color \\
			if (isset($message['user_color']))
			{
				$message['sender'] = "<a href='#' style='color:#" . $message['user_color'] . ";'>" . $message['username'] . "</a>";
			}
			else
			{
				$message['sender'] = "<a href='#'>" . $message['username'] . "</a>";
			}

			// Establishes Message Color \\
			if (isset($message['color']))
			{
				$message['text'] = "<span style='color:#" . $message['color'] . ";'>" . $message['message'] . "</span>";
			}
			else
			{
				$message['text'] = "<span>" . $message['message'] . "</span>";
			}

			$_SESSION['last_message'] = $message['id'];
			$Smarty->assign('message', $message);
			$Messages[] = $Smarty->fetch('_message.tpl');
		}
		echo json_encode($Messages);
	}
	elseif ($mode == 'users')
	{
		$offline_time = time() - 15;
		// Getting [new] online users \\
		$online_users = $OSPHPChat->db->Execute("SELECT * FROM `" . $OSPHPChat->config['database']['tables']['users'] . "` WHERE `online_time`> '" . $offline_time . "'");
		while ($online_user = $online_users->FetchRow())
		{
			$All_users[$online_user['id']] = array(
				'id' => $online_user['id'],
				'username' => $online_user['username']
			);
			if (!isset($_SESSION['online_users'][$online_user['id']]))
			{
				$Smarty->assign('user', $online_user);
				$Users[online][$online_user['id']] = array(
					'html' => $Smarty->fetch('_user_list.tpl'),
					'id' => $online_user['id']
				);
			}
			$Userids_online[$online_user['id']] = $online_user['username'];
		}
		// Removing offline users \\
		if (is_array($_SESSION['online_users']))
		{
			foreach ($_SESSION['online_users'] as $online_user)
			{
				if (!isset($Userids_online[$online_user['id']]))
				{
					$Users['offline'][] = $online_user;
				}
			}
		}
		$_SESSION['online_users'] = $All_users;
		echo json_encode($Users);
	}
?>

New Annotation

Summary:
Author:
Mode:
Body: