1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-07 01:06:48 +02:00

[ticket/16643] Refactor installer tasks to use Doctrine DBAL

PHPBB3-16643
This commit is contained in:
Máté Bartus
2021-01-16 11:57:35 +01:00
parent aab2679966
commit 2a57477718
19 changed files with 1102 additions and 676 deletions

View File

@@ -13,43 +13,49 @@
namespace phpbb\install\module\install_database\task;
use phpbb\install\exception\resource_limit_reached_exception;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Statement;
use phpbb\filesystem\filesystem_interface;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
use phpbb\language\language;
/**
* Create database schema
*/
class add_config_settings extends \phpbb\install\task_base
class add_config_settings extends database_task
{
use sequential_task;
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* @var \phpbb\filesystem\filesystem_interface
* @var filesystem_interface
*/
protected $filesystem;
/**
* @var \phpbb\install\helper\config
* @var config
*/
protected $install_config;
/**
* @var \phpbb\install\helper\iohandler\iohandler_interface
* @var iohandler_interface
*/
protected $iohandler;
/**
* @var \phpbb\language\language
* @var language
*/
protected $language;
/**
* @var \phpbb\passwords\manager
*/
protected $password_manager;
/**
* @var string
*/
@@ -61,64 +67,44 @@ class add_config_settings extends \phpbb\install\task_base
protected $config_table;
/**
* @var string
* @var Statement|DriverStatement
*/
protected $user_table;
/**
* @var string
*/
protected $topics_table;
/**
* @var string
*/
protected $forums_table;
/**
* @var string
*/
protected $posts_table;
/**
* @var string
*/
protected $moderator_cache_table;
protected $stmt;
/**
* Constructor
*
* @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem service
* @param \phpbb\install\helper\config $install_config Installer's config helper
* @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler Installer's input-output handler
* @param \phpbb\install\helper\container_factory $container Installer's DI container
* @param \phpbb\language\language $language Language service
* @param string $phpbb_root_path Path to phpBB's root
* @param database $db_helper Database helper
* @param filesystem_interface $filesystem Filesystem service
* @param config $install_config Installer's config helper
* @param iohandler_interface $iohandler Installer's input-output handler
* @param container_factory $container Installer's DI container
* @param language $language Language service
* @param string $phpbb_root_path Path to phpBB's root
*/
public function __construct(\phpbb\filesystem\filesystem_interface $filesystem,
\phpbb\install\helper\config $install_config,
\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
\phpbb\install\helper\container_factory $container,
\phpbb\language\language $language,
$phpbb_root_path)
public function __construct(database $db_helper,
filesystem_interface $filesystem,
config $install_config,
iohandler_interface $iohandler,
container_factory $container,
language $language,
string $phpbb_root_path)
{
$this->db = $container->get('dbal.conn');
$this->filesystem = $filesystem;
$this->install_config = $install_config;
$this->iohandler = $iohandler;
$this->language = $language;
$this->password_manager = $container->get('passwords.manager');
$this->phpbb_root_path = $phpbb_root_path;
// Table names
$this->config_table = $container->get_parameter('tables.config');
$this->forums_table = $container->get_parameter('tables.forums');
$this->topics_table = $container->get_parameter('tables.topics');
$this->user_table = $container->get_parameter('tables.users');
$this->moderator_cache_table = $container->get_parameter('tables.moderator_cache');
$this->posts_table = $container->get_parameter('tables.posts');
$this->config_table = $container->get_parameter('tables.config');
parent::__construct(true);
parent::__construct(
self::get_doctrine_connection($db_helper, $install_config),
$this->iohandler,
true
);
}
/**
@@ -126,12 +112,14 @@ class add_config_settings extends \phpbb\install\task_base
*/
public function run()
{
$this->db->sql_return_on_error(true);
$current_time = $this->install_config->get('install_board_time');
if ($current_time === false)
{
$current_time = time();
$this->install_config->set('install_board_time', $current_time);
}
$server_name = $this->install_config->get('server_name');
$current_time = time();
$user_ip = phpbb_ip_normalise($this->iohandler->get_server_variable('REMOTE_ADDR'));
$user_ip = ($user_ip === false) ? '' : $user_ip;
$referer = $this->iohandler->get_server_variable('REFERER');
// Calculate cookie domain
@@ -142,160 +130,54 @@ class add_config_settings extends \phpbb\install\task_base
$cookie_domain = substr($cookie_domain, 3);
}
// Set default config and post data, this applies to all DB's
$sql_ary = array(
'INSERT INTO ' . $this->config_table . " (config_name, config_value)
VALUES ('board_startdate', '$current_time')",
$updates = [
'board_startdate' => (string) $current_time,
'default_lang' => $this->install_config->get('default_lang'),
'INSERT INTO ' . $this->config_table . " (config_name, config_value)
VALUES ('default_lang', '" . $this->db->sql_escape($this->install_config->get('default_lang')) . "')",
'server_name' => $this->install_config->get('server_name'),
'server_port' => $this->install_config->get('server_port'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('server_name')) . "'
WHERE config_name = 'server_name'",
'board_email' => $this->install_config->get('board_email'),
'board_contact' => $this->install_config->get('board_email'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('server_port')) . "'
WHERE config_name = 'server_port'",
'cookie_domain' => $cookie_domain,
'cookie_secure' => $this->install_config->get('cookie_secure'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_email')) . "'
WHERE config_name = 'board_email'",
'default_dateformat' => $this->language->lang('default_dateformat'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_email')) . "'
WHERE config_name = 'board_contact'",
'email_enable' => $this->install_config->get('email_enable'),
'smtp_delivery' => $this->install_config->get('smtp_delivery'),
'smtp_host' => $this->install_config->get('smtp_host'),
'smtp_port' => $this->install_config->get('smtp_port'),
'smtp_auth_method' => $this->install_config->get('smtp_auth'),
'smtp_username' => $this->install_config->get('smtp_user'),
'smtp_password' => $this->install_config->get('smtp_pass'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($cookie_domain) . "'
WHERE config_name = 'cookie_domain'",
'force_server_vars' => $this->install_config->get('force_server_vars'),
'script_path' => $this->install_config->get('script_path'),
'server_protocol' => $this->install_config->get('server_protocol'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->language->lang('default_dateformat')) . "'
WHERE config_name = 'default_dateformat'",
'newest_username' => $this->install_config->get('admin_name'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('email_enable')) . "'
WHERE config_name = 'email_enable'",
'avatar_salt' => md5(mt_rand()),
'plupload_salt' => md5(mt_rand()),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_delivery')) . "'
WHERE config_name = 'smtp_delivery'",
'sitename' => $this->install_config->get('board_name'),
'site_desc' => $this->install_config->get('board_description'),
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_host')) . "'
WHERE config_name = 'smtp_host'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_port')) . "'
WHERE config_name = 'smtp_port'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_auth')) . "'
WHERE config_name = 'smtp_auth_method'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_user')) . "'
WHERE config_name = 'smtp_username'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('smtp_pass')) . "'
WHERE config_name = 'smtp_password'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('cookie_secure')) . "'
WHERE config_name = 'cookie_secure'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('force_server_vars')) . "'
WHERE config_name = 'force_server_vars'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('script_path')) . "'
WHERE config_name = 'script_path'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('server_protocol')) . "'
WHERE config_name = 'server_protocol'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
WHERE config_name = 'newest_username'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . md5(mt_rand()) . "'
WHERE config_name = 'avatar_salt'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . md5(mt_rand()) . "'
WHERE config_name = 'plupload_salt'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_name')) . "'
WHERE config_name = 'sitename'",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->install_config->get('board_description')) . "'
WHERE config_name = 'site_desc'",
'UPDATE ' . $this->user_table . "
SET username = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "',
user_password='" . $this->password_manager->hash($this->install_config->get('admin_passwd')) . "',
user_ip = '" . $this->db->sql_escape($user_ip) . "',
user_lang = '" . $this->db->sql_escape($this->install_config->get('user_language', 'en')) . "',
user_email='" . $this->db->sql_escape($this->install_config->get('board_email')) . "',
user_dateformat='" . $this->db->sql_escape($this->language->lang('default_dateformat')) . "',
username_clean = '" . $this->db->sql_escape(utf8_clean_string($this->install_config->get('admin_name'))) . "'
WHERE username = 'Admin'",
'UPDATE ' . $this->moderator_cache_table . "
SET username = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
WHERE username = 'Admin'",
'UPDATE ' . $this->forums_table . "
SET forum_last_poster_name = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
WHERE forum_last_poster_name = 'Admin'",
'UPDATE ' . $this->topics_table . "
SET topic_first_poster_name = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "',
topic_last_poster_name = '" . $this->db->sql_escape($this->install_config->get('admin_name')) . "'
WHERE topic_first_poster_name = 'Admin'
OR topic_last_poster_name = 'Admin'",
'UPDATE ' . $this->user_table . "
SET user_regdate = $current_time",
'UPDATE ' . $this->posts_table . "
SET post_time = $current_time, poster_ip = '" . $this->db->sql_escape($user_ip) . "'",
'UPDATE ' . $this->topics_table . "
SET topic_time = $current_time, topic_last_post_time = $current_time",
'UPDATE ' . $this->forums_table . "
SET forum_last_post_time = $current_time",
'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($this->db->sql_server_info(true)) . "'
WHERE config_name = 'dbms_version'",
);
'dbms_version' => $this->db->sql_server_info(true),
];
if (@extension_loaded('gd'))
{
$sql_ary[] = 'UPDATE ' . $this->config_table . "
SET config_value = 'core.captcha.plugins.gd'
WHERE config_name = 'captcha_plugin'";
$sql_ary[] = 'UPDATE ' . $this->config_table . "
SET config_value = '1'
WHERE config_name = 'captcha_gd'";
$updates['captcha_plugin'] = 'core.captcha.plugins.gd';
$updates['captcha_gd'] = '1';
}
$ref = substr($referer, strpos($referer, '://') + 3);
if (!(stripos($ref, $server_name) === 0))
{
$sql_ary[] = 'UPDATE ' . $this->config_table . "
SET config_value = '0'
WHERE config_name = 'referer_validation'";
$updates['referer_validation'] = '0';
}
// We set a (semi-)unique cookie name to bypass login issues related to the cookie name.
@@ -305,54 +187,40 @@ class add_config_settings extends \phpbb\install\task_base
$rand_str = substr($rand_str, 0, 5);
$cookie_name .= strtolower($rand_str);
$sql_ary[] = 'UPDATE ' . $this->config_table . "
SET config_value = '" . $this->db->sql_escape($cookie_name) . "'
WHERE config_name = 'cookie_name'";
$updates['cookie_name'] = $cookie_name;
// Disable avatars if upload directory is not writable
if (!$this->filesystem->is_writable($this->phpbb_root_path . 'images/avatars/upload/'))
{
$sql_ary[] = 'UPDATE ' . $this->config_table . "
SET config_value = '0'
WHERE config_name = 'allow_avatar'";
$sql_ary[] = 'UPDATE ' . $this->config_table . "
SET config_value = '0'
WHERE config_name = 'allow_avatar_upload'";
$updates['allow_avatar'] = '0';
$updates['allow_avatar_upload'] = '0';
}
$i = $this->install_config->get('add_config_settings_index', 0);
$total = count($sql_ary);
$sql_ary = array_slice($sql_ary, $i);
$this->stmt = $this->create_prepared_stmt(
'UPDATE ' . $this->config_table . ' SET config_value = :value WHERE config_name = :name'
);
foreach ($sql_ary as $sql)
if ($this->stmt !== null)
{
if (!$this->db->sql_query($sql))
{
$error = $this->db->sql_error($this->db->get_sql_error_sql());
$this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
}
$i++;
// Stop execution if resource limit is reached
if ($this->install_config->get_time_remaining() <= 0 || $this->install_config->get_memory_remaining() <= 0)
{
break;
}
}
if ($i < $total)
{
$this->install_config->set('add_config_settings_index', $i);
throw new resource_limit_reached_exception();
$this->execute($this->install_config, $updates);
}
}
/**
* {@inheritdoc}
*/
static public function get_step_count()
protected function execute_step($key, $value) : void
{
$this->exec_prepared_stmt($this->stmt, [
'name' => $key,
'value' => $value,
]);
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
@@ -360,7 +228,7 @@ class add_config_settings extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_ADD_CONFIG_SETTINGS';
}

View File

@@ -13,35 +13,38 @@
namespace phpbb\install\module\install_database\task;
use phpbb\install\exception\resource_limit_reached_exception;
use Doctrine\DBAL\Connection;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\install\sequential_task;
use phpbb\language\language;
/**
* Create database schema
*/
class add_default_data extends \phpbb\install\task_base
class add_default_data extends database_task
{
use sequential_task;
/**
* @var \phpbb\db\driver\driver_interface
* @var Connection
*/
protected $db;
/**
* @var \phpbb\install\helper\database
* @var database
*/
protected $database_helper;
/**
* @var \phpbb\install\helper\config
* @var config
*/
protected $config;
/**
* @var \phpbb\install\helper\iohandler\iohandler_interface
*/
protected $iohandler;
/**
* @var \phpbb\language\language
* @var language
*/
protected $language;
@@ -53,28 +56,25 @@ class add_default_data extends \phpbb\install\task_base
/**
* Constructor
*
* @param \phpbb\install\helper\database $db_helper Installer's database helper
* @param \phpbb\install\helper\config $config Installer config
* @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler Installer's input-output handler
* @param \phpbb\install\helper\container_factory $container Installer's DI container
* @param \phpbb\language\language $language Language service
* @param string $root_path Root path of phpBB
* @param database $db_helper Installer's database helper
* @param config $config Installer config
* @param iohandler_interface $iohandler Installer's input-output handler
* @param language $language Language service
* @param string $root_path Root path of phpBB
*/
public function __construct(\phpbb\install\helper\database $db_helper,
\phpbb\install\helper\config $config,
\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
\phpbb\install\helper\container_factory $container,
\phpbb\language\language $language,
$root_path)
public function __construct(database $db_helper,
config $config,
iohandler_interface $iohandler,
language $language,
string $root_path)
{
$this->db = $container->get('dbal.conn.driver');
$this->db = self::get_doctrine_connection($db_helper, $config);
$this->database_helper = $db_helper;
$this->config = $config;
$this->iohandler = $iohandler;
$this->language = $language;
$this->phpbb_root_path = $root_path;
parent::__construct(true);
parent::__construct($this->db, $iohandler, true);
}
/**
@@ -82,8 +82,6 @@ class add_default_data extends \phpbb\install\task_base
*/
public function run()
{
$this->db->sql_return_on_error(true);
$table_prefix = $this->config->get('table_prefix');
$dbms = $this->config->get('dbms');
$dbms_info = $this->database_helper->get_available_dbms($dbms);
@@ -98,53 +96,55 @@ class add_default_data extends \phpbb\install\task_base
$sql_query = $this->database_helper->remove_comments($sql_query);
$sql_query = $this->database_helper->split_sql_file($sql_query, $dbms_info[$dbms]['DELIM']);
$i = $this->config->get('add_default_data_index', 0);
$total = count($sql_query);
$sql_query = array_slice($sql_query, $i);
$this->execute($this->config, $sql_query);
}
foreach ($sql_query as $sql)
/**
* {@inheritdoc}
*/
protected function execute_step($key, $value) : void
{
$sql = trim($value);
switch ($sql)
{
if (!$this->db->sql_query($sql))
{
$error = $this->db->sql_error($this->db->get_sql_error_sql());
$this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
}
case 'BEGIN':
$this->db->beginTransaction();
break;
$i++;
case 'COMMIT':
$this->db->commit();
break;
// Stop execution if resource limit is reached
if ($this->config->get_time_remaining() <= 0 || $this->config->get_memory_remaining() <= 0)
{
break;
}
}
$this->config->set('add_default_data_index', $i);
if ($i < $total)
{
throw new resource_limit_reached_exception();
default:
$this->exec_sql($sql);
break;
}
}
/**
* Process DB specific SQL
*
* @param string $query
*
* @return string
*/
protected function replace_dbms_specific_sql($query)
protected function replace_dbms_specific_sql(string $query) : string
{
if ($this->db instanceof \phpbb\db\driver\mssql_base)
$dbms = $this->config->get('dbms');
switch ($dbms)
{
$query = preg_replace('#\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \##s', 'SET IDENTITY_INSERT \1 \2;', $query);
}
else if ($this->db instanceof \phpbb\db\driver\postgres)
{
$query = preg_replace('#\# POSTGRES (BEGIN|COMMIT) \##s', '\1; ', $query);
}
else if ($this->db instanceof \phpbb\db\driver\mysql_base)
{
$query = str_replace('\\', '\\\\', $query);
case 'mssql_odbc':
case 'mssqlnative':
$query = preg_replace('#\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \##s', 'SET IDENTITY_INSERT \1 \2;', $query);
break;
case 'postgres':
$query = preg_replace('#\# POSTGRES (BEGIN|COMMIT) \##s', '\1; ', $query);
break;
case 'mysqli':
$query = str_replace('\\', '\\\\', $query);
break;
}
return $query;
@@ -156,11 +156,15 @@ class add_default_data extends \phpbb\install\task_base
* @param array $matches
* @return string
*/
public function lang_replace_callback($matches)
public function lang_replace_callback(array $matches) : string
{
if (!empty($matches[1]))
{
return $this->db->sql_escape($this->language->lang($matches[1]));
$translation = $this->language->lang($matches[1]);
// This is might not be secure, but these queries should not be malicious anyway.
$quoted = $this->db->quote($translation) ?: '\'' . addcslashes($translation, '\'') . '\'';
return substr($quoted, 1, -1);
}
return '';
@@ -169,7 +173,7 @@ class add_default_data extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
static public function get_step_count()
static public function get_step_count() : int
{
return 1;
}
@@ -177,7 +181,7 @@ class add_default_data extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_ADD_DEFAULT_DATA';
}

View File

@@ -13,25 +13,32 @@
namespace phpbb\install\module\install_database\task;
use phpbb\install\exception\resource_limit_reached_exception;
use phpbb\db\driver\driver_interface;
use phpbb\db\tools\tools_interface;
use phpbb\install\helper\config;
use phpbb\install\helper\database;
use phpbb\install\sequential_task;
use phpbb\install\task_base;
/**
* Create tables
*/
class add_tables extends \phpbb\install\task_base
class add_tables extends task_base
{
use sequential_task;
/**
* @var \phpbb\install\helper\config
* @var config
*/
protected $config;
/**
* @var \phpbb\db\driver\driver_interface
* @var driver_interface
*/
protected $db;
/**
* @var \phpbb\db\tools\tools_interface
* @var tools_interface
*/
protected $db_tools;
@@ -40,16 +47,26 @@ class add_tables extends \phpbb\install\task_base
*/
protected $schema_file_path;
/**
* @var string
*/
protected $table_prefix;
/**
* @var bool
*/
protected $change_prefix;
/**
* Constructor
*
* @param \phpbb\install\helper\config $config
* @param \phpbb\install\helper\database $db_helper
* @param string $phpbb_root_path
* @param config $config
* @param database $db_helper
* @param string $phpbb_root_path
*/
public function __construct(\phpbb\install\helper\config $config,
\phpbb\install\helper\database $db_helper,
$phpbb_root_path)
public function __construct(config $config,
database $db_helper,
string $phpbb_root_path)
{
$dbms = $db_helper->get_available_dbms($config->get('dbms'));
$dbms = $dbms[$config->get('dbms')]['DRIVER'];
@@ -69,6 +86,8 @@ class add_tables extends \phpbb\install\task_base
$this->config = $config;
$this->db_tools = $factory->get($this->db);
$this->schema_file_path = $phpbb_root_path . 'store/schema.json';
$this->table_prefix = $this->config->get('table_prefix');
$this->change_prefix = $this->config->get('change_table_prefix', true);
parent::__construct(true);
}
@@ -80,55 +99,37 @@ class add_tables extends \phpbb\install\task_base
{
$this->db->sql_return_on_error(true);
$table_prefix = $this->config->get('table_prefix');
$change_prefix = $this->config->get('change_table_prefix', true);
if (!defined('CONFIG_TABLE'))
{
// CONFIG_TABLE is required by sql_create_index() to check the
// length of index names. However table_prefix is not defined
// here yet, so we need to create the constant ourselves.
define('CONFIG_TABLE', $table_prefix . 'config');
define('CONFIG_TABLE', $this->table_prefix . 'config');
}
$db_table_schema = @file_get_contents($this->schema_file_path);
$db_table_schema = json_decode($db_table_schema, true);
$total = count($db_table_schema);
$i = $this->config->get('add_table_index', 0);
$db_table_schema = array_slice($db_table_schema, $i);
foreach ($db_table_schema as $table_name => $table_data)
{
$i++;
$this->execute($this->config, $db_table_schema);
$this->db_tools->sql_create_table(
( ($change_prefix) ? ($table_prefix . substr($table_name, 6)) : $table_name ),
$table_data
);
// Stop execution if resource limit is reached
if ($this->config->get_time_remaining() <= 0 || $this->config->get_memory_remaining() <= 0)
{
break;
}
}
$this->config->set('add_table_index', $i);
if ($i < $total)
{
throw new resource_limit_reached_exception();
}
else
{
@unlink($this->schema_file_path);
}
@unlink($this->schema_file_path);
}
/**
* {@inheritdoc}
*/
static public function get_step_count()
protected function execute_step($key, $value) : void
{
$this->db_tools->sql_create_table(
($this->change_prefix) ? ($this->table_prefix . substr($key, 6)) : $key,
$value
);
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
@@ -136,7 +137,7 @@ class add_tables extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_CREATE_TABLES';
}

View File

@@ -13,36 +13,32 @@
namespace phpbb\install\module\install_database\task;
use phpbb\filesystem\filesystem_interface;
use phpbb\install\database_task;
use phpbb\install\helper\config;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
/**
* Set up database for table generation
*/
class set_up_database extends \phpbb\install\task_base
class set_up_database extends database_task
{
/**
* @var \phpbb\install\helper\config
* @var config
*/
protected $config;
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* @var \phpbb\install\helper\database
* @var database
*/
protected $database_helper;
/**
* @var \phpbb\filesystem\filesystem_interface
* @var filesystem_interface
*/
protected $filesystem;
/**
* @var \phpbb\install\helper\iohandler\iohandler_interface
*/
protected $iohandler;
/**
* @var string
*/
@@ -56,45 +52,34 @@ class set_up_database extends \phpbb\install\task_base
/**
* Constructor
*
* @param \phpbb\install\helper\config $config
* @param \phpbb\install\helper\database $db_helper
* @param \phpbb\filesystem\filesystem_interface $filesystem
* @param \phpbb\install\helper\iohandler\iohandler_interface $iohandler
* @param string $phpbb_root_path
* @param config $config
* @param database $db_helper
* @param filesystem_interface $filesystem
* @param iohandler_interface $iohandler
* @param string $phpbb_root_path
*/
public function __construct(\phpbb\install\helper\config $config,
\phpbb\install\helper\database $db_helper,
\phpbb\filesystem\filesystem_interface $filesystem,
\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
$phpbb_root_path)
public function __construct(config $config,
database $db_helper,
filesystem_interface $filesystem,
iohandler_interface $iohandler,
string $phpbb_root_path)
{
$dbms = $db_helper->get_available_dbms($config->get('dbms'));
$dbms = $dbms[$config->get('dbms')]['DRIVER'];
$this->db = new $dbms();
$this->db->sql_connect(
$config->get('dbhost'),
$config->get('dbuser'),
$config->get('dbpasswd'),
$config->get('dbname'),
$config->get('dbport'),
false,
false
);
$this->config = $config;
$this->database_helper = $db_helper;
$this->filesystem = $filesystem;
$this->iohandler = $iohandler;
$this->phpbb_root_path = $phpbb_root_path;
parent::__construct(false);
parent::__construct(
self::get_doctrine_connection($db_helper, $config),
$iohandler,
false
);
}
/**
* {@inheritdoc}
*/
public function check_requirements()
public function check_requirements() : bool
{
$dbms = $this->config->get('dbms');
$dbms_info = $this->database_helper->get_available_dbms($dbms);
@@ -115,25 +100,19 @@ class set_up_database extends \phpbb\install\task_base
*/
public function run()
{
$this->db->sql_return_on_error(true);
$dbms = $this->config->get('dbms');
$dbms_info = $this->database_helper->get_available_dbms($dbms);
$delimiter = $dbms_info[$dbms]['DELIM'];
$table_prefix = $this->config->get('table_prefix');
$sql_query = @file_get_contents($this->schema_file_path);
$sql_query = preg_replace('#phpbb_#i', $table_prefix, $sql_query);
$sql_query = str_replace('phpbb_', ' ' . $table_prefix, $sql_query);
$sql_query = $this->database_helper->remove_comments($sql_query);
$sql_query = $this->database_helper->split_sql_file($sql_query, $delimiter);
foreach ($sql_query as $sql)
{
if (!$this->db->sql_query($sql))
{
$error = $this->db->sql_error($this->db->get_sql_error_sql());
$this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
}
$this->exec_sql($sql);
}
unset($sql_query);
@@ -142,7 +121,7 @@ class set_up_database extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
static public function get_step_count()
static public function get_step_count() : int
{
return 1;
}
@@ -150,7 +129,7 @@ class set_up_database extends \phpbb\install\task_base
/**
* {@inheritdoc}
*/
public function get_task_lang_name()
public function get_task_lang_name() : string
{
return 'TASK_SETUP_DATABASE';
}

View File

@@ -0,0 +1,186 @@
<?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\install\module\install_database\task;
use phpbb\install\database_task;
use phpbb\install\exception\resource_limit_reached_exception;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\database;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\language\language;
/**
* Update the admin's info as well as the welcome post.
*/
class update_user_and_post_data extends database_task
{
/** @var config */
private $install_config;
private $iohandler;
/** @var language */
private $language;
/** @var \phpbb\passwords\manager */
private $password_manager;
/** @var string */
private $forums_table;
/** @var string */
private $moderator_cache_table;
/** @var string */
private $posts_table;
/** @var string */
private $topics_table;
/** @var string */
private $user_table;
/**
* Constructor.
*
* @param config $install_config
* @param container_factory $container
* @param database $db_helper
* @param iohandler_interface $iohandler
* @param language $language
*/
public function __construct(
config $install_config,
container_factory $container,
database $db_helper,
iohandler_interface $iohandler,
language $language)
{
$this->install_config = $install_config;
$this->iohandler = $iohandler;
$this->language = $language;
$this->password_manager = $container->get('passwords.manager');
$this->forums_table = $container->get_parameter('tables.forums');
$this->moderator_cache_table = $container->get_parameter('tables.moderator_cache');
$this->posts_table = $container->get_parameter('tables.posts');
$this->topics_table = $container->get_parameter('tables.topics');
$this->user_table = $container->get_parameter('tables.users');
parent::__construct(
self::get_doctrine_connection($db_helper, $install_config),
$this->iohandler,
true
);
}
/**
* {@inheritdoc}
*/
public function run()
{
// Force a refresh.
$count = $this->install_config->get('correct_user_and_post_data_count');
if ($count === false)
{
if ($this->install_config->get_time_remaining() < 5)
{
$this->install_config->set('correct_user_and_post_data_count', 1);
throw new resource_limit_reached_exception();
}
}
$user_ip = phpbb_ip_normalise($this->iohandler->get_server_variable('REMOTE_ADDR'));
$user_ip = ($user_ip === false) ? '' : $user_ip;
$current_time = $this->install_config->get('install_board_time', time());
// Update user data
$sql = 'UPDATE ' . $this->user_table
. ' SET username = :username,'
. ' user_password = :password,'
. ' user_ip = :ip,'
. ' user_lang = :lang,'
. ' user_email = :email,'
. ' user_dateformat = :dateformat,'
. ' username_clean = :clean_username'
. ' WHERE username = \'Admin\'';
$this->create_and_execute_prepared_stmt($sql, [
'username' => $this->install_config->get('admin_name'),
'password' => $this->password_manager->hash($this->install_config->get('admin_passwd')),
'ip' => $user_ip,
'lang' => $this->install_config->get('user_language', 'en'),
'email' => $this->install_config->get('board_email'),
'dateformat' => $this->language->lang('default_dateformat'),
'clean_username' => utf8_clean_string($this->install_config->get('admin_name')),
]);
$this->exec_sql('UPDATE ' . $this->user_table . ' SET user_regdate = ' . $current_time);
// Update forums table
$sql = 'UPDATE ' . $this->forums_table
. ' SET forum_last_poster_name = :poster_name'
. ' WHERE forum_last_poster_name = \'Admin\'';
$this->create_and_execute_prepared_stmt($sql, [
'poster_name' => $this->install_config->get('admin_name'),
]);
$this->exec_sql('UPDATE ' . $this->forums_table . ' SET forum_last_post_time = ' . $current_time);
// Topics table
$sql = 'UPDATE ' . $this->topics_table
. ' SET topic_first_poster_name = :poster_name,'
. ' topic_last_poster_name = :poster_name'
. ' WHERE topic_first_poster_name = \'Admin\''
. ' OR topic_last_poster_name = \'Admin\'';
$this->create_and_execute_prepared_stmt($sql, [
'poster_name' => $this->install_config->get('admin_name'),
]);
$this->exec_sql('UPDATE ' . $this->topics_table
. ' SET topic_time = ' . $current_time . ', topic_last_post_time = ' . $current_time
);
// Posts table
$sql = 'UPDATE ' . $this->posts_table
. ' SET post_time = :post_time,'
. ' poster_ip = :poster_ip';
$this->create_and_execute_prepared_stmt($sql, [
'post_time' => $current_time,
'poster_ip' => $user_ip,
]);
// Moderator cache
$sql = 'UPDATE ' . $this->moderator_cache_table
. ' SET username = :username'
. ' WHERE username = \'Admin\'';
$this->create_and_execute_prepared_stmt($sql, [
'username' => $this->install_config->get('admin_name'),
]);
}
/**
* {@inheritdoc}
*/
static public function get_step_count() : int
{
return 1;
}
/**
* {@inheritdoc}
*/
public function get_task_lang_name() : string
{
return 'TASK_UPDATE_POSTS';
}
}