Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
obtain_admin_data
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 7
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 process_form
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 request_form_data
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
6
 check_admin_data
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
90
 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\obtain_data\task;
15
16use phpbb\install\exception\user_interaction_required_exception;
17
18/**
19 * This class requests and validates admin account data from the user
20 */
21class obtain_admin_data extends \phpbb\install\task_base implements \phpbb\install\task_interface
22{
23    /**
24     * @var \phpbb\install\helper\config
25     */
26    protected $install_config;
27
28    /**
29     * @var \phpbb\install\helper\iohandler\iohandler_interface
30     */
31    protected $io_handler;
32
33    /**
34     * Constructor
35     *
36     * @param \phpbb\install\helper\config                            $install_config    Installer's config helper
37     * @param \phpbb\install\helper\iohandler\iohandler_interface    $iohandler        Installer's input-output handler
38     */
39    public function __construct(\phpbb\install\helper\config $install_config,
40                                \phpbb\install\helper\iohandler\iohandler_interface $iohandler)
41    {
42        $this->install_config    = $install_config;
43        $this->io_handler        = $iohandler;
44
45        parent::__construct(true);
46    }
47
48    /**
49     * {@inheritdoc}
50     */
51    public function run()
52    {
53        // Check if data is sent
54        if ($this->io_handler->get_input('submit_admin', false))
55        {
56            $this->process_form();
57        }
58        else
59        {
60            $this->request_form_data();
61        }
62    }
63
64    /**
65     * Process form data
66     */
67    protected function process_form()
68    {
69        // Admin data
70        $admin_name        = $this->io_handler->get_input('admin_name', '', true);
71        $admin_pass1    = $this->io_handler->get_input('admin_pass1', '', true);
72        $admin_pass2    = $this->io_handler->get_input('admin_pass2', '', true);
73        $board_email    = $this->io_handler->get_input('board_email', '', true);
74        $admin_timezone    = $this->io_handler->get_input('admin_timezone', 'UTC', true);
75
76        $admin_data_valid = $this->check_admin_data($admin_name, $admin_pass1, $admin_pass2, $board_email);
77
78        if ($admin_data_valid)
79        {
80            $this->install_config->set('admin_name', $admin_name);
81            $this->install_config->set('admin_passwd', $admin_pass1);
82            $this->install_config->set('board_email', $board_email);
83            $this->install_config->set('admin_timezone', $admin_timezone);
84        }
85        else
86        {
87            $this->request_form_data(true);
88        }
89    }
90
91    /**
92     * Request data from the user
93     *
94     * @param bool $use_request_data Whether to use submited data
95     *
96     * @throws user_interaction_required_exception When the user is required to provide data
97     */
98    protected function request_form_data($use_request_data = false)
99    {
100        if ($use_request_data)
101        {
102            $admin_username    = $this->io_handler->get_input('admin_name', '', true);
103            $admin_email    = $this->io_handler->get_input('board_email', '', true);
104        }
105        else
106        {
107            $admin_username    = '';
108            $admin_email    = '';
109        }
110
111        $admin_form = array(
112            'admin_name'    => array(
113                'label'            => 'ADMIN_USERNAME',
114                'description'    => 'ADMIN_USERNAME_EXPLAIN',
115                'type'            => 'text',
116                'default'        => $admin_username,
117            ),
118            'board_email'    => array(
119                'label'        => 'CONTACT_EMAIL',
120                'type'        => 'email',
121                'default'    => $admin_email,
122            ),
123            'admin_pass1'    => array(
124                'label'            => 'ADMIN_PASSWORD',
125                'description'    => 'ADMIN_PASSWORD_EXPLAIN',
126                'type'            => 'password',
127            ),
128            'admin_pass2'    => array(
129                'label'    => 'ADMIN_PASSWORD_CONFIRM',
130                'type'    => 'password',
131            ),
132            'submit_admin'    => array(
133                'label'    => 'SUBMIT',
134                'type'    => 'submit',
135            ),
136        );
137
138        $this->io_handler->add_user_form_group('ADMIN_CONFIG', $admin_form);
139
140        // Require user interaction
141        throw new user_interaction_required_exception();
142    }
143
144    /**
145     * Check admin data
146     *
147     * @param string    $username    Admin username
148     * @param string    $pass1        Admin password
149     * @param string    $pass2        Admin password confirmation
150     * @param string    $email        Admin e-mail address
151     *
152     * @return bool    True if data is valid, false otherwise
153     */
154    protected function check_admin_data($username, $pass1, $pass2, $email)
155    {
156        $data_valid = true;
157
158        // Check if none of admin data is empty
159        if (in_array('', array($username, $pass1, $pass2, $email), true))
160        {
161            $this->io_handler->add_error_message('INST_ERR_MISSING_DATA');
162            $data_valid = false;
163        }
164
165        if (utf8_strlen($username) < 3)
166        {
167            $this->io_handler->add_error_message('INST_ERR_USER_TOO_SHORT');
168            $data_valid = false;
169        }
170
171        if (utf8_strlen($username) > 20)
172        {
173            $this->io_handler->add_error_message('INST_ERR_USER_TOO_LONG');
174            $data_valid = false;
175        }
176
177        if ($pass1 !== $pass2 && $pass1 !== '')
178        {
179            $this->io_handler->add_error_message('INST_ERR_PASSWORD_MISMATCH');
180            $data_valid = false;
181        }
182
183        // Test against the default password rules
184        if (utf8_strlen($pass1) < 6)
185        {
186            $this->io_handler->add_error_message('INST_ERR_PASSWORD_TOO_SHORT');
187            $data_valid = false;
188        }
189
190        if (utf8_strlen($pass1) > 30)
191        {
192            $this->io_handler->add_error_message('INST_ERR_PASSWORD_TOO_LONG');
193            $data_valid = false;
194        }
195
196        if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
197        {
198            $this->io_handler->add_error_message('INST_ERR_EMAIL_INVALID');
199            $data_valid = false;
200        }
201
202        return $data_valid;
203    }
204
205    /**
206     * {@inheritdoc}
207     */
208    public static function get_step_count()
209    {
210        return 0;
211    }
212
213    /**
214     * {@inheritdoc}
215     */
216    public function get_task_lang_name()
217    {
218        return '';
219    }
220}