Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
update_user_and_post_data
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
20
 get_step_count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_task_lang_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\install\module\install_database\task;
15
16use phpbb\install\database_task;
17use phpbb\install\exception\resource_limit_reached_exception;
18use phpbb\install\helper\config;
19use phpbb\install\helper\container_factory;
20use phpbb\install\helper\database;
21use phpbb\install\helper\iohandler\iohandler_interface;
22use phpbb\language\language;
23
24/**
25 * Update the admin's info as well as the welcome post.
26 */
27class update_user_and_post_data extends database_task
28{
29    /** @var config */
30    private $install_config;
31
32    private $iohandler;
33
34    /** @var language */
35    private $language;
36
37    /** @var \phpbb\passwords\manager */
38    private $password_manager;
39
40    /** @var string */
41    private $forums_table;
42
43    /** @var string */
44    private $moderator_cache_table;
45
46    /** @var string */
47    private $posts_table;
48
49    /** @var string */
50    private $topics_table;
51
52    /** @var string */
53    private $user_table;
54
55    /**
56     * Constructor.
57     *
58     * @param config                $install_config
59     * @param container_factory        $container
60     * @param database                $db_helper
61     * @param iohandler_interface    $iohandler
62     * @param language                $language
63     */
64    public function __construct(
65        config $install_config,
66        container_factory $container,
67        database $db_helper,
68        iohandler_interface $iohandler,
69        language $language)
70    {
71        $this->install_config    = $install_config;
72        $this->iohandler        = $iohandler;
73        $this->language            = $language;
74        $this->password_manager    = $container->get('passwords.manager');
75
76        $this->forums_table                = $container->get_parameter('tables.forums');
77        $this->moderator_cache_table    = $container->get_parameter('tables.moderator_cache');
78        $this->posts_table                = $container->get_parameter('tables.posts');
79        $this->topics_table                = $container->get_parameter('tables.topics');
80        $this->user_table                = $container->get_parameter('tables.users');
81
82        parent::__construct(
83            self::get_doctrine_connection($db_helper, $install_config),
84            $this->iohandler,
85            true
86        );
87    }
88
89    /**
90     * {@inheritdoc}
91     */
92    public function run()
93    {
94        // Force a refresh.
95        $count = $this->install_config->get('correct_user_and_post_data_count');
96        if ($count === false)
97        {
98            if ($this->install_config->get_time_remaining() < 5)
99            {
100                $this->install_config->set('correct_user_and_post_data_count', 1);
101                throw new resource_limit_reached_exception();
102            }
103        }
104
105        $user_ip = phpbb_ip_normalise($this->iohandler->get_server_variable('REMOTE_ADDR'));
106        $user_ip = ($user_ip === false) ? '' : $user_ip;
107        $current_time = $this->install_config->get('install_board_time', time());
108
109        // Update user data
110        $sql = 'UPDATE ' . $this->user_table
111            . ' SET username = :username,'
112            . '        user_password = :password,'
113            . '        user_ip = :ip,'
114            . '        user_lang = :lang,'
115            . '        user_email = :email,'
116            . '        user_dateformat = :dateformat,'
117            . '        username_clean = :clean_username'
118            . ' WHERE username = \'Admin\'';
119
120        $this->create_and_execute_prepared_stmt($sql, [
121            'username'            => $this->install_config->get('admin_name'),
122            'password'            => $this->password_manager->hash($this->install_config->get('admin_passwd')),
123            'ip'                => $user_ip,
124            'lang'                => $this->install_config->get('user_language', 'en'),
125            'email'                => $this->install_config->get('board_email'),
126            'dateformat'        => $this->language->lang('default_dateformat'),
127            'clean_username'    => utf8_clean_string($this->install_config->get('admin_name')),
128        ]);
129        $this->exec_sql('UPDATE ' . $this->user_table . ' SET user_regdate = ' . $current_time);
130
131        // Update forums table
132        $sql = 'UPDATE ' . $this->forums_table
133            . ' SET forum_last_poster_name = :poster_name'
134            . ' WHERE forum_last_poster_name = \'Admin\'';
135        $this->create_and_execute_prepared_stmt($sql, [
136            'poster_name' => $this->install_config->get('admin_name'),
137        ]);
138        $this->exec_sql('UPDATE ' . $this->forums_table . ' SET forum_last_post_time = ' . $current_time);
139
140        // Topics table
141        $sql = 'UPDATE ' . $this->topics_table
142            . '    SET topic_first_poster_name = :poster_name,'
143            . '        topic_last_poster_name = :poster_name'
144            . ' WHERE topic_first_poster_name = \'Admin\''
145            . '    OR topic_last_poster_name = \'Admin\'';
146        $this->create_and_execute_prepared_stmt($sql, [
147            'poster_name' => $this->install_config->get('admin_name'),
148        ]);
149        $this->exec_sql('UPDATE ' . $this->topics_table
150            . ' SET topic_time = ' . $current_time . ', topic_last_post_time = ' . $current_time
151        );
152
153        // Posts table
154        $sql = 'UPDATE ' . $this->posts_table
155            . ' SET post_time = :post_time,'
156            . '        poster_ip = :poster_ip';
157        $this->create_and_execute_prepared_stmt($sql, [
158            'post_time' => $current_time,
159            'poster_ip' => $user_ip,
160        ]);
161
162        // Moderator cache
163        $sql = 'UPDATE ' . $this->moderator_cache_table
164            . ' SET username = :username'
165            . ' WHERE username =  \'Admin\'';
166        $this->create_and_execute_prepared_stmt($sql, [
167            'username' => $this->install_config->get('admin_name'),
168        ]);
169    }
170
171    /**
172     * {@inheritdoc}
173     */
174    public static function get_step_count() : int
175    {
176        return 1;
177    }
178
179    /**
180     * {@inheritdoc}
181     */
182    public function get_task_lang_name() : string
183    {
184        return 'TASK_UPDATE_POSTS';
185    }
186}