diff --git a/phpBB/phpbb/ban/manager.php b/phpBB/phpbb/ban/manager.php index d4c8383a1f..1a2d5184a2 100644 --- a/phpBB/phpbb/ban/manager.php +++ b/phpBB/phpbb/ban/manager.php @@ -16,6 +16,7 @@ namespace phpbb\ban; use phpbb\ban\exception\ban_insert_failed_exception; use phpbb\ban\exception\invalid_length_exception; use phpbb\ban\exception\type_not_found_exception; +use phpbb\ban\type\type_interface; class manager { @@ -64,28 +65,29 @@ class manager * Creates ban entries for the given $items. Returns true if successful * and false if no entries were added to the database * - * @param string $mode A string which identifies a ban type + * @param string $mode A string which identifies a ban type * @param array $items An array of items which should be banned * @param \DateTimeInterface $start A DateTimeInterface object which is the start of the ban * @param \DateTimeInterface $end A DateTimeInterface object which is the end of the ban (or 0 if permanent) - * @param string $reason An (internal) reason for the ban - * @param string $display_reason An optional reason which should be displayed to the banned + * @param string $reason An (internal) reason for the ban + * @param string $display_reason An optional reason which should be displayed to the banned * * @return bool */ - public function ban($mode, array $items, \DateTimeInterface $start, \DateTimeInterface $end, $reason, $display_reason = '') + public function ban(string $mode, array $items, \DateTimeInterface $start, \DateTimeInterface $end, string $reason, string $display_reason = '') { if ($start > $end && $end->getTimestamp() !== 0) { throw new invalid_length_exception(); // TODO } - /** @var \phpbb\ban\type\type_interface $ban_mode */ + /** @var type_interface $ban_mode */ $ban_mode = $this->find_type($mode); if ($ban_mode === false) { throw new type_not_found_exception(); // TODO } + if (!empty($this->user)) { $ban_mode->set_user($this->user); @@ -151,7 +153,7 @@ class manager */ public function unban($mode, array $items) { - /** @var \phpbb\ban\type\type_interface $ban_mode */ + /** @var type_interface $ban_mode */ $ban_mode = $this->find_type($mode); if ($ban_mode === false) { @@ -205,7 +207,7 @@ class manager foreach ($ban_info as $mode => $ban_rows) { - /** @var \phpbb\ban\type\type_interface $ban_mode */ + /** @var type_interface $ban_mode */ $ban_mode = $this->find_type($mode); if ($ban_mode === false) { @@ -264,7 +266,7 @@ class manager */ public function get_bans($mode) { - /** @var \phpbb\ban\type\type_interface $ban_mode */ + /** @var type_interface $ban_mode */ $ban_mode = $this->find_type($mode); if ($ban_mode === false) { @@ -298,7 +300,7 @@ class manager $manual_modes = []; $where_array = []; - /** @var \phpbb\ban\type\type_interface $ban_mode */ + /** @var type_interface $ban_mode */ foreach ($this->types as $ban_mode) { $user_column = $ban_mode->get_user_column(); @@ -345,7 +347,7 @@ class manager } $this->db->sql_freeresult($result); - /** @var \phpbb\ban\type\type_interface $manual_mode */ + /** @var type_interface $manual_mode */ foreach ($manual_modes as $manual_mode) { $mode_banned_users = $manual_mode->get_banned_users(); @@ -368,6 +370,39 @@ class manager }); } + public function get_ban_end(\phpbb\user $user, \DateTimeInterface $ban_start, $length, $end_date): \DateTimeInterface + { + $current_time = $ban_start->getTimestamp(); + $end_time = 0; + + if ($length) + { + if ($length != -1 || !$end_date) + { + $end_time = max($current_time, $current_time + ($length) * 60); + } + else + { + if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $end_date)) + { + $end_time = max( + $current_time, + \DateTime::createFromFormat('Y-m-d', $end_date, $user->timezone)->getTimestamp() + ); + } + else + { + trigger_error('LENGTH_BAN_INVALID', E_USER_WARNING); + } + } + } + + $ban_end = new \DateTime(); + $ban_end->setTimestamp($end_time); + + return $ban_end; + } + /** * Sets the current user to exclude from banning * @@ -388,7 +423,7 @@ class manager WHERE ban_end > 0 AND ban_end < ' . (int) time(); $this->db->sql_query($sql); - /** @var \phpbb\ban\type\type_interface $type */ + /** @var type_interface $type */ foreach ($this->types as $type) { $type->tidy(); @@ -405,7 +440,7 @@ class manager */ protected function find_type($mode) { - /** @var \phpbb\ban\type\type_interface $type */ + /** @var type_interface $type */ foreach ($this->types as $type) { if ($type->get_type() === $mode) diff --git a/phpBB/phpbb/ban/type/base.php b/phpBB/phpbb/ban/type/base.php index c269bb640e..b5d07afd6b 100644 --- a/phpBB/phpbb/ban/type/base.php +++ b/phpBB/phpbb/ban/type/base.php @@ -13,9 +13,11 @@ namespace phpbb\ban\type; +use phpbb\db\driver\driver_interface; + abstract class base implements type_interface { - /** @var \phpbb\db\driver\driver_interface */ + /** @var driver_interface */ protected $db; /** @var array */ @@ -36,12 +38,12 @@ abstract class base implements type_interface /** * Creates a ban type. * - * @param \phpbb\db\driver\driver_interface $db A phpBB DBAL object + * @param driver_interface $db A phpBB DBAL object * @param string $users_table The users table * @param string $sessions_table The sessions table * @param string $sessions_keys_table The sessions keys table */ - public function __construct(\phpbb\db\driver\driver_interface $db, $users_table, $sessions_table, $sessions_keys_table) + public function __construct(driver_interface $db, string $users_table, string $sessions_table, string $sessions_keys_table) { $this->db = $db; $this->users_table = $users_table; @@ -52,7 +54,7 @@ abstract class base implements type_interface /** * {@inheritDoc} */ - public function set_user(\phpbb\user $user) + public function set_user(\phpbb\user $user): void { // TODO: Implement new logging $this->user = $user; @@ -63,6 +65,7 @@ abstract class base implements type_interface */ public function after_ban(array $data) { + $this->logout_affected_users($data['items']); return $data['items']; } @@ -85,14 +88,14 @@ abstract class base implements type_interface /** * {@inheritDoc} */ - public function tidy() + public function tidy(): void { } /** * {@inheritDoc} */ - public function get_banned_users() + public function get_banned_users(): array { return []; } diff --git a/phpBB/phpbb/ban/type/email.php b/phpBB/phpbb/ban/type/email.php index ec3e56315f..a3e1dd4705 100644 --- a/phpBB/phpbb/ban/type/email.php +++ b/phpBB/phpbb/ban/type/email.php @@ -21,7 +21,7 @@ class email extends base /** * {@inheritDoc} */ - public function get_type() + public function get_type(): string { return 'email'; } @@ -29,7 +29,7 @@ class email extends base /** * {@inheritDoc} */ - public function get_user_column() + public function get_user_column(): ?string { return 'user_email'; } @@ -37,17 +37,7 @@ class email extends base /** * {@inheritDoc} */ - public function after_ban(array $data) - { - $this->logout_affected_users($data['items']); - - return parent::after_ban($data); - } - - /** - * {@inheritDoc} - */ - public function prepare_for_storage(array $items) + public function prepare_for_storage(array $items): array { if (!$this->get_excluded()) { diff --git a/phpBB/phpbb/ban/type/type_interface.php b/phpBB/phpbb/ban/type/type_interface.php index d0678bc897..b807b3e018 100644 --- a/phpBB/phpbb/ban/type/type_interface.php +++ b/phpBB/phpbb/ban/type/type_interface.php @@ -23,7 +23,7 @@ interface type_interface * * @return string */ - public function get_type(); + public function get_type(): string; /** * Returns the column in the users table which contains @@ -33,7 +33,7 @@ interface type_interface * * @return string|null */ - public function get_user_column(); + public function get_user_column(): ?string; /** * Sets a user object to the ban type to have it excluded @@ -41,9 +41,9 @@ interface type_interface * * @param \phpbb\user $user An user object * - * @return null + * @return void */ - public function set_user(\phpbb\user $user); + public function set_user(\phpbb\user $user): void; /** * Gives the possibility to do some clean up after banning. @@ -98,7 +98,7 @@ interface type_interface * @return array An array of banned users, where the user ids are the keys * and the value is the end of the ban (or 0 if permanent) */ - public function get_banned_users(); + public function get_banned_users(): array; /** * Prepares the given ban items before saving them in the database @@ -107,13 +107,13 @@ interface type_interface * * @return array */ - public function prepare_for_storage(array $items); + public function prepare_for_storage(array $items): array; /** * Does some cleanup work for the banning mode. * Is called before banning and unbanning and as cron job. * - * @return null + * @return void */ - public function tidy(); + public function tidy(): void; } diff --git a/phpBB/phpbb/ban/type/user.php b/phpBB/phpbb/ban/type/user.php index 7bc2bfefaa..c6b94a0388 100644 --- a/phpBB/phpbb/ban/type/user.php +++ b/phpBB/phpbb/ban/type/user.php @@ -24,7 +24,7 @@ class user extends base /** * {@inheritDoc} */ - public function get_type() + public function get_type(): string { return 'user'; } @@ -32,7 +32,7 @@ class user extends base /** * {@inheritDoc} */ - public function get_user_column() + public function get_user_column(): string { return 'user_id'; } @@ -71,7 +71,7 @@ class user extends base /** * {@inheritDoc} */ - public function prepare_for_storage(array $items) + public function prepare_for_storage(array $items): array { if (!$this->get_excluded()) {