<?php
/**
*
* @package Chat
* @version 0.2
* @copyright (c) 2008 phpBB3.PL Group
* @license
http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* Chat
*
* @package Chat
* @property array $messagesFilter Messages filter (SQL WHERE clause, for example c.message_time > x)
* @property array $messagesLimit Limit of messages number
* @property array $messages List of messages
* @property array $lastMessageID ID of last loaded message
**/
class Chat extends OAPI_Base implements IteratorAggregate
{
public static function classInfo()
{
return array(
'autoloadLang' => 'mods/chat',
);
}
public static function getLink($mode, array $params = array(), $ampify = true)
{
global $phpbb_root_path;
$params['mode'] = $mode;
return append_sid($phpbb_root_path . 'chat'.php, $params, $ampify);
}
public static function isAllowed($type, $permission)
{
global $auth;
return $auth->acl_get($type . '_chat_' . $permission);
}
public static function purge()
{
if (!self::isAllowed('a', 'purge'))
{
throw new OAPI_Exception('::NotAllowed', 'User');
}
global $db;
switch ($db->sql_layer)
{
case 'sqlite':
case 'firebird':
$sql = 'DELETE FROM ';
break;
default:
$sql = 'TRUNCATE TABLE ';
break;
}
$sql .= CHAT_TABLE;
$db->sql_query($sql);
}
public static function deleteMessage($messageID)
{
if (!$messageID)
{
throw new OAPI_Exception('Chat::NoMessageSelected', 'User');
}
$message = new Chat_Message();
$message->ID = $messageID;
global $db;
$sql = 'SELECT user_id
FROM ' . CHAT_TABLE . '
WHERE message_id = ' . $message->ID;
$db->sql_query_limit($sql, 1);
$message->author->ID = $db->sql_fetchfield('user_id');
$db->sql_freeresult();
if ($message->author->ID === false)
{
throw new OAPI_Exception('Chat::MessageNotFound', 'User');
}
$message->delete();
}
public function construct()
{
global $config;
$config['chat_displaying_time'] = 0 * 24;
$config['chat_max_messages'] = '100'; $this->messagesFilter = (!empty($config['chat_displaying_time'])) ? 'c.message_time > ' . (time() - ($config['chat_displaying_time'] * 3600)) : '';
$this->messagesLimit = (!empty($config['chat_max_messages'])) ? $config['chat_max_messages'] : 0;
}
protected function loadMessages()
{
if (!Chat::isAllowed('u', 'view'))
{
$this->messages = array();
return false;
}
global $db;
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'c.*, u.username, u.user_colour',
'FROM' => array(CHAT_TABLE => 'c'),
'LEFT_JOIN' => array(
array(
'FROM' => array(USERS_TABLE => 'u'),
'ON' => 'c.user_id = u.user_id',
),
),
'WHERE' => $this->messagesFilter,
'ORDER_BY' => 'c.message_id DESC',
));
if ($this->messagesLimit)
{
$result = $db->sql_query_limit($sql, $this->messagesLimit);
}
else
{
$result = $db->sql_query($sql);
}
$this->messages = array();
$this->lastMessageID = 0;
while ($row = $db->sql_fetchrow($result))
{
$message = new Chat_Message($row);
if ($this->lastMessageID < $message->ID)
{
$this->lastMessageID = $message->ID;
}
$date = $message->time->format('date');
if (empty($this->messages) || $this->messages[0]['date'] !== $date)
{
array_unshift($this->messages, array(
'date' => $date,
));
}
array_unshift($this->messages[0], $message);
}
$db->sql_freeresult($result);
if (!$this->lastMessageID)
{
// So it will be possible to use loadLastMessageID.
unset($this->lastMessageID);
}
}
protected function loadLastMessageID()
{
global $db;
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'MAX(message_id) as last_message_id',
'FROM' => array(CHAT_TABLE => 'c'),
));
$result = $db->sql_query($sql);
$this->lastMessageID = $db->sql_fetchfield('last_message_id', false, $result);
$db->sql_freeresult($result);
}
public function getIterator()
{
return $this->messages;
}
public function display()
{
global $template, $phpbb_root_path;
$this->messages; // Load it
if (!self::isAllowed('u', 'view'))
{
return;
}
$template->assign_block_vars('chat', array());
if (class_exists('xmlwriter') && version_compare(LIBXML_DOTTED_VERSI
ON, '2.6.18', '>='))
{
$template->assign_block_vars('chat.ajax', array(
'GETNEW_URL' => self::getLink('getNew'),
'LAST_MESSAGE_ID' => $this->lastMessageID,
'LANG' => '{
confirmDelete: \'' . $this->lang->confirmDelete . '\',
engineRunning: \'' . $this->lang->_Exceptions->EngineRunning . '\',
emptyMessage: \'' . $this->lang->_Exceptions->EmptyMessage . '\',
messagePosted: \'' . $this->lang->_Exceptions->MessagePosted . '\',
messageDeleted: \'' . $this->lang->_Exceptions->MessageDeleted . '\',
scriptError: \'' . $this->lang->_Exceptions->ScriptError . '\'
}',
));
}
if (!empty($this->messages))
{
foreach ($this->messages as $messagesToday)
{
$template->assign_block_vars('chat.messages', array(
'ID' => str_replace(' ', '', $messagesToday['date']),
'DATE' => $messagesToday['date'],
));
foreach ($messagesToday as $key => $message)
{
if (!is_numeric($key))
{
continue;
}
$template->assign_block_vars('chat.messages.message', array(
'ID' => $message->ID,
'AUTHOR' => $message->author->format(),
'TIME' => $message->time->format('time'),
'TEXT' => $message->text->displayable,
'DELETE' => ($message->isAllowed('delete')) ? self::getLink('delete', array('messageID' => $message->ID)) : false,
));
}
}
}
if (self::isAllowed('u', 'post'))
{
$template->assign_block_vars('chat.post', array(
'ACTION' => self::getLink('post'),
));
}
else
{
global $user;
$template->assign_block_vars('chat.no_post', array(
'MESSAGE' => ($user->data['is_registered']) ? $this->lang->_Exceptions->PostingNotAllowed : $this->lang->_Exceptions->LoginToPost,
));
}
}
public function displayXML()
{
header('Content-Type: text/xml');
$output = new XMLWriter();
$output->openURI('php://output');
$output->startDocument('1.0', 'UTF-8', 'yes');
$output->startElementNS(null, 'chat', '
http://phpbb3.pl/xmlns/chat/');
$this->messages; // Load it
if (!empty($this->messages))
{
foreach ($this->messages as $messagesToday)
{
$output->startElement('messages');
$output->writeAttribute('date', $messagesToday['date']);
$output->writeAttribute('id', str_replace(' ', '', $messagesToday['date']));
foreach ($messagesToday as $key => $message)
{
if (!is_numeric($key))
{
continue;
}
$output->startElement('message');
$output->writeAttribute('id', $message->ID);
if ($message->isAllowed('delete'))
{
$output->writeAttributeNS('action', 'delete', '
http://phpbb3.pl/xmlns/chat/actions/', self::getLink('delete', array('messageID' => $message->ID), false));
}
$output->startElement('author');
$output->writeCdata($message->author->format());
$output->endElement();
$output->startElement('time');
$output->writeCdata($message->time->format('time'));
$output->endElement();
$output->startElement('text');
$output->writeCdata($message->text->displayable);
$output->endElement();
$output->endElement();
}
$output->endElement();
}
}
$output->endElement();
$output->endDocument();
// Be paranoid with flushing
$output->flush();
flush();
// And the end
exit;
}
}
/*EOF*/