[ticket/16955] Improve consistency of user and session class

PHPBB3-16955
This commit is contained in:
Marc Alexander 2022-12-26 14:54:31 +01:00
parent b90f384466
commit 077ceba2a9
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
3 changed files with 70 additions and 33 deletions

View file

@ -215,6 +215,21 @@ class session
return $host;
}
/**
* Setup basic user-specific items (style, language, ...)
*
* @param array|string|false $lang_set Lang set(s) to include, false if none shall be included
* @param int|false $style_id Style ID to load, false to load default style
*
* @throws \RuntimeException When called on session and not user instance
*
* @return void
*/
public function setup($lang_set = false, $style_id = false)
{
throw new \RuntimeException('Calling setup on session class is not supported.');
}
/**
* Start session management
*
@ -1124,7 +1139,7 @@ class session
*/
function check_ban($user_id = false, $user_ips = false, $user_email = false, $return = false)
{
global $config, $db, $phpbb_dispatcher;
global $db, $phpbb_dispatcher;
if (defined('IN_CHECK_BAN') || defined('SKIP_CHECK_BAN'))
{
@ -1254,14 +1269,7 @@ class session
if ($banned && !$return)
{
global $phpbb_root_path, $phpEx;
// If the session is empty we need to create a valid one...
if (empty($this->session_id))
{
// This seems to be no longer needed? - #14971
// $this->session_create(ANONYMOUS);
}
global $phpEx;
// Initiate environment ... since it won't be set at this stage
$this->setup();
@ -1295,13 +1303,7 @@ class session
}
// Determine which message to output
$till_date = ($ban_row['ban_end']) ? $this->format_date($ban_row['ban_end']) : '';
$message = ($ban_row['ban_end']) ? 'BOARD_BAN_TIME' : 'BOARD_BAN_PERM';
$contact_link = phpbb_get_board_contact_link($config, $phpbb_root_path, $phpEx);
$message = sprintf($this->lang[$message], $till_date, '<a href="' . $contact_link . '">', '</a>');
$message .= ($ban_row['ban_give_reason']) ? '<br /><br />' . sprintf($this->lang['BOARD_BAN_REASON'], $ban_row['ban_give_reason']) : '';
$message .= '<br /><br /><em>' . $this->lang['BAN_TRIGGERED_BY_' . strtoupper($ban_triggered_by)] . '</em>';
$message = $this->get_ban_message($ban_row, $ban_triggered_by);
// A very special case... we are within the cron script which is not supposed to print out the ban message... show blank page
if (defined('IN_CRON'))
@ -1345,6 +1347,19 @@ class session
}
}
/**
* Get ban info message
*
* @param array $ban_row Ban data row from database
* @param string $ban_triggered_by Ban triggered by; allowed 'user', 'ip', 'email'
*
* @return string
*/
protected function get_ban_message(array $ban_row, string $ban_triggered_by): string
{
return ($ban_row['ban_end']) ? 'BOARD_BAN_TIME' : 'BOARD_BAN_PERM';
}
/**
* Check if ip is blacklisted
* This should be called only where absolutely necessary
@ -1355,7 +1370,7 @@ class session
* @param string $mode register/post - spamcop for example is omitted for posting
* @param string|false $ip the IPv4 address to check
*
* @return false if ip is not blacklisted, else an array([checked server], [lookup])
* @return array|false false if ip is not blacklisted, else an array([checked server], [lookup])
*/
function check_dnsbl($mode, $ip = false)
{
@ -1680,7 +1695,9 @@ class session
$this->data = array_merge($this->data, $sql_ary);
if ($this->data['user_id'] != ANONYMOUS && isset($config['new_member_post_limit']) && $this->data['user_new'] && $config['new_member_post_limit'] <= $this->data['user_posts'])
if ($this->data['user_id'] != ANONYMOUS && isset($config['new_member_post_limit'])
&& $this->data['user_new'] && $config['new_member_post_limit'] <= $this->data['user_posts']
&& $this instanceof user)
{
$this->leave_newly_registered();
}

View file

@ -108,8 +108,13 @@ class user extends \phpbb\session
/**
* Setup basic user-specific items (style, language, ...)
*
* @param array|string|false $lang_set Lang set(s) to include, false if none shall be included
* @param int|false $style_id Style ID to load, false to load default style
*
* @return void
*/
function setup($lang_set = false, $style_id = false)
public function setup($lang_set = false, $style_id = false)
{
global $db, $request, $template, $config, $auth, $phpEx, $phpbb_root_path, $cache;
global $phpbb_dispatcher, $phpbb_container;
@ -437,8 +442,6 @@ class user extends \phpbb\session
}
$this->is_setup_flag = true;
return;
}
/**
@ -590,7 +593,7 @@ class user extends \phpbb\session
* Format user date
*
* @param int $gmepoch unix timestamp
* @param string $format date format in date() notation. | used to indicate relative dates, for example |d m Y|, h:i is translated to Today, h:i.
* @param string|false $format date format in date() notation. | used to indicate relative dates, for example |d m Y|, h:i is translated to Today, h:i.
* @param bool $forcedate force non-relative date format.
*
* @return mixed translated date
@ -614,7 +617,7 @@ class user extends \phpbb\session
* set $format_date_override to new return value
*
* @event core.user_format_date_override
* @var DateTimeZone utc Is DateTimeZone in UTC
* @var \DateTimeZone utc Is DateTimeZone in UTC
* @var array function_arguments is array comprising a function's argument list
* @var string format_date_override Shall we return custom format (string) or not (false)
* @since 3.2.1-RC1
@ -668,12 +671,11 @@ class user extends \phpbb\session
/**
* Create a \phpbb\datetime object in the context of the current user
*
* @since 3.1
* @param string $time String in a format accepted by strtotime().
* @param DateTimeZone|null $timezone Time zone of the time.
* @param ?\DateTimeZone $timezone Time zone of the time.
* @return \phpbb\datetime Date time object linked to the current users locale
*/
public function create_datetime($time = 'now', \DateTimeZone $timezone = null)
public function create_datetime(string $time = 'now', ?\DateTimeZone $timezone = null)
{
$timezone = $timezone ?: $this->create_timezone();
return new $this->datetime($this, $time, $timezone);
@ -684,14 +686,14 @@ class user extends \phpbb\session
*
* @param string $format Format of the entered date/time
* @param string $time Date/time with the timezone applied
* @param DateTimeZone|null $timezone Timezone of the date/time, falls back to timezone of current user
* @return int Returns the unix timestamp
* @param ?\DateTimeZone $timezone Timezone of the date/time, falls back to timezone of current user
* @return string|false Returns the unix timestamp or false if date is invalid
*/
public function get_timestamp_from_format($format, $time, \DateTimeZone $timezone = null)
public function get_timestamp_from_format($format, $time, ?\DateTimeZone $timezone = null)
{
$timezone = $timezone ?: $this->create_timezone();
$date = \DateTime::createFromFormat($format, $time, $timezone);
return ($date !== false) ? $date->format('U') : false;
return $date !== false ? $date->format('U') : false;
}
/**
@ -760,7 +762,7 @@ class user extends \phpbb\session
* Get option bit field from user options.
*
* @param int $key option key, as defined in $keyoptions property.
* @param int $data bit field value to use, or false to use $this->data['user_options']
* @param int|false $data bit field value to use, or false to use $this->data['user_options']
* @return bool true if the option is set in the bit field, false otherwise
*/
function optionget($key, $data = false)
@ -774,7 +776,7 @@ class user extends \phpbb\session
*
* @param int $key Option key, as defined in $keyoptions property.
* @param bool $value True to set the option, false to clear the option.
* @param int $data Current bit field value, or false to use $this->data['user_options']
* @param int|false $data Current bit field value, or false to use $this->data['user_options']
* @return int|bool If $data is false, the bit field is modified and
* written back to $this->data['user_options'], and
* return value is true if the bit field changed and
@ -865,4 +867,22 @@ class user extends \phpbb\session
return $forum_ids;
}
/**
* {@inheritDoc}
*/
protected function get_ban_message(array $ban_row, string $ban_triggered_by): string
{
global $config, $phpbb_root_path, $phpEx;
$till_date = ($ban_row['ban_end']) ? $this->format_date($ban_row['ban_end']) : '';
$message = ($ban_row['ban_end']) ? 'BOARD_BAN_TIME' : 'BOARD_BAN_PERM';
$contact_link = phpbb_get_board_contact_link($config, $phpbb_root_path, $phpEx);
$message = $this->language->lang($message, $till_date, '<a href="' . $contact_link . '">', '</a>');
$message .= ($ban_row['ban_give_reason']) ? '<br><br>' . $this->language->lang('BOARD_BAN_REASON', $ban_row['ban_give_reason']) : '';
$message .= '<br><br><em>' . $this->language->lang('BAN_TRIGGERED_BY_' . strtoupper($ban_triggered_by)) . '</em>';
return $message;
}
}

View file

@ -62,7 +62,7 @@ class phpbb_mock_session_testable extends \phpbb\session
}
}
public function setup()
public function setup($lang_set = false, $style_id = false)
{
}
}