Version 0.3.3

This commit is contained in:
dmzx
2016-01-28 20:54:41 +01:00
parent 08b6b57225
commit 6a3cb1cd04
54 changed files with 1906 additions and 4475 deletions

View File

@@ -14,9 +14,6 @@ class functions_mchat
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\user */
protected $user;
@@ -29,137 +26,103 @@ class functions_mchat
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\cache\service */
/** @var \phpbb\cache\driver\driver_interface */
protected $cache;
/** @var string */
protected $phpbb_root_path;
/** @var string */
protected $phpEx;
protected $php_ext;
/** @var string */
protected $mchat_table;
/** @var string */
protected $mchat_config_table;
/** @var string */
protected $mchat_sessions_table;
/** @var array */
protected $foes = null;
/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\template\template $template
* @param \phpbb\user $user
* @param \phpbb\auth\auth $auth
* @param \phpbb\log\log_interface $log
* @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\cache\service $cache
* @param string $phpbb_root_path
* @param string $phpEx
* @param string $mchat_table
* @param string $mchat_config_table
* @param string $mchat_sessions_table
* @param \phpbb\config\config $config
* @param \phpbb\user $user
* @param \phpbb\auth\auth $auth
* @param \phpbb\log\log_interface $log
* @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\cache\driver\driver_interface $cache
* @param string $phpbb_root_path
* @param string $php_ext
* @param string $mchat_table
* @param string $mchat_sessions_table
*/
function __construct(\phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\db\driver\driver_interface $db, \phpbb\cache\service $cache, $phpbb_root_path, $phpEx, $mchat_table, $mchat_config_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, $phpbb_root_path, $php_ext, $mchat_table, $mchat_sessions_table)
{
$this->config = $config;
$this->template = $template;
$this->user = $user;
$this->auth = $auth;
$this->log = $log;
$this->db = $db;
$this->cache = $cache;
$this->phpbb_root_path = $phpbb_root_path;
$this->phpEx = $phpEx;
$this->php_ext = $php_ext;
$this->mchat_table = $mchat_table;
$this->mchat_config_table = $mchat_config_table;
$this->mchat_sessions_table = $mchat_sessions_table;
}
/**
* Builds the cache if it doesn't exist
* Converts a number of seconds to a string in the format 'x hours y minutes z seconds'
*/
function mchat_cache()
protected function mchat_format_seconds($time)
{
// Grab the config entries in the ACP...and cache em :P
$config_mchat = $this->cache->get('_mchat_config');
$times = array();
if ($config_mchat === false)
$hours = floor($time / 3600);
if ($hours)
{
$sql = 'SELECT *
FROM ' . $this->mchat_config_table;
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$config_mchat = array();
foreach ($rows as $row)
{
$config_mchat[$row['config_name']] = $row['config_value'];
}
$this->cache->put('_mchat_config', $config_mchat);
$time -= $hours * 3600;
$times[] = $hours . ' ' . $this->user->lang($hours > 1 ? 'MCHAT_HOURS' : 'MCHAT_HOUR');
}
return $config_mchat;
}
/**
* @param $time the amount of time to display
*/
function mchat_session_time($time)
{
// Fix the display of the time limit
$chat_session = '';
$chat_timeout = (int) $time;
$hours = $minutes = $seconds = 0;
if ($chat_timeout >= 3600)
{
$hours = floor($chat_timeout / 3600);
$chat_timeout = $chat_timeout - ($hours * 3600);
$chat_session .= $hours > 1 ? ($hours . ' ' . $this->user->lang('MCHAT_HOURS')) : ($hours . ' ' . $this->user->lang('MCHAT_HOUR'));
}
$minutes = floor($chat_timeout / 60);
$minutes = floor($time / 60);
if ($minutes)
{
$minutes = $minutes > 1 ? ($minutes . ' ' . $this->user->lang('MCHAT_MINUTES')) : ($minutes . ' ' . $this->user->lang('MCHAT_MINUTE'));
$chat_timeout = $chat_timeout - ($minutes * 60);
$chat_session .= $minutes;
$time -= $minutes * 60;
$times[] = $minutes . ' ' . $this->user->lang($minutes > 1 ? 'MCHAT_MINUTES' : 'MCHAT_MINUTE');
}
$seconds = ceil($chat_timeout);
$seconds = ceil($time);
if ($seconds)
{
$seconds = $seconds > 1 ? ($seconds . ' ' . $this->user->lang('MCHAT_SECONDS')) : ($seconds . ' ' . $this->user->lang('MCHAT_SECOND'));
$chat_session .= $seconds;
$times[] = $seconds . ' ' . $this->user->lang($seconds > 1 ? 'MCHAT_SECONDS' : 'MCHAT_SECOND');
}
return sprintf($this->user->lang('MCHAT_ONLINE_EXPLAIN'), $chat_session);
return sprintf($this->user->lang('MCHAT_ONLINE_EXPLAIN'), implode(' ', $times));
}
/**
* @param $session_time amount of time before a users session times out
* Returns the total session time in seconds
*/
function mchat_users($session_time)
protected function mchat_session_time()
{
$check_time = time() - (int) $session_time;
return !empty($this->config['mchat_timeout']) ? $this->config['mchat_timeout'] : (!empty($this->config['load_online_time']) ? $this->config['load_online_time'] * 60 : $this->config['session_length']);
}
$sql = 'DELETE FROM ' . $this->mchat_sessions_table . '
WHERE user_lastupdate < ' . $check_time;
$this->db->sql_query($sql);
/**
* Returns data about users who are currently chatting
*/
public function mchat_active_users()
{
$mchat_users = array();
$mchat_user_count = 0;
$mchat_user_list = '';
$check_time = time() - $this->mchat_session_time();
$sql = 'SELECT m.user_id, u.username, u.user_type, u.user_allow_viewonline, u.user_colour
FROM ' . $this->mchat_sessions_table . ' m
LEFT JOIN ' . USERS_TABLE . ' u ON m.user_id = u.user_id
WHERE m.user_lastupdate > ' . $check_time . '
WHERE m.user_lastupdate >= ' . (int) $check_time . '
ORDER BY u.username ASC';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
@@ -174,48 +137,31 @@ class functions_mchat
{
continue;
}
else
{
$row['username'] = '<em>' . $row['username'] . '</em>';
}
$row['username'] = '<em>' . $row['username'] . '</em>';
}
$mchat_user_count++;
$mchat_user_online_link = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST'));
$mchat_user_list .= ($mchat_user_list != '') ? $this->user->lang('COMMA_SEPARATOR') . $mchat_user_online_link : $mchat_user_online_link;
$mchat_users[] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST'));
}
$refresh_message = $this->mchat_session_time($session_time);
if (!$mchat_user_count)
{
return array(
'online_userlist' => '',
'mchat_users_count' => $this->user->lang('MCHAT_NO_CHATTERS'),
'refresh_message' => $refresh_message,
);
}
else
{
return array(
'online_userlist' => $mchat_user_list,
'mchat_users_count' => sprintf($this->user->lang($mchat_user_count > 1 ? 'MCHAT_ONLINE_USERS_TOTAL' : 'MCHAT_ONLINE_USER_TOTAL'), $mchat_user_count),
'refresh_message' => $refresh_message,
);
}
return array(
'online_userlist' => implode($this->user->lang('COMMA_SEPARATOR'), $mchat_users),
'mchat_users_count' => count($mchat_users) ? $this->user->lang(count($mchat_users) > 1 ? 'MCHAT_ONLINE_USERS_TOTAL' : 'MCHAT_ONLINE_USER_TOTAL', count($mchat_users)) : $this->user->lang('MCHAT_NO_CHATTERS'),
'refresh_message' => $this->mchat_format_seconds($this->mchat_session_time()),
);
}
/**
* @param mixed $session_time amount of time before a user is not shown as being in the chat
* Inserts the current user into the mchat_sessions table
*/
function mchat_sessions($session_time)
public function mchat_add_user_session()
{
$check_time = time() - (int) $session_time;
// Remove expired sessions from the database
$check_time = time() - $this->mchat_session_time();
$sql = 'DELETE FROM ' . $this->mchat_sessions_table . '
WHERE user_lastupdate < ' . $check_time;
$this->db->sql_query($sql);
// Insert user into the mChat sessions table
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 *
@@ -225,12 +171,10 @@ class functions_mchat
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
$user_lastupdate = time();
if ($row)
{
$sql = 'UPDATE ' . $this->mchat_sessions_table . '
SET user_lastupdate = ' . $user_lastupdate . '
SET user_lastupdate = ' . time() . '
WHERE user_id = ' . (int) $this->user->data['user_id'];
}
else
@@ -238,7 +182,7 @@ class functions_mchat
$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'],
'user_lastupdate' => $user_lastupdate,
'user_lastupdate' => time(),
));
}
@@ -247,100 +191,44 @@ class functions_mchat
}
/**
* mChat add-on Topic Notification
*
* @param mixed $post_id limits deletion to a post_id in the forum
* Prune messages
*/
function mchat_delete_topic($post_id)
public function mchat_prune()
{
if ($post_id)
if ($this->config['mchat_prune'])
{
$sql = 'DELETE FROM ' . $this->mchat_table . '
WHERE post_id = ' . (int) $post_id;
$this->db->sql_query($sql);
}
}
$mchat_total_messages = $this->mchat_total_message_count();
/**
* AutoPrune Chats
*
* @param mixed $mchat_prune_amount set from mchat config entry
*/
function mchat_prune($mchat_prune_amount)
{
// How many chats do we have?
$sql = 'SELECT COUNT(message_id) AS messages
FROM ' . $this->mchat_table;
$result = $this->db->sql_query($sql);
$mchat_total_messages = (int) $this->db->sql_fetchfield('messages');
$this->db->sql_freeresult($result);
if ($mchat_total_messages <= $mchat_prune_amount)
{
return;
}
$result = $this->db->sql_query_limit('SELECT message_id
FROM '. $this->mchat_table . '
ORDER BY message_id ASC', 1);
$first_id = (int) $this->db->sql_fetchfield('message_id');
$this->db->sql_freeresult($result);
// Compute the delete id
$delete_id = $mchat_total_messages - $mchat_prune_amount + $first_id;
$sql = 'DELETE FROM ' . $this->mchat_table . '
WHERE message_id < ' . (int) $delete_id;
$this->db->sql_query($sql);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MCHAT_TABLE_PRUNED');
}
/**
* @param mixed $mchat_prune_amount set from mchat config entry
*/
function display_mchat_bbcodes()
{
$default_bbcodes = array('B', 'I', 'U', 'QUOTE', 'CODE', 'LIST', 'IMG', 'URL', 'SIZE', 'COLOR', 'EMAIL', 'FLASH');
$disallowed_bbcode_array = $this->get_disallowed_bbcodes();
// Let's remove the default bbcodes
if (!empty($disallowed_bbcode_array))
{
$disallowed_bbcode_array = array_map('strtoupper', $disallowed_bbcode_array);
foreach ($default_bbcodes as $default_bbcode)
if ($mchat_total_messages > $this->config['mchat_prune_num'])
{
if (!in_array($default_bbcode, $disallowed_bbcode_array))
{
$this->template->assign_vars(array(
'S_MCHAT_BBCODE_' . $default_bbcode => true,
));
}
$sql = 'SELECT message_id
FROM '. $this->mchat_table . '
ORDER BY message_id ASC';
$result = $this->db->sql_query_limit($sql, 1);
$first_id = (int) $this->db->sql_fetchfield('message_id');
$this->db->sql_freeresult($result);
// Compute new oldest message id
$delete_id = $mchat_total_messages - $this->config['mchat_prune_num'] + $first_id;
// Delete older messages
$this->mchat_action('prune', null, $delete_id);
}
}
display_custom_bbcodes();
}
public function get_disallowed_bbcodes()
/**
* Returns the total number of messages
*/
public function mchat_total_message_count()
{
$config_mchat = $this->mchat_cache();
$disallowed_bbcode = $config_mchat['bbcode_disallowed'];
$disallowed_bbcode_array = explode('|', $disallowed_bbcode);
return $disallowed_bbcode_array;
return $this->db->get_row_count($this->mchat_table);
}
function mchat_avatar($row)
{
return phpbb_get_user_avatar(array(
'avatar' => $row['user_avatar'],
'avatar_type' => $row['user_avatar_type'],
'avatar_width' => $row['user_avatar_width'] > $row['user_avatar_height'] ? 40 : (40 / $row['user_avatar_height']) * $row['user_avatar_width'],
'avatar_height' => $row['user_avatar_height'] > $row['user_avatar_width'] ? 40 : (40 / $row['user_avatar_width']) * $row['user_avatar_height'],
));
}
function mchat_messages($sql_where, $total, $offset = 0)
/**
* Fetch messages from the database
*/
public function mchat_get_messages($sql_where, $total = 0, $offset = 0)
{
$sql_array = array(
'SELECT' => 'm.*, u.username, u.user_colour, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height, u.user_allow_pm',
@@ -363,7 +251,10 @@ class functions_mchat
return $rows;
}
function mchat_legend()
/**
* Generates the user legend markup
*/
public function mchat_legend()
{
// Grab group details for legend display for who is online on the custom page
$order_legend = $this->config['legend_sort_groupname'] ? 'group_name' : 'group_legend';
@@ -399,69 +290,77 @@ class functions_mchat
}
else
{
$legend[] = '<a' . $colour_text . ' href="' . append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", 'mode=group&amp;g='.$row['group_id']) . '">' . $group_name . '</a>';
$legend[] = '<a' . $colour_text . ' href="' . append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=group&amp;g='.$row['group_id']) . '">' . $group_name . '</a>';
}
}
return $legend;
}
function mchat_truncate_messages()
/**
* Returns a list of all foes of the current user
*/
public function mchat_foes()
{
$sql = 'TRUNCATE TABLE ' . $this->mchat_table;
$this->db->sql_query($sql);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MCHAT_TABLE_PRUNED');
}
function mchat_foes()
{
$sql = 'SELECT *
FROM ' . ZEBRA_TABLE . '
WHERE user_id = ' . (int) $this->user->data['user_id'] . '
AND foe = 1';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$foes = array();
foreach ($rows as $row)
if (is_null($this->foes))
{
$foes[] = $row['zebra_id'];
$sql = 'SELECT *
FROM ' . ZEBRA_TABLE . '
WHERE foe = 1 AND user_id = ' . (int) $this->user->data['user_id'];
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$this->foes = array();
foreach ($rows as $row)
{
$this->foes[] = $row['zebra_id'];
}
}
return $foes;
return $this->foes;
}
function mchat_insert_posting($mode, $data)
/**
* Adds forbidden BBCodes to the passed SQL where statement
*/
public function mchat_sql_append_forbidden_bbcodes($sql_where)
{
if (empty($this->config['mchat_enable']) || empty($this->config['mchat_new_posts']))
$disallowed_bbcodes = explode('|', strtoupper($this->config['mchat_bbcode_disallowed']));
if (!empty($disallowed_bbcodes))
{
$sql_where .= ' AND ' . $this->db->sql_in_set('UPPER(b.bbcode_tag)', $disallowed_bbcodes, true);
}
return $sql_where;
}
/**
* Inserts a message with posting information into the database
*/
public function mchat_insert_posting($mode, $data)
{
if (!$this->config['mchat_new_posts'])
{
return;
}
if ($mode == 'post' && !empty($this->config['mchat_new_posts_topic']))
{
$mchat_new_data = $this->user->lang('MCHAT_NEW_TOPIC');
}
else if ($mode == 'quote' && !empty($this->config['mchat_new_posts_quote']))
{
$mchat_new_data = $this->user->lang('MCHAT_NEW_QUOTE');
}
else if ($mode == 'edit' && !empty($this->config['mchat_new_posts_edit']))
{
$mchat_new_data = $this->user->lang('MCHAT_NEW_EDIT');
}
else if ($mode == 'reply' && !empty($this->config['mchat_new_posts_reply']))
{
$mchat_new_data = $this->user->lang('MCHAT_NEW_REPLY');
}
else
$mode_config = array(
'post' => $this->config['mchat_new_posts_topic'],
'quote' => $this->config['mchat_new_posts_quote'],
'edit' => $this->config['mchat_new_posts_edit'],
'reply' => $this->config['mchat_new_posts_reply'],
);
if (empty($mode_config[$mode]))
{
return;
}
$message = utf8_normalize_nfc($mchat_new_data . ': [url=' . generate_board_url() . '/viewtopic.' . $this->phpEx . '?p=' . $data['post_id'] . '#p' . $data['post_id'] . ']' . $data['post_subject'] . '[/url] '. $this->user->lang('MCHAT_IN') . ' [url=' . generate_board_url() . '/viewforum.' . $this->phpEx . '?f=' . $data['forum_id'] . ']' . $data['forum_name'] . ' [/url] ' . $this->user->lang('MCHAT_IN_SECTION'));
$mchat_new_data = $this->user->lang('MCHAT_NEW_' . strtoupper($mode));
$message = utf8_normalize_nfc($mchat_new_data . ': [url=' . generate_board_url() . '/viewtopic.' . $this->php_ext . '?p=' . $data['post_id'] . '#p' . $data['post_id'] . ']' . $data['post_subject'] . '[/url] '. $this->user->lang('MCHAT_IN') . ' [url=' . generate_board_url() . '/viewforum.' . $this->php_ext . '?f=' . $data['forum_id'] . ']' . $data['forum_name'] . ' [/url] ' . $this->user->lang('MCHAT_IN_SECTION'));
$uid = $bitfield = $options = ''; // will be modified by generate_text_for_storage
generate_text_for_storage($message, $uid, $bitfield, $options, true, false, false);
@@ -479,4 +378,171 @@ class functions_mchat
$sql = 'INSERT INTO ' . $this->mchat_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
}
/**
* Checks if the current user is flooding the chat
*/
public function mchat_is_user_flooding()
{
if (!$this->config['mchat_flood_time'] || $this->auth->acl_get('u_mchat_flood_ignore'))
{
return false;
}
$sql = 'SELECT message_time
FROM ' . $this->mchat_table . '
WHERE user_id = ' . (int) $this->user->data['user_id'] . '
ORDER BY message_time DESC';
$result = $this->db->sql_query_limit($sql, 1);
$message_time = (int) $this->db->sql_fetchfield('message_time');
$this->db->sql_freeresult($result);
return $message_time && time() - $message_time < $this->config['mchat_flood_time'];
}
/**
* Returns user ID & name of the specified message
*/
public function mchat_author_for_message($message_id)
{
$sql = 'SELECT u.user_id, u.username, m.message_time
FROM ' . $this->mchat_table . ' m
LEFT JOIN ' . USERS_TABLE . ' u ON m.user_id = u.user_id
WHERE m.message_id = ' . (int) $message_id;
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
return $row;
}
/**
* Returns an array of message IDs that have been deleted from the message table
*/
public function mchat_missing_ids($start_id, $end_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 . '
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);
$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);
}
return $missing_ids;
}
/**
* Performs add|edit|del|clean|prune actions
*/
public function mchat_action($action, $sql_ary = null, $message_id = 0, $log_username = '')
{
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();
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->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->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;
// Founder purges all messages
case 'clean':
$sql = 'TRUNCATE TABLE ' . $this->mchat_table;
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MCHAT_TABLE_PRUNED');
$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);
break;
default:
return;
}
$this->db->sql_query($sql);
}
}

822
core/mchat.php Normal file
View File

@@ -0,0 +1,822 @@
<?php
/**
*
* @package phpBB Extension - mChat
* @copyright (c) 2015 dmzx - http://www.dmzx-web.net
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace dmzx\mchat\core;
class mchat
{
/** @var \dmzx\mchat\core\functions_mchat */
protected $functions_mchat;
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\controller\helper */
protected $helper;
/** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\user */
protected $user;
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \phpbb\pagination */
protected $pagination;
/** @var \phpbb\request\request */
protected $request;
/** @var \phpbb\event\dispatcher_interface */
protected $dispatcher;
/** @var string */
protected $root_path;
/** @var string */
protected $php_ext;
/** @var boolean */
protected $is_mchat_rendered = false;
/**
* Constructor
*
* @param \dmzx\mchat\core\functions_mchat $functions_mchat
* @param \phpbb\config\config $config
* @param \phpbb\controller\helper $helper
* @param \phpbb\template\template $template
* @param \phpbb\user $user
* @param \phpbb\auth\auth $auth
* @param \phpbb\pagination $pagination
* @param \phpbb\request\request $request
* @param \phpbb\event\dispatcher_interface $dispatcher
* @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)
{
$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;
}
/**
* Render mChat on the index page
*/
public function page_index()
{
if (!$this->auth->acl_get('u_mchat_view'))
{
return;
}
$this->assign_whois();
if (!$this->config['mchat_on_index'])
{
return;
}
// If mChat is used on the index by a user without an avatar, a default avatar is used.
// However, T_THEME_PATH points to ./../styles/... because the controller at /mchat is called, but we need it to be ./styles...
// Setting this value to true solves this.
if (!defined('PHPBB_USE_BOARD_URL_PATH'))
{
define('PHPBB_USE_BOARD_URL_PATH', true);
}
global $root_path;
$root_path = './';
$this->assign_bbcodes_smilies();
$this->render_page('index');
}
/**
* Render the mChat custom page
*/
public function page_custom()
{
if (!$this->auth->acl_get('u_mchat_view') || !$this->config['mchat_custom_page'])
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NO_CUSTOM_PAGE');
}
$this->functions_mchat->mchat_prune();
$this->functions_mchat->mchat_add_user_session();
$this->assign_whois();
$this->assign_bbcodes_smilies();
$this->template->assign_var('MCHAT_CUSTOM_PAGE', true);
$this->render_page('custom');
// Add to navlinks
$this->template->assign_block_vars('navlinks', array(
'FORUM_NAME' => $this->user->lang('MCHAT_TITLE'),
'U_VIEW_FORUM' => $this->helper->route('dmzx_mchat_controller'),
));
return $this->helper->render('mchat_body.html', $this->user->lang('MCHAT_TITLE'));
}
/**
* Render the mChat archive
*/
public function page_archive()
{
if (!$this->auth->acl_get('u_mchat_view') || !$this->auth->acl_get('u_mchat_archive'))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS_ARCHIVE');
}
$this->functions_mchat->mchat_prune();
$this->template->assign_var('MCHAT_ARCHIVE_PAGE', true);
$this->render_page('archive');
// Add to navlinks
$this->template->assign_block_vars_array('navlinks', array(
array(
'FORUM_NAME' => $this->user->lang('MCHAT_TITLE'),
'U_VIEW_FORUM' => $this->helper->route('dmzx_mchat_controller'),
),
array(
'FORUM_NAME' => $this->user->lang('MCHAT_ARCHIVE'),
'U_VIEW_FORUM' => $this->helper->route('dmzx_mchat_page_controller', array('page' => 'archive')),
),
));
return $this->helper->render('mchat_body.html', $this->user->lang('MCHAT_ARCHIVE_PAGE'));
}
/**
* Controller for mChat IP WHOIS
*
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
*/
public function page_whois()
{
if (!$this->auth->acl_get('u_mchat_ip'))
{
throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
}
if (!function_exists('user_ipwhois'))
{
include($this->root_path . 'includes/functions_user.' . $this->php_ext);
}
$this->template->assign_var('WHOIS', user_ipwhois($this->request->variable('ip', '')));
return $this->helper->render('viewonline_whois.html', $this->user->lang('WHO_IS_ONLINE'));
}
/**
* Controller for mChat Rules page
*
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
*/
public function page_rules()
{
if (empty($this->config['mchat_rules']) && empty($this->user->lang['MCHAT_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 = isset($this->user->lang['MCHAT_RULES']) ? $this->user->lang('MCHAT_RULES') : $this->config['mchat_rules'];
$mchat_rules = explode("\n", $mchat_rules);
$mchat_rules = array_map('utf8_htmlspecialchars', $mchat_rules);
$mchat_rules = implode('<br />', $mchat_rules);
$this->template->assign_var('MCHAT_RULES', $mchat_rules);
return $this->helper->render('mchat_rules.html', $this->user->lang('MCHAT_HELP'));
}
/**
*
*/
public function action_add()
{
if (!$this->auth->acl_get('u_mchat_use') || !check_form_key('mchat', -1))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
if ($this->functions_mchat->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(
'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);
/**
* Event render_helper_add
*
* @event dmzx.mchat.core.render_helper_add
* @since 0.1.2
*/
$this->dispatcher->dispatch('dmzx.mchat.core.render_helper_add');
return array('add' => true);
}
/**
*
*/
public function action_edit()
{
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))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$author = $this->functions_mchat->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');
}
$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']);
/**
* Event render_helper_edit
*
* @event dmzx.mchat.core.render_helper_edit
* @since 0.1.4
*/
$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);
$this->assign_global_template_data();
$this->assign_messages($rows);
return array('edit' => $this->render_template('mchat_messages.html'));
}
/**
*
*/
public function action_del()
{
$message_id = $this->request->variable('message_id', 0);
if (!$message_id || !check_form_key('mchat', -1))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$author = $this->functions_mchat->mchat_author_for_message($message_id);
if (!$author || !$this->auth_message('u_mchat_delete', $author['user_id'], $author['message_time']))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
/**
* Event render_helper_delete
*
* @event dmzx.mchat.core.render_helper_delete
* @since 0.1.4
*/
$this->dispatcher->dispatch('dmzx.mchat.core.render_helper_delete');
$this->functions_mchat->mchat_action('del', null, $message_id, $author['username']);
return array('del' => true);
}
/**
*
*/
public function action_clean()
{
if ($this->user->data['user_type'] != USER_FOUNDER || !check_form_key('mchat', -1))
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$this->functions_mchat->mchat_action('clean');
return array('clean' => true);
}
/**
*
*/
public function action_refresh()
{
if (!defined('PHPBB_USE_BOARD_URL_PATH'))
{
define('PHPBB_USE_BOARD_URL_PATH', true);
}
$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));
// Request new messages
$sql_where = 'm.message_id > ' . (int) $message_last_id;
// 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);
}
// Exclude post notifications
if (!$this->user->data['user_mchat_topics'])
{
$sql_where = '(' . $sql_where . ') AND m.forum_id = 0';
}
$rows = $this->functions_mchat->mchat_get_messages($sql_where);
$rows_refresh = array();
$rows_edit = array();
foreach ($rows as $row)
{
$message_id = $row['message_id'];
if ($message_id > $message_last_id)
{
$rows_refresh[] = $row;
}
else if (!isset($message_edits[$message_id]) || $message_edits[$message_id] < $row['edit_time'])
{
$rows_edit[] = $row;
}
}
// Assign new messages
$this->assign_global_template_data();
$this->assign_messages($rows_refresh);
$response = array('refresh' => $this->render_template('mchat_messages.html'));
// Assign edited messages
if (!empty($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');
}
}
// Request 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))
{
$response['del'] = $deleted_message_ids;
}
}
return $response;
}
/**
*
*/
public function action_whois()
{
$this->assign_whois();
return array('whois' => $this->render_template('mchat_whois.html'));
}
/**
*
*/
public function render_page_header_link()
{
$this->template->assign_vars(array(
'MCHAT_ALLOW_VIEW' => $this->auth->acl_get('u_mchat_view'),
'S_MCHAT_CUSTOM_PAGE' => $this->config['mchat_custom_page'],
'U_MCHAT' => $this->helper->route('dmzx_mchat_controller'),
));
}
/**
*
*/
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;
}
/**
* Method to render the page data
*
* @var page The page we are rendering for, one of index|custom|archive
* @return null|array|string If we are rendering for the index, null is returned. For modes that are only
* called via AJAX, an array is returned, otherwise the rendered content is returned.
*/
protected function render_page($page)
{
// Add lang file
$this->user->add_lang('posting');
// If the static message is defined in the language file use it, else the entry in the database is used
if (isset($this->user->lang['STATIC_MESSAGE']))
{
$this->config['mchat_static_message'] = $this->user->lang('STATIC_MESSAGE');
}
$this->template->assign_vars(array(
'MCHAT_FILE_NAME' => $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' => !empty($this->user->lang['MCHAT_RULES']) || !empty($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'),
'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_FOUNDER' => $this->user->data['user_type'] == USER_FOUNDER,
'MCHAT_STATIC_MESS' => !empty($this->config['mchat_static_message']) ? htmlspecialchars_decode($this->config['mchat_static_message']) : '',
'L_MCHAT_COPYRIGHT' => base64_decode('PGEgaHJlZj0iaHR0cDovL3JtY2dpcnI4My5vcmciPlJNY0dpcnI4MzwvYT4gJmNvcHk7IDxhIGhyZWY9Imh0dHA6Ly93d3cuZG16eC13ZWIubmV0IiB0aXRsZT0id3d3LmRtengtd2ViLm5ldCI+ZG16eDwvYT4='),
'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_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_PAUSE_ON_INPUT' => $this->config['mchat_pause_on_input'],
'MCHAT_REFRESH_YES' => sprintf($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']),
));
$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);
$this->assign_global_template_data();
$this->assign_messages($rows);
// 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();
$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));
}
// Render legend
if ($page != 'index' && $this->config['mchat_whois'])
{
$legend = $this->functions_mchat->mchat_legend();
$this->template->assign_var('LEGEND', implode(', ', $legend));
}
if ($this->auth->acl_get('u_mchat_use'))
{
add_form_key('mchat');
}
$this->is_mchat_rendered = true;
/**
* Event render_helper_aft
*
* @event dmzx.mchat.core.render_helper_aft
* @since 0.1.2
*/
$this->dispatcher->dispatch('dmzx.mchat.core.render_helper_aft');
}
/**
* Assigns all message rows to the template
*/
protected function assign_global_template_data()
{
$this->template->assign_vars(array(
'MCHAT_ALLOW_IP' => $this->auth->acl_get('u_mchat_ip'),
'MCHAT_ALLOW_PM' => $this->auth->acl_get('u_mchat_pm'),
'MCHAT_ALLOW_LIKE' => $this->auth->acl_get('u_mchat_like'),
'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_USER_TIMEOUT' => 1000 * $this->config['mchat_timeout'],
'S_MCHAT_AVATARS' => !empty($this->config['mchat_avatars']) && $this->user->optionget('viewavatars') && $this->user->data['user_mchat_avatars'],
'EXT_URL' => generate_board_url() . '/ext/dmzx/mchat/',
'STYLE_PATH' => generate_board_url() . '/styles/' . $this->user->style['style_path'],
));
}
/**
* Assigns all message rows to the template
*/
protected function assign_messages($rows)
{
if (empty($rows))
{
return;
}
// Reverse the array if messages appear at the bottom
if (!$this->config['mchat_message_top'])
{
$rows = array_reverse($rows);
}
$foes = $this->functions_mchat->mchat_foes();
$this->template->destroy_block_vars('mchatrow');
foreach ($rows as $i => $row)
{
// Auth checks
if ($row['forum_id'] != 0 && !$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['username'] = mb_ereg_replace("'", "&#146;", $row['username']);
$message = str_replace("'", '&rsquo;', $row['message']);
$user_avatar = !$row['user_avatar'] ? '' : phpbb_get_user_avatar(array(
'avatar' => $row['user_avatar'],
'avatar_type' => $row['user_avatar_type'],
'avatar_width' => $row['user_avatar_width'] > $row['user_avatar_height'] ? 40 : (40 / $row['user_avatar_height']) * $row['user_avatar_width'],
'avatar_height' => $row['user_avatar_height'] > $row['user_avatar_width'] ? 40 : (40 / $row['user_avatar_width']) * $row['user_avatar_height'],
));
$username_full = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST'));
// Remove root path if we render messages for the index page
if (strpos($this->user->data['session_page'], 'app.' . $this->php_ext) === false)
{
$username_full = str_replace('.' . $this->root_path, '', $username_full);
}
$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_avatar,
'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']) : '',
'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_MESSAGE_EDIT' => $message_edit,
'MCHAT_MESSAGE_ID' => $row['message_id'],
'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_USER_IP' => $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_MESSAGE_TIME' => $row['message_time'],
'MCHAT_EDIT_TIME' => $row['edit_time'],
));
}
}
/**
* Assigns BBCodes and smilies to the template
*/
protected function assign_bbcodes_smilies()
{
// Display custom 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');
// Let's remove the default bbcodes
$disallowed_bbcode_array = explode('|', strtoupper($this->config['mchat_bbcode_disallowed']));
foreach ($default_bbcodes as $default_bbcode)
{
if (!in_array($default_bbcode, $disallowed_bbcode_array))
{
$this->template->assign_vars(array(
'S_MCHAT_BBCODE_' . $default_bbcode => true,
));
}
}
if (!function_exists('display_custom_bbcodes'))
{
include($this->root_path . 'includes/functions_display.' . $this->php_ext);
}
display_custom_bbcodes();
}
// Smile row
if ($this->config['allow_smilies'] && $this->auth->acl_get('u_mchat_smilies'))
{
if (!function_exists('generate_smilies'))
{
include($this->root_path . 'includes/functions_posting.' . $this->php_ext);
}
generate_smilies('inline', 0);
}
}
/**
* Assigns whois and stats at the bottom of the index page
*/
protected function assign_whois()
{
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();
$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_ONLINE_EXPLAIN' => $mchat_stats['refresh_message'],
));
}
}
/**
* Checks whether an author has edit or delete permissions for a message
*/
protected function auth_message($permission, $author_id, $message_time)
{
if (!$this->auth->acl_get($permission))
{
return false;
}
if ($this->auth->acl_get('m_'))
{
return true;
}
$can_edit_delete = $this->config['mchat_edit_delete_limit'] == 0 || $message_time >= time() - $this->config['mchat_edit_delete_limit'];
return $can_edit_delete && $this->user->data['user_id'] == $author_id && $this->user->data['is_registered'];
}
/**
* Performs bound checks on the message and returns an array containing the message,
* BBCode options and additional data ready to be sent to the database
*/
protected function process_message($message, $merge_ary)
{
// Must have something other than bbcode in the message
$message_chars = trim(preg_replace('#\[/?[^\[\]]+\]#mi', '', $message));
if (!$message || !utf8_strlen($message_chars))
{
throw new \phpbb\exception\http_exception(501, 'MCHAT_NOACCESS');
}
// Must not exceed character limit, excluding whitespaces
$message_chars = preg_replace('#\s#m', '', $message);
if (utf8_strlen($message_chars) > $this->config['mchat_max_message_lngth'])
{
throw new \phpbb\exception\http_exception(413, 'MCHAT_MESS_LONG', array($this->config['mchat_max_message_lngth']));
}
// We override the $this->config['min_post_chars'] entry?
if ($this->config['mchat_override_min_post_chars'])
{
$old_cfg['min_post_chars'] = $this->config['min_post_chars'];
$this->config['min_post_chars'] = 0;
}
// We do the same for the max number of smilies?
if ($this->config['mchat_override_smilie_limit'])
{
$old_cfg['max_post_smilies'] = $this->config['max_post_smilies'];
$this->config['max_post_smilies'] = 0;
}
$mchat_bbcode = $this->config['allow_bbcode'] && $this->auth->acl_get('u_mchat_bbcode');
$mchat_urls = $this->config['allow_post_links'] && $this->auth->acl_get('u_mchat_urls');
$mchat_smilies = $this->config['allow_smilies'] && $this->auth->acl_get('u_mchat_smilies');
// Add function part code from http://wiki.phpbb.com/Parsing_text
$uid = $bitfield = $options = '';
generate_text_for_storage($message, $uid, $bitfield, $options, $mchat_bbcode, $mchat_urls, $mchat_smilies);
// Not allowed bbcodes
if (!$mchat_bbcode)
{
$message = preg_replace('#\[/?[^\[\]]+\]#Usi', '', $message);
}
// Disallowed bbcodes
if ($this->config['mchat_bbcode_disallowed'])
{
$bbcode_replace = array(
'#\[(' . $this->config['mchat_bbcode_disallowed'] . ')[^\[\]]+\]#Usi',
'#\[/(' . $this->config['mchat_bbcode_disallowed'] . ')[^\[\]]+\]#Usi',
);
$message = preg_replace($bbcode_replace, '', $message);
}
// Reset the config settings
if (isset($old_cfg['min_post_chars']))
{
$this->config['min_post_chars'] = $old_cfg['min_post_chars'];
}
if (isset($old_cfg['max_post_smilies']))
{
$this->config['max_post_smilies'] = $old_cfg['max_post_smilies'];
}
return array_merge($merge_ary, array(
'message' => str_replace("'", '&#39;', $message),
'bbcode_bitfield' => $bitfield,
'bbcode_uid' => $uid,
'bbcode_options' => $options,
));
}
/**
* Renders a template file and returns it
* @return string
*/
protected function render_template($template_file)
{
$this->template->set_filenames(array('body' => $template_file));
$content = $this->template->assign_display('body', '', true);
return trim(str_replace(array("\r", "\n"), '', $content));
}
}

View File

@@ -1,908 +0,0 @@
<?php
/**
*
* @package phpBB Extension - mChat
* @copyright (c) 2015 dmzx - http://www.dmzx-web.net
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace dmzx\mchat\core;
class render_helper
{
/** @var \dmzx\mchat\core\functions_mchat */
protected $functions_mchat;
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\controller\helper */
protected $helper;
/** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\log\log_interface */
protected $log;
/** @var \phpbb\user */
protected $user;
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\pagination */
protected $pagination;
/** @var \phpbb\request\request */
protected $request;
/** @var \phpbb\event\dispatcher_interface */
protected $dispatcher;
/** @var string */
protected $phpbb_root_path;
/** @var string */
protected $phpEx;
/** @var string */
protected $mchat_table;
/** @var boolean */
public $is_mchat_rendered = false;
/**
* Constructor
*
* @param \dmzx\mchat\core\functions_mchat $functions_mchat
* @param \phpbb\config\config $config
* @param \phpbb\controller\helper $helper
* @param \phpbb\template\template $template
* @param \phpbb\log\log_interface $log
* @param \phpbb\user $user
* @param \phpbb\auth\auth $auth
* @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\pagination $pagination
* @param \phpbb\request\request $request
* @param \phpbb\event\dispatcher_interface $dispatcher
* @param string $phpbb_root_path
* @param string $phpEx
* @param string $mchat_table
*/
public function __construct(\dmzx\mchat\core\functions_mchat $functions_mchat, \phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\log\log_interface $log, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db, \phpbb\pagination $pagination, \phpbb\request\request $request, \phpbb\event\dispatcher_interface $dispatcher, $phpbb_root_path, $phpEx, $mchat_table)
{
$this->functions_mchat = $functions_mchat;
$this->config = $config;
$this->helper = $helper;
$this->template = $template;
$this->log = $log;
$this->user = $user;
$this->auth = $auth;
$this->db = $db;
$this->pagination = $pagination;
$this->request = $request;
$this->dispatcher = $dispatcher;
$this->phpbb_root_path = $phpbb_root_path;
$this->phpEx = $phpEx;
$this->mchat_table = $mchat_table;
}
/**
* Method to render the page data
*
* @var bool Bool if the rendering is only for index
* @return null|array|string If we are rendering for the index, null is returned. For modes that are only
* called via AJAX, an array is returned, otherwise the rendered content is returned.
*/
public function render_data_for_page($on_index)
{
// If mChat is used on the index by a user without an avatar, a default avatar is used.
// However, T_THEME_PATH points to ./../styles/... because the controller at /mchat is called, but we need it to be ./styles...
// Setting this value to true solves this.
if (!defined('PHPBB_USE_BOARD_URL_PATH'))
{
define('PHPBB_USE_BOARD_URL_PATH', true);
}
$this->template->assign_vars(array(
'MCHAT_ENABLE' => $this->config['mchat_enable'],
'MCHAT_DISABLE' => !$this->config['mchat_enable'],
));
if (!$this->config['mchat_enable'])
{
if ($this->request->is_ajax())
{
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
else if (!$on_index)
{
return $this->helper->render('mchat_body.html', $this->user->lang('MCHAT_TITLE'));
}
return;
}
$mchat_view = $this->auth->acl_get('u_mchat_view');
if ($on_index && (!$this->config['mchat_on_index'] || !$mchat_view))
{
return;
}
if (!$mchat_view)
{
// Forbidden
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
// Add lang file
$this->user->add_lang('posting');
$config_mchat = $this->functions_mchat->mchat_cache();
// Access rights
$mchat_allow_bbcode = $this->config['allow_bbcode'] && $this->auth->acl_get('u_mchat_bbcode');
$mchat_smilies = $this->config['allow_smilies'] && $this->auth->acl_get('u_mchat_smilies');
$mchat_urls = $this->config['allow_post_links'] && $this->auth->acl_get('u_mchat_urls');
$mchat_ip = $this->auth->acl_get('u_mchat_ip');
$mchat_pm = $this->auth->acl_get('u_mchat_pm');
$mchat_use = $this->auth->acl_get('u_mchat_use');
$mchat_no_flood = $this->auth->acl_get('u_mchat_flood_ignore');
$mchat_read_archive = $this->auth->acl_get('u_mchat_archive');
$mchat_founder = $this->user->data['user_type'] == USER_FOUNDER;
$mchat_session_time = !empty($config_mchat['timeout']) ? $config_mchat['timeout'] : (!empty($this->config['load_online_time']) ? $this->config['load_online_time'] * 60 : $this->config['session_length']);
$mchat_rules = !empty($config_mchat['rules']) || isset($this->user->lang['MCHAT_RULES']);
$mchat_avatars = !empty($config_mchat['avatars']) && $this->user->optionget('viewavatars') && $this->user->data['user_mchat_avatars'];
$mchat_mode = $this->request->variable('mode', '');
$in_archive = $mchat_mode == 'archive';
// Return early for all regular HTTP requests that don't require message rendering. No AJAX here!
switch ($mchat_mode)
{
case 'clean':
if (!$this->user->data['is_registered'])
{
// Login box
login_box('', $this->user->lang('LOGIN'));
}
if (!$mchat_founder)
{
throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
}
$mchat_redirect = $this->request->variable('redirect', '');
$mchat_redirect = ($mchat_redirect == 'index' ? append_sid("{$this->phpbb_root_path}index.{$this->phpEx}") : $this->helper->route('dmzx_mchat_controller')) . '#mChat';
if (confirm_box(true))
{
// Prune is confirmed
$this->functions_mchat->mchat_truncate_messages();
meta_refresh(3, $mchat_redirect);
trigger_error($this->user->lang('MCHAT_CLEANED'). '<br /><br />' . sprintf($this->user->lang('RETURN_PAGE'), '<a href="' . $mchat_redirect . '">', '</a>'));
}
else
{
// Display confirm box
confirm_box(false, $this->user->lang('MCHAT_DELALLMESS'));
}
return;
case 'rules':
if (!$mchat_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 = isset($this->user->lang['MCHAT_RULES']) ? $this->user->lang('MCHAT_RULES') : $config_mchat['rules'];
$mchat_rules = explode("\n", $mchat_rules);
$mchat_rules = array_map('utf8_htmlspecialchars', $mchat_rules);
$mchat_rules = implode('<br />', $mchat_rules);
$this->template->assign_var('MCHAT_RULES', $mchat_rules);
return $this->helper->render('mchat_rules.html', $this->user->lang('MCHAT_HELP'));
case 'ip':
if (!$mchat_ip)
{
throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
}
if (!function_exists('user_ipwhois'))
{
include($this->phpbb_root_path . 'includes/functions_user.' . $this->phpEx);
}
$user_ip = $this->request->variable('ip', '');
$this->template->assign_var('WHOIS', user_ipwhois($user_ip));
return $this->helper->render('viewonline_whois.html', $this->user->lang('WHO_IS_ONLINE'));
}
$foes_array = $this->functions_mchat->mchat_foes();
// If the static message is defined in the language file use it, else the entry in the database is used
if (isset($this->user->lang['STATIC_MESSAGE']))
{
$config_mchat['static_message'] = $this->user->lang('STATIC_MESSAGE');
}
// If the static message is defined in the language file use it, else the entry in the database is used
if (isset($this->user->lang['MCHAT_RULES']))
{
$config_mchat['rules'] = $this->user->lang('MCHAT_RULES');
}
$this->template->assign_vars(array(
'MCHAT_FILE_NAME' => $this->helper->route('dmzx_mchat_controller'),
'MCHAT_REFRESH_JS' => 1000 * $config_mchat['refresh'],
'MCHAT_ARCHIVE_MODE' => $in_archive,
'MCHAT_INPUT_TYPE' => $this->user->data['user_mchat_input_area'],
'MCHAT_RULES' => $mchat_rules,
'MCHAT_ALLOW_VIEW' => $mchat_view,
'MCHAT_ALLOW_USE' => $mchat_use,
'MCHAT_ALLOW_SMILES' => $mchat_smilies,
'MCHAT_ALLOW_IP' => $mchat_ip,
'MCHAT_ALLOW_PM' => $mchat_pm,
'MCHAT_ALLOW_LIKE' => $mchat_use && $this->auth->acl_get('u_mchat_like'),
'MCHAT_ALLOW_QUOTE' => $mchat_use && $this->auth->acl_get('u_mchat_quote'),
'MCHAT_ALLOW_BBCODES' => $mchat_allow_bbcode,
'MCHAT_MESSAGE_TOP' => $this->config['mchat_message_top'],
'MCHAT_ARCHIVE_URL' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'archive')),
'MCHAT_CUSTOM_PAGE' => !$on_index,
'MCHAT_INDEX_HEIGHT' => $config_mchat['index_height'],
'MCHAT_CUSTOM_HEIGHT' => $config_mchat['custom_height'],
'MCHAT_READ_ARCHIVE_BUTTON' => $mchat_read_archive,
'MCHAT_FOUNDER' => $mchat_founder,
'MCHAT_CLEAN_URL' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'clean', 'redirect' => $on_index ? 'index' : 'mchat')),
'MCHAT_STATIC_MESS' => !empty($config_mchat['static_message']) ? htmlspecialchars_decode($config_mchat['static_message']) : '',
'L_MCHAT_COPYRIGHT' => base64_decode('PGEgaHJlZj0iaHR0cDovL3JtY2dpcnI4My5vcmciPlJNY0dpcnI4MzwvYT4gJmNvcHk7IDxhIGhyZWY9Imh0dHA6Ly93d3cuZG16eC13ZWIubmV0IiB0aXRsZT0id3d3LmRtengtd2ViLm5ldCI+ZG16eDwvYT4='),
'MCHAT_MESSAGE_LNGTH' => $config_mchat['max_message_lngth'],
//'MCHAT_MESSAGE_LNGTH_EXPLAIN' => $config_mchat['max_message_lngth']) ? sprintf($this->user->lang('MCHAT_MESSAGE_LNGTH_EXPLAIN'), $config_mchat['max_message_lngth']) : '', TODO not used
'MCHAT_MESS_LONG' => sprintf($this->user->lang('MCHAT_MESS_LONG'), $config_mchat['max_message_lngth']),
'MCHAT_USER_TIMEOUT' => 1000 * $config_mchat['timeout'],
'MCHAT_WHOIS_REFRESH' => $config_mchat['whois'] ? 1000 * $config_mchat['whois_refresh'] : 0,
'MCHAT_PAUSE_ON_INPUT' => $config_mchat['pause_on_input'],
'MCHAT_REFRESH_YES' => sprintf($this->user->lang('MCHAT_REFRESH_YES'), $config_mchat['refresh']),
'MCHAT_WHOIS_REFRESH_EXPLAIN' => sprintf($this->user->lang('WHO_IS_REFRESH_EXPLAIN'), $config_mchat['whois_refresh']),
'S_MCHAT_AVATARS' => $mchat_avatars,
'S_MCHAT_LOCATION' => $config_mchat['location'],
'S_MCHAT_SOUND_YES' => $this->user->data['user_mchat_sound'],
'U_MORE_SMILIES' => append_sid("{$this->phpbb_root_path}posting.{$this->phpEx}", 'mode=smilies'),
'U_MCHAT_RULES' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'rules')),
'S_MCHAT_ON_INDEX' => $this->config['mchat_on_index'] && !empty($this->user->data['user_mchat_index']),
'EXT_URL' => generate_board_url() . '/ext/dmzx/mchat/',
'STYLE_PATH' => generate_board_url() . '/styles/' . $this->user->style['style_path'],
));
if (!$on_index)
{
$this->template->assign_block_vars('navlinks', array(
'FORUM_NAME' => $this->user->lang('MCHAT_TITLE'),
'U_VIEW_FORUM' => $this->helper->route('dmzx_mchat_controller'),
));
}
// Request mode
switch ($mchat_mode)
{
case 'archive':
if (!$mchat_read_archive)
{
// Redirect to correct page
$mchat_redirect = append_sid("{$this->phpbb_root_path}index.{$this->phpEx}");
// Redirect to previous page
meta_refresh(3, $mchat_redirect);
trigger_error($this->user->lang('MCHAT_NOACCESS_ARCHIVE'). '<br /><br />' . sprintf($this->user->lang('RETURN_PAGE'), '<a href="' . $mchat_redirect . '">', '</a>'));
}
// Prune the chats
if ($config_mchat['prune_enable'] && $config_mchat['prune_num'] > 0)
{
$this->functions_mchat->mchat_prune($config_mchat['prune_num']);
}
break;
case 'refresh':
// Request new messages
$mchat_message_last_id = $this->request->variable('message_last_id', 0);
$sql_where = 'm.message_id > ' . (int) $mchat_message_last_id . ($this->user->data['user_mchat_topics'] ? '' : ' AND m.forum_id = 0');
$limit = (int) $config_mchat['message_limit'];
$rows = $this->functions_mchat->mchat_messages($sql_where, $limit);
// Reverse the array if messages appear at the bottom
if (!$this->config['mchat_message_top'])
{
$rows = array_reverse($rows);
}
foreach ($rows as $i => $row)
{
// Auth checks
if ($row['forum_id'] != 0 && !$this->auth->acl_get('f_read', $row['forum_id']))
{
continue;
}
if ($this->user->data['user_id'] == ANONYMOUS && $this->user->data['user_id'] == $row['user_id'])
{
$chat_auths = $this->user->data['session_ip'] == $row['user_ip'];
}
else
{
$chat_auths = $this->user->data['user_id'] == $row['user_id'];
}
$mchat_ban = $this->auth->acl_get('a_authusers') && $this->user->data['user_id'] != $row['user_id'];
$mchat_edit = $this->auth->acl_get('u_mchat_edit') && ($this->auth->acl_get('m_') || $chat_auths);
$mchat_del = $this->auth->acl_get('u_mchat_delete') && ($this->auth->acl_get('m_') || $chat_auths);
$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_array))
{
$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['username'] = mb_ereg_replace("'", "&#146;", $row['username']);
$this->template->assign_block_vars('mchatrow', array(
'S_ROW_COUNT' => $i,
'MCHAT_ALLOW_BAN' => $mchat_ban,
'MCHAT_ALLOW_EDIT' => $mchat_edit,
'MCHAT_ALLOW_DEL' => $mchat_del,
'MCHAT_USER_AVATAR' => $row['user_avatar'] ? $this->functions_mchat->mchat_avatar($row) : '',
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", '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_')) ? append_sid("{$this->phpbb_root_path}ucp.{$this->phpEx}", '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' => 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_USER_IP' => $row['user_ip'],
'MCHAT_U_IP' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'ip', 'ip' => $row['user_ip'])),
'MCHAT_U_BAN' => append_sid("{$this->phpbb_root_path}adm/index.{$this->phpEx}" ,'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'], $config_mchat['date']),
));
}
return array(
'refresh' => $this->render('mchat_messages.html'),
);
case 'whois':
if (!$config_mchat['whois'])
{
throw new \phpbb\exception\http_exception(403, 'NO_AUTH_OPERATION');
}
$this->assign_whois();
return array(
'whois' => $this->render('mchat_whois.html'),
);
case 'add':
if (!$mchat_use || !check_form_key('mchat_posting', -1))
{
// Forbidden (for jQ AJAX request)
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$message = utf8_ucfirst($this->request->variable('message', '', true));
// Must have something other than bbcode in the message
$message_chars = trim(preg_replace('#\[/?[^\[\]]+\]#mi', '', $message));
if (!$message || !utf8_strlen($message_chars))
{
// Not Implemented
throw new \phpbb\exception\http_exception(501, 'MCHAT_ERROR_NOT_IMPLEMENTED');
}
// Flood control
if (!$mchat_no_flood && $config_mchat['flood_time'])
{
$mchat_flood_current_time = time();
$sql = 'SELECT message_time
FROM ' . $this->mchat_table . '
WHERE user_id = ' . (int) $this->user->data['user_id'] . '
ORDER BY message_time DESC';
$result = $this->db->sql_query_limit($sql, 1);
$message_time = (int) $this->db->sql_fetchfield('message_time');
$this->db->sql_freeresult($result);
if ($message_time && time() - $message_time < $config_mchat['flood_time'])
{
// Locked
throw new \phpbb\exception\http_exception(400, 'MCHAT_BAD_REQUEST');
}
}
// Insert user into the mChat sessions table
$this->functions_mchat->mchat_sessions($mchat_session_time);
// We override the $this->config['min_post_chars'] entry?
if ($config_mchat['override_min_post_chars'])
{
$old_cfg['min_post_chars'] = $this->config['min_post_chars'];
$this->config['min_post_chars'] = 0;
}
// We do the same for the max number of smilies?
if ($config_mchat['override_smilie_limit'])
{
$old_cfg['max_post_smilies'] = $this->config['max_post_smilies'];
$this->config['max_post_smilies'] = 0;
}
// Add function part code from http://wiki.phpbb.com/Parsing_text
$uid = $bitfield = $options = '';
generate_text_for_storage($message, $uid, $bitfield, $options, $mchat_allow_bbcode, $mchat_urls, $mchat_smilies);
// Not allowed bbcodes
if (!$mchat_allow_bbcode)
{
$bbcode_remove = '#\[/?[^\[\]]+\]#Usi';
$message = preg_replace($bbcode_remove, '', $message);
}
// Disallowed bbcodes
if ($config_mchat['bbcode_disallowed'])
{
$bbcode_replace = array(
'#\[(' . $config_mchat['bbcode_disallowed'] . ')[^\[\]]+\]#Usi',
'#\[/(' . $config_mchat['bbcode_disallowed'] . ')[^\[\]]+\]#Usi',
);
$message = preg_replace($bbcode_replace, '', $message);
}
/**
* Event render_helper_add
*
* @event dmzx.mchat.core.render_helper_add
* @since 0.1.2
*/
$this->dispatcher->trigger_event('dmzx.mchat.core.render_helper_add');
$sql_ary = array(
'forum_id' => 0,
'post_id' => 0,
'user_id' => $this->user->data['user_id'],
'user_ip' => $this->user->data['session_ip'],
'message' => str_replace('\'', '&#39;', $message),
'bbcode_bitfield' => $bitfield,
'bbcode_uid' => $uid,
'bbcode_options' => $options,
'message_time' => time(),
);
$sql = 'INSERT INTO ' . $this->mchat_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
// Reset the config settings
if (isset($old_cfg['min_post_chars']))
{
$this->config['min_post_chars'] = $old_cfg['min_post_chars'];
unset($old_cfg['min_post_chars']);
}
if (isset($old_cfg['max_post_smilies']))
{
$this->config['max_post_smilies'] = $old_cfg['max_post_smilies'];
unset($old_cfg['max_post_smilies']);
}
return array(
'add' => true,
);
case 'edit':
$message_id = $this->request->variable('message_id', 0);
if (!$message_id)
{
// Forbidden
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
// Check for the correct user
if ($this->auth->acl_get('m_'))
{
// Always allow users with 'm_' auth to edit and delete
$user_id = $this->user->data['user_id'];
}
else
{
$sql = 'SELECT user_id
FROM ' . $this->mchat_table . '
WHERE message_id = ' . (int) $message_id;
$result = $this->db->sql_query($sql);
$user_id = (int) $this->db->sql_fetchfield('user_id');
$this->db->sql_freeresult($result);
}
// Edit and delete auths
$mchat_ban = $this->auth->acl_get('a_authusers') && $this->user->data['user_id'] != $user_id;
$mchat_edit = $this->auth->acl_get('u_mchat_edit') && $this->user->data['user_id'] == $user_id;
$mchat_del = $this->auth->acl_get('u_mchat_delete') && $this->user->data['user_id'] == $user_id;
if (!$mchat_edit)
{
// Forbidden
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
$message = $this->request->variable('message', '', true);
// Must have something other than bbcode in the message
$message_chars = trim(preg_replace('#\[/?[^\[\]]+\]#mi', '', $message));
if (!$message || !utf8_strlen($message_chars))
{
// Not Implemented (for jQ AJAX request)
throw new \phpbb\exception\http_exception(501, 'MCHAT_ERROR_NOT_IMPLEMENTED');
}
// Message limit
$message = $config_mchat['max_message_lngth'] && utf8_strlen($message) >= $config_mchat['max_message_lngth'] + 3 ? utf8_substr($message, 0, $config_mchat['max_message_lngth']) . '...' : $message;
// We override the $this->config['min_post_chars'] entry?
if ($config_mchat['override_min_post_chars'])
{
$old_cfg['min_post_chars'] = $this->config['min_post_chars'];
$this->config['min_post_chars'] = 0;
}
// We do the same for the max number of smilies?
if ($config_mchat['override_smilie_limit'])
{
$old_cfg['max_post_smilies'] = $this->config['max_post_smilies'];
$this->config['max_post_smilies'] = 0;
}
// Edit function part code from http://wiki.phpbb.com/Parsing_text
$uid = $bitfield = $options = '';
generate_text_for_storage($message, $uid, $bitfield, $options, $mchat_allow_bbcode, $mchat_urls, $mchat_smilies);
// Not allowed bbcodes
if (!$mchat_allow_bbcode)
{
$bbcode_remove = '#\[/?[^\[\]]+\]#Usi';
$message = preg_replace($bbcode_remove, '', $message);
}
// Disallowed bbcodes
if ($config_mchat['bbcode_disallowed'])
{
$bbcode_replace = array(
'#\[(' . $config_mchat['bbcode_disallowed'] . ')[^\[\]]+\]#Usi',
'#\[/(' . $config_mchat['bbcode_disallowed'] . ')[^\[\]]+\]#Usi',
);
$message = preg_replace($bbcode_replace, '', $message);
}
$sql_ary = array(
'message' => str_replace('\'', '&#39;', $message),
'bbcode_bitfield' => $bitfield,
'bbcode_uid' => $uid,
'bbcode_options' => $options,
);
$sql = 'UPDATE ' . $this->mchat_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
WHERE message_id = ' . (int) $message_id;
$this->db->sql_query($sql);
// Message edited...now read it
$sql_where = 'm.message_id = ' . (int) $message_id;
$rows = $this->functions_mchat->mchat_messages($sql_where, 1);
$row = $rows[0];
$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);
$this->template->assign_block_vars('mchatrow', array(
'S_ROW_COUNT' => 0,
'MCHAT_ALLOW_BAN' => $mchat_ban,
'MCHAT_ALLOW_EDIT' => $mchat_edit,
'MCHAT_ALLOW_DEL' => $mchat_del,
'MCHAT_MESSAGE_EDIT' => $message_edit,
'MCHAT_USER_AVATAR' => $row['user_avatar'] ? $this->functions_mchat->mchat_avatar($row) : '',
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", '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_')) ? append_sid("{$this->phpbb_root_path}ucp.{$this->phpEx}", 'i=pm&amp;mode=compose&amp;u=' . $row['user_id']) : '',
'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' => 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_USER_IP' => $row['user_ip'],
'MCHAT_U_IP' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'ip', 'ip' => $row['user_ip'])),
'MCHAT_U_BAN' => append_sid("{$this->phpbb_root_path}adm/index.{$this->phpEx}" ,'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'], $config_mchat['date']),
));
// Reset the config settings
if (isset($old_cfg['min_post_chars']))
{
$this->config['min_post_chars'] = $old_cfg['min_post_chars'];
unset($old_cfg['min_post_chars']);
}
if (isset($old_cfg['max_post_smilies']))
{
$this->config['max_post_smilies'] = $old_cfg['max_post_smilies'];
unset($old_cfg['max_post_smilies']);
}
// Add a log
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EDITED_MCHAT', false, array($row['username']));
$this->functions_mchat->mchat_sessions($mchat_session_time);
/**
* Event render_helper_edit
*
* @event dmzx.mchat.core.render_helper_edit
* @since 0.1.4
*/
$this->dispatcher->trigger_event('dmzx.mchat.core.render_helper_edit');
return array(
'edit' => $this->render('mchat_messages.html'),
);
case 'del':
$message_id = $this->request->variable('message_id', 0);
if (!$message_id)
{
// Forbidden
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
// Check for the correct user
$sql = 'SELECT u.user_id, u.username
FROM ' . $this->mchat_table . ' m
LEFT JOIN ' . USERS_TABLE . ' u ON m.user_id = u.user_id
WHERE m.message_id = ' . (int) $message_id;
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
// Delete auths
$mchat_del = $this->auth->acl_get('u_mchat_delete') && ($this->auth->acl_get('m_') || $this->user->data['user_id'] == $row['user_id']);
if (!$mchat_del)
{
// Forbidden
throw new \phpbb\exception\http_exception(403, 'MCHAT_NOACCESS');
}
/**
* Event render_helper_delete
*
* @event dmzx.mchat.core.render_helper_delete
* @since 0.1.4
*/
$this->dispatcher->trigger_event('dmzx.mchat.core.render_helper_delete');
// Run delete
$sql = 'DELETE FROM ' . $this->mchat_table . '
WHERE message_id = ' . (int) $message_id;
$this->db->sql_query($sql);
// Add a log
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DELETED_MCHAT', false, array($row['username']));
$this->functions_mchat->mchat_sessions($mchat_session_time);
return array(
'del' => true,
);
}
// If not include in index.php set mchat.php page true
if (!$on_index)
{
if (!$in_archive)
{
// If custom page false mchat.php page redirect to index...
if (!$config_mchat['custom_page'])
{
$mchat_redirect = append_sid("{$this->phpbb_root_path}index.{$this->phpEx}");
meta_refresh(3, $mchat_redirect);
trigger_error($this->user->lang('MCHAT_NO_CUSTOM_PAGE'). '<br /><br />' . sprintf($this->user->lang('RETURN_PAGE'), '<a href="' . $mchat_redirect . '">', '</a>'));
}
$this->functions_mchat->mchat_sessions($mchat_session_time);
}
if ($config_mchat['whois'])
{
$legend = $this->functions_mchat->mchat_legend();
$this->template->assign_var('LEGEND', implode(', ', $legend));
}
}
$sql_where = $this->user->data['user_mchat_topics'] ? '' : 'm.forum_id = 0';
$limit = $in_archive ? $config_mchat['archive_limit'] : $config_mchat[$on_index ? 'message_num' : 'message_limit'];
$start = $in_archive ? $this->request->variable('start', 0) : 0;
$rows = $this->functions_mchat->mchat_messages($sql_where, $limit, $start);
// Reverse the array if messages appear at the bottom
if (!$this->config['mchat_message_top'] && !$in_archive)
{
$rows = array_reverse($rows, true);
}
foreach ($rows as $i => $row)
{
// Auth checks
if ($row['forum_id'] && !$this->auth->acl_get('f_read', $row['forum_id']))
{
continue;
}
if ($this->user->data['user_id'] == ANONYMOUS && $this->user->data['user_id'] == $row['user_id'])
{
$chat_auths = $this->user->data['session_ip'] == $row['user_ip'];
}
else
{
$chat_auths = $this->user->data['user_id'] == $row['user_id'];
}
$mchat_ban = $this->auth->acl_get('a_authusers');
$mchat_edit = $this->auth->acl_get('u_mchat_edit') && ($this->auth->acl_get('m_') || $chat_auths);
$mchat_del = $this->auth->acl_get('u_mchat_delete') && ($this->auth->acl_get('m_') || $chat_auths);
$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_array))
{
$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['username'] = mb_ereg_replace("'", "&#146;", $row['username']);
$message = str_replace('\'', '&rsquo;', $row['message']);
$this->template->assign_block_vars('mchatrow', array(
'S_ROW_COUNT' => $i,
'MCHAT_ALLOW_BAN' => $mchat_ban,
'MCHAT_ALLOW_EDIT' => $mchat_edit,
'MCHAT_ALLOW_DEL' => $mchat_del,
'MCHAT_USER_AVATAR' => $row['user_avatar'] ? $this->functions_mchat->mchat_avatar($row) : '',
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? append_sid("{$this->phpbb_root_path}memberlist.{$this->phpEx}", '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_')) ? append_sid("{$this->phpbb_root_path}ucp.{$this->phpEx}", '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' => 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_USER_IP' => $row['user_ip'],
'MCHAT_U_IP' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'ip', 'ip' => $row['user_ip'])),
'MCHAT_U_BAN' => append_sid("{$this->phpbb_root_path}adm/index.{$this->phpEx}" ,'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($message, $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options'])),
'MCHAT_TIME' => $this->user->format_date($row['message_time'], $config_mchat['date']),
));
}
if ($in_archive)
{
// Run query again to get the total number of message for pagination
$sql = 'SELECT COUNT(message_id) AS mess_id
FROM ' . $this->mchat_table;
$result = $this->db->sql_query($sql);
$mchat_total_message = (int) $this->db->sql_fetchfield('mess_id');
$this->db->sql_freeresult($result);
$pagination_url = $this->helper->route('dmzx_mchat_controller', array('mode' => 'archive'));
$this->pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $mchat_total_message, $limit, $start);
$this->template->assign_var('MCHAT_TOTAL_MESSAGES', sprintf($this->user->lang('MCHAT_TOTALMESSAGES'), $mchat_total_message));
// Add to navlinks
$this->template->assign_block_vars('navlinks', array(
'FORUM_NAME' => $this->user->lang('MCHAT_ARCHIVE'),
'U_VIEW_FORUM' => $this->helper->route('dmzx_mchat_controller', array('mode' => 'archive')),
));
}
else
{
$this->assign_whois();
// Display custom bbcodes
if ($mchat_allow_bbcode)
{
if (!function_exists('display_custom_bbcodes'))
{
include($this->phpbb_root_path . 'includes/functions_display.' . $this->phpEx);
}
$this->functions_mchat->display_mchat_bbcodes();
}
// Smile row
if ($mchat_smilies)
{
if (!function_exists('generate_smilies'))
{
include($this->phpbb_root_path . 'includes/functions_posting.' . $this->phpEx);
}
generate_smilies('inline', 0);
}
add_form_key('mchat_posting');
}
/**
* Event render_helper_aft
*
* @event dmzx.mchat.core.render_helper_aft
* @since 0.1.2
*/
$this->dispatcher->trigger_event('dmzx.mchat.core.render_helper_aft');
// If we're on the index, we must not render anything
// here, only for the custom page and the archive
if (!$on_index)
{
return $this->helper->render('mchat_body.html', $this->user->lang($in_archive ? 'MCHAT_ARCHIVE_PAGE' : 'MCHAT_TITLE'));
}
}
/**
* Renders the statistics for whois and at the bottom of the index page
*/
public function assign_whois()
{
if ($this->config['mchat_enable'] && $this->auth->acl_get('u_mchat_view') && !$this->is_mchat_rendered)
{
$this->is_mchat_rendered = true;
$config_mchat = $this->functions_mchat->mchat_cache();
$mchat_session_time = !empty($config_mchat['timeout']) ? $config_mchat['timeout'] : (!empty($this->config['load_online_time']) ? $this->config['load_online_time'] * 60 : $this->config['session_length']);
$mchat_stats = $this->functions_mchat->mchat_users($mchat_session_time);
$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_ONLINE_EXPLAIN' => $mchat_stats['refresh_message'],
));
}
}
/**
* Renders a template file and returns it
* @return string
*/
protected function render($template_file)
{
$this->template->set_filenames(array('body' => $template_file));
$content = $this->template->assign_display('body', '', true);
return trim(str_replace(array("\r", "\n"), '', $content));
}
}