Version 1.0.0-RC2

This commit is contained in:
dmzx
2016-03-06 16:06:56 +01:00
parent fdde6ee2b2
commit 7fc464ff17
92 changed files with 2909 additions and 1845 deletions

View File

@@ -3,14 +3,15 @@
/**
*
* @package phpBB Extension - mChat
* @copyright (c) 2015 dmzx - http://www.dmzx-web.net
* @copyright (c) 2016 dmzx - http://www.dmzx-web.net
* @copyright (c) 2016 kasimi
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace dmzx\mchat\core;
class functions_mchat
class functions
{
/** @var \phpbb\config\config */
protected $config;
@@ -39,6 +40,9 @@ class functions_mchat
/** @var string */
protected $mchat_table;
/** @var string */
protected $mchat_deleted_messages_table;
/** @var string */
protected $mchat_sessions_table;
@@ -57,20 +61,22 @@ class functions_mchat
* @param string $root_path
* @param string $php_ext
* @param string $mchat_table
* @param string $mchat_deleted_messages_table
* @param string $mchat_sessions_table
*/
function __construct(\phpbb\config\config $config, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, $root_path, $php_ext, $mchat_table, $mchat_sessions_table)
function __construct(\phpbb\config\config $config, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, $root_path, $php_ext, $mchat_table, $mchat_deleted_messages_table, $mchat_sessions_table)
{
$this->config = $config;
$this->user = $user;
$this->auth = $auth;
$this->log = $log;
$this->db = $db;
$this->cache = $cache;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->mchat_table = $mchat_table;
$this->mchat_sessions_table = $mchat_sessions_table;
$this->config = $config;
$this->user = $user;
$this->auth = $auth;
$this->log = $log;
$this->db = $db;
$this->cache = $cache;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->mchat_table = $mchat_table;
$this->mchat_deleted_messages_table = $mchat_deleted_messages_table;
$this->mchat_sessions_table = $mchat_sessions_table;
}
/**
@@ -161,6 +167,8 @@ class functions_mchat
/**
* Inserts the current user into the mchat_sessions table
*
* @return bool
*/
public function mchat_add_user_session()
{
@@ -170,6 +178,8 @@ class functions_mchat
WHERE user_lastupdate < ' . $check_time;
$this->db->sql_query($sql);
$is_new_session = false;
if ($this->user->data['user_type'] == USER_FOUNDER || $this->user->data['user_type'] == USER_NORMAL && $this->user->data['user_id'] != ANONYMOUS && !$this->user->data['is_bot'])
{
$sql = 'SELECT *
@@ -187,6 +197,7 @@ class functions_mchat
}
else
{
$is_new_session = true;
$sql = 'INSERT INTO ' . $this->mchat_sessions_table . ' ' . $this->db->sql_build_array('INSERT', array(
'user_id' => $this->user->data['user_id'],
'user_ip' => $this->user->data['user_ip'],
@@ -196,6 +207,8 @@ class functions_mchat
$this->db->sql_query($sql);
}
return $is_new_session;
}
/**
@@ -220,7 +233,7 @@ class functions_mchat
$delete_id = $mchat_total_messages - $this->config['mchat_prune_num'] + $first_id;
// Delete older messages
$this->mchat_action('prune', null, $delete_id);
$this->mchat_action('prune', null, $delete_id, $this->user->data['username']);
}
}
}
@@ -348,11 +361,11 @@ class functions_mchat
*/
public function mchat_sql_append_forbidden_bbcodes($sql_where)
{
$disallowed_bbcodes = explode('|', strtoupper($this->config['mchat_bbcode_disallowed']));
$disallowed_bbcodes = explode('|', $this->config['mchat_bbcode_disallowed']);
if (!empty($disallowed_bbcodes))
{
$sql_where .= ' AND ' . $this->db->sql_in_set('UPPER(b.bbcode_tag)', $disallowed_bbcodes, true);
$sql_where .= ' AND ' . $this->db->sql_in_set('b.bbcode_tag', $disallowed_bbcodes, true);
}
return $sql_where;
@@ -446,87 +459,22 @@ class functions_mchat
* Returns an array of message IDs that have been deleted from the message table
*
* @param $start_id
* @param $end_id
* @return array
*/
public function mchat_missing_ids($start_id, $end_id)
public function mchat_deleted_ids($start_id)
{
if ($this->config['mchat_edit_delete_limit'])
{
$sql_where = 'message_time < ' . (time() - $this->config['mchat_edit_delete_limit']);
$cache_ttl = 0;
}
else
{
$sql_where = 'message_id < ' . (int) $start_id;
$cache_ttl = 3600;
}
$sql = 'SELECT message_id
FROM ' . $this->mchat_table . '
WHERE ' . $sql_where . '
FROM ' . $this->mchat_deleted_messages_table . '
WHERE message_id >= ' . (int) $start_id . '
ORDER BY message_id DESC';
$result = $this->db->sql_query_limit($sql, 1, 0, $cache_ttl);
$earliest_id = (int) $this->db->sql_fetchfield('message_id');
$this->db->sql_freeresult($result);
if (!$earliest_id)
{
$sql = 'SELECT MIN(message_id) as earliest_id
FROM ' . $this->mchat_table;
$result = $this->db->sql_query($sql, 3600);
$earliest_id = $this->db->sql_fetchfield('earliest_id');
$this->db->sql_freeresult($result);
}
if (!$earliest_id)
{
return range($start_id, $end_id);
}
$sql = 'SELECT (t1.message_id + 1) AS start, (
SELECT MIN(t3.message_id) - 1
FROM ' . $this->mchat_table . ' t3
WHERE t3.message_id > t1.message_id
) AS end
FROM ' . $this->mchat_table . ' t1
WHERE t1.message_id > ' . (int) $earliest_id . ' AND NOT EXISTS (
SELECT t2.message_id
FROM ' . $this->mchat_table . ' t2
WHERE t2.message_id = t1.message_id + 1
)';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$result = $this->db->sql_query($sql, 3600);
$rows = $this->db->sql_fetchrowset();
$this->db->sql_freeresult($result);
$missing_ids = array();
if ($start_id < $earliest_id && !$this->config['mchat_edit_delete_limit'])
{
$missing_ids[] = range($start_id, $earliest_id - 1);
}
foreach ($rows as $row)
{
if ($row['end'])
{
$missing_ids[] = range($row['start'], $row['end']);
}
else
{
$latest_message = $row['start'] - 1;
if ($end_id > $latest_message)
{
$missing_ids[] = range($latest_message + 1, $end_id);
}
}
}
// Flatten
if (!empty($missing_ids))
{
$missing_ids = call_user_func_array('array_merge', $missing_ids);
$missing_ids[] = (int) $row['message_id'];
}
return $missing_ids;
@@ -535,56 +483,66 @@ class functions_mchat
/**
* Performs AJAX actions
*
* @param string $action One of add|edit|del|clean|prune
* @param string $action One of add|edit|del|prune
* @param array $sql_ary
* @param int $message_id
* @param string $log_username
* @return bool
*/
public function mchat_action($action, $sql_ary = null, $message_id = 0, $log_username = '')
{
$is_new_session = false;
switch ($action)
{
// User adds a message
case 'add':
$sql = 'INSERT INTO ' . $this->mchat_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
$this->mchat_add_user_session();
$this->user->update_session_infos();
$is_new_session = $this->mchat_add_user_session();
$this->db->sql_query('INSERT INTO ' . $this->mchat_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary));
break;
// User edits a message
case 'edit':
$sql = 'UPDATE ' . $this->mchat_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' WHERE message_id = ' . (int) $message_id;
$this->mchat_add_user_session();
$this->user->update_session_infos();
$is_new_session = $this->mchat_add_user_session();
$this->db->sql_query('UPDATE ' . $this->mchat_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' WHERE message_id = ' . (int) $message_id);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EDITED_MCHAT', false, array($log_username));
break;
// User deletes a message
case 'del':
$sql = 'DELETE FROM ' . $this->mchat_table . ' WHERE message_id = ' . (int) $message_id;
$this->mchat_add_user_session();
$this->user->update_session_infos();
$is_new_session = $this->mchat_add_user_session();
$this->db->sql_query('DELETE FROM ' . $this->mchat_table . ' WHERE message_id = ' . (int) $message_id);
$this->db->sql_query('INSERT INTO ' . $this->mchat_deleted_messages_table . ' ' . $this->db->sql_build_array('INSERT', array('message_id' => (int) $message_id)));
$this->cache->destroy('sql', $this->mchat_deleted_messages_table);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DELETED_MCHAT', false, array($log_username));
$this->cache->destroy('sql', $this->mchat_table);
break;
// User triggers messages to be pruned
case 'prune':
$sql = 'DELETE FROM ' . $this->mchat_table . ' WHERE message_id < ' . (int) $message_id;
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MCHAT_TABLE_PRUNED');
$this->cache->destroy('sql', $this->mchat_table);
$sql = 'SELECT message_id
FROM ' . $this->mchat_table . '
WHERE message_id < ' . (int) $message_id . '
ORDER BY message_id DESC';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset();
$this->db->sql_freeresult($result);
$prune_ids = array();
foreach ($rows as $row)
{
$prune_ids[] = (int) $row['message_id'];
}
$this->db->sql_query('DELETE FROM ' . $this->mchat_table . ' WHERE ' .$this->db->sql_in_set('message_id', $prune_ids));
$this->db->sql_multi_insert($this->mchat_deleted_messages_table, $rows);
$this->cache->destroy('sql', $this->mchat_deleted_messages_table);
// $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MCHAT_TABLE_PRUNED', false, array($log_username));
break;
default:
return;
}
$result = $this->db->sql_query($sql);
if ($result !== false)
{
switch ($action)
{
case 'add':
if ($this->db->sql_nextid() == 1)
{
$this->cache->destroy('sql', $this->mchat_table);
}
break;
}
}
return $is_new_session;
}
}

View File

@@ -3,7 +3,8 @@
/**
*
* @package phpBB Extension - mChat
* @copyright (c) 2015 dmzx - http://www.dmzx-web.net
* @copyright (c) 2016 dmzx - http://www.dmzx-web.net
* @copyright (c) 2016 kasimi
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
@@ -12,8 +13,8 @@ namespace dmzx\mchat\core;
class mchat
{
/** @var \dmzx\mchat\core\functions_mchat */
protected $functions_mchat;
/** @var \dmzx\mchat\core\functions */
protected $functions;
/** @var \phpbb\config\config */
protected $config;
@@ -39,6 +40,9 @@ class mchat
/** @var \phpbb\event\dispatcher_interface */
protected $dispatcher;
/** @var \phpbb\extension\manager */
protected $extension_manager;
/** @var string */
protected $root_path;
@@ -46,12 +50,12 @@ class mchat
protected $php_ext;
/** @var boolean */
protected $is_mchat_rendered = false;
protected $remove_disallowed_bbcodes = false;
/**
* Constructor
*
* @param \dmzx\mchat\core\functions_mchat $functions_mchat
* @param \dmzx\mchat\core\functions $functions
* @param \phpbb\config\config $config
* @param \phpbb\controller\helper $helper
* @param \phpbb\template\template $template
@@ -60,22 +64,24 @@ class mchat
* @param \phpbb\pagination $pagination
* @param \phpbb\request\request $request
* @param \phpbb\event\dispatcher_interface $dispatcher
* @param \phpbb\extension\manager $extension_manager
* @param string $root_path
* @param string $php_ext
*/
public function __construct(\dmzx\mchat\core\functions_mchat $functions_mchat, \phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\pagination $pagination, \phpbb\request\request $request, \phpbb\event\dispatcher_interface $dispatcher, $root_path, $php_ext)
public function __construct(\dmzx\mchat\core\functions $functions, \phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\pagination $pagination, \phpbb\request\request $request, \phpbb\event\dispatcher_interface $dispatcher,\phpbb\extension\manager $extension_manager, $root_path, $php_ext)
{
$this->functions_mchat = $functions_mchat;
$this->config = $config;
$this->helper = $helper;
$this->template = $template;
$this->user = $user;
$this->auth = $auth;
$this->pagination = $pagination;
$this->request = $request;
$this->dispatcher = $dispatcher;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->functions = $functions;
$this->config = $config;
$this->helper = $helper;
$this->template = $template;
$this->user = $user;
$this->auth = $auth;
$this->pagination = $pagination;
$this->request = $request;
$this->dispatcher = $dispatcher;
$this->extension_manager = $extension_manager;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
}
/**
@@ -95,6 +101,13 @@ class mchat
return;
}
if (!$this->user->data['user_mchat_index'])
{
return;
}
$this->user->add_lang_ext('dmzx/mchat', 'mchat');
$this->assign_bbcodes_smilies();
$this->render_page('index');
@@ -107,14 +120,19 @@ class mchat
*/
public function page_custom()
{
if (!$this->auth->acl_get('u_mchat_view') || !$this->config['mchat_custom_page'])
if (!$this->auth->acl_get('u_mchat_view'))
{
throw new \phpbb\exception\http_exception(403, 'NOT_AUTHORISED');
}
if (!$this->config['mchat_custom_page'])
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NO_CUSTOM_PAGE');
}
$this->functions_mchat->mchat_prune();
$this->functions->mchat_prune();
$this->functions_mchat->mchat_add_user_session();
$this->functions->mchat_add_user_session();
$this->assign_whois();
@@ -145,7 +163,7 @@ class mchat
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS_ARCHIVE');
}
$this->functions_mchat->mchat_prune();
$this->functions->mchat_prune();
$this->template->assign_var('MCHAT_ARCHIVE_PAGE', true);
@@ -175,7 +193,7 @@ class mchat
{
if (!$this->auth->acl_get('u_mchat_ip'))
{
throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
throw new \phpbb\exception\http_exception(403, 'NOT_AUTHORISED');
}
if (!function_exists('user_ipwhois'))
@@ -195,17 +213,21 @@ class mchat
*/
public function page_rules()
{
if (empty($this->config['mchat_rules']) && !$this->user->lang('MCHAT_RULES_MESSAGE'))
if (!$this->auth->acl_get('u_mchat_view'))
{
throw new \phpbb\exception\http_exception(403, 'NOT_AUTHORISED');
}
$lang_rules = $this->user->lang('MCHAT_RULES_MESSAGE');
if (!$this->config['mchat_rules'] && !$lang_rules)
{
throw new \phpbb\exception\http_exception(404, 'MCHAT_NO_RULES');
}
// If the rules are defined in the language file use them, else just use the entry in the database
$mchat_rules = $this->user->lang('MCHAT_RULES_MESSAGE');
$mchat_rules = !empty($mchat_rules) ? $mchat_rules : $this->config['mchat_rules'];
$mchat_rules = explode("\n", $mchat_rules);
$mchat_rules = array_map('htmlspecialchars_decode', $mchat_rules);
$mchat_rules = implode('<br />', $mchat_rules);
$mchat_rules = $lang_rules ?: $this->config['mchat_rules'];
$mchat_rules = htmlspecialchars_decode($mchat_rules);
$mchat_rules = str_replace("\n", '<br />', $mchat_rules);
$this->template->assign_var('MCHAT_RULES', $mchat_rules);
@@ -224,20 +246,25 @@ class mchat
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
if ($this->functions_mchat->mchat_is_user_flooding())
if ($this->functions->mchat_is_user_flooding())
{
throw new \phpbb\exception\http_exception(400, 'MCHAT_NOACCESS');
}
$message = $this->request->variable('message', '', true);
$sql_ary = $this->process_message(utf8_ucfirst($message), array(
if ($this->user->data['user_mchat_capital_letter'])
{
$message = utf8_ucfirst($message);
}
$sql_ary = $this->process_message($message, array(
'user_id' => $this->user->data['user_id'],
'user_ip' => $this->user->data['session_ip'],
'message_time' => time(),
));
$this->functions_mchat->mchat_action('add', $sql_ary);
$is_new_session = $this->functions->mchat_action('add', $sql_ary);
/**
* Event render_helper_add
@@ -247,7 +274,14 @@ class mchat
*/
$this->dispatcher->dispatch('dmzx.mchat.core.render_helper_add');
return $this->action_refresh();
$data = $this->action_refresh();
if ($is_new_session)
{
$data['whois'] = true;
}
return $data;
}
/**
@@ -257,12 +291,6 @@ class mchat
*/
public function action_edit()
{
// Fix avatar path when editing archived messages
if (!defined('PHPBB_USE_BOARD_URL_PATH'))
{
define('PHPBB_USE_BOARD_URL_PATH', true);
}
$message_id = $this->request->variable('message_id', 0);
if (!$message_id || !check_form_key('mchat', -1))
@@ -270,21 +298,23 @@ class mchat
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$author = $this->functions_mchat->mchat_author_for_message($message_id);
$author = $this->functions->mchat_author_for_message($message_id);
if (!$author || !$this->auth_message('u_mchat_edit', $author['user_id'], $author['message_time']))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$is_archive = $this->request->variable('archive', 0);
$this->template->assign_var('MCHAT_ARCHIVE_PAGE', $is_archive);
$message = $this->request->variable('message', '', true);
$sql_ary = $this->process_message($message, array(
'edit_time' => time(),
));
// TODO Don't update the message if the user submitted it unedited
$this->functions_mchat->mchat_action('edit', $sql_ary, $message_id, $author['username']);
$this->functions->mchat_action('edit', $sql_ary, $message_id, $author['username']);
/**
* Event render_helper_edit
@@ -295,10 +325,10 @@ class mchat
$this->dispatcher->dispatch('dmzx.mchat.core.render_helper_edit');
$sql_where = 'm.message_id = ' . (int) $message_id;
$rows = $this->functions_mchat->mchat_get_messages($sql_where, 1);
$rows = $this->functions->mchat_get_messages($sql_where, 1);
$this->assign_global_template_data();
$this->assign_messages($rows);
$this->assign_messages($rows, $is_archive ? 'archive' : '');
return array('edit' => $this->render_template('mchat_messages.html'));
}
@@ -317,7 +347,7 @@ class mchat
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$author = $this->functions_mchat->mchat_author_for_message($message_id);
$author = $this->functions->mchat_author_for_message($message_id);
if (!$author || !$this->auth_message('u_mchat_delete', $author['user_id'], $author['message_time']))
{
@@ -332,7 +362,7 @@ class mchat
*/
$this->dispatcher->dispatch('dmzx.mchat.core.render_helper_delete');
$this->functions_mchat->mchat_action('del', null, $message_id, $author['username']);
$this->functions->mchat_action('del', null, $message_id, $author['username']);
return array('del' => true);
}
@@ -344,6 +374,17 @@ class mchat
*/
public function action_refresh()
{
if (!$this->auth->acl_get('u_mchat_view'))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
// Keep the session alive forever if there is no user session timeout
if (!$this->config['mchat_timeout'])
{
$this->user->update_session_infos();
}
$message_first_id = $this->request->variable('message_first_id', 0);
$message_last_id = $this->request->variable('message_last_id', 0);
$message_edits = $this->request->variable('message_edits', array(0));
@@ -354,8 +395,11 @@ class mchat
// Request edited messages
if ($this->config['mchat_live_updates'] && $message_last_id > 0)
{
$sql_time_limit = $this->config['mchat_edit_delete_limit'] ? sprintf(' AND m.message_time > %d', time() - $this->config['mchat_edit_delete_limit']) : '';
$sql_where .= sprintf(' OR (m.message_id BETWEEN %d AND %d AND m.edit_time > 0%s)', (int) $message_first_id , (int) $message_last_id, $sql_time_limit);
$sql_where .= sprintf(' OR (m.message_id BETWEEN %d AND %d AND m.edit_time > 0)', (int) $message_first_id , (int) $message_last_id);
if ($this->config['mchat_edit_delete_limit'])
{
$sql_where .= sprintf(' AND m.message_time > %d', time() - $this->config['mchat_edit_delete_limit']);
}
}
// Exclude post notifications
@@ -364,7 +408,7 @@ class mchat
$sql_where = '(' . $sql_where . ') AND m.forum_id = 0';
}
$rows = $this->functions_mchat->mchat_get_messages($sql_where);
$rows = $this->functions->mchat_get_messages($sql_where);
$rows_refresh = array();
$rows_edit = array();
@@ -381,27 +425,32 @@ class mchat
}
}
// Assign new messages
$this->assign_global_template_data();
$this->assign_messages($rows_refresh);
$response = array('refresh' => true, 'add' => $this->render_template('mchat_messages.html'));
// Assign edited messages
if (!empty($rows_edit))
if ($rows_refresh || $rows_edit)
{
$response['edit'] = array();
foreach ($rows_edit as $row)
{
$this->assign_messages(array($row));
$response['edit'][$row['message_id']] = $this->render_template('mchat_messages.html');
}
$this->assign_global_template_data();
}
// Request deleted messages
$response = array('refresh' => true);
// Assign new messages
if ($rows_refresh)
{
$this->assign_messages($rows_refresh);
$response['add'] = $this->render_template('mchat_messages.html');
}
// Assign edited messages
if ($rows_edit)
{
$this->assign_messages($rows_edit);
$response['edit'] = $this->render_template('mchat_messages.html');
}
// Assign deleted messages
if ($this->config['mchat_live_updates'] && $message_last_id > 0)
{
$deleted_message_ids = $this->functions_mchat->mchat_missing_ids($message_first_id, $message_last_id);
if (!empty($deleted_message_ids))
$deleted_message_ids = $this->functions->mchat_deleted_ids($message_first_id);
if ($deleted_message_ids)
{
$response['del'] = $deleted_message_ids;
}
@@ -429,28 +478,12 @@ class mchat
{
$this->template->assign_vars(array(
'MCHAT_ALLOW_VIEW' => $this->auth->acl_get('u_mchat_view'),
'MCHAT_NAVBAR_LINK' => $this->config['mchat_navbar_link'],
'S_MCHAT_CUSTOM_PAGE' => $this->config['mchat_custom_page'],
'U_MCHAT' => $this->helper->route('dmzx_mchat_controller'),
));
}
/**
* Appends a condition to the WHERE key of the SQL array to not fetch disallowed BBCodes from the database
*
* @param array $sql_ary
* @return array
*/
public function remove_disallowed_bbcodes($sql_ary)
{
// Add disallowed BBCodes to the template only if we're rendering for mChat
if ($this->is_mchat_rendered)
{
$sql_ary['WHERE'] = $this->functions_mchat->mchat_sql_append_forbidden_bbcodes($sql_ary['WHERE']);
}
return $sql_ary;
}
/**
* Renders data for a page
*
@@ -463,59 +496,95 @@ class mchat
// If the static message is defined in the language file use it, else the entry in the database is used
$lang_static_message = $this->user->lang('MCHAT_STATIC_MESSAGE');
$static_message = !empty($lang_static_message) ? $lang_static_message : $this->config['mchat_static_message'];
$static_message = $lang_static_message ?: $this->config['mchat_static_message'];
$this->template->assign_vars(array(
'MCHAT_FILE_NAME' => $this->helper->route('dmzx_mchat_controller'),
'U_MCHAT_CUSTOM_PAGE' => $this->helper->route('dmzx_mchat_controller'),
'MCHAT_REFRESH_JS' => 1000 * $this->config['mchat_refresh'],
'MCHAT_INPUT_TYPE' => $this->user->data['user_mchat_input_area'],
'MCHAT_RULES' => $this->user->lang('MCHAT_RULES_MESSAGE') || !empty($this->config['mchat_rules']),
'MCHAT_RULES' => $this->user->lang('MCHAT_RULES_MESSAGE') || $this->config['mchat_rules'],
'MCHAT_ALLOW_USE' => $this->auth->acl_get('u_mchat_use'),
'MCHAT_ALLOW_SMILES' => $this->config['allow_smilies'] && $this->auth->acl_get('u_mchat_smilies'),
'MCHAT_ALLOW_BBCODES' => $this->config['allow_bbcode'] && $this->auth->acl_get('u_mchat_bbcode'),
'S_BBCODE_ALLOWED' => $this->config['allow_bbcode'] && $this->auth->acl_get('u_mchat_bbcode'),
'MCHAT_MESSAGE_TOP' => $this->config['mchat_message_top'],
'MCHAT_ARCHIVE_URL' => $this->helper->route('dmzx_mchat_page_controller', array('page' => 'archive')),
'MCHAT_INDEX_HEIGHT' => $this->config['mchat_index_height'],
'MCHAT_CUSTOM_HEIGHT' => $this->config['mchat_custom_height'],
'MCHAT_READ_ARCHIVE_BUTTON' => $this->auth->acl_get('u_mchat_archive'),
'MCHAT_STATIC_MESS' => htmlspecialchars_decode($static_message),
'L_MCHAT_COPYRIGHT' => base64_decode('PGEgaHJlZj0iaHR0cDovL3JtY2dpcnI4My5vcmciPlJNY0dpcnI4MzwvYT4gJmNvcHk7IDxhIGhyZWY9Imh0dHA6Ly93d3cuZG16eC13ZWIubmV0IiB0aXRsZT0id3d3LmRtengtd2ViLm5ldCI+ZG16eDwvYT4='),
'L_MCHAT_COPYRIGHT' => base64_decode('PHNwYW4gY2xhc3M9Im1jaGF0LWNvcHlyaWdodCIgdGl0bGU9ImRtenggJmJ1bGw7IGthc2ltaSAmYnVsbDsgUk1jR2lycjgzIj4mY29weTs8L3NwYW4+'),
'MCHAT_MESSAGE_LNGTH' => $this->config['mchat_max_message_lngth'],
'MCHAT_MESS_LONG' => sprintf($this->user->lang('MCHAT_MESS_LONG'), $this->config['mchat_max_message_lngth']),
'MCHAT_MESS_LONG' => $this->user->lang('MCHAT_MESS_LONG', $this->config['mchat_max_message_lngth']),
'MCHAT_USER_TIMEOUT_TIME' => gmdate('H:i:s', (int) $this->config['mchat_timeout']),
'MCHAT_WHOIS_REFRESH' => $this->config['mchat_whois'] ? 1000 * $this->config['mchat_whois_refresh'] : 0,
'MCHAT_WHOIS_REFRESH_EXPLAIN' => sprintf($this->user->lang('WHO_IS_REFRESH_EXPLAIN'), $this->config['mchat_whois_refresh']),
'MCHAT_WHOIS_REFRESH_EXPLAIN' => $this->user->lang('MCHAT_WHO_IS_REFRESH_EXPLAIN', $this->config['mchat_whois_refresh']),
'MCHAT_PAUSE_ON_INPUT' => $this->config['mchat_pause_on_input'],
'MCHAT_REFRESH_YES' => sprintf($this->user->lang('MCHAT_REFRESH_YES'), $this->config['mchat_refresh']),
'MCHAT_REFRESH_YES' => $this->user->lang('MCHAT_REFRESH_YES', $this->config['mchat_refresh']),
'MCHAT_LIVE_UPDATES' => $this->config['mchat_live_updates'],
'S_MCHAT_LOCATION' => $this->config['mchat_location'],
'S_MCHAT_SOUND_YES' => $this->user->data['user_mchat_sound'],
'U_MORE_SMILIES' => generate_board_url() . append_sid("/{$this->root_path}/posting.{$this->php_ext}", 'mode=smilies'),
'U_MCHAT_RULES' => $this->helper->route('dmzx_mchat_page_controller', array('page' => 'rules')),
'S_MCHAT_ON_INDEX' => $this->config['mchat_on_index'] && !empty($this->user->data['user_mchat_index']),
'S_MCHAT_ON_INDEX' => $this->config['mchat_on_index'] && $this->user->data['user_mchat_index'],
));
$md_manager = $this->extension_manager->create_extension_metadata_manager('dmzx/mchat', $this->template);
$md_manager->get_metadata('all');
$md_manager->output_template_data();
// The template needs some language variables if we display relative time for messages
if ($this->config['mchat_relative_time'] && $page != 'archive')
{
$minutes_limit = $this->get_relative_minutes_limit();
for ($i = 0; $i < $minutes_limit; $i++)
{
$this->template->assign_block_vars('mchattime', array(
'KEY' => $i,
'LANG' => $this->user->lang('MCHAT_MINUTES_AGO', $i),
'IS_LAST' => $i + 1 === $minutes_limit,
));
}
}
// Get actions which the user is allowed to perform on the current page
$actions = array_keys(array_filter(array(
'edit' => $this->auth_message('u_mchat_edit', true, time()),
'del' => $this->auth_message('u_mchat_delete', true, time()),
'refresh' => $page != 'archive' && $this->auth->acl_get('u_mchat_view'),
'add' => $page != 'archive' && $this->auth->acl_get('u_mchat_use'),
'whois' => $page != 'archive' && $this->config['mchat_whois'],
)));
foreach ($actions as $i => $action)
{
$this->template->assign_block_vars('mchaturl', array(
'ACTION' => $action,
'URL' => $this->helper->route('dmzx_mchat_action_controller', array('action' => $action)),
'IS_LAST' => $i + 1 === count($actions),
));
}
$sql_where = $this->user->data['user_mchat_topics'] ? '' : 'm.forum_id = 0';
$limit = $page == 'archive' ? $this->config['mchat_archive_limit'] : $this->config[$page == 'index' ? 'mchat_message_num' : 'mchat_message_limit'];
$start = $page == 'archive' ? $this->request->variable('start', 0) : 0;
$rows = $this->functions_mchat->mchat_get_messages($sql_where, $limit, $start);
$rows = $this->functions->mchat_get_messages($sql_where, $limit, $start);
$this->assign_global_template_data();
$this->assign_messages($rows);
$this->assign_messages($rows, $page);
// Render pagination
if ($page == 'archive')
{
$archive_url = $this->helper->route('dmzx_mchat_page_controller', array('page' => 'archive'));
$total_messages = $this->functions_mchat->mchat_total_message_count();
$total_messages = $this->functions->mchat_total_message_count();
$this->pagination->generate_template_pagination($archive_url, 'pagination', 'start', $total_messages, $limit, $start);
$this->template->assign_var('MCHAT_TOTAL_MESSAGES', sprintf($this->user->lang('MCHAT_TOTALMESSAGES'), $total_messages));
$this->template->assign_var('MCHAT_TOTAL_MESSAGES', $this->user->lang('MCHAT_TOTALMESSAGES', $total_messages));
}
// Render legend
if ($page != 'index' && $this->config['mchat_whois'])
{
$legend = $this->functions_mchat->mchat_legend();
$legend = $this->functions->mchat_legend();
$this->template->assign_var('LEGEND', implode(', ', $legend));
}
@@ -524,8 +593,6 @@ class mchat
add_form_key('mchat');
}
$this->is_mchat_rendered = true;
/**
* Event render_helper_aft
*
@@ -536,7 +603,7 @@ class mchat
}
/**
* Assigns all message rows to the template
* Assigns common template data that is required for displaying messages
*/
protected function assign_global_template_data()
{
@@ -547,6 +614,7 @@ class mchat
'MCHAT_ALLOW_QUOTE' => $this->auth->acl_get('u_mchat_quote'),
'MCHAT_EDIT_DELETE_LIMIT' => 1000 * $this->config['mchat_edit_delete_limit'],
'MCHAT_EDIT_DELETE_IGNORE' => $this->config['mchat_edit_delete_limit'] && $this->auth->acl_get('m_'),
'MCHAT_RELATIVE_TIME' => $this->config['mchat_relative_time'],
'MCHAT_USER_TIMEOUT' => 1000 * $this->config['mchat_timeout'],
'S_MCHAT_AVATARS' => $this->display_avatars(),
'EXT_URL' => generate_board_url() . '/ext/dmzx/mchat/',
@@ -568,10 +636,11 @@ class mchat
* Assigns all message rows to the template
*
* @param array $rows
* @param string $page
*/
protected function assign_messages($rows)
protected function assign_messages($rows, $page = '')
{
if (empty($rows))
if (!$rows)
{
return;
}
@@ -582,7 +651,7 @@ class mchat
$rows = array_reverse($rows);
}
$foes = $this->functions_mchat->mchat_foes();
$foes = $this->functions->mchat_foes();
$this->template->destroy_block_vars('mchatrow');
@@ -602,84 +671,149 @@ class mchat
}
}
$board_url = generate_board_url() . '/';
foreach ($rows as $i => $row)
{
// Auth checks
if ($row['forum_id'] != 0 && !$this->auth->acl_get('f_read', $row['forum_id']))
if ($row['forum_id'] && !$this->auth->acl_get('f_read', $row['forum_id']))
{
continue;
}
$message_edit = $row['message'];
decode_message($message_edit, $row['bbcode_uid']);
$message_edit = str_replace('"', '&quot;', $message_edit);
$message_edit = mb_ereg_replace("'", '&#146;', $message_edit);
if (in_array($row['user_id'], $foes))
{
$row['message'] = sprintf($this->user->lang('MCHAT_FOE'), get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST')));
$row['message'] = $this->user->lang('MCHAT_FOE', get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST')));
}
$row['username'] = mb_ereg_replace("'", "&#146;", $row['username']);
$message = str_replace("'", '&rsquo;', $row['message']);
$username_full = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST'));
// Fix profile link root path by replacing relative paths with absolute board URL
if ($this->request->is_ajax())
{
$username_full = preg_replace('#(?<=href=")[\./]+?/(?=\w)#', $board_url, $username_full);
}
$message_age = time() - $row['message_time'];
$minutes_ago = $this->get_minutes_ago($message_age, $page);
$datetime = $this->user->format_date($row['message_time'], $this->config['mchat_date']);
$this->template->assign_block_vars('mchatrow', array(
'S_ROW_COUNT' => $i,
'MCHAT_ALLOW_BAN' => $this->auth->acl_get('a_authusers'),
'MCHAT_ALLOW_EDIT' => $this->auth_message('u_mchat_edit', $row['user_id'], $row['message_time']),
'MCHAT_ALLOW_DEL' => $this->auth_message('u_mchat_delete', $row['user_id'], $row['message_time']),
'MCHAT_USER_AVATAR' => $user_avatars[$row['user_id']],
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? generate_board_url() . append_sid("/{$this->root_path}memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $row['user_id']) : '',
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? append_sid("{$board_url}{$this->root_path}memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $row['user_id']) : '',
'MCHAT_IS_POSTER' => $row['user_id'] != ANONYMOUS && $this->user->data['user_id'] == $row['user_id'],
'MCHAT_PM' => $row['user_id'] != ANONYMOUS && $this->user->data['user_id'] != $row['user_id'] && $this->config['allow_privmsg'] && $this->auth->acl_get('u_sendpm') && ($row['user_allow_pm'] || $this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_')) ? generate_board_url() . append_sid("/{$this->root_path}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;u=' . $row['user_id']) : '',
'MCHAT_PM' => $row['user_id'] != ANONYMOUS && $this->user->data['user_id'] != $row['user_id'] && $this->config['allow_privmsg'] && $this->auth->acl_get('u_sendpm') && ($row['user_allow_pm'] || $this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_')) ? append_sid("{$board_url}{$this->root_path}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;u=' . $row['user_id']) : '',
'MCHAT_MESSAGE_EDIT' => $message_edit,
'MCHAT_MESSAGE_ID' => $row['message_id'],
'MCHAT_USERNAME_FULL' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST')),
'MCHAT_USERNAME_FULL' => $username_full,
'MCHAT_USERNAME' => get_username_string('username', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST')),
'MCHAT_USERNAME_COLOR' => get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST')),
'MCHAT_WHOIS_USER' => $this->user->lang('MCHAT_WHOIS_USER', $row['user_ip']),
'MCHAT_U_IP' => $this->helper->route('dmzx_mchat_page_controller', array('page' => 'whois', 'ip' => $row['user_ip'])),
'MCHAT_U_BAN' => generate_board_url() . append_sid("/{$this->root_path}adm/index.{$this->php_ext}" ,'i=permissions&amp;mode=setting_user_global&amp;user_id[0]=' . $row['user_id'], true, $this->user->session_id),
'MCHAT_MESSAGE' => censor_text(generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options'])),
'MCHAT_TIME' => $this->user->format_date($row['message_time'], $this->config['mchat_date']),
'MCHAT_U_BAN' => append_sid("{$board_url}{$this->root_path}adm/index.{$this->php_ext}" ,'i=permissions&amp;mode=setting_user_global&amp;user_id[0]=' . $row['user_id'], true, $this->user->session_id),
'MCHAT_MESSAGE' => generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']),
'MCHAT_TIME' => $minutes_ago === -1 ? $datetime : $this->user->lang('MCHAT_MINUTES_AGO', $minutes_ago),
'MCHAT_DATETIME' => $datetime,
'MCHAT_MINUTES_AGO' => $minutes_ago,
'MCHAT_RELATIVE_UPDATE' => 60 - $message_age % 60,
'MCHAT_MESSAGE_TIME' => $row['message_time'],
'MCHAT_EDIT_TIME' => $row['edit_time'],
));
}
}
/**
* Calculates the number of minutes that have passed since the message was posted. If relative time is disabled
* or the message is older than 59 minutes or we render for the archive, -1 is returned.
*
* @param int $message_age
* @param string $page
* @return int
*/
protected function get_minutes_ago($message_age, $page)
{
if ($this->config['mchat_relative_time'] && $page != 'archive')
{
$minutes_ago = floor($message_age / 60);
if ($minutes_ago < $this->get_relative_minutes_limit())
{
return $minutes_ago;
}
}
return -1;
}
/**
* Calculates the amount of time after which messages switch from displaying relative time
* to displaying absolute time. Uses mChat's timeout if it's not zero, otherwise phpBB's
* global session timeout, but never shorter than 1 minute and never longer than 60 minutes.
*
* @return int
*/
protected function get_relative_minutes_limit()
{
$timeout = $this->config['session_length'];
if ($this->config['mchat_timeout'])
{
$timeout = $this->config['mchat_timeout'];
}
return min(max((int) ceil($timeout / 60), 1), 60);
}
/**
* Assigns BBCodes and smilies to the template
*/
protected function assign_bbcodes_smilies()
{
// Display custom bbcodes
// Display BBCodes
if ($this->config['allow_bbcode'] && $this->auth->acl_get('u_mchat_bbcode'))
{
$default_bbcodes = array('B', 'I', 'U', 'QUOTE', 'CODE', 'LIST', 'IMG', 'URL', 'SIZE', 'COLOR', 'EMAIL', 'FLASH');
$bbcode_template_vars = array(
'quote' => array(
'allow' => true,
'template_var' => 'S_BBCODE_QUOTE',
),
'img' => array(
'allow' => true,
'template_var' => 'S_BBCODE_IMG',
),
'url' => array(
'allow' => $this->config['allow_post_links'],
'template_var' => 'S_LINKS_ALLOWED',
),
'flash' => array(
'allow' => $this->config['allow_post_flash'],
'template_var' => 'S_BBCODE_FLASH',
),
);
// Let's remove the default bbcodes
$disallowed_bbcode_array = explode('|', strtoupper($this->config['mchat_bbcode_disallowed']));
foreach ($default_bbcodes as $default_bbcode)
foreach ($bbcode_template_vars as $bbcode => $option)
{
if (!in_array($default_bbcode, $disallowed_bbcode_array))
{
$this->template->assign_vars(array(
'S_MCHAT_BBCODE_' . $default_bbcode => true,
));
}
$is_disallowed = preg_match('#(^|\|)' . $bbcode . '($|\|)#Usi', $this->config['mchat_bbcode_disallowed']) || !$option['allow'];
$this->template->assign_var($option['template_var'], !$is_disallowed);
}
$this->template->assign_var('S_DISALLOWED_BBCODES', str_replace('=', '-', $this->config['mchat_bbcode_disallowed']));
if (!function_exists('display_custom_bbcodes'))
{
include($this->root_path . 'includes/functions_display.' . $this->php_ext);
}
$this->remove_disallowed_bbcodes = true;
display_custom_bbcodes();
}
// Smile row
// Display smilies
if ($this->config['allow_smilies'] && $this->auth->acl_get('u_mchat_smilies'))
{
if (!function_exists('generate_smilies'))
@@ -691,6 +825,23 @@ class mchat
}
}
/**
* Appends a condition to the WHERE key of the SQL array to not fetch disallowed BBCodes from the database
*
* @param array $sql_ary
* @return array
*/
public function remove_disallowed_bbcodes($sql_ary)
{
// Add disallowed BBCodes to the template only if we're rendering for mChat
if ($this->remove_disallowed_bbcodes)
{
$sql_ary['WHERE'] = $this->functions->mchat_sql_append_forbidden_bbcodes($sql_ary['WHERE']);
}
return $sql_ary;
}
/**
* Assigns whois and stats at the bottom of the index page
*/
@@ -698,11 +849,11 @@ class mchat
{
if ($this->config['mchat_whois'] || $this->config['mchat_stats_index'] && $this->user->data['user_mchat_stats_index'])
{
$mchat_stats = $this->functions_mchat->mchat_active_users();
$mchat_stats = $this->functions->mchat_active_users();
$this->template->assign_vars(array(
'MCHAT_INDEX_STATS' => $this->config['mchat_stats_index'] && $this->user->data['user_mchat_stats_index'],
'MCHAT_USERS_COUNT' => $mchat_stats['mchat_users_count'],
'MCHAT_USERS_LIST' => !empty($mchat_stats['online_userlist']) ? $mchat_stats['online_userlist'] : '',
'MCHAT_USERS_LIST' => $mchat_stats['online_userlist'] ?: '',
'MCHAT_ONLINE_EXPLAIN' => $mchat_stats['refresh_message'],
));
}
@@ -744,7 +895,7 @@ class mchat
{
// Must have something other than bbcode in the message
$message_chars = trim(preg_replace('#\[/?[^\[\]]+\]#mi', '', $message));
if (!$message || !utf8_strlen($message_chars))
if (!utf8_strlen($message_chars))
{
throw new \phpbb\exception\http_exception(501, 'MCHAT_NOACCESS');
}