aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/install/installer.php
blob: e04e233a76461496632bb023b8da185dcc92779d (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?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;

use phpbb\cache\driver\driver_interface;
use phpbb\di\ordered_service_collection;
use phpbb\install\exception\cannot_build_container_exception;
use phpbb\install\exception\installer_config_not_writable_exception;
use phpbb\install\exception\jump_to_restart_point_exception;
use phpbb\install\exception\resource_limit_reached_exception;
use phpbb\install\exception\user_interaction_required_exception;
use phpbb\install\helper\config;
use phpbb\install\helper\container_factory;
use phpbb\install\helper\iohandler\ajax_iohandler;
use phpbb\install\helper\iohandler\cli_iohandler;
use phpbb\install\helper\iohandler\iohandler_interface;
use phpbb\path_helper;

class installer
{
	/**
	 * @var driver_interface
	 */
	protected $cache;

	/**
	 * @var container_factory
	 */
	protected $container_factory;

	/**
	 * @var config
	 */
	protected $install_config;

	/**
	 * @var ordered_service_collection
	 */
	protected $installer_modules;

	/**
	 * @var iohandler_interface
	 */
	protected $iohandler;

	/**
	 * @var string
	 */
	protected $web_root;

	/**
	 * Stores the number of steps that a given module has
	 *
	 * @var array
	 */
	protected $module_step_count;

	/**
	 * @var bool
	 */
	protected $purge_cache_before;

	/**
	 * Constructor
	 *
	 * @param driver_interface	$cache			Cache service
	 * @param config			$config			Installer config handler
	 * @param path_helper		$path_helper	Path helper
	 * @param container_factory	$container		Container
	 */
	public function __construct(driver_interface $cache, config $config, path_helper $path_helper, container_factory $container)
	{
		$this->cache				= $cache;
		$this->install_config		= $config;
		$this->container_factory	= $container;
		$this->installer_modules	= null;
		$this->web_root				= $path_helper->get_web_root_path();
		$this->purge_cache_before	= false;
	}

	/**
	 * Sets modules to execute
	 *
	 * Note: The installer will run modules in the order they are set in
	 * the array.
	 *
	 * @param ordered_service_collection	$modules	Service collection of module service names
	 */
	public function set_modules(ordered_service_collection $modules)
	{
		$this->installer_modules = $modules;
	}

	/**
	 * Sets input-output handler objects
	 *
	 * @param iohandler_interface	$iohandler
	 */
	public function set_iohandler(iohandler_interface $iohandler)
	{
		$this->iohandler = $iohandler;
	}

	/**
	 * Sets whether to purge cache before the installation process
	 *
	 * @param bool	$purge_cache_before
	 */
	public function set_purge_cache_before($purge_cache_before)
	{
		$this->purge_cache_before = $purge_cache_before;
	}

	/**
	 * Run phpBB installer
	 */
	public function run()
	{
		if ($this->iohandler instanceof ajax_iohandler)
		{
			$this->iohandler->acquire_lock();
		}

		// Load install progress
		$this->install_config->load_config();

		if (!$this->install_config->get('cache_purged_before', false) && $this->purge_cache_before)
		{
			/** @var \phpbb\cache\driver\driver_interface $cache */
			$cache = $this->container_factory->get('cache.driver');
			$cache->purge();
			$this->install_config->set('cache_purged_before', true);
		}

		// Recover install progress
		$module_index = $this->recover_progress();

		// Variable used to check if the install process have been finished
		$install_finished	= false;
		$fail_cleanup		= false;
		$send_refresh		= false;

		// We are installing something, so the introduction stage can go now...
		$this->install_config->set_finished_navigation_stage(array('install', 0, 'introduction'));
		$this->iohandler->set_finished_stage_menu(array('install', 0, 'introduction'));

		if ($this->install_config->get_task_progress_count() === 0)
		{
			// Count all tasks in the current installer modules
			$step_count = 0;

			/** @var \phpbb\install\module_interface $module */
			foreach ($this->installer_modules as $name => $module)
			{
				$module_step_count = $module->get_step_count();
				$step_count += $module_step_count;
				$this->module_step_count[$name] = $module_step_count;
			}

			// Set task count
			$this->install_config->set_task_progress_count($step_count);
		}

		// Set up progress information
		$this->iohandler->set_task_count(
			$this->install_config->get_task_progress_count()
		);

		try
		{
			$iterator = $this->installer_modules->getIterator();

			if ($module_index < $iterator->count())
			{
				$iterator->seek($module_index);
			}
			else
			{
				$iterator->seek($module_index - 1);
				$iterator->next();
			}

			while ($iterator->valid())
			{
				$module	= $iterator->current();
				$name	= $iterator->key();

				// Check if module should be executed
				if (!$module->is_essential() && !$module->check_requirements())
				{
					$this->install_config->set_finished_navigation_stage($module->get_navigation_stage_path());
					$this->iohandler->set_finished_stage_menu($module->get_navigation_stage_path());

					$this->iohandler->add_log_message(array(
						'SKIP_MODULE',
						$name,
					));
					$this->install_config->increment_current_task_progress($this->module_step_count[$name]);
				}
				else
				{
					// Set the correct stage in the navigation bar
					$this->install_config->set_active_navigation_stage($module->get_navigation_stage_path());
					$this->iohandler->set_active_stage_menu($module->get_navigation_stage_path());

					$this->iohandler->send_response();

					$module->run();

					$this->install_config->set_finished_navigation_stage($module->get_navigation_stage_path());
					$this->iohandler->set_finished_stage_menu($module->get_navigation_stage_path());
				}

				$module_index++;
				$iterator->next();

				// Save progress
				$this->install_config->set_active_module($name, $module_index);

				if ($iterator->valid() && ($this->install_config->get_time_remaining() <= 0 || $this->install_config->get_memory_remaining() <= 0))
				{
					throw new resource_limit_reached_exception();
				}
			}

			// Installation finished
			$install_finished = true;

			if ($this->iohandler instanceof cli_iohandler)
			{
				$this->iohandler->add_success_message('INSTALLER_FINISHED');
			}
			else
			{
				// Start session if not installing and get user object
				// to allow redirecting to ACP
				$user = $this->container_factory->get('user');
				if (!isset($module) || !($module instanceof \phpbb\install\module\install_finish\module))
				{
					$auth = $this->container_factory->get('auth');

					$user->session_begin();
					$auth->acl($user->data);
					$user->setup();
				}

				$phpbb_root_path = $this->container_factory->get_parameter('core.root_path');

				$acp_url = append_sid($phpbb_root_path . 'adm/index.php', 'i=acp_help_phpbb&mode=help_phpbb', true, $user->session_id);
				$this->iohandler->add_success_message('INSTALLER_FINISHED', array(
					'ACP_LINK',
					$acp_url,
				));
			}
		}
		catch (user_interaction_required_exception $e)
		{
			$this->iohandler->send_response(true);
		}
		catch (resource_limit_reached_exception $e)
		{
			$send_refresh = true;
		}
		catch (jump_to_restart_point_exception $e)
		{
			$this->install_config->jump_to_restart_point($e->get_restart_point_name());
			$send_refresh = true;
		}
		catch (\Exception $e)
		{
			$this->iohandler->add_error_message($e->getMessage());
			$this->iohandler->send_response(true);
			$fail_cleanup = true;
		}

		if ($this->iohandler instanceof ajax_iohandler)
		{
			$this->iohandler->release_lock();
		}

		if ($install_finished)
		{
			// Send install finished message
			$this->iohandler->set_progress('INSTALLER_FINISHED', $this->install_config->get_task_progress_count());
			$this->iohandler->send_response(true);
		}
		else if ($send_refresh)
		{
			$this->iohandler->request_refresh();
			$this->iohandler->send_response(true);
		}

		// Save install progress
		try
		{
			if ($install_finished || $fail_cleanup)
			{
				$this->install_config->clean_up_config_file();
				$this->cache->purge();

				try
				{
					/** @var \phpbb\cache\driver\driver_interface $cache */
					$cache = $this->container_factory->get('cache.driver');
					$cache->purge();
				}
				catch (cannot_build_container_exception $e)
				{
					// Do not do anything, this just means there is no config.php yet
				}
			}
			else
			{
				$this->install_config->save_config();
			}
		}
		catch (installer_config_not_writable_exception $e)
		{
			// It is allowed to fail this test during requirements testing
			$progress_data = $this->install_config->get_progress_data();

			if ($progress_data['last_task_module_name'] !== 'installer.module.requirements_install')
			{
				$this->iohandler->add_error_message('INSTALLER_CONFIG_NOT_WRITABLE');
			}
		}
	}

	/**
	 * Recover install progress
	 *
	 * @return string	Index of the next installer module to execute
	 */
	protected function recover_progress()
	{
		$progress_array = $this->install_config->get_progress_data();
		return $progress_array['last_task_module_index'];
	}
}