1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-09-02 04:42:37 +02:00

[ticket/9687] Introduce new ban manager (WIP)

PHPBB3-9687
This commit is contained in:
Oliver Schramm
2018-09-28 17:09:31 +02:00
committed by Marc Alexander
parent caddc73e06
commit 5ae1d9eac6
9 changed files with 742 additions and 0 deletions

160
phpBB/phpbb/ban/manager.php Normal file
View File

@@ -0,0 +1,160 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\ban;
use phpbb\ban\exception\invalid_length_exception;
use phpbb\ban\exception\type_not_found_exception;
class manager
{
protected $ban_table;
protected $db;
protected $log;
protected $sessions_keys_table;
protected $sessions_table;
protected $types;
protected $user;
protected $users_table;
public function __construct($types, \phpbb\db\driver\driver_interface $db, \phpbb\log\log_interface $log, \phpbb\user $user, $ban_table, $users_table = '', $sessions_table = '', $sessions_keys_table = '')
{
$this->ban_table = $ban_table;
$this->db = $db;
$this->log = $log;
$this->sessions_keys_table = $sessions_keys_table;
$this->sessions_table = $sessions_table;
$this->types = $types;
$this->user = $user;
$this->users_table = $users_table;
}
public function ban($mode, array $items, \DateTimeInterface $start, \DateTimeInterface $end, $reason, $display_reason = '', $logging = true)
{
if (!isset($this->types[$mode]))
{
throw new type_not_found_exception(); // TODO
}
if ($start > $end && $end->getTimestamp() !== 0)
{
throw new invalid_length_exception(); // TODO
}
/** @var \phpbb\ban\type\type_interface $ban_mode */
$ban_mode = $this->types[$mode];
$ban_items = $ban_mode->prepare_for_storage($items);
// Prevent duplicate bans
$sql = 'DELETE FROM ' . $this->ban_table . "
WHERE ban_mode = '" . $this->db->sql_escape($mode) . "'
AND " . $this->db->sql_in_set('ban_item', $ban_items);
$this->db->sql_query($sql);
$insert_array = [];
foreach ($ban_items as $ban_item)
{
$insert_array[] = [
'ban_mode' => $mode,
'ban_item' => $ban_item,
'ban_start' => $start->getTimestamp(),
'ban_end' => $end->getTimestamp(),
'ban_reason' => $reason,
'ban_reason_display' => $display_reason,
];
}
if (empty($insert_array))
{
return;
}
$result = $this->db->sql_multi_insert($this->ban_table, $insert_array);
if ($result === false)
{
// Something went wrong
// TODO throw exception
}
if ($logging)
{
// TODO logging
}
if (!$ban_mode->after_ban())
{
return;
}
$user_column = $ban_mode->get_user_column();
if (!empty($user_column) && !empty($this->users_table))
{
$ban_items_sql = [];
$ban_or_like = '';
foreach ($ban_items as $ban_item)
{
if (stripos($ban_item, '*') === false)
{
$ban_items_sql[] = $ban_item;
}
else
{
$ban_or_like .= ' OR ' . $user_column . ' ' . $this->db->sql_like_expression(str_replace('*', $this->db->get_any_char(), $ban_item));
}
}
$sql = 'SELECT user_id
FROM ' . $this->users_table . '
WHERE ' . $this->db->sql_in_set('u.' . $user_column, $ban_items) . $ban_or_like;
$result = $this->db->sql_query($sql);
$user_ids = [];
while ($row = $this->db->sql_fetchrow($result))
{
$user_ids[] = (int)$row['user_id'];
}
$this->db->sql_freeresult($result);
if (!empty($user_ids) && !empty($this->sessions_table))
{
$sql = 'DELETE FROM ' . $this->sessions_table . '
WHERE ' . $this->db->sql_in_set('session_user_id', $user_ids);
$this->db->sql_query($sql);
}
if (!empty($user_ids) && !empty($this->sessions_keys_table))
{
$sql = 'DELETE FROM ' . $this->sessions_keys_table . '
WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
$this->db->sql_query($sql);
}
}
}
public function unban($mode, array $items, $reason, $logging = true)
{
}
public function check(array $user_data = [])
{
}
public function tidy()
{
}
}