aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/install/module/obtain_data/task/obtain_admin_data.php
blob: b2250e524b0e431f479281b5a6b318d787feae7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?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\obtain_data\task;

use phpbb\install\exception\user_interaction_required_exception;

/**
 * This class requests and validates admin account data from the user
 */
class obtain_admin_data extends \phpbb\install\task_base implements \phpbb\install\task_interface
{
	/**
	 * @var \phpbb\install\helper\config
	 */
	protected $install_config;

	/**
	 * @var \phpbb\install\helper\iohandler\iohandler_interface
	 */
	protected $io_handler;

	/**
	 * Constructor
	 *
	 * @param \phpbb\install\helper\config							$install_config	Installer's config helper
	 * @param \phpbb\install\helper\iohandler\iohandler_interface	$iohandler		Installer's input-output handler
	 */
	public function __construct(\phpbb\install\helper\config $install_config,
								\phpbb\install\helper\iohandler\iohandler_interface $iohandler)
	{
		$this->install_config	= $install_config;
		$this->io_handler		= $iohandler;

		parent::__construct(true);
	}

	/**
	 * {@inheritdoc}
	 */
	public function run()
	{
		// Check if data is sent
		if ($this->io_handler->get_input('submit_admin', false))
		{
			$this->process_form();
		}
		else
		{
			$this->request_form_data();
		}
	}

	/**
	 * Process form data
	 */
	protected function process_form()
	{
		// Admin data
		$admin_name		= $this->io_handler->get_input('admin_name', '', true);
		$admin_pass1	= $this->io_handler->get_input('admin_pass1', '', true);
		$admin_pass2	= $this->io_handler->get_input('admin_pass2', '', true);
		$board_email	= $this->io_handler->get_input('board_email', '');

		$admin_data_valid = $this->check_admin_data($admin_name, $admin_pass1, $admin_pass2, $board_email);

		if ($admin_data_valid)
		{
			$this->install_config->set('admin_name', $admin_name);
			$this->install_config->set('admin_passwd', $admin_pass1);
			$this->install_config->set('board_email', $board_email);
		}
		else
		{
			$this->request_form_data(true);
		}
	}

	/**
	 * Request data from the user
	 *
	 * @param bool $use_request_data Whether to use submited data
	 *
	 * @throws \phpbb\install\exception\user_interaction_required_exception When the user is required to provide data
	 */
	protected function request_form_data($use_request_data = false)
	{
		if ($use_request_data)
		{
			$admin_username	= $this->io_handler->get_input('admin_name', '', true);
			$admin_email	= $this->io_handler->get_input('board_email', '', true);
		}
		else
		{
			$admin_username	= '';
			$admin_email	= '';
		}

		$admin_form = array(
			'admin_name'	=> array(
				'label'			=> 'ADMIN_USERNAME',
				'description'	=> 'ADMIN_USERNAME_EXPLAIN',
				'type'			=> 'text',
				'default'		=> $admin_username,
			),
			'board_email'	=> array(
				'label'		=> 'CONTACT_EMAIL',
				'type'		=> 'email',
				'default'	=> $admin_email,
			),
			'admin_pass1'	=> array(
				'label'			=> 'ADMIN_PASSWORD',
				'description'	=> 'ADMIN_PASSWORD_EXPLAIN',
				'type'			=> 'password',
			),
			'admin_pass2'	=> array(
				'label'	=> 'ADMIN_PASSWORD_CONFIRM',
				'type'	=> 'password',
			),
			'submit_admin'	=> array(
				'label'	=> 'SUBMIT',
				'type'	=> 'submit',
			),
		);

		$this->io_handler->add_user_form_group('ADMIN_CONFIG', $admin_form);

		// Require user interaction
		$this->io_handler->send_response();
		throw new user_interaction_required_exception();
	}

	/**
	 * Check admin data
	 *
	 * @param string	$username	Admin username
	 * @param string	$pass1		Admin password
	 * @param string	$pass2		Admin password confirmation
	 * @param string	$email		Admin e-mail address
	 *
	 * @return bool	True if data is valid, false otherwise
	 */
	protected function check_admin_data($username, $pass1, $pass2, $email)
	{
		$data_valid = true;

		// Check if none of admin data is empty
		if (in_array('', array($username, $pass1, $pass2, $email)))
		{
			$this->io_handler->add_error_message('INST_ERR_MISSING_DATA');
			$data_valid = false;
		}

		if (utf8_strlen($username) < 3)
		{
			$this->io_handler->add_error_message('INST_ERR_USER_TOO_SHORT');
			$data_valid = false;
		}

		if (utf8_strlen($username) > 20)
		{
			$this->io_handler->add_error_message('INST_ERR_USER_TOO_LONG');
			$data_valid = false;
		}

		if ($pass1 !== $pass2 && $pass1 !== '')
		{
			$this->io_handler->add_error_message('INST_ERR_PASSWORD_MISMATCH');
			$data_valid = false;
		}

		// Test against the default password rules
		if (utf8_strlen($pass1) < 6)
		{
			$this->io_handler->add_error_message('INST_ERR_PASSWORD_TOO_SHORT');
			$data_valid = false;
		}

		if (utf8_strlen($pass1) > 30)
		{
			$this->io_handler->add_error_message('INST_ERR_PASSWORD_TOO_LONG');
			$data_valid = false;
		}

		if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email))
		{
			$this->io_handler->add_error_message('INST_ERR_EMAIL_INVALID');
			$data_valid = false;
		}

		return $data_valid;
	}

	/**
	 * {@inheritdoc}
	 */
	static public function get_step_count()
	{
		return 0;
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_task_lang_name()
	{
		return '';
	}
}