Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.67% |
40 / 96 |
|
32.00% |
8 / 25 |
CRAP | |
0.00% |
0 / 1 |
| config | |
41.67% |
40 / 96 |
|
32.00% |
8 / 25 |
410.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| set | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| system_get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| get_time_remaining | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| get_memory_remaining | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| set_finished_task | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_active_module | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_progress_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| exists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| load_config | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
| create_progress_restart_point | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| jump_to_restart_point | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| has_restart_point | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| save_config | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| increment_current_task_progress | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| set_current_task_progress | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_task_progress_count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_current_task_progress | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_task_progress_count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_finished_navigation_stage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| set_active_navigation_stage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_navigation_data | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| clean_up_config_file | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setup_system_data | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | |
| 14 | namespace phpbb\install\helper; |
| 15 | |
| 16 | use phpbb\install\exception\installer_config_not_writable_exception; |
| 17 | |
| 18 | /** |
| 19 | * Stores common settings and installation status |
| 20 | */ |
| 21 | class config |
| 22 | { |
| 23 | /** |
| 24 | * @var \phpbb\filesystem\filesystem_interface |
| 25 | */ |
| 26 | protected $filesystem; |
| 27 | |
| 28 | /** |
| 29 | * Array which contains config settings for the installer |
| 30 | * |
| 31 | * The array will also store all the user input, as well as any |
| 32 | * data that is passed to other tasks by a task. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $installer_config; |
| 37 | |
| 38 | /** |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $install_config_file; |
| 42 | |
| 43 | /** |
| 44 | * @var \bantu\IniGetWrapper\IniGetWrapper |
| 45 | */ |
| 46 | protected $php_ini; |
| 47 | |
| 48 | /** |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $phpbb_root_path; |
| 52 | |
| 53 | /** |
| 54 | * Array containing progress information |
| 55 | * |
| 56 | * @var array |
| 57 | */ |
| 58 | protected $progress_data; |
| 59 | |
| 60 | /** |
| 61 | * Array containing system information |
| 62 | * |
| 63 | * The array contains run time and memory limitations. |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | protected $system_data; |
| 68 | |
| 69 | /** |
| 70 | * Array containing navigation bar information |
| 71 | * |
| 72 | * @var array |
| 73 | */ |
| 74 | protected $navigation_data; |
| 75 | |
| 76 | /** |
| 77 | * Flag indicating that config file should be cleaned up |
| 78 | * |
| 79 | * @var bool |
| 80 | */ |
| 81 | protected $do_clean_up; |
| 82 | |
| 83 | /** |
| 84 | * Constructor |
| 85 | */ |
| 86 | public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, \bantu\IniGetWrapper\IniGetWrapper $php_ini, $phpbb_root_path) |
| 87 | { |
| 88 | $this->filesystem = $filesystem; |
| 89 | $this->php_ini = $php_ini; |
| 90 | $this->phpbb_root_path = $phpbb_root_path; |
| 91 | $this->do_clean_up = false; |
| 92 | |
| 93 | // Set up data arrays |
| 94 | $this->navigation_data = array(); |
| 95 | $this->installer_config = array(); |
| 96 | $this->system_data = array(); |
| 97 | $this->progress_data = array( |
| 98 | 'last_task_module_name' => '', // Stores the service name of the latest finished module |
| 99 | 'last_task_module_index' => 0, // Stores the index of the latest finished module |
| 100 | 'last_task_index' => 0, // Stores the index of the latest finished task |
| 101 | 'max_task_progress' => 0, |
| 102 | 'current_task_progress' => 0, |
| 103 | '_restart_points' => array(), |
| 104 | 'use_restart_point' => false, |
| 105 | ); |
| 106 | |
| 107 | $this->install_config_file = $this->phpbb_root_path . 'store/install_config.php'; |
| 108 | |
| 109 | $this->setup_system_data(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns data for a specified parameter |
| 114 | * |
| 115 | * @param string $param_name Name of the parameter to return |
| 116 | * @param mixed $default Default value to return when the specified data |
| 117 | * does not exist. |
| 118 | * |
| 119 | * @return mixed value of the specified parameter or the default value if the data |
| 120 | * cannot be recovered. |
| 121 | */ |
| 122 | public function get($param_name, $default = false) |
| 123 | { |
| 124 | return (isset($this->installer_config[$param_name])) ? $this->installer_config[$param_name] : $default; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Sets a parameter in installer_config |
| 129 | * |
| 130 | * @param string $param_name Name of the parameter |
| 131 | * @param mixed $value Values to set the parameter |
| 132 | */ |
| 133 | public function set($param_name, $value) |
| 134 | { |
| 135 | $this->installer_config = array_merge($this->installer_config, array( |
| 136 | $param_name => $value, |
| 137 | )); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns system parameter |
| 142 | * |
| 143 | * @param string $param_name Name of the parameter |
| 144 | * |
| 145 | * @return mixed Returns system parameter if it is defined, false otherwise |
| 146 | */ |
| 147 | public function system_get($param_name) |
| 148 | { |
| 149 | return (isset($this->system_data[$param_name])) ? $this->system_data[$param_name] : false; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns remaining time until the run time limit |
| 154 | * |
| 155 | * @return int Remaining time until the run time limit in seconds |
| 156 | */ |
| 157 | public function get_time_remaining() |
| 158 | { |
| 159 | if ($this->system_data['max_execution_time'] <= 0) |
| 160 | { |
| 161 | return PHP_INT_MAX; |
| 162 | } |
| 163 | |
| 164 | return ($this->system_data['start_time'] + $this->system_data['max_execution_time']) - microtime(true); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns remaining memory available for PHP |
| 169 | * |
| 170 | * @return int Remaining memory until reaching the limit |
| 171 | */ |
| 172 | public function get_memory_remaining() |
| 173 | { |
| 174 | if ($this->system_data['memory_limit'] <= 0) |
| 175 | { |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | if (function_exists('memory_get_usage')) |
| 180 | { |
| 181 | return ($this->system_data['memory_limit'] - memory_get_usage()); |
| 182 | } |
| 183 | |
| 184 | // If we cannot get the information then just return a positive number (and cross fingers) |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Saves the latest executed task |
| 190 | * |
| 191 | * @param int $task_service_index Index of the installer task service in the module |
| 192 | */ |
| 193 | public function set_finished_task($task_service_index) |
| 194 | { |
| 195 | $this->progress_data['last_task_index'] = $task_service_index; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Set active module |
| 200 | * |
| 201 | * @param string $module_service_name Name of the installer module service |
| 202 | * @param int $module_service_index Index of the installer module service |
| 203 | */ |
| 204 | public function set_active_module($module_service_name, $module_service_index) |
| 205 | { |
| 206 | $this->progress_data['last_task_module_name'] = $module_service_name; |
| 207 | $this->progress_data['last_task_module_index'] = $module_service_index; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Getter for progress data |
| 212 | * |
| 213 | * @return array |
| 214 | */ |
| 215 | public function get_progress_data() |
| 216 | { |
| 217 | return $this->progress_data; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Checks whether install config exists, i.e. whether an installation is in progress |
| 222 | * |
| 223 | * @return bool |
| 224 | */ |
| 225 | public function exists(): bool |
| 226 | { |
| 227 | return $this->filesystem->exists($this->install_config_file); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Recovers install configuration from file |
| 232 | */ |
| 233 | public function load_config() |
| 234 | { |
| 235 | if (!$this->exists()) |
| 236 | { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | $file_content = @file_get_contents($this->install_config_file); |
| 241 | $serialized_data = trim(substr($file_content, 8)); |
| 242 | |
| 243 | $installer_config = array(); |
| 244 | $progress_data = array(); |
| 245 | $navigation_data = array(); |
| 246 | |
| 247 | if (!empty($serialized_data)) |
| 248 | { |
| 249 | $unserialized_data = json_decode($serialized_data, true); |
| 250 | |
| 251 | $installer_config = (is_array($unserialized_data['installer_config'])) ? $unserialized_data['installer_config'] : array(); |
| 252 | $progress_data = (is_array($unserialized_data['progress_data'])) ? $unserialized_data['progress_data'] : array(); |
| 253 | $navigation_data = (is_array($unserialized_data['navigation_data'])) ? $unserialized_data['navigation_data'] : array(); |
| 254 | } |
| 255 | |
| 256 | $this->installer_config = array_merge($this->installer_config, $installer_config); |
| 257 | $this->progress_data = array_merge($this->progress_data, $progress_data); |
| 258 | $this->navigation_data = array_merge($this->navigation_data, $navigation_data); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Creates a progress restart point |
| 263 | * |
| 264 | * Restart points can be used to repeat certain tasks periodically. |
| 265 | * You need to call this method from the first task you want to repeat. |
| 266 | * |
| 267 | * @param string $name Name of the restart point |
| 268 | */ |
| 269 | public function create_progress_restart_point($name) |
| 270 | { |
| 271 | $tmp_progress_data = $this->progress_data; |
| 272 | unset($tmp_progress_data['_restart_points']); |
| 273 | |
| 274 | $this->progress_data['_restart_points'][$name] = $tmp_progress_data; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Set restart point to continue from |
| 279 | * |
| 280 | * @param string $name Name of the restart point |
| 281 | * |
| 282 | * @return bool Returns false if the restart point name does not exist, otherwise true |
| 283 | */ |
| 284 | public function jump_to_restart_point($name) |
| 285 | { |
| 286 | if (!isset($this->progress_data['_restart_points'][$name]) || empty($this->progress_data['_restart_points'][$name])) |
| 287 | { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | foreach ($this->progress_data['_restart_points'][$name] as $key => $value) |
| 292 | { |
| 293 | $this->progress_data[$key] = $value; |
| 294 | } |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns whether a restart point with a given name exists or not |
| 301 | * |
| 302 | * @param string $name Name of the restart point |
| 303 | * |
| 304 | * @return bool |
| 305 | */ |
| 306 | public function has_restart_point($name) |
| 307 | { |
| 308 | return isset($this->progress_data['_restart_points'][$name]); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Dumps install configuration to disk |
| 313 | */ |
| 314 | public function save_config() |
| 315 | { |
| 316 | if ($this->do_clean_up) |
| 317 | { |
| 318 | @unlink($this->install_config_file); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // Create array to save |
| 323 | $save_array = array( |
| 324 | 'installer_config' => $this->installer_config, |
| 325 | 'progress_data' => $this->progress_data, |
| 326 | 'navigation_data' => $this->navigation_data, |
| 327 | ); |
| 328 | |
| 329 | // Create file content |
| 330 | $file_content = '<?php // '; |
| 331 | $file_content .= json_encode($save_array); |
| 332 | $file_content .= "\n"; |
| 333 | |
| 334 | // Dump file_content to disk |
| 335 | $fp = @fopen($this->install_config_file, 'w'); |
| 336 | if (!$fp) |
| 337 | { |
| 338 | throw new installer_config_not_writable_exception(); |
| 339 | } |
| 340 | |
| 341 | fwrite($fp, $file_content); |
| 342 | fclose($fp); |
| 343 | // Enforce 0600 permission for install config |
| 344 | $this->filesystem->chmod([$this->install_config_file], 0600); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Increments the task progress |
| 349 | * |
| 350 | * @param int $increment_by The amount to increment by |
| 351 | */ |
| 352 | public function increment_current_task_progress($increment_by = 1) |
| 353 | { |
| 354 | $this->progress_data['current_task_progress'] += $increment_by; |
| 355 | |
| 356 | if ($this->progress_data['current_task_progress'] > $this->progress_data['max_task_progress']) |
| 357 | { |
| 358 | $this->progress_data['current_task_progress'] = $this->progress_data['max_task_progress']; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Sets the task progress to a specific number |
| 364 | * |
| 365 | * @param int $task_progress The task progress number to be set |
| 366 | */ |
| 367 | public function set_current_task_progress($task_progress) |
| 368 | { |
| 369 | $this->progress_data['current_task_progress'] = $task_progress; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Sets the number of tasks belonging to the installer in the current mode. |
| 374 | * |
| 375 | * @param int $task_progress_count Number of tasks |
| 376 | */ |
| 377 | public function set_task_progress_count($task_progress_count) |
| 378 | { |
| 379 | $this->progress_data['max_task_progress'] = $task_progress_count; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Returns the number of the current task being executed |
| 384 | * |
| 385 | * @return int |
| 386 | */ |
| 387 | public function get_current_task_progress() |
| 388 | { |
| 389 | return $this->progress_data['current_task_progress']; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Returns the number of tasks belonging to the installer in the current mode. |
| 394 | * |
| 395 | * @return int |
| 396 | */ |
| 397 | public function get_task_progress_count() |
| 398 | { |
| 399 | return $this->progress_data['max_task_progress']; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Marks stage as completed in the navigation bar |
| 404 | * |
| 405 | * @param array $nav_path Array to the navigation elem |
| 406 | */ |
| 407 | public function set_finished_navigation_stage($nav_path) |
| 408 | { |
| 409 | if (isset($this->navigation_data['finished']) && in_array($nav_path, $this->navigation_data['finished'])) |
| 410 | { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | $this->navigation_data['finished'][] = $nav_path; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Marks stage as active in the navigation bar |
| 419 | * |
| 420 | * @param array $nav_path Array to the navigation elem |
| 421 | */ |
| 422 | public function set_active_navigation_stage($nav_path) |
| 423 | { |
| 424 | $this->navigation_data['active'] = $nav_path; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Returns navigation data |
| 429 | * |
| 430 | * @return array |
| 431 | */ |
| 432 | public function get_navigation_data() |
| 433 | { |
| 434 | return $this->navigation_data; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Removes install config file |
| 439 | */ |
| 440 | public function clean_up_config_file() |
| 441 | { |
| 442 | $this->do_clean_up = true; |
| 443 | @unlink($this->install_config_file); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Filling up system_data array |
| 448 | */ |
| 449 | protected function setup_system_data() |
| 450 | { |
| 451 | // Query maximum runtime from php.ini |
| 452 | $execution_time = $this->php_ini->getNumeric('max_execution_time'); |
| 453 | $execution_time = min(15, $execution_time / 2); |
| 454 | $this->system_data['max_execution_time'] = $execution_time; |
| 455 | |
| 456 | // Set start time |
| 457 | $this->system_data['start_time'] = microtime(true); |
| 458 | |
| 459 | // Get memory limit |
| 460 | $this->system_data['memory_limit'] = $this->php_ini->getBytes('memory_limit'); |
| 461 | } |
| 462 | } |