aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/install/module/obtain_data/task/obtain_database_data.php
blob: 0c1146d9f5a6750011409c480aa81355026a9d1e (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?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 database information from the user
 */
class obtain_database_data extends \phpbb\install\task_base implements \phpbb\install\task_interface
{
	/**
	 * @var \phpbb\install\helper\database
	 */
	protected $database_helper;

	/**
	 * @var \phpbb\install\helper\config
	 */
	protected $install_config;

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

	/**
	 * Constructor
	 *
	 * @param \phpbb\install\helper\database						$database_helper	Installer's database helper
	 * @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\database $database_helper,
								\phpbb\install\helper\config $install_config,
								\phpbb\install\helper\iohandler\iohandler_interface $iohandler)
	{
		$this->database_helper	= $database_helper;
		$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_database', false))
		{
			$this->process_form();
		}
		else
		{
			$this->request_form_data();
		}
	}

	/**
	 * Process form data
	 */
	protected function process_form()
	{
		// Collect database data
		$dbms			= $this->io_handler->get_input('dbms', '');
		$dbhost			= $this->io_handler->get_input('dbhost', '');
		$dbport			= $this->io_handler->get_input('dbport', '');
		$dbuser			= $this->io_handler->get_input('dbuser', '');
		$dbpasswd		= $this->io_handler->get_input('dbpasswd', '', true);
		$dbname			= $this->io_handler->get_input('dbname', '');
		$table_prefix	= $this->io_handler->get_input('table_prefix', '');

		// Check database data
		$user_data_vaild = $this->check_database_data($dbms, $dbhost, $dbport, $dbuser, $dbpasswd, $dbname, $table_prefix);

		// Save database data if it is correct
		if ($user_data_vaild)
		{
			$this->install_config->set('dbms', $dbms);
			$this->install_config->set('dbhost', $dbhost);
			$this->install_config->set('dbport', $dbport);
			$this->install_config->set('dbuser', $dbuser);
			$this->install_config->set('dbpasswd', $dbpasswd);
			$this->install_config->set('dbname', $dbname);
			$this->install_config->set('table_prefix', $table_prefix);
		}
		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)
		{
			$dbms			= $this->io_handler->get_input('dbms', '');
			$dbhost			= $this->io_handler->get_input('dbhost', '');
			$dbport			= $this->io_handler->get_input('dbport', '');
			$dbuser			= $this->io_handler->get_input('dbuser', '');
			$dbname			= $this->io_handler->get_input('dbname', '');
			$table_prefix	= $this->io_handler->get_input('table_prefix', 'phpbb_');
		}
		else
		{
			$dbms			= '';
			$dbhost			= '';
			$dbport			= '';
			$dbuser			= '';
			$dbname			= '';
			$table_prefix	= 'phpbb_';
		}

		$dbms_select = array();
		foreach ($this->database_helper->get_available_dbms() as $dbms_key => $dbms_array)
		{
			$dbms_select[] = array(
				'value'		=> $dbms_key,
				'label'		=> 'DB_OPTION_' . strtoupper($dbms_key),
				'selected'	=> ($dbms_key === $dbms),
			);
		}

		$database_form = array(
			'dbms' => array(
				'label'		=> 'DBMS',
				'type'		=> 'select',
				'options'	=> $dbms_select,
			),
			'dbhost' => array(
				'label'			=> 'DB_HOST',
				'description'	=> 'DB_HOST_EXPLAIN',
				'type'			=> 'text',
				'default'		=> $dbhost,
			),
			'dbport' => array(
				'label'			=> 'DB_PORT',
				'description'	=> 'DB_PORT_EXPLAIN',
				'type'			=> 'text',
				'default'		=> $dbport,
			),
			'dbuser' => array(
				'label'		=> 'DB_USERNAME',
				'type'		=> 'text',
				'default'	=> $dbuser,
			),
			'dbpasswd' => array(
				'label'		=> 'DB_PASSWORD',
				'type'	=> 'password',
			),
			'dbname' => array(
				'label'		=> 'DB_NAME',
				'type'		=> 'text',
				'default'	=> $dbname,
			),
			'table_prefix' => array(
				'label'			=> 'TABLE_PREFIX',
				'description'	=> 'TABLE_PREFIX_EXPLAIN',
				'type'			=> 'text',
				'default'		=> $table_prefix,
			),
			'submit_database' => array(
				'label'	=> 'SUBMIT',
				'type'	=> 'submit',
			),
		);

		$this->io_handler->add_user_form_group('DB_CONFIG', $database_form);

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

	/**
	 * Check database data
	 *
	 * @param string	$dbms			Selected database type
	 * @param string	$dbhost			Database host address
	 * @param int		$dbport			Database port number
	 * @param string	$dbuser			Database username
	 * @param string	$dbpass			Database password
	 * @param string	$dbname			Database name
	 * @param string	$table_prefix	Database table prefix
	 *
	 * @return bool	True if database data is correct, false otherwise
	 */
	protected function check_database_data($dbms, $dbhost, $dbport, $dbuser, $dbpass, $dbname, $table_prefix)
	{
		$available_dbms = $this->database_helper->get_available_dbms();
		$data_valid = true;

		// Check if PHP has the database extensions for the specified DBMS
		if (!isset($available_dbms[$dbms]))
		{
			$this->io_handler->add_error_message('INST_ERR_NO_DB');
			$data_valid = false;
		}

		// Validate table prefix
		$prefix_valid = $this->database_helper->validate_table_prefix($dbms, $table_prefix);
		if (is_array($prefix_valid))
		{
			foreach ($prefix_valid as $error)
			{
				$this->io_handler->add_error_message(
					$error['title'],
					(isset($error['description'])) ? $error['description'] : false
				);
			}

			$data_valid = false;
		}

		// Try to connect to database if all provided data is valid
		if ($data_valid)
		{
			$connect_test = $this->database_helper->check_database_connection($dbms, $dbhost, $dbport, $dbuser, $dbpass, $dbname, $table_prefix);
			if (is_array($connect_test))
			{
				foreach ($prefix_valid as $error)
				{
					$this->io_handler->add_error_message(
						$error['title'],
						(isset($error['description'])) ? $error['description'] : false
					);
				}

				$data_valid = false;
			}
		}

		return $data_valid;
	}

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

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