1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2017 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\filemanager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package sitemaker |
14
|
|
|
*/ |
15
|
|
|
class settings |
16
|
|
|
{ |
17
|
|
|
/** @var \phpbb\filesystem\filesystem */ |
18
|
|
|
protected $filesystem; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $config_path; |
22
|
|
|
|
23
|
|
|
/** @var string phpEx */ |
24
|
|
|
protected $php_ext; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $config_template; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
* |
32
|
|
|
* @param \phpbb\filesystem\filesystem $filesystem File system |
33
|
|
|
* @param string $config_path Filemanager config path |
34
|
|
|
* @param string $php_ext phpEx |
35
|
|
|
*/ |
36
|
2 |
|
public function __construct(\phpbb\filesystem\filesystem $filesystem, $config_path, $php_ext) |
37
|
|
|
{ |
38
|
2 |
|
$this->filesystem = $filesystem; |
39
|
2 |
|
$this->config_path = $config_path; |
40
|
2 |
|
$this->php_ext = $php_ext; |
41
|
|
|
|
42
|
2 |
|
$this->config_template = __DIR__ . '/default.config'; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param bool $retry |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
2 |
|
public function get_settings($retry = true) |
50
|
|
|
{ |
51
|
|
|
// return empty array if filemanager is not installed |
52
|
2 |
|
if (!is_dir($this->config_path)) |
53
|
2 |
|
{ |
54
|
|
|
return array(); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$config_file = $this->get_config_file(); |
58
|
|
|
|
59
|
2 |
|
if ($retry && file($config_file)[1] !== '// Auto-generated configuration file for phpBB sitemaker') |
60
|
2 |
|
{ |
61
|
2 |
|
$this->filesystem->remove($config_file); |
62
|
2 |
|
return $this->get_settings(false); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return include($config_file); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $file |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
2 |
|
public function set_config_template($file) |
73
|
|
|
{ |
74
|
2 |
|
$this->config_template = $file; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $settings |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
1 |
|
public function save(array $settings) |
82
|
|
|
{ |
83
|
1 |
|
$curr_settings = $this->get_settings(); |
84
|
1 |
|
$config_file = $this->get_config_file(); |
85
|
1 |
|
$config_str = file_get_contents($config_file); |
86
|
|
|
|
87
|
1 |
|
foreach ($settings as $prop => $value) |
88
|
|
|
{ |
89
|
1 |
|
$this->type_cast_config_value($curr_settings[$prop], $value); |
90
|
1 |
|
$config_str = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", " '$prop'$1=> $value,", $config_str); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
1 |
|
$this->filesystem->dump_file($config_file, $config_str); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $curr_val |
98
|
|
|
* @param mixed $value |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
1 |
|
protected function type_cast_config_value($curr_val, &$value) |
102
|
|
|
{ |
103
|
1 |
|
$type = gettype($curr_val); |
104
|
|
|
switch ($type) |
105
|
|
|
{ |
106
|
1 |
|
case 'string': |
107
|
1 |
|
$value = "'$value'"; |
108
|
1 |
|
break; |
109
|
1 |
|
case 'integer': |
110
|
1 |
|
$value = (int) $value; |
111
|
1 |
|
break; |
112
|
|
|
} |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
2 |
|
protected function get_config_file() |
119
|
|
|
{ |
120
|
2 |
|
$config_file = $this->config_path . 'config.' . $this->php_ext; |
121
|
|
|
|
122
|
2 |
|
if (!$this->filesystem->exists($config_file)) |
123
|
2 |
|
{ |
124
|
2 |
|
$this->filesystem->copy($this->config_template, $config_file, true); |
125
|
2 |
|
} |
126
|
|
|
|
127
|
2 |
|
return $config_file; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|