Version 2.1.0

This commit is contained in:
dmzx
2018-08-02 11:34:39 +02:00
parent cd0f139598
commit 006a4e404e
140 changed files with 2695 additions and 2699 deletions

View File

@@ -15,17 +15,28 @@ use phpbb\auth\auth;
use phpbb\cache\driver\driver_interface as cache_interface;
use phpbb\db\driver\driver_interface as db_interface;
use phpbb\event\dispatcher_interface;
use phpbb\group\helper;
use phpbb\language\language;
use phpbb\log\log_interface;
use phpbb\user;
class functions
{
/** @var settings */
protected $settings;
protected $mchat_settings;
/** @var notifications */
protected $mchat_notifications;
/** @var log */
protected $mchat_log;
/** @var user */
protected $user;
/** @var language */
protected $lang;
/** @var auth */
protected $auth;
@@ -41,85 +52,53 @@ class functions
/** @var dispatcher_interface */
protected $dispatcher;
/** @var string */
protected $root_path;
/** @var string */
protected $php_ext;
/** @var string */
protected $mchat_table;
/** @var string */
protected $mchat_log_table;
/** @var string */
protected $mchat_sessions_table;
/** @var helper */
protected $group_helper;
/** @var array */
protected $active_users;
/** @var array */
public $log_types = array(
1 => 'edit',
2 => 'del',
);
/**
* Value of the phpbb_mchat.post_id field for login notification
* messages if the user session is visible at the time of login
* Constructor
*
* @param settings $mchat_settings
* @param notifications $mchat_notifications
* @param log $mchat_log
* @param user $user
* @param language $lang
* @param auth $auth
* @param log_interface $log
* @param db_interface $db
* @param cache_interface $cache
* @param dispatcher_interface $dispatcher
* @param helper $group_helper
*/
const LOGIN_VISIBLE = 1;
/**
* Value of the phpbb_mchat.post_id field for login notification
* messages if the user session is hidden at the time of login
*/
const LOGIN_HIDDEN = 2;
/**
* Constructor
*
* @param settings $settings
* @param user $user
* @param auth $auth
* @param log_interface $log
* @param db_interface $db
* @param cache_interface $cache
* @param dispatcher_interface $dispatcher
* @param string $root_path
* @param string $php_ext
* @param string $mchat_table
* @param string $mchat_log_table
* @param string $mchat_sessions_table
*/
function __construct(
settings $settings,
settings $mchat_settings,
notifications $mchat_notifications,
log $mchat_log,
user $user,
language $lang,
auth $auth,
log_interface $log,
db_interface $db,
cache_interface $cache,
dispatcher_interface $dispatcher,
$root_path,
$php_ext,
$mchat_table,
$mchat_log_table,
$mchat_sessions_table
helper $group_helper
)
{
$this->settings = $settings;
$this->mchat_settings = $mchat_settings;
$this->mchat_notifications = $mchat_notifications;
$this->mchat_log = $mchat_log;
$this->user = $user;
$this->lang = $lang;
$this->auth = $auth;
$this->log = $log;
$this->db = $db;
$this->cache = $cache;
$this->dispatcher = $dispatcher;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->mchat_table = $mchat_table;
$this->mchat_log_table = $mchat_log_table;
$this->mchat_sessions_table = $mchat_sessions_table;
$this->group_helper = $group_helper;
}
/**
@@ -130,29 +109,29 @@ class functions
*/
protected function mchat_format_seconds($time)
{
$times = array();
$times = [];
$hours = floor($time / 3600);
if ($hours)
{
$time -= $hours * 3600;
$times[] = $this->user->lang('MCHAT_HOURS', $hours);
$times[] = $this->lang->lang('MCHAT_HOURS', $hours);
}
$minutes = floor($time / 60);
if ($minutes)
{
$time -= $minutes * 60;
$times[] = $this->user->lang('MCHAT_MINUTES', $minutes);
$times[] = $this->lang->lang('MCHAT_MINUTES', $minutes);
}
$seconds = ceil($time);
if ($seconds)
{
$times[] = $this->user->lang('MCHAT_SECONDS', $seconds);
$times[] = $this->lang->lang('MCHAT_SECONDS', $seconds);
}
return $this->user->lang('MCHAT_ONLINE_EXPLAIN', implode(' ', $times));
return $this->lang->lang('MCHAT_ONLINE_EXPLAIN', implode(' ', $times));
}
/**
@@ -162,19 +141,19 @@ class functions
*/
protected function mchat_session_time()
{
$mchat_timeout = $this->settings->cfg('mchat_timeout');
$mchat_timeout = $this->mchat_settings->cfg('mchat_timeout');
if ($mchat_timeout)
{
return $mchat_timeout;
}
$load_online_time = $this->settings->cfg('load_online_time');
$load_online_time = $this->mchat_settings->cfg('load_online_time');
if ($load_online_time)
{
return $load_online_time * 60;
}
return $this->settings->cfg('session_length');
return $this->mchat_settings->cfg('session_length');
}
/**
@@ -192,24 +171,22 @@ class functions
$check_time = time() - $this->mchat_session_time();
$sql_array = array(
$sql_array = [
'SELECT' => 'u.user_id, u.username, u.user_colour, s.session_viewonline',
'FROM' => array(
$this->mchat_sessions_table => 'ms'
),
'LEFT_JOIN' => array(
array(
'FROM' => array(SESSIONS_TABLE => 's'),
'FROM' => [$this->mchat_settings->get_table_mchat_sessions() => 'ms'],
'LEFT_JOIN' => [
[
'FROM' => [SESSIONS_TABLE => 's'],
'ON' => 'ms.user_id = s.session_user_id',
),
array(
'FROM' => array(USERS_TABLE => 'u'),
],
[
'FROM' => [USERS_TABLE => 'u'],
'ON' => 'ms.user_id = u.user_id',
),
),
],
],
'WHERE' => 'u.user_id <> ' . ANONYMOUS . ' AND s.session_viewonline IS NOT NULL AND ms.user_lastupdate > ' . (int) $check_time,
'ORDER_BY' => 'u.username ASC',
);
];
/**
* Event to modify the SQL query that fetches active mChat users
@@ -218,9 +195,9 @@ class functions
* @var array sql_array Array with SQL query data to fetch the current active sessions
* @since 2.0.0-RC6
*/
$vars = array(
$vars = [
'sql_array',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.active_users_sql_before', compact($vars)));
$sql = $this->db->sql_build_query('SELECT', $sql_array);
@@ -228,7 +205,7 @@ class functions
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$mchat_users = array();
$mchat_users = [];
$can_view_hidden = $this->auth->acl_get('u_viewonline');
foreach ($rows as $row)
@@ -243,15 +220,15 @@ class functions
$row['username'] = '<em>' . $row['username'] . '</em>';
}
$mchat_users[$row['user_id']] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST'));
$mchat_users[$row['user_id']] = get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $this->lang->lang('GUEST'));
}
$active_users = array(
'online_userlist' => implode($this->user->lang('COMMA_SEPARATOR'), $mchat_users),
'users_count_title' => $this->user->lang('MCHAT_TITLE_COUNT', count($mchat_users)),
'users_total' => $this->user->lang('MCHAT_ONLINE_USERS_TOTAL', count($mchat_users)),
$active_users = [
'online_userlist' => implode($this->lang->lang('COMMA_SEPARATOR'), $mchat_users),
'users_count_title' => $this->lang->lang('MCHAT_TITLE_COUNT', count($mchat_users)),
'users_total' => $this->lang->lang('MCHAT_ONLINE_USERS_TOTAL', count($mchat_users)),
'refresh_message' => $this->mchat_format_seconds($this->mchat_session_time()),
);
];
/**
* Event to modify collected data about active mChat users
@@ -261,10 +238,10 @@ class functions
* @var array active_users Array containing info about currently active mChat users
* @since 2.0.0-RC6
*/
$vars = array(
$vars = [
'mchat_users',
'active_users',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.active_users_after', compact($vars)));
$this->active_users = $active_users;
@@ -284,7 +261,7 @@ class functions
return false;
}
$sql = 'UPDATE ' . $this->mchat_sessions_table . '
$sql = 'UPDATE ' . $this->mchat_settings->get_table_mchat_sessions() . '
SET user_lastupdate = ' . time() . '
WHERE user_id = ' . (int) $this->user->data['user_id'];
$this->db->sql_query($sql);
@@ -293,11 +270,11 @@ class functions
if ($is_new_session)
{
$sql = 'INSERT INTO ' . $this->mchat_sessions_table . ' ' . $this->db->sql_build_array('INSERT', array(
$sql = 'INSERT INTO ' . $this->mchat_settings->get_table_mchat_sessions() . ' ' . $this->db->sql_build_array('INSERT', [
'user_id' => (int) $this->user->data['user_id'],
'user_ip' => $this->user->ip,
'user_lastupdate' => time(),
));
]);
$this->db->sql_query($sql);
}
@@ -311,7 +288,7 @@ class functions
{
$check_time = time() - $this->mchat_session_time();
$sql = 'DELETE FROM ' . $this->mchat_sessions_table . '
$sql = 'DELETE FROM ' . $this->mchat_settings->get_table_mchat_sessions() . '
WHERE user_lastupdate <= ' . (int) $check_time;
$this->db->sql_query($sql);
}
@@ -322,32 +299,32 @@ class functions
* @param int|array $user_ids
* @return array
*/
public function mchat_prune($user_ids = array())
public function mchat_prune($user_ids = [])
{
$prune_num = (int) $this->settings->cfg('mchat_prune_num');
$prune_mode = (int) $this->settings->cfg('mchat_prune_mode');
$prune_num = (int) $this->mchat_settings->cfg('mchat_prune_num');
$prune_mode = (int) $this->mchat_settings->cfg('mchat_prune_mode');
if (empty($this->settings->prune_modes[$prune_mode]))
if (empty($this->mchat_settings->prune_modes[$prune_mode]))
{
return array();
return [];
}
$sql_array = array(
$sql_array = [
'SELECT' => 'message_id',
'FROM' => array($this->mchat_table => 'm'),
);
'FROM' => [$this->mchat_settings->get_table_mchat() => 'm'],
];
if ($user_ids)
{
if (!is_array($user_ids))
{
$user_ids = array($user_ids);
$user_ids = [$user_ids];
}
$sql_array['WHERE'] = $this->db->sql_in_set('m.user_id', $user_ids);
$offset = 0;
}
else if ($this->settings->prune_modes[$prune_mode] === 'messages')
else if ($this->mchat_settings->prune_modes[$prune_mode] === 'messages')
{
// Skip fixed number of messages, delete all others
$sql_array['ORDER_BY'] = 'm.message_id DESC';
@@ -368,10 +345,10 @@ class functions
* @var array sql_array SQL query data
* @since 2.0.2
*/
$vars = array(
$vars = [
'user_ids',
'sql_array',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.prune_sql_before', compact($vars)));
$sql = $this->db->sql_build_query('SELECT', $sql_array);
@@ -379,7 +356,7 @@ class functions
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$prune_ids = array();
$prune_ids = [];
foreach ($rows as $row)
{
@@ -395,22 +372,22 @@ class functions
* @since 2.0.0-RC6
* @changed 2.0.1 Added user_ids
*/
$vars = array(
$vars = [
'prune_ids',
'user_ids',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.prune_before', compact($vars)));
if ($prune_ids)
{
$this->db->sql_query('DELETE FROM ' . $this->mchat_table . ' WHERE ' . $this->db->sql_in_set('message_id', $prune_ids));
$this->db->sql_query('DELETE FROM ' . $this->mchat_log_table . ' WHERE ' . $this->db->sql_in_set('message_id', $prune_ids));
$this->cache->destroy('sql', $this->mchat_log_table);
$this->db->sql_query('DELETE FROM ' . $this->mchat_settings->get_table_mchat() . ' WHERE ' . $this->db->sql_in_set('message_id', $prune_ids));
$this->db->sql_query('DELETE FROM ' . $this->mchat_settings->get_table_mchat_log() . ' WHERE ' . $this->db->sql_in_set('message_id', $prune_ids));
$this->cache->destroy('sql', $this->mchat_settings->get_table_mchat_log());
// Only add a log entry if message pruning was not triggered by user pruning
if (!$user_ids)
{
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_MCHAT_TABLE_PRUNED', false, array($this->user->data['username'], count($prune_ids)));
$this->phpbb_log('LOG_MCHAT_TABLE_PRUNED', [count($prune_ids)]);
}
}
@@ -424,13 +401,11 @@ class functions
*/
public function mchat_total_message_count()
{
$sql_where_ary = $this->get_sql_where_for_notifcation_messages();
$sql_array = array(
$sql_array = [
'SELECT' => 'COUNT(*) AS rows_total',
'FROM' => array($this->mchat_table => 'm'),
'WHERE' => $sql_where_ary ? $this->db->sql_escape('(' . implode(') AND (', $sql_where_ary) . ')') : '',
);
'FROM' => [$this->mchat_settings->get_table_mchat() => 'm'],
'WHERE' => $this->mchat_notifications->get_sql_where(),
];
/**
* Event to modifying the SQL query that fetches the total number of mChat messages
@@ -439,9 +414,9 @@ class functions
* @var array sql_array Array with SQL query data to fetch the total message count
* @since 2.0.0-RC6
*/
$vars = array(
$vars = [
'sql_array',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.total_message_count_modify_sql', compact($vars)));
$sql = $this->db->sql_build_query('SELECT', $sql_array);
@@ -463,7 +438,7 @@ class functions
*/
public function mchat_get_messages($message_ids, $last_id = 0, $total = 0, $offset = 0)
{
$sql_where_message_id = array();
$sql_where_message_id = [];
// Fetch new messages
if ($last_id)
@@ -476,35 +451,33 @@ class functions
{
if (!is_array($message_ids))
{
$message_ids = array($message_ids);
$message_ids = [$message_ids];
}
$sql_where_message_id[] = $this->db->sql_in_set('m.message_id', array_map('intval', $message_ids));
}
$sql_where_ary = $this->get_sql_where_for_notifcation_messages();
$sql_where_ary = array_filter([
implode(' OR ', $sql_where_message_id),
$this->mchat_notifications->get_sql_where(),
]);
if ($sql_where_message_id)
{
$sql_where_ary[] = implode(' OR ', $sql_where_message_id);
}
$sql_array = array(
$sql_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, p.post_visibility',
'FROM' => array($this->mchat_table => 'm'),
'LEFT_JOIN' => array(
array(
'FROM' => array(USERS_TABLE => 'u'),
'FROM' => [$this->mchat_settings->get_table_mchat() => 'm'],
'LEFT_JOIN' => [
[
'FROM' => [USERS_TABLE => 'u'],
'ON' => 'm.user_id = u.user_id',
),
array(
'FROM' => array(POSTS_TABLE => 'p'),
],
[
'FROM' => [POSTS_TABLE => 'p'],
'ON' => 'm.post_id = p.post_id AND m.forum_id <> 0',
),
),
],
],
'WHERE' => $sql_where_ary ? $this->db->sql_escape('(' . implode(') AND (', $sql_where_ary) . ')') : '',
'ORDER_BY' => 'm.message_id DESC',
);
];
/**
* Event to modify the SQL query that fetches mChat messages
@@ -517,13 +490,13 @@ class functions
* @var array sql_array Array containing the SQL query data
* @since 2.0.0-RC6
*/
$vars = array(
$vars = [
'message_ids',
'last_id',
'total',
'offset',
'sql_array',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.get_messages_modify_sql', compact($vars)));
$sql = $this->db->sql_build_query('SELECT', $sql_array);
@@ -543,88 +516,6 @@ class functions
return $rows;
}
/**
* Generates SQL where conditions to include or exlude notifacation
* messages based on the current user's settings and permissions
*
* @return array
*/
protected function get_sql_where_for_notifcation_messages()
{
$sql_where_ary = array();
if ($this->settings->cfg('mchat_posts'))
{
// If the current user doesn't have permission to see hidden users, exclude their login posts
if (!$this->auth->acl_get('u_viewonline'))
{
$sql_where_ary[] = 'm.post_id <> ' . (int) self::LOGIN_HIDDEN . // Exclude all notifications that were created by hidden users ...
' OR m.user_id = ' . (int) $this->user->data['user_id'] . // ... but include all login notifications of the current user
' OR m.forum_id <> 0'; // ... and include all post notifications
}
}
else
{
// Exclude all post notifications
$sql_where_ary[] = 'm.post_id = 0';
}
return $sql_where_ary;
}
/**
* Fetches log entries from the database and sorts them
*
* @param int $log_id The ID of the latest log entry that the user has
* @return array
*/
public function mchat_get_logs($log_id)
{
$sql_array = array(
'SELECT' => 'ml.*',
'FROM' => array($this->mchat_log_table => 'ml'),
'WHERE' => 'ml.log_id > ' . (int) $log_id,
);
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql, 3600);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$logs = array(
'id' => $log_id,
);
foreach ($rows as $row)
{
$logs['id'] = max((int) $logs['id'], (int) $row['log_id']);
$logs[] = $row;
}
return $logs;
}
/**
* Fetches the highest log ID
*
* @return int
*/
public function get_latest_log_id()
{
$sql_array = array(
'SELECT' => 'ml.log_id',
'FROM' => array($this->mchat_log_table => 'ml'),
'ORDER_BY' => 'log_id DESC',
);
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query_limit($sql, 1);
$max_log_id = (int) $this->db->sql_fetchfield('log_id');
$this->db->sql_freeresult($result);
return $max_log_id;
}
/**
* Generates the user legend markup
*
@@ -633,23 +524,23 @@ class functions
public function mchat_legend()
{
// Grab group details for legend display for who is online on the custom page
$order_legend = $this->settings->cfg('legend_sort_groupname') ? 'group_name' : 'group_legend';
$order_legend = $this->mchat_settings->cfg('legend_sort_groupname') ? 'group_name' : 'group_legend';
$sql_array = array(
'SELECT' => 'g.group_id, g.group_name, g.group_colour, g.group_type',
'FROM' => array(GROUPS_TABLE => 'g'),
'WHERE' => 'group_legend <> 0',
$sql_array = [
'SELECT' => 'g.group_id, g.group_name, g.group_colour',
'FROM' => [GROUPS_TABLE => 'g'],
'WHERE' => 'g.group_legend <> 0',
'ORDER_BY' => 'g.' . $order_legend . ' ASC',
);
];
if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
{
$sql_array['LEFT_JOIN'] = array(
array(
'FROM' => array(USER_GROUP_TABLE => 'ug'),
$sql_array['LEFT_JOIN'] = [
[
'FROM' => [USER_GROUP_TABLE => 'ug'],
'ON' => 'g.group_id = ug.group_id AND ug.user_id = ' . (int) $this->user->data['user_id'] . ' AND ug.user_pending = 0',
),
);
],
];
$sql_array['WHERE'] .= ' AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . (int) $this->user->data['user_id'] . ')';
}
@@ -659,18 +550,18 @@ class functions
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$legend = array();
$legend = [];
foreach ($rows as $row)
{
$colour_text = $row['group_colour'] ? ' style="color:#' . $row['group_colour'] . '"' : '';
$group_name = $row['group_type'] == GROUP_SPECIAL ? $this->user->lang('G_' . $row['group_name']) : $row['group_name'];
$group_name = $this->group_helper->get_name($row['group_name']);
if ($row['group_name'] == 'BOTS' || $this->user->data['user_id'] != ANONYMOUS && !$this->auth->acl_get('u_viewprofile'))
{
$legend[] = '<span' . $colour_text . '>' . $group_name . '</span>';
}
else
{
$legend[] = '<a' . $colour_text . ' href="' . append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=group&amp;g='. $row['group_id']) . '">' . $group_name . '</a>';
$legend[] = '<a' . $colour_text . ' href="' . append_sid($this->mchat_settings->url('memberlist'), ['mode' => 'group', 'g' => $row['group_id']]) . '">' . $group_name . '</a>';
}
}
@@ -684,15 +575,18 @@ class functions
*/
public function mchat_foes()
{
$sql = 'SELECT zebra_id
FROM ' . ZEBRA_TABLE . '
WHERE foe = 1
AND user_id = ' . (int) $this->user->data['user_id'];
$sql_array = [
'SELECT' => 'z.zebra_id',
'FROM' => [ZEBRA_TABLE => 'z'],
'WHERE' => 'z.foe = 1 AND z.user_id = ' . (int) $this->user->data['user_id'],
];
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$foes = array();
$foes = [];
foreach ($rows as $row)
{
@@ -702,50 +596,6 @@ class functions
return $foes;
}
/**
* Fetches post subjects and their forum names
*
* @param array $post_ids
* @return array
*/
public function mchat_get_post_data($post_ids)
{
if (!$post_ids)
{
return array();
}
$sql = 'SELECT p.post_id, p.post_subject, f.forum_id, f.forum_name
FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
WHERE p.forum_id = f.forum_id
AND ' . $this->db->sql_in_set('p.post_id', $post_ids);
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$post_subjects = array();
foreach ($rows as $row)
{
$post_subjects[$row['post_id']] = array(
'post_subject' => $row['post_subject'],
'forum_id' => $row['forum_id'],
'forum_name' => $row['forum_name'],
);
}
// Handle deleted posts
$non_existent_post_ids = array_diff($post_ids, array_keys($post_subjects));
foreach ($non_existent_post_ids as $post_id)
{
$post_subjects[$post_id] = null;
}
return $post_subjects;
}
/**
* Adds forbidden BBCodes to the passed SQL where statement
*
@@ -754,7 +604,7 @@ class functions
*/
public function mchat_sql_append_forbidden_bbcodes($sql_where)
{
$disallowed_bbcodes = explode('|', $this->settings->cfg('mchat_bbcode_disallowed'));
$disallowed_bbcodes = explode('|', $this->mchat_settings->cfg('mchat_bbcode_disallowed'));
if (!empty($disallowed_bbcodes))
{
@@ -764,72 +614,6 @@ class functions
return $sql_where;
}
/**
* Inserts a message with posting information into the database
*
* @param string $mode One of post|quote|edit|reply|login
* @param int $forum_id
* @param int $post_id
* @param bool $is_hidden_login
*/
public function mchat_insert_posting($mode, $forum_id, $post_id, $is_hidden_login)
{
$mode_config = array(
'post' => 'mchat_posts_topic',
'quote' => 'mchat_posts_quote',
'edit' => 'mchat_posts_edit',
'reply' => 'mchat_posts_reply',
'login' => 'mchat_posts_login',
);
$is_mode_enabled = !empty($mode_config[$mode]) && $this->settings->cfg($mode_config[$mode]) && (!$this->settings->cfg('mchat_posts_auth_check') || $this->auth->acl_get('u_mchat_use'));
// Special treatment for login notifications
if ($mode === 'login')
{
$forum_id = 0;
$post_id = $is_hidden_login ? self::LOGIN_HIDDEN : self::LOGIN_VISIBLE;
}
$sql_array = array(
'forum_id' => (int) $forum_id,
'post_id' => (int) $post_id,
'user_id' => (int) $this->user->data['user_id'],
'user_ip' => $this->user->ip,
'message' => 'MCHAT_NEW_' . strtoupper($mode),
'message_time' => time(),
);
/**
* Event that allows to modify data of a posting notification before it is inserted in the database
*
* @event dmzx.mchat.insert_posting_before
* @var string mode The posting mode, one of post|quote|edit|reply|login
* @var int forum_id The ID of the forum where the post was made, or 0 if mode is login.
* @var int post_id The ID of the post that was made. If mode is login this value is
* one of the constants LOGIN_HIDDEN|LOGIN_VISIBLE
* @var bool is_hidden_login Whether or not the user session is hidden. Only used if mode is login.
* @var array is_mode_enabled Whether or not the posting should be added to the database.
* @var array sql_array An array containing the data that is about to be inserted into the messages table.
* @since 2.0.0-RC6
*/
$vars = array(
'mode',
'forum_id',
'post_id',
'is_hidden_login',
'is_mode_enabled',
'sql_array',
);
extract($this->dispatcher->trigger_event('dmzx.mchat.insert_posting_before', compact($vars)));
if ($is_mode_enabled)
{
$sql = 'INSERT INTO ' . $this->mchat_table . ' ' . $this->db->sql_build_array('INSERT', $sql_array);
$this->db->sql_query($sql);
}
}
/**
* Checks if the current user is flooding the chat
*
@@ -837,20 +621,24 @@ class functions
*/
public function mchat_is_user_flooding()
{
if (!$this->settings->cfg('mchat_flood_time') || $this->auth->acl_get('u_mchat_flood_ignore'))
if (!$this->mchat_settings->cfg('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';
$sql_array = [
'SELECT' => 'm.message_time',
'FROM' => [$this->mchat_settings->get_table_mchat() => 'm'],
'WHERE' => 'm.user_id = ' . (int) $this->user->data['user_id'],
'ORDER_BY' => 'm.message_time DESC',
];
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$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->settings->cfg('mchat_flood_time');
return $message_time && time() - $message_time < $this->mchat_settings->cfg('mchat_flood_time');
}
/**
@@ -861,9 +649,13 @@ class functions
*/
public function mchat_author_for_message($message_id)
{
$sql = 'SELECT m.user_id, m.message_time, m.post_id
FROM ' . $this->mchat_table . ' m
WHERE m.message_id = ' . (int) $message_id;
$sql_array = [
'SELECT' => 'm.user_id, m.message_time, m.post_id',
'FROM' => [$this->mchat_settings->get_table_mchat() => 'm'],
'WHERE' => 'm.message_id = ' . (int) $message_id,
];
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
@@ -871,6 +663,42 @@ class functions
return $row;
}
/**
* Adds an entry to phpBB's admin log
*
* @param string $log_lang_key
* @param array $additional_data
*/
public function phpbb_log($log_lang_key, $additional_data = [])
{
$mode = 'admin';
$log_enabled = $this->mchat_settings->cfg('mchat_log_enabled');
$additional_data = array_merge([$this->user->data['username']], $additional_data);
/**
* Event to modify the phpBB log data before it is added to the log table
*
* @event dmzx.mchat.phpbb_log_add_before
* @var string mode The log mode, one of admin|mod|user|critical
* @var string log_lang_key The language key of the log entry
* @var bool log_enabled Flag indicating whether this log entry should be added or not
* @var array additional_data Array with additional data for the log message
* @since 2.1.0-RC1
*/
$vars = [
'mode',
'log_lang_key',
'log_enabled',
'additional_data',
];
extract($this->dispatcher->trigger_event('dmzx.mchat.phpbb_log_add_before', compact($vars)));
if ($log_enabled)
{
$this->log->add($mode, $this->user->data['user_id'], $this->user->ip, $log_lang_key, false, $additional_data);
}
}
/**
* Performs AJAX actions
*
@@ -893,12 +721,12 @@ class functions
* @var bool update_session_infos Whether or not to update the user session
* @since 2.0.0-RC6
*/
$vars = array(
$vars = [
'action',
'sql_ary',
'message_id',
'update_session_infos',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.action_before', compact($vars)));
$is_new_session = false;
@@ -912,7 +740,7 @@ class functions
$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));
$this->db->sql_query('INSERT INTO ' . $this->mchat_settings->get_table_mchat() . ' ' . $this->db->sql_build_array('INSERT', $sql_ary));
break;
// User edits a message
@@ -922,9 +750,9 @@ class functions
$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->mchat_insert_log('edit', $message_id);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_EDITED_MCHAT', false, array($this->user->data['username']));
$this->db->sql_query('UPDATE ' . $this->mchat_settings->get_table_mchat() . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' WHERE message_id = ' . (int) $message_id);
$this->mchat_log->add_log('edit', $message_id);
$this->phpbb_log('LOG_EDITED_MCHAT');
break;
// User deletes a message
@@ -934,34 +762,12 @@ class functions
$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->mchat_insert_log('del', $message_id);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DELETED_MCHAT', false, array($this->user->data['username']));
$this->db->sql_query('DELETE FROM ' . $this->mchat_settings->get_table_mchat() . ' WHERE message_id = ' . (int) $message_id);
$this->mchat_log->add_log('del', $message_id);
$this->phpbb_log('LOG_DELETED_MCHAT');
break;
}
return $is_new_session;
}
/**
* @param string $log_type The log type, one of edit|del
* @param int $message_id The ID of the message to which this log entry belongs
* @return int The ID of the newly added log row
*/
public function mchat_insert_log($log_type, $message_id)
{
$this->db->sql_query('INSERT INTO ' . $this->mchat_log_table . ' ' . $this->db->sql_build_array('INSERT', array(
'log_type' => array_search($log_type, $this->log_types),
'user_id' => (int) $this->user->data['user_id'],
'message_id' => (int) $message_id,
'log_ip' => $this->user->ip,
'log_time' => time(),
)));
$log_id = (int) $this->db->sql_nextid();
$this->cache->destroy('sql', $this->mchat_log_table);
return $log_id;
}
}

210
core/log.php Normal file
View File

@@ -0,0 +1,210 @@
<?php
/**
*
* @package phpBB Extension - mChat
* @copyright (c) 2018 kasimi - https://kasimi.net
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace dmzx\mchat\core;
use phpbb\cache\driver\driver_interface as cache_interface;
use phpbb\event\dispatcher_interface;
use phpbb\user;
use phpbb\db\driver\driver_interface as db_interface;
class log
{
/** @var settings */
protected $mchat_settings;
/** @var user */
protected $user;
/** @var db_interface */
protected $db;
/** @var cache_interface */
protected $cache;
/** @var dispatcher_interface */
protected $dispatcher;
/** @var array */
protected $log_types;
/**
* Constructor
*
* @param settings $mchat_settings
* @param user $user
* @param db_interface $db
* @param cache_interface $cache
* @param dispatcher_interface $dispatcher
*/
public function __construct(
settings $mchat_settings,
user $user,
db_interface $db,
cache_interface $cache,
dispatcher_interface $dispatcher
)
{
$this->mchat_settings = $mchat_settings;
$this->user = $user;
$this->db = $db;
$this->cache = $cache;
$this->dispatcher = $dispatcher;
}
/**
* Returns an array with all registered log types
*
* @return array
*/
public function get_types()
{
if (!$this->log_types)
{
// Default log types
$log_types = [
1 => 'edit',
2 => 'del',
];
/**
* Event that allows adding log types
*
* @event dmzx.mchat.log_types_init
* @var array log_types Array containing log types
* @since 2.1.0-RC1
*/
$vars = [
'log_types',
];
extract($this->dispatcher->trigger_event('dmzx.mchat.log_types_init', compact($vars)));
$this->log_types = $log_types;
}
return $this->log_types;
}
/**
* Returns the log type ID for the given string type
*
* @param string $type
* @return int
*/
public function get_type_id($type)
{
return (int) array_search($type, $this->get_types());
}
/**
* @param string $log_type The log type, one of edit|del
* @param int $message_id The ID of the message to which this log entry belongs
* @return int The ID of the newly added log row
*/
public function add_log($log_type, $message_id)
{
$this->db->sql_query('INSERT INTO ' . $this->mchat_settings->get_table_mchat_log() . ' ' . $this->db->sql_build_array('INSERT', [
'log_type' => $this->get_type_id($log_type),
'user_id' => (int) $this->user->data['user_id'],
'message_id' => (int) $message_id,
'log_ip' => $this->user->ip,
'log_time' => time(),
]));
$log_id = (int) $this->db->sql_nextid();
$this->cache->destroy('sql', $this->mchat_settings->get_table_mchat_log());
return $log_id;
}
/**
* Fetches log entries from the database and sorts them
*
* @param int $log_id The ID of the latest log entry that the user has
* @return array
*/
public function get_logs($log_id)
{
$sql_array = [
'SELECT' => 'ml.*',
'FROM' => [$this->mchat_settings->get_table_mchat_log() => 'ml'],
'WHERE' => 'ml.log_id > ' . (int) $log_id,
];
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql, 3600);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$log_rows = array_merge(array_fill_keys($this->get_types(), []), [
'latest' => (int) $log_id,
]);
$log_types = $this->get_types();
$edit_delete_limit = $this->mchat_settings->cfg('mchat_edit_delete_limit');
$time_limit = $edit_delete_limit ? time() - $edit_delete_limit : 0;
foreach ($rows as $log_row)
{
$log_rows['latest'] = max($log_rows['latest'], (int) $log_row['log_id']);
$log_type = $log_row['log_type'];
if (isset($log_types[$log_type]))
{
if ($log_row['user_id'] != $this->user->data['user_id'] && $log_row['log_time'] > $time_limit)
{
$log_type_name = $log_types[$log_type];
$log_rows[$log_type_name][] = (int) $log_row['message_id'];
}
}
/**
* Event that allows processing log messages
*
* @event dmzx.mchat.action_refresh_process_log_row
* @var array response The data that is sent back to the user (still incomplete at this point)
* @var array log_row The log data (read only)
* @since 2.0.0-RC6
*/
$vars = [
'response',
'log_row',
];
extract($this->dispatcher->trigger_event('dmzx.mchat.action_refresh_process_log_row', compact($vars)));
unset($log_row);
}
return $log_rows;
}
/**
* Fetches the highest log ID
*
* @return int
*/
public function get_latest_id()
{
$sql_array = [
'SELECT' => 'ml.log_id',
'FROM' => [$this->mchat_settings->get_table_mchat_log() => 'ml'],
'ORDER_BY' => 'log_id DESC',
];
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query_limit($sql, 1);
$max_log_id = (int) $this->db->sql_fetchfield('log_id');
$this->db->sql_freeresult($result);
return $max_log_id;
}
}

File diff suppressed because it is too large Load Diff

399
core/notifications.php Normal file
View File

@@ -0,0 +1,399 @@
<?php
/**
*
* @package phpBB Extension - mChat
* @copyright (c) 2018 kasimi - https://kasimi.net
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace dmzx\mchat\core;
use phpbb\auth\auth;
use phpbb\event\dispatcher_interface;
use phpbb\language\language;
use phpbb\textformatter\parser_interface;
use phpbb\user;
use phpbb\db\driver\driver_interface as db_interface;
class notifications
{
/**
* Value of the phpbb_mchat.post_id field for login notification
* messages if the user session is visible at the time of login
*/
const LOGIN_VISIBLE = 1;
/**
* Value of the phpbb_mchat.post_id field for login notification
* messages if the user session is hidden at the time of login
*/
const LOGIN_HIDDEN = 2;
/**
* A notification of a new topic, quote, edit or reply
*/
const POST = 3;
/** @var settings */
protected $mchat_settings;
/** @var user */
protected $user;
/** @var language */
protected $lang;
/** @var auth */
protected $auth;
/** @var db_interface */
protected $db;
/** @var dispatcher_interface */
protected $dispatcher;
/** @var parser_interface */
protected $textformatter_parser;
/**
* Constructor
*
* @param settings $mchat_settings
* @param user $user
* @param language $lang
* @param auth $auth
* @param db_interface $db
* @param dispatcher_interface $dispatcher
* @param parser_interface $textformatter_parser
*/
public function __construct(
settings $mchat_settings,
user $user,
language $lang,
auth $auth,
db_interface $db,
dispatcher_interface $dispatcher,
parser_interface $textformatter_parser
)
{
$this->mchat_settings = $mchat_settings;
$this->user = $user;
$this->lang = $lang;
$this->auth = $auth;
$this->db = $db;
$this->dispatcher = $dispatcher;
$this->textformatter_parser = $textformatter_parser;
}
/**
* Checks whether or not the given message row is a notification
*
* @param array $row The message row
* @return int the notification type, or 0 if the $row is not a notification
*/
public function is_notification($row)
{
// If post_id is 0 it's not a notification
if (isset($row['post_id']) && $row['post_id'])
{
// If forum_id is 0 it's a login notification
if (isset($row['forum_id']) && !$row['forum_id'])
{
// post_id is either LOGIN_VISIBLE or LOGIN_HIDDEN
return $row['post_id'];
}
return self::POST;
}
return 0;
}
/**
* Checks the post rows for notifications and converts their language keys
*
* @param array $rows The rows to modify
* @return array
*/
public function process($rows)
{
// All language keys of valid notifications for which we need to fetch post information
// from the database. We need to check for them here because notifications in < 2.0.0-RC6
// are plain text and don't need to be processed.
$notification_lang = [
'MCHAT_NEW_POST',
'MCHAT_NEW_QUOTE',
'MCHAT_NEW_EDIT',
'MCHAT_NEW_REPLY',
'MCHAT_NEW_LOGIN',
];
$notification_langs = array_merge(
// Raw notification messages in phpBB < 3.2
array_combine($notification_lang, $notification_lang),
// XML notification messages in phpBB >= 3.2
array_combine(array_map([$this->textformatter_parser, 'parse'], $notification_lang), $notification_lang)
);
$notifications = [];
$post_ids = [];
foreach ($rows as $i => $row)
{
$type = $this->is_notification($row);
if ($type && isset($notification_langs[$row['message']]))
{
$notifications[$i] = $type;
if ($type == self::POST)
{
$post_ids[$i] = $row['post_id'];
}
}
}
$notification_post_data = $this->mchat_get_post_data($post_ids);
foreach ($notifications as $i => $type)
{
$lang_key = $notification_langs[$rows[$i]['message']];
$post_data = $type == self::POST ? $notification_post_data[$post_ids[$i]] : null;
$rows[$i] = $this->process_notification($rows[$i], $type, $lang_key, $post_data);
}
return $rows;
}
/**
* Fetches post subjects and their forum names. If a post_id can't be found the value for the post_id is set to null.
*
* @param array $post_ids
* @return array
*/
protected function mchat_get_post_data($post_ids)
{
if (!$post_ids)
{
return [];
}
$sql_array = [
'SELECT' => 'p.post_id, p.post_subject, f.forum_id, f.forum_name',
'FROM' => [POSTS_TABLE => 'p', FORUMS_TABLE => 'f'],
'WHERE' => 'p.forum_id = f.forum_id AND ' . $this->db->sql_in_set('p.post_id', $post_ids),
];
$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$post_subjects = [];
foreach ($rows as $row)
{
$post_subjects[$row['post_id']] = [
'post_subject' => $row['post_subject'],
'forum_id' => $row['forum_id'],
'forum_name' => $row['forum_name'],
];
}
// Map IDs of missing posts to null
$missing_post_subjects = array_fill_keys(array_diff($post_ids, array_keys($post_subjects)), null);
return $post_subjects + $missing_post_subjects;
}
/**
* Converts the message field of the post row so that it can be passed to generate_text_for_display()
*
* @param array $row
* @param int $type
* @param string $lang_key
* @param array $post_data
* @return array
*/
protected function process_notification($row, $type, $lang_key, $post_data = null)
{
$lang_args = [];
$replacements = [];
$post_subject_placeholder = '%POST_SUBJECT%';
$forum_name_placeholder = '%FORUM_NAME%';
if ($type == self::POST)
{
if ($post_data)
{
$viewtopic_url = append_sid($this->mchat_settings->url('viewtopic', true), [
'p' => $row['post_id'],
'#' => 'p' . $row['post_id'],
]);
// We prefer $post_data because it was fetched from the forums table just now.
// $row might contain outdated data if a post was moved to a new forum.
$forum_id = isset($post_data['forum_id']) ? $post_data['forum_id'] : $row['forum_id'];
$viewforum_url = append_sid($this->mchat_settings->url('viewforum', true), [
'f' => $forum_id,
]);
$lang_args[] = '[url=' . $viewtopic_url . ']' . $post_subject_placeholder . '[/url]';
$lang_args[] = '[url=' . $viewforum_url . ']' . $forum_name_placeholder . '[/url]';
$replacements = [
$post_subject_placeholder => $post_data['post_subject'],
$forum_name_placeholder => $post_data['forum_name'],
];
}
else
{
$lang_key .= '_DELETED';
}
}
else if ($type == self::LOGIN_HIDDEN)
{
$row['username'] = '<em>' . $row['username'] . '</em>';
}
$row['message'] = $this->lang->lang_array($lang_key, $lang_args);
// Quick'n'dirty check if BBCodes are in the message
if (strpos($row['message'], '[') !== false)
{
generate_text_for_storage($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options'], true, true, true, true, true, true, true, 'mchat');
}
$row['message'] = strtr($row['message'], $replacements);
return $row;
}
/**
* Inserts a message with posting information into the database
*
* @param string $mode One of post|quote|edit|reply
* @param int $forum_id
* @param int $post_id
*/
public function insert_post($mode, $forum_id, $post_id)
{
$this->insert($mode, $forum_id, $post_id);
}
/**
* Inserts a message with login information into the database
*
* @param bool $is_hidden
*/
public function insert_login($is_hidden)
{
$this->insert('login', 0, $is_hidden ? self::LOGIN_HIDDEN : self::LOGIN_VISIBLE);
}
/**
* Inserts a message with posting or login information into the database
*
* @param string $mode One of post|quote|edit|reply|login
* @param int $forum_id
* @param int $post_id Can be 0 if mode is login.
*/
protected function insert($mode, $forum_id, $post_id)
{
$mode_config = [
'post' => 'mchat_posts_topic',
'quote' => 'mchat_posts_quote',
'edit' => 'mchat_posts_edit',
'reply' => 'mchat_posts_reply',
'login' => 'mchat_posts_login',
];
$is_mode_enabled = !empty($mode_config[$mode]) && $this->mchat_settings->cfg($mode_config[$mode]) && (!$this->mchat_settings->cfg('mchat_posts_auth_check') || $this->can_use_mchat());
$sql_array = [
'forum_id' => (int) $forum_id,
'post_id' => (int) $post_id,
'user_id' => (int) $this->user->data['user_id'],
'user_ip' => $this->user->ip,
'message' => $this->textformatter_parser->parse('MCHAT_NEW_' . strtoupper($mode)),
'message_time' => time(),
];
/**
* Event that allows to modify data of a posting or login notification before it is inserted in the database
*
* @event dmzx.mchat.insert_posting_before
* @var string mode The posting mode, one of post|quote|edit|reply|login
* @var int forum_id The ID of the forum where the post was made, or 0 if mode is login.
* @var int post_id The ID of the post that was made. If mode is login this value is
* one of the constants LOGIN_HIDDEN|LOGIN_VISIBLE
* @var array is_mode_enabled Whether or not the posting should be added to the database.
* @var array sql_array An array containing the data that is about to be inserted into the messages table.
* @since 2.0.0-RC6
* @changed 2.1.0-RC1 Removed is_hidden_login
*/
$vars = [
'mode',
'forum_id',
'post_id',
'is_mode_enabled',
'sql_array',
];
extract($this->dispatcher->trigger_event('dmzx.mchat.insert_posting_before', compact($vars)));
if ($is_mode_enabled)
{
$sql = 'INSERT INTO ' . $this->mchat_settings->get_table_mchat() . ' ' . $this->db->sql_build_array('INSERT', $sql_array);
$this->db->sql_query($sql);
}
}
/**
* The user might have just logged in successfully in which case the permissions haven't been updated yet.
* Let's do that here so that notifications are recorded correctly.
*
* @return bool
*/
protected function can_use_mchat()
{
if ($this->auth->acl_get('u_mchat_use'))
{
return true;
}
$auth = new auth();
$auth->acl($this->user->data);
return $auth->acl_get('u_mchat_use');
}
/**
* Generates an SQL WHERE condition to include or exlude notifacation
* messages based on the current user's settings and permissions
*
* @return string
*/
public function get_sql_where()
{
// Exclude all post notifications
if (!$this->mchat_settings->cfg('mchat_posts'))
{
return 'm.post_id = 0';
}
// If the current user doesn't have permission to see hidden users, exclude their login posts
if (!$this->auth->acl_get('u_viewonline'))
{
return implode(' OR ', [
'm.post_id <> ' . self::LOGIN_HIDDEN, // Exclude all notifications that were created by hidden users ...
'm.user_id = ' . (int) $this->user->data['user_id'], // ... but include all login notifications of the current user
'm.forum_id <> 0', // ... and include all post notifications
]);
}
return '';
}
}

View File

@@ -15,6 +15,7 @@ use phpbb\auth\auth;
use phpbb\config\config;
use phpbb\config\db_text;
use phpbb\event\dispatcher_interface;
use phpbb\language\language;
use phpbb\user;
class settings
@@ -22,6 +23,9 @@ class settings
/** @var user */
protected $user;
/** @var language */
protected $lang;
/** @var config */
protected $config;
@@ -34,6 +38,24 @@ class settings
/** @var dispatcher_interface */
protected $dispatcher;
/** @var string */
protected $root_path;
/** @var string */
protected $php_ext;
/** @var string */
protected $mchat_table;
/** @var string */
protected $mchat_log_table;
/** @var string */
protected $mchat_sessions_table;
/** @var string */
protected $board_url;
/**
* Keys for global settings that only the administrator is allowed to modify.
* The values are stored in the phpbb_config table.
@@ -74,18 +96,12 @@ class settings
*
* @var array
*/
public $prune_modes = array(
public $prune_modes = [
0 => 'messages',
1 => 'hours',
24 => 'days',
168 => 'weeks',
);
/** @var bool */
public $is_phpbb31;
/** @var bool */
public $is_phpbb32;
];
/**
* Possible values of the global setting mchat_archive_sort
@@ -98,27 +114,42 @@ class settings
* Constructor
*
* @param user $user
* @param language $lang
* @param config $config
* @param db_text $config_text
* @param auth $auth
* @param dispatcher_interface $dispatcher
* @param string $root_path
* @param string $php_ext
* @param string $mchat_table
* @param string $mchat_log_table
* @param string $mchat_sessions_table
*/
public function __construct(
user $user,
language $lang,
config $config,
db_text $config_text,
auth $auth,
dispatcher_interface $dispatcher
dispatcher_interface $dispatcher,
$root_path,
$php_ext,
$mchat_table,
$mchat_log_table,
$mchat_sessions_table
)
{
$this->user = $user;
$this->config = $config;
$this->config_text = $config_text;
$this->auth = $auth;
$this->dispatcher = $dispatcher;
$this->is_phpbb31 = phpbb_version_compare(PHPBB_VERSION, '3.1.0@dev', '>=') && phpbb_version_compare(PHPBB_VERSION, '3.2.0@dev', '<');
$this->is_phpbb32 = phpbb_version_compare(PHPBB_VERSION, '3.2.0@dev', '>=') && phpbb_version_compare(PHPBB_VERSION, '3.3.0@dev', '<');
$this->user = $user;
$this->lang = $lang;
$this->config = $config;
$this->config_text = $config_text;
$this->auth = $auth;
$this->dispatcher = $dispatcher;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->mchat_table = $mchat_table;
$this->mchat_log_table = $mchat_log_table;
$this->mchat_sessions_table = $mchat_sessions_table;
}
/**
@@ -126,37 +157,38 @@ class settings
*/
public function initialize_global_settings()
{
$global_settings = array(
'mchat_archive_sort' => array('default' => self::ARCHIVE_SORT_BOTTOM_TOP),
'mchat_bbcode_disallowed' => array('default' => '', 'validation' => array('string', false, 0, 255)),
'mchat_custom_height' => array('default' => 350, 'validation' => array('num', false, 50, 1000)),
'mchat_custom_page' => array('default' => 1),
'mchat_edit_delete_limit' => array('default' => 0),
'mchat_flood_time' => array('default' => 0, 'validation' => array('num', false, 0, 60)),
'mchat_index_height' => array('default' => 250, 'validation' => array('num', false, 50, 1000)),
'mchat_live_updates' => array('default' => 1),
'mchat_max_message_lngth' => array('default' => 500, 'validation' => array('num', false, 0, 1000)),
'mchat_message_num_archive' => array('default' => 25, 'validation' => array('num', false, 10, 100)),
'mchat_message_num_custom' => array('default' => 10, 'validation' => array('num', false, 5, 50)),
'mchat_message_num_index' => array('default' => 10, 'validation' => array('num', false, 5, 50)),
'mchat_navbar_link' => array('default' => 1),
'mchat_navbar_link_count' => array('default' => 1),
'mchat_override_min_post_chars' => array('default' => 0),
'mchat_override_smilie_limit' => array('default' => 0),
'mchat_posts_auth_check' => array('default' => 0),
'mchat_posts_edit' => array('default' => 0),
'mchat_posts_quote' => array('default' => 0),
'mchat_posts_reply' => array('default' => 0),
'mchat_posts_topic' => array('default' => 0),
'mchat_posts_login' => array('default' => 0),
'mchat_prune' => array('default' => 0),
'mchat_prune_gc' => array('default' => strtotime('1 day', 0)),
'mchat_prune_mode' => array('default' => 0),
'mchat_prune_num' => array('default' => 0),
'mchat_refresh' => array('default' => 10, 'validation' => array('num', false, 5, 60)),
'mchat_timeout' => array('default' => 0, 'validation' => array('num', false, 0, (int) $this->cfg('session_length'))),
'mchat_whois_refresh' => array('default' => 60, 'validation' => array('num', false, 10, 300)),
);
$global_settings = [
'mchat_archive_sort' => ['default' => self::ARCHIVE_SORT_BOTTOM_TOP],
'mchat_bbcode_disallowed' => ['default' => '', 'validation' => ['string', false, 0, 255]],
'mchat_custom_height' => ['default' => 350, 'validation' => ['num', false, 50, 1000]],
'mchat_custom_page' => ['default' => 1],
'mchat_edit_delete_limit' => ['default' => 0],
'mchat_flood_time' => ['default' => 0, 'validation' => ['num', false, 0, 60]],
'mchat_index_height' => ['default' => 250, 'validation' => ['num', false, 50, 1000]],
'mchat_live_updates' => ['default' => 1],
'mchat_log_enabled' => ['default' => 1],
'mchat_max_input_height' => ['default' => 150, 'validation' => ['num', false, 0, 1000]],
'mchat_max_message_lngth' => ['default' => 500, 'validation' => ['num', false, 0, 1000]],
'mchat_message_num_archive' => ['default' => 25, 'validation' => ['num', false, 10, 100]],
'mchat_message_num_custom' => ['default' => 10, 'validation' => ['num', false, 5, 50]],
'mchat_message_num_index' => ['default' => 10, 'validation' => ['num', false, 5, 50]],
'mchat_navbar_link_count' => ['default' => 1],
'mchat_override_min_post_chars' => ['default' => 0],
'mchat_override_smilie_limit' => ['default' => 0],
'mchat_posts_auth_check' => ['default' => 0],
'mchat_posts_edit' => ['default' => 0],
'mchat_posts_quote' => ['default' => 0],
'mchat_posts_reply' => ['default' => 0],
'mchat_posts_topic' => ['default' => 0],
'mchat_posts_login' => ['default' => 0],
'mchat_prune' => ['default' => 0],
'mchat_prune_gc' => ['default' => strtotime('1 day', 0)],
'mchat_prune_mode' => ['default' => 0],
'mchat_prune_num' => ['default' => 0],
'mchat_refresh' => ['default' => 10, 'validation' => ['num', false, 5, 60]],
'mchat_timeout' => ['default' => 0, 'validation' => ['num', false, 0, (int) $this->cfg('session_length')]],
'mchat_whois_refresh' => ['default' => 60, 'validation' => ['num', false, 10, 300]],
];
/**
* Event to modify global settings data
@@ -165,9 +197,9 @@ class settings
* @var array global_settings Array containing global settings data
* @since 2.0.0-RC7
*/
$vars = array(
$vars = [
'global_settings',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.global_settings_modify', compact($vars)));
return $global_settings;
@@ -178,10 +210,10 @@ class settings
*/
public function initialize_global_text_settings()
{
$global_text_settings = array(
'mchat_rules' => array('default' => ''),
'mchat_static_message' => array('default' => ''),
);
$global_text_settings = [
'mchat_rules' => ['default' => ''],
'mchat_static_message' => ['default' => ''],
];
/**
* Event to modify global text settings data
@@ -190,9 +222,9 @@ class settings
* @var array global_text_settings Array containing global text settings data
* @since 2.0.2
*/
$vars = array(
$vars = [
'global_text_settings',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.global_text_settings_modify', compact($vars)));
return $global_text_settings;
@@ -203,22 +235,21 @@ class settings
*/
public function initialize_ucp_settings()
{
$ucp_settings = array(
'mchat_avatars' => array('default' => 1),
'mchat_capital_letter' => array('default' => 1),
'mchat_character_count' => array('default' => 1),
'mchat_date' => array('default' => 'D M d, Y g:i a', 'validation' => array('string', false, 0, 64)),
'mchat_index' => array('default' => 1),
'mchat_input_area' => array('default' => 1),
'mchat_location' => array('default' => 1),
'mchat_message_top' => array('default' => 1),
'mchat_pause_on_input' => array('default' => 0),
'mchat_posts' => array('default' => 1),
'mchat_relative_time' => array('default' => 1),
'mchat_sound' => array('default' => 1),
'mchat_stats_index' => array('default' => 0),
'mchat_whois_index' => array('default' => 1),
);
$ucp_settings = [
'mchat_avatars' => ['default' => 1],
'mchat_capital_letter' => ['default' => 1],
'mchat_character_count' => ['default' => 1],
'mchat_date' => ['default' => 'D M d, Y g:i a', 'validation' => ['string', false, 0, 64]],
'mchat_index' => ['default' => 1],
'mchat_input_area' => ['default' => 1],
'mchat_location' => ['default' => 1],
'mchat_message_top' => ['default' => 1],
'mchat_posts' => ['default' => 1],
'mchat_relative_time' => ['default' => 1],
'mchat_sound' => ['default' => 1],
'mchat_stats_index' => ['default' => 0],
'mchat_whois_index' => ['default' => 1],
];
/**
* Event to modify UCP settings data
@@ -227,9 +258,9 @@ class settings
* @var array ucp_settings Array containing UCP settings data
* @since 2.0.0-RC7
*/
$vars = array(
$vars = [
'ucp_settings',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.ucp_settings_modify', compact($vars)));
return $ucp_settings;
@@ -277,9 +308,9 @@ class settings
* @var array global_text_values Array containing global text values
* @since 2.0.2
*/
$vars = array(
$vars = [
'global_text_values',
);
];
extract($this->dispatcher->trigger_event('dmzx.mchat.global_text_values_modify', compact($vars)));
$this->global_text_values = $global_text_values;
@@ -372,6 +403,30 @@ class settings
}
}
/**
* @return string
*/
public function get_table_mchat()
{
return $this->mchat_table;
}
/**
* @return string
*/
public function get_table_mchat_log()
{
return $this->mchat_log_table;
}
/**
* @return string
*/
public function get_table_mchat_sessions()
{
return $this->mchat_sessions_table;
}
/**
* @param string $selected
* @return array
@@ -379,31 +434,32 @@ class settings
public function get_date_template_data($selected)
{
$dateformat_options = '';
$dateformats = $this->lang->lang_raw('dateformats');
foreach ($this->user->lang['dateformats'] as $format => $null)
foreach (array_keys($dateformats) as $format)
{
$dateformat_options .= '<option value="' . $format . '"' . (($format == $selected) ? ' selected="selected"' : '') . '>';
$dateformat_options .= $this->user->format_date(time(), $format, false) . ((strpos($format, '|') !== false) ? $this->user->lang('VARIANT_DATE_SEPARATOR') . $this->user->format_date(time(), $format, true) : '');
$dateformat_options .= $this->user->format_date(time(), $format, false) . ((strpos($format, '|') !== false) ? $this->lang->lang('VARIANT_DATE_SEPARATOR') . $this->user->format_date(time(), $format, true) : '');
$dateformat_options .= '</option>';
}
$s_custom = false;
$dateformat_options .= '<option value="custom"';
if (!isset($this->user->lang['dateformats'][$selected]))
if (!isset($dateformats[$selected]))
{
$dateformat_options .= ' selected="selected"';
$s_custom = true;
}
$dateformat_options .= '>' . $this->user->lang('MCHAT_CUSTOM_DATEFORMAT') . '</option>';
$dateformat_options .= '>' . $this->lang->lang('MCHAT_CUSTOM_DATEFORMAT') . '</option>';
$ucp_settings = $this->ucp_settings();
return array(
return [
'S_MCHAT_DATEFORMAT_OPTIONS' => $dateformat_options,
'MCHAT_DEFAULT_DATEFORMAT' => $ucp_settings['mchat_date']['default'],
'S_MCHAT_CUSTOM_DATEFORMAT' => $s_custom,
);
];
}
/**
@@ -411,16 +467,51 @@ class settings
*/
public function get_enabled_post_notifications_lang()
{
$enabled_notifications_lang = array();
$enabled_notifications_lang = [];
foreach (array('topic', 'reply', 'quote', 'edit', 'login') as $notification)
foreach (['topic', 'reply', 'quote', 'edit', 'login'] as $notification)
{
if ($this->cfg('mchat_posts_' . $notification))
{
$enabled_notifications_lang[] = $this->user->lang('MCHAT_POSTS_' . strtoupper($notification));
$enabled_notifications_lang[] = $this->lang->lang('MCHAT_POSTS_' . strtoupper($notification));
}
}
return implode($this->user->lang('COMMA_SEPARATOR'), $enabled_notifications_lang);
return implode($this->lang->lang('COMMA_SEPARATOR'), $enabled_notifications_lang);
}
/**
* @param string $path
* @param bool $absolute_url
* @param bool $append_ext
* @return string
*/
public function url($path, $absolute_url = false, $append_ext = true)
{
if ($absolute_url && !$this->board_url)
{
$this->board_url = generate_board_url() . '/';
}
$url = ($absolute_url ? $this->board_url : $this->root_path) . $path;
if ($append_ext)
{
$url .= '.' . $this->php_ext;
}
return $url;
}
/**
* @param string $file
* @param string $function
*/
public function include_functions($file, $function)
{
if (!function_exists($function))
{
include($this->root_path . 'includes/functions_' . $file . '.' . $this->php_ext);
}
}
}