|
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
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function init() |
|
49
|
|
|
{ |
|
50
|
2 |
|
$searcString = 'phpbb'; |
|
51
|
2 |
|
$config_file = $this->get_config_file(); |
|
52
|
|
|
|
|
53
|
|
|
// credit: http://jafty.com/blog/quick-way-to-check-if-string-exists-in-a-file-with-php/ |
|
54
|
2 |
|
if (!$this->filesystem->exists($config_file) || !exec('grep ' . escapeshellarg($searcString) . ' ' . $config_file)) |
|
55
|
2 |
|
{ |
|
56
|
2 |
|
$this->filesystem->copy($this->config_template, $config_file, true); |
|
57
|
2 |
|
} |
|
58
|
2 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return array|void |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function get_settings() |
|
64
|
|
|
{ |
|
65
|
2 |
|
if ($this->filesystem->exists($this->config_path)) |
|
66
|
2 |
|
{ |
|
67
|
2 |
|
$this->init(); |
|
68
|
|
|
|
|
69
|
2 |
|
$editing = true; |
|
70
|
2 |
|
return include($this->get_config_file()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $file |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function set_config_template($file) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$this->config_template = $file; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $settings |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function save(array $settings) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$curr_settings = $this->get_settings(); |
|
90
|
1 |
|
$config_file = $this->get_config_file(); |
|
91
|
1 |
|
$config_str = file_get_contents($config_file); |
|
92
|
|
|
|
|
93
|
1 |
|
foreach ($settings as $prop => $value) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$this->type_cast_config_value($curr_settings[$prop], $value); |
|
96
|
1 |
|
$config_str = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", " '$prop'$1=> $value,", $config_str); |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
$this->filesystem->dump_file($config_file, $config_str); |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param mixed $curr_val |
|
104
|
|
|
* @param mixed $value |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
1 |
|
protected function type_cast_config_value($curr_val, &$value) |
|
108
|
|
|
{ |
|
109
|
1 |
|
$type = gettype($curr_val); |
|
110
|
|
|
switch ($type) |
|
111
|
|
|
{ |
|
112
|
1 |
|
case 'string': |
|
113
|
1 |
|
$value = "'$value'"; |
|
114
|
1 |
|
break; |
|
115
|
1 |
|
case 'integer': |
|
116
|
1 |
|
$value = (int) $value; |
|
117
|
1 |
|
break; |
|
118
|
|
|
} |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
2 |
|
protected function get_config_file() |
|
125
|
|
|
{ |
|
126
|
2 |
|
return $this->config_path . 'config.' . $this->php_ext; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|