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 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
obtain_admin_data
0.00% covered (danger)
0.00%
0 / 74
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 / 10
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
75        $admin_data_valid = $this->check_admin_data($admin_name, $admin_pass1, $admin_pass2, $board_email);
76
77        if ($admin_data_valid)
78        {
79            $this->install_config->set('admin_name', $admin_name);
80            $this->install_config->set('admin_passwd', $admin_pass1);
81            $this->install_config->set('board_email', $board_email);
82        }
83        else
84        {
85            $this->request_form_data(true);
86        }
87    }
88
89    /**
90     * Request data from the user
91     *
92     * @param bool $use_request_data Whether to use submited data
93     *
94     * @throws user_interaction_required_exception When the user is required to provide data
95     */
96    protected function request_form_data($use_request_data = false)
97    {
98        if ($use_request_data)
99        {
100            $admin_username    = $this->io_handler->get_input('admin_name', '', true);
101            $admin_email    = $this->io_handler->get_input('board_email', '', true);
102        }
103        else
104        {
105            $admin_username    = '';
106            $admin_email    = '';
107        }
108
109        $admin_form = array(
110            'admin_name'    => array(
111                'label'            => 'ADMIN_USERNAME',
112                'description'    => 'ADMIN_USERNAME_EXPLAIN',
113                'type'            => 'text',
114                'default'        => $admin_username,
115            ),
116            'board_email'    => array(
117                'label'        => 'CONTACT_EMAIL',
118                'type'        => 'email',
119                'default'    => $admin_email,
120            ),
121            'admin_pass1'    => array(
122                'label'            => 'ADMIN_PASSWORD',
123                'description'    => 'ADMIN_PASSWORD_EXPLAIN',
124                'type'            => 'password',
125            ),
126            'admin_pass2'    => array(
127                'label'    => 'ADMIN_PASSWORD_CONFIRM',
128                'type'    => 'password',
129            ),
130            'submit_admin'    => array(
131                'label'    => 'SUBMIT',
132                'type'    => 'submit',
133            ),
134        );
135
136        $this->io_handler->add_user_form_group('ADMIN_CONFIG', $admin_form);
137
138        // Require user interaction
139        throw new user_interaction_required_exception();
140    }
141
142    /**
143     * Check admin data
144     *
145     * @param string    $username    Admin username
146     * @param string    $pass1        Admin password
147     * @param string    $pass2        Admin password confirmation
148     * @param string    $email        Admin e-mail address
149     *
150     * @return bool    True if data is valid, false otherwise
151     */
152    protected function check_admin_data($username, $pass1, $pass2, $email)
153    {
154        $data_valid = true;
155
156        // Check if none of admin data is empty
157        if (in_array('', array($username, $pass1, $pass2, $email), true))
158        {
159            $this->io_handler->add_error_message('INST_ERR_MISSING_DATA');
160            $data_valid = false;
161        }
162
163        if (utf8_strlen($username) < 3)
164        {
165            $this->io_handler->add_error_message('INST_ERR_USER_TOO_SHORT');
166            $data_valid = false;
167        }
168
169        if (utf8_strlen($username) > 20)
170        {
171            $this->io_handler->add_error_message('INST_ERR_USER_TOO_LONG');
172            $data_valid = false;
173        }
174
175        if ($pass1 !== $pass2 && $pass1 !== '')
176        {
177            $this->io_handler->add_error_message('INST_ERR_PASSWORD_MISMATCH');
178            $data_valid = false;
179        }
180
181        // Test against the default password rules
182        if (utf8_strlen($pass1) < 6)
183        {
184            $this->io_handler->add_error_message('INST_ERR_PASSWORD_TOO_SHORT');
185            $data_valid = false;
186        }
187
188        if (utf8_strlen($pass1) > 30)
189        {
190            $this->io_handler->add_error_message('INST_ERR_PASSWORD_TOO_LONG');
191            $data_valid = false;
192        }
193
194        if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
195        {
196            $this->io_handler->add_error_message('INST_ERR_EMAIL_INVALID');
197            $data_valid = false;
198        }
199
200        return $data_valid;
201    }
202
203    /**
204     * {@inheritdoc}
205     */
206    public static function get_step_count()
207    {
208        return 0;
209    }
210
211    /**
212     * {@inheritdoc}
213     */
214    public function get_task_lang_name()
215    {
216        return '';
217    }
218}