Version 2.0.2

This commit is contained in:
dmzx
2017-12-09 08:09:57 +01:00
parent 6405c7c7ab
commit 0addbac5c0
92 changed files with 1051 additions and 776 deletions

View File

@@ -56,6 +56,9 @@ class functions
/** @var string */
protected $mchat_sessions_table;
/** @var array */
protected $active_users;
/** @var array */
public $log_types = array(
1 => 'edit',
@@ -177,10 +180,16 @@ class functions
/**
* Returns data about users who are currently chatting
*
* @param bool $cached Whether to return possibly cached data
* @return array
*/
public function mchat_active_users()
public function mchat_active_users($cached = true)
{
if ($cached && $this->active_users)
{
return $this->active_users;
}
$check_time = time() - $this->mchat_session_time();
$sql_array = array(
@@ -258,6 +267,8 @@ class functions
);
extract($this->dispatcher->trigger_event('dmzx.mchat.active_users_after', compact($vars)));
$this->active_users = $active_users;
return $active_users;
}
@@ -284,7 +295,7 @@ class functions
{
$sql = 'INSERT INTO ' . $this->mchat_sessions_table . ' ' . $this->db->sql_build_array('INSERT', array(
'user_id' => (int) $this->user->data['user_id'],
'user_ip' => $this->user->data['user_ip'],
'user_ip' => $this->user->ip,
'user_lastupdate' => time(),
));
$this->db->sql_query($sql);
@@ -349,9 +360,23 @@ class functions
$offset = 0;
}
/**
* Allow modifying SQL query before message ids to be pruned are retrieved.
*
* @event dmzx.mchat.prune_sql_before
* @var array user_ids Array of user IDs that are being pruned, empty when pruning via cron
* @var array sql_array SQL query data
* @since 2.0.2
*/
$vars = array(
'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);
$result = $this->db->sql_query_limit($sql, 0, $offset);
$rows = $this->db->sql_fetchrowset();
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$prune_ids = array();
@@ -366,7 +391,7 @@ class functions
*
* @event dmzx.mchat.prune_before
* @var array prune_ids Array of message IDs that are about to be pruned
* @var array user_ids Array of user IDs that are being pruned
* @var array user_ids Array of user IDs that are being pruned, empty when pruning via cron
* @since 2.0.0-RC6
* @changed 2.0.1 Added user_ids
*/
@@ -395,7 +420,7 @@ class functions
/**
* Returns the total number of messages
*
* @return string
* @return int
*/
public function mchat_total_message_count()
{
@@ -757,7 +782,7 @@ class functions
'login' => 'mchat_posts_login',
);
$is_mode_enabled = !empty($mode_config[$mode]) && $this->settings->cfg($mode_config[$mode]);
$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')
@@ -770,7 +795,7 @@ class functions
'forum_id' => (int) $forum_id,
'post_id' => (int) $post_id,
'user_id' => (int) $this->user->data['user_id'],
'user_ip' => $this->user->data['session_ip'],
'user_ip' => $this->user->ip,
'message' => 'MCHAT_NEW_' . strtoupper($mode),
'message_time' => time(),
);

View File

@@ -71,9 +71,6 @@ class mchat
/** @var boolean */
protected $remove_disallowed_bbcodes = false;
/** @var array */
protected $active_users = null;
/** @var array */
protected $foes = null;
@@ -155,8 +152,6 @@ class mchat
$this->assign_bbcodes_smilies();
$this->render_page('index');
$this->template->assign_var('MCHAT_IS_INDEX', true);
}
/**
@@ -189,8 +184,6 @@ class mchat
$this->assign_bbcodes_smilies();
$this->template->assign_var('MCHAT_IS_CUSTOM_PAGE', true);
$this->render_page('custom');
// Add to navlinks
@@ -221,8 +214,6 @@ class mchat
throw new http_exception(403, 'MCHAT_NOACCESS_ARCHIVE');
}
$this->template->assign_var('MCHAT_IS_ARCHIVE_PAGE', true);
$this->render_page('archive');
// Add to navlinks
@@ -291,7 +282,7 @@ class mchat
$lang_rules = $this->user->lang('MCHAT_RULES_MESSAGE');
if (!$this->settings->cfg('mchat_rules') && !$lang_rules)
if (!$lang_rules && !$this->settings->cfg('mchat_rules'))
{
throw new http_exception(404, 'MCHAT_NO_RULES');
}
@@ -332,7 +323,7 @@ class mchat
* User submits a message
*
* @param bool $return_raw
* @return array data sent to client as JSON
* @return array|JsonResponse data sent to client as JSON
*/
public function action_add($return_raw = false)
{
@@ -354,7 +345,7 @@ class mchat
$message_data = array_merge($message_data, array(
'user_id' => $this->user->data['user_id'],
'user_ip' => $this->user->data['session_ip'],
'user_ip' => $this->user->ip,
'message_time' => time(),
));
@@ -408,7 +399,7 @@ class mchat
* User edits a message
*
* @param bool $return_raw
* @return array data sent to client as JSON
* @return array|JsonResponse data sent to client as JSON
*/
public function action_edit($return_raw = false)
{
@@ -434,7 +425,7 @@ class mchat
throw new http_exception(403, 'NO_AUTH_OPERATION');
}
$this->template->assign_var('MCHAT_IS_ARCHIVE_PAGE', $this->request->variable('archive', false));
$this->template->assign_var('MCHAT_PAGE', $this->request->variable('page', ''));
$message = $this->request->variable('message', '', true);
$sql_ary = $this->process_message($message);
@@ -474,7 +465,7 @@ class mchat
* User deletes a message
*
* @param bool $return_raw
* @return array data sent to client as JSON
* @return array|JsonResponse data sent to client as JSON
*/
public function action_del($return_raw = false)
{
@@ -528,7 +519,7 @@ class mchat
* User checks for new messages
*
* @param bool $return_raw
* @return array sent to client as JSON
* @return array|JsonResponse sent to client as JSON
*/
public function action_refresh($return_raw = false)
{
@@ -693,7 +684,7 @@ class mchat
* User requests who is chatting
*
* @param bool $return_raw
* @return array data sent to client as JSON
* @return array|JsonResponse data sent to client as JSON
*/
public function action_whois($return_raw = false)
{
@@ -701,12 +692,18 @@ class mchat
$this->assign_whois();
$response = array('whois' => $this->render_template('mchat_whois.html'));
$response = array('whois' => true);
if ($this->settings->cfg('mchat_navbar_link_count') && $this->settings->cfg('mchat_navbar_link') && $this->settings->cfg('mchat_custom_page') && $this->active_users)
if ($this->settings->cfg('mchat_whois_index'))
{
$response['navlink'] = $this->active_users['users_count_title'];
$response['navlink_title'] = strip_tags($this->active_users['users_total']);
$response['container'] = $this->render_template('mchat_whois.html');
}
if ($this->settings->cfg('mchat_custom_page') && $this->settings->cfg('mchat_navbar_link') && $this->settings->cfg('mchat_navbar_link_count'))
{
$active_users = $this->functions->mchat_active_users();
$response['navlink'] = $active_users['users_count_title'];
$response['navlink_title'] = strip_tags($active_users['users_total']);
}
/**
@@ -749,13 +746,9 @@ class mchat
if ($navbar_link && $custom_page && $this->settings->cfg('mchat_navbar_link_count'))
{
if ($this->active_users === null)
{
$this->active_users = $this->functions->mchat_active_users();
}
$template_data['MCHAT_TITLE'] = $this->active_users['users_count_title'];
$template_data['MCHAT_TITLE_HINT'] = strip_tags($this->active_users['users_total']);
$active_users = $this->functions->mchat_active_users();
$template_data['MCHAT_TITLE'] = $active_users['users_count_title'];
$template_data['MCHAT_TITLE_HINT'] = strip_tags($active_users['users_total']);
}
$this->template->assign_vars($template_data);
@@ -786,6 +779,7 @@ class mchat
// If the static message is defined in the language file use it, else the entry in the database is used
$lang_static_message = $this->user->lang('MCHAT_STATIC_MESSAGE');
$static_message = $lang_static_message ?: $this->settings->cfg('mchat_static_message');
$whois_refresh = $this->settings->cfg('mchat_whois_index') || ($this->settings->cfg('mchat_custom_page') && $this->settings->cfg('mchat_navbar_link') && $this->settings->cfg('mchat_navbar_link_count'));
$this->template->assign_vars(array(
'MCHAT_PAGE' => $page,
@@ -803,17 +797,16 @@ class mchat
'MCHAT_PAUSE_ON_INPUT' => $this->settings->cfg('mchat_pause_on_input'),
'MCHAT_MESSAGE_LNGTH' => $this->settings->cfg('mchat_max_message_lngth'),
'MCHAT_WHOIS_INDEX' => $this->settings->cfg('mchat_whois_index'),
'MCHAT_WHOIS_REFRESH' => $this->settings->cfg('mchat_whois_index') || $this->settings->cfg('mchat_stats_index') ? $this->settings->cfg('mchat_whois_refresh') * 1000 : 0,
'MCHAT_WHOIS_REFRESH' => $whois_refresh ? $this->settings->cfg('mchat_whois_refresh') * 1000 : 0,
'MCHAT_REFRESH_JS' => $this->settings->cfg('mchat_refresh') * 1000,
'MCHAT_ARCHIVE' => $this->auth->acl_get('u_mchat_archive'),
'MCHAT_RULES' => $this->user->lang('MCHAT_RULES_MESSAGE') || $this->settings->cfg('mchat_rules'),
'MCHAT_WHOIS_REFRESH_EXPLAIN' => $this->user->lang('MCHAT_WHO_IS_REFRESH_EXPLAIN', $this->settings->cfg('mchat_whois_refresh')),
'MCHAT_SESSION_TIMELEFT' => $this->user->lang('MCHAT_SESSION_ENDS', gmdate($this->settings->cfg('mchat_timeout') >= 3600 ? 'H:i:s' : 'i:s', $this->settings->cfg('mchat_timeout'))),
'MCHAT_LOG_ID' => $this->functions->get_latest_log_id(),
'MCHAT_STATIC_MESS' => htmlspecialchars_decode($static_message),
'A_MCHAT_MESS_LONG' => addslashes($this->user->lang('MCHAT_MESS_LONG', $this->settings->cfg('mchat_max_message_lngth'))),
'A_MCHAT_REFRESH_YES' => addslashes($this->user->lang('MCHAT_REFRESH_YES', $this->settings->cfg('mchat_refresh'))),
'A_COOKIE_NAME' => addslashes($this->settings->cfg('cookie_name', true) . '_'),
'MCHAT_MAX_MESSAGE_LENGTH' => $this->settings->cfg('mchat_max_message_lngth'),
'MCHAT_REFRESH_RATE' => $this->settings->cfg('mchat_refresh'),
'COOKIE_NAME' => $this->settings->cfg('cookie_name', true) . '_',
'U_MCHAT_CUSTOM_PAGE' => $this->helper->route('dmzx_mchat_page_custom_controller'),
'U_MCHAT_RULES' => $this->helper->route('dmzx_mchat_page_rules_controller'),
'U_MCHAT_ARCHIVE_URL' => $this->helper->route('dmzx_mchat_page_archive_controller'),
@@ -822,15 +815,7 @@ class mchat
// The template needs some language variables if we display relative time for messages
if ($this->settings->cfg('mchat_relative_time'))
{
$minutes_limit = $this->get_relative_minutes_limit();
for ($i = 0; $i < $minutes_limit; $i++)
{
$this->template->assign_block_vars('mchattime', array(
'KEY' => $i,
'A_LANG' => addslashes($this->user->lang('MCHAT_MINUTES_AGO', $i)),
'IS_LAST' => $i + 1 === $minutes_limit,
));
}
$this->template->assign_var('MCHAT_MINUTES_AGO_LIMIT', $this->get_relative_minutes_limit());
}
// Get actions which the user is allowed to perform on the current page
@@ -839,15 +824,14 @@ class mchat
'del' => $this->auth_message('delete', true, time()),
'refresh' => $page !== 'archive' && $this->auth->acl_get('u_mchat_view'),
'add' => $page !== 'archive' && $this->auth->acl_get('u_mchat_use'),
'whois' => $page !== 'archive' && ($this->settings->cfg('mchat_whois_index') || $this->settings->cfg('mchat_stats_index')),
'whois' => $page !== 'archive' && $whois_refresh,
)));
foreach ($actions as $i => $action)
foreach ($actions as $action)
{
$this->template->assign_block_vars('mchaturl', array(
'ACTION' => $action,
'URL' => $this->helper->route('dmzx_mchat_action_' . $action . '_controller', array(), false),
'IS_LAST' => $i + 1 === count($actions),
));
}
@@ -994,7 +978,7 @@ class mchat
*/
protected function display_avatars()
{
return $this->settings->cfg('mchat_avatars') && $this->user->optionget('viewavatars') && $this->settings->cfg('mchat_avatars');
return $this->settings->cfg('mchat_avatars') && $this->user->optionget('viewavatars');
}
/**
@@ -1099,10 +1083,10 @@ class mchat
'MCHAT_ALLOW_EDIT' => $this->auth_message('edit', $row['user_id'], $row['message_time']),
'MCHAT_ALLOW_DEL' => $this->auth_message('delete', $row['user_id'], $row['message_time']),
'MCHAT_USER_AVATAR' => $user_avatars[$row['user_id']],
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? append_sid("{$board_url}{$this->root_path}memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $row['user_id']) : '',
'U_VIEWPROFILE' => $row['user_id'] != ANONYMOUS ? append_sid("{$board_url}memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $row['user_id']) : '',
'MCHAT_IS_POSTER' => $is_poster,
'MCHAT_IS_NOTIFICATION' => (bool) $row['post_id'],
'MCHAT_PM' => !$is_poster && $this->settings->cfg('allow_privmsg') && $this->auth->acl_get('u_sendpm') && ($row['user_allow_pm'] || $this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_')) ? append_sid("{$board_url}{$this->root_path}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;mchat_pm_quote_message=' . (int) $row['message_id'] . '&amp;u=' . $row['user_id']) : '',
'MCHAT_PM' => !$is_poster && $this->settings->cfg('allow_privmsg') && $this->auth->acl_get('u_sendpm') && ($row['user_allow_pm'] || $this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_')) ? append_sid("{$board_url}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;mchat_pm_quote_message=' . (int) $row['message_id'] . '&amp;u=' . $row['user_id']) : '',
'MCHAT_MESSAGE_EDIT' => $message_for_edit['text'],
'MCHAT_MESSAGE_ID' => $row['message_id'],
'MCHAT_USERNAME_FULL' => $username_full,
@@ -1110,7 +1094,7 @@ class mchat
'MCHAT_USERNAME_COLOR' => get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour'], $this->user->lang('GUEST')),
'MCHAT_WHOIS_USER' => $this->user->lang('MCHAT_WHOIS_USER', $row['user_ip']),
'MCHAT_U_IP' => $this->helper->route('dmzx_mchat_page_whois_controller', array('ip' => $row['user_ip'])),
'MCHAT_U_PERMISSIONS' => append_sid("{$board_url}{$this->root_path}adm/index.{$this->php_ext}", 'i=permissions&amp;mode=setting_user_global&amp;user_id%5B0%5D=' . $row['user_id'], true, $this->user->session_id),
'MCHAT_U_PERMISSIONS' => append_sid("{$board_url}adm/index.{$this->php_ext}", 'i=permissions&amp;mode=setting_user_global&amp;user_id%5B0%5D=' . $row['user_id'], true, $this->user->session_id),
'MCHAT_MESSAGE' => generate_text_for_display($row['message'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']),
'MCHAT_TIME' => $minutes_ago === -1 ? $datetime : $this->user->lang('MCHAT_MINUTES_AGO', $minutes_ago),
'MCHAT_DATETIME' => $absolute_datetime,
@@ -1152,7 +1136,7 @@ class mchat
}
/**
* Returns true of the user is allowed to read the given message row
* Returns true if the user is allowed to read the given message row
*
* @param array $row
* @return bool
@@ -1355,7 +1339,7 @@ class mchat
$this->template->assign_var($option['template_var'], !$is_disallowed);
}
$this->template->assign_var('A_MCHAT_DISALLOWED_BBCODES', addslashes($this->settings->cfg('mchat_bbcode_disallowed')));
$this->template->assign_var('MCHAT_DISALLOWED_BBCODES', $this->settings->cfg('mchat_bbcode_disallowed'));
if (!function_exists('display_custom_bbcodes'))
{
@@ -1472,22 +1456,19 @@ class mchat
{
if ($this->settings->cfg('mchat_whois_index') || $this->settings->cfg('mchat_stats_index'))
{
if ($this->active_users === null)
{
$this->active_users = $this->functions->mchat_active_users();
}
$active_users = $this->functions->mchat_active_users();
$this->template->assign_vars(array(
'MCHAT_STATS_INDEX' => $this->settings->cfg('mchat_stats_index'),
'MCHAT_USERS_TOTAL' => $this->active_users['users_total'],
'MCHAT_USERS_LIST' => $this->active_users['online_userlist'] ?: '',
'MCHAT_ONLINE_EXPLAIN' => $this->active_users['refresh_message'],
'MCHAT_USERS_TOTAL' => $active_users['users_total'],
'MCHAT_USERS_LIST' => $active_users['online_userlist'] ?: '',
'MCHAT_ONLINE_EXPLAIN' => $active_users['refresh_message'],
));
}
}
/**
* Checks whether an author has edit or delete permissions for a message
* Checks whether the current user has edit or delete permissions for a message written by $author_id
*
* @param string $mode One of edit|delete
* @param int $author_id The user id of the message

View File

@@ -13,6 +13,7 @@ namespace dmzx\mchat\core;
use phpbb\auth\auth;
use phpbb\config\config;
use phpbb\config\db_text;
use phpbb\event\dispatcher_interface;
use phpbb\user;
@@ -24,6 +25,9 @@ class settings
/** @var config */
protected $config;
/** @var db_text */
protected $config_text;
/** @var auth */
protected $auth;
@@ -38,6 +42,21 @@ class settings
*/
protected $global_settings;
/**
* Keys for global text settings that only the administrator is allowed to modify.
* The values are stored in the phpbb_config_text table.
*
* @var array
*/
protected $global_text_settings;
/**
* Values for global text settings.
*
* @var array
*/
protected $global_text_values;
/**
* Keys for user-specific settings for which the administrator can set default
* values as well as adjust permissions to allow users to customize them.
@@ -80,18 +99,21 @@ class settings
*
* @param user $user
* @param config $config
* @param db_text $config_text
* @param auth $auth
* @param dispatcher_interface $dispatcher
*/
public function __construct(
user $user,
config $config,
db_text $config_text,
auth $auth,
dispatcher_interface $dispatcher
)
{
$this->user = $user;
$this->config = $config;
$this->config_text = $config_text;
$this->auth = $auth;
$this->dispatcher = $dispatcher;
@@ -121,6 +143,7 @@ class settings
'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),
@@ -131,8 +154,6 @@ class settings
'mchat_prune_mode' => array('default' => 0),
'mchat_prune_num' => array('default' => 0),
'mchat_refresh' => array('default' => 10, 'validation' => array('num', false, 5, 60)),
'mchat_rules' => array('default' => '', 'validation' => array('string', false, 0, 255)),
'mchat_static_message' => array('default' => '', 'validation' => array('string', false, 0, 255)),
'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)),
);
@@ -152,6 +173,31 @@ class settings
return $global_settings;
}
/**
* @return array
*/
public function initialize_global_text_settings()
{
$global_text_settings = array(
'mchat_rules' => array('default' => ''),
'mchat_static_message' => array('default' => ''),
);
/**
* Event to modify global text settings data
*
* @event dmzx.mchat.global_text_settings_modify
* @var array global_text_settings Array containing global text settings data
* @since 2.0.2
*/
$vars = array(
'global_text_settings',
);
extract($this->dispatcher->trigger_event('dmzx.mchat.global_text_settings_modify', compact($vars)));
return $global_text_settings;
}
/**
* @return array
*/
@@ -202,6 +248,46 @@ class settings
return $this->global_settings;
}
/**
* @return array
*/
public function global_text_settings()
{
if (empty($this->global_text_settings))
{
$this->global_text_settings = $this->initialize_global_text_settings();
}
return $this->global_text_settings;
}
/**
* @return array
*/
public function global_text_values()
{
if (empty($this->global_text_values))
{
$global_text_values = $this->config_text->get_array(array_keys($this->global_text_settings()));
/**
* Event to modify global text values
*
* @event dmzx.mchat.global_text_values_modify
* @var array global_text_values Array containing global text values
* @since 2.0.2
*/
$vars = array(
'global_text_values',
);
extract($this->dispatcher->trigger_event('dmzx.mchat.global_text_values_modify', compact($vars)));
$this->global_text_values = $global_text_values;
}
return $this->global_text_values;
}
/**
* @return array
*/
@@ -234,23 +320,48 @@ class settings
*/
public function cfg_user($config, $user_data, $auth, $force_global = false)
{
$ucp_settings = $this->ucp_settings();
if (!$force_global && isset($ucp_settings[$config]) && $auth->acl_get('u_' . $config))
if (!$force_global)
{
return $user_data['user_' . $config];
$ucp_settings = $this->ucp_settings();
if (isset($ucp_settings[$config]) && $auth->acl_get('u_' . $config))
{
return $user_data['user_' . $config];
}
}
$global_text_settings = $this->global_text_settings();
if (isset($global_text_settings[$config]))
{
$global_text_values = $this->global_text_values();
return $global_text_values[$config];
}
return $this->config[$config];
}
/**
* @param $config
* @param $value
* @param string $config
* @param mixed $value
* @param bool $volatile
*/
public function set_cfg($config, $value, $volatile = false)
{
$global_text_settings = $this->global_text_settings();
if (isset($global_text_settings[$config]))
{
$this->global_text_values[$config] = $value;
if (!$volatile)
{
$this->config_text->set($config, $value);
}
return;
}
if ($volatile)
{
$this->config[$config] = $value;
@@ -290,7 +401,7 @@ class settings
return array(
'S_MCHAT_DATEFORMAT_OPTIONS' => $dateformat_options,
'A_MCHAT_DEFAULT_DATEFORMAT' => addslashes($ucp_settings['mchat_date']['default']),
'MCHAT_DEFAULT_DATEFORMAT' => $ucp_settings['mchat_date']['default'],
'S_MCHAT_CUSTOM_DATEFORMAT' => $s_custom,
);
}