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
351
|
<?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\helper;
use phpbb\install\exception\installer_config_not_writable_exception;
/**
* Stores common settings and installation status
*/
class config
{
/**
* @var \phpbb\filesystem\filesystem_interface
*/
protected $filesystem;
/**
* Array which contains config settings for the installer
*
* The array will also store all the user input, as well as any
* data that is passed to other tasks by a task.
*
* @var array
*/
protected $installer_config;
/**
* @var string
*/
protected $install_config_file;
/**
* @var \phpbb\php\ini
*/
protected $php_ini;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* Array containing progress information
*
* @var array
*/
protected $progress_data;
/**
* Array containing system information
*
* The array contains run time and memory limitations.
*
* @var array
*/
protected $system_data;
/**
* Array containing navigation bar information
*
* @var array
*/
protected $navigation_data;
/**
* Constructor
*/
public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, \phpbb\php\ini $php_ini, $phpbb_root_path)
{
$this->filesystem = $filesystem;
$this->php_ini = $php_ini;
$this->phpbb_root_path = $phpbb_root_path;
// Set up data arrays
$this->navigation_data = array();
$this->installer_config = array();
$this->system_data = array();
$this->progress_data = array(
'last_task_module_index' => 0,
'last_task_module_name' => '', // Stores the service name of the latest finished module
'last_task_index' => 0,
'last_task_name' => '', // Stores the service name of the latest finished task
'max_task_progress' => 0,
'current_task_progress' => 0,
);
$this->install_config_file = $this->phpbb_root_path . 'store/install_config.php';
$this->setup_system_data();
}
/**
* Returns data for a specified parameter
*
* @param string $param_name Name of the parameter to return
* @param mixed $default Default value to return when the specified data
* does not exist.
*
* @return mixed value of the specified parameter or the default value if the data
* cannot be recovered.
*/
public function get($param_name, $default = false)
{
return (isset($this->installer_config[$param_name])) ? $this->installer_config[$param_name] : $default;
}
/**
* Sets a parameter in installer_config
*
* @param string $param_name Name of the parameter
* @param mixed $value Values to set the parameter
*/
public function set($param_name, $value)
{
$this->installer_config = array_merge($this->installer_config, array(
$param_name => $value,
));
}
/**
* Returns system parameter
*
* @param string $param_name Name of the parameter
*
* @return mixed Returns system parameter if it is defined, false otherwise
*/
public function system_get($param_name)
{
return (isset($this->system_data[$param_name])) ? $this->system_data[$param_name] : false;
}
/**
* Returns remaining time until the run time limit
*
* @return int Remaining time until the run time limit in seconds
*/
public function get_time_remaining()
{
return ($this->system_data['start_time'] + $this->system_data['max_execution_time']) - time();
}
/**
* Returns remaining memory available for PHP
*
* @return int Remaining memory until reaching the limit
*/
public function get_memory_remaining()
{
if (function_exists('memory_get_usage'))
{
return ($this->system_data['memory_limit'] - memory_get_usage());
}
// If we cannot get the information then just return a positive number (and cross fingers)
return 1;
}
/**
* Saves the latest executed task
*
* @param string $task_service_name Name of the installer task service
* @param int $task_index Index of the task in the task list array
*/
public function set_finished_task($task_service_name, $task_index)
{
$this->progress_data['last_task_name'] = $task_service_name;
$this->progress_data['last_task_index'] = $task_index;
}
/**
* Set active module
*
* @param string $module_service_name Name of the installer module service
* @param int $module_index Index of the module in the module list array
*/
public function set_active_module($module_service_name, $module_index)
{
$this->progress_data['last_task_module_name'] = $module_service_name;
$this->progress_data['last_task_module_index'] = $module_index;
}
/**
* Getter for progress data
*
* @return array
*/
public function get_progress_data()
{
return $this->progress_data;
}
/**
* Recovers install configuration from file
*/
public function load_config()
{
if (!$this->filesystem->exists($this->install_config_file))
{
return;
}
$file_content = @file_get_contents($this->install_config_file);
$serialized_data = trim(substr($file_content, 8));
$unserialized_data = unserialize($serialized_data);
$this->installer_config = $unserialized_data['installer_config'];
$this->progress_data = $unserialized_data['progress_data'];
$this->navigation_data = $unserialized_data['navigation_data'];
}
/**
* Dumps install configuration to disk
*/
public function save_config()
{
// Create array to save
$save_array = array(
'installer_config' => $this->installer_config,
'progress_data' => $this->progress_data,
'navigation_data' => $this->navigation_data,
);
// Create file content
$file_content = '<?php // ';
$file_content .= serialize($save_array);
$file_content .= "\n";
// Dump file_content to disk
$fp = @fopen($this->install_config_file, 'w');
if (!$fp)
{
throw new installer_config_not_writable_exception();
}
fwrite($fp, $file_content);
fclose($fp);
}
/**
* Increments the task progress
*
* @param int $increment_by The amount to increment by
*/
public function increment_current_task_progress($increment_by = 1)
{
$this->progress_data['current_task_progress'] += $increment_by;
if ($this->progress_data['current_task_progress'] > $this->progress_data['max_task_progress'])
{
$this->progress_data['current_task_progress'] = $this->progress_data['max_task_progress'];
}
}
/**
* Sets the task progress to a specific number
*
* @param int $task_progress The task progress number to be set
*/
public function set_current_task_progress($task_progress)
{
$this->progress_data['current_task_progress'] = $task_progress;
}
/**
* Sets the number of tasks belonging to the installer in the current mode.
*
* @param int $task_progress_count Number of tasks
*/
public function set_task_progress_count($task_progress_count)
{
$this->progress_data['max_task_progress'] = $task_progress_count;
}
/**
* Returns the number of the current task being executed
*
* @return int
*/
public function get_current_task_progress()
{
return $this->progress_data['current_task_progress'];
}
/**
* Returns the number of tasks belonging to the installer in the current mode.
*
* @return int
*/
public function get_task_progress_count()
{
return $this->progress_data['max_task_progress'];
}
/**
* Marks stage as completed in the navigation bar
*
* @param array $nav_path Array to the navigation elem
*/
public function set_finished_navigation_stage($nav_path)
{
$this->navigation_data['finished'][] = $nav_path;
}
/**
* Marks stage as active in the navigation bar
*
* @param array $nav_path Array to the navigation elem
*/
public function set_active_navigation_stage($nav_path)
{
$this->navigation_data['active'] = $nav_path;
}
/**
* Returns navigation data
*
* @return array
*/
public function get_navigation_data()
{
return $this->navigation_data;
}
/**
* Filling up system_data array
*/
protected function setup_system_data()
{
// Query maximum runtime from php.ini
$execution_time = $this->php_ini->get_int('max_execution_time');
$execution_time = min(15, $execution_time / 2);
$this->system_data['max_execution_time'] = $execution_time;
// Set start time
$this->system_data['start_time'] = time();
// Get memory limit
$this->system_data['memory_limit'] = $this->php_ini->get_bytes('memory_limit');
}
}
|