|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The main class for fetching the configuration, update and delete items. This |
|
5
|
|
|
* class is also a small Dependency Injection Container for phpMyFAQ. |
|
6
|
|
|
* |
|
7
|
|
|
* PHP Version 5.5 |
|
8
|
|
|
* |
|
9
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
|
10
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
|
11
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
|
12
|
|
|
* |
|
13
|
|
|
* @category phpMyFAQ |
|
14
|
|
|
* @author Thorsten Rinne <[email protected]> |
|
15
|
|
|
* @copyright 2006-2015 phpMyFAQ Team |
|
16
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
|
17
|
|
|
* @link http://www.phpmyfaq.de |
|
18
|
|
|
* @since 2006-01-04 |
|
19
|
|
|
*/ |
|
20
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
|
21
|
|
|
exit(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* PMF_Configuration. |
|
26
|
|
|
* |
|
27
|
|
|
* @category phpMyFAQ |
|
28
|
|
|
* @author Thorsten Rinne <[email protected]> |
|
29
|
|
|
* @copyright 2006-2015 phpMyFAQ Team |
|
30
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
|
31
|
|
|
* @link http://www.phpmyfaq.de |
|
32
|
|
|
* @since 2006-01-04 |
|
33
|
|
|
*/ |
|
34
|
|
|
class PMF_Configuration |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $_tableName = 'faqconfig'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
public $config = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param PMF_DB_Driver $database |
|
50
|
|
|
* |
|
51
|
|
|
* @return PMF_Configuration |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(PMF_DB_Driver $database) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setDb($database); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Fetches all configuration items into an array. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getAll() |
|
62
|
|
|
{ |
|
63
|
|
|
$config = []; |
|
64
|
|
|
$query = sprintf(' |
|
65
|
|
|
SELECT |
|
66
|
|
|
config_name, config_value |
|
67
|
|
|
FROM |
|
68
|
|
|
%s%s', |
|
69
|
|
|
PMF_Db::getTablePrefix(), |
|
70
|
|
|
$this->_tableName |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$result = $this->getDb()->query($query); |
|
74
|
|
|
try { |
|
75
|
|
|
$config = $this->getDb()->fetchAll($result); |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
// @todo Added proper handling of exception |
|
78
|
|
|
echo $e->getMessage(); |
|
79
|
|
|
} |
|
80
|
|
|
foreach ($config as $items) { |
|
81
|
|
|
$this->config[$items->config_name] = $items->config_value; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns a configuration item. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $item Configuration item |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function get($item) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!isset($this->config[$item])) { |
|
95
|
|
|
$this->getAll(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (isset($this->config[$item])) { |
|
99
|
|
|
switch ($this->config[$item]) { |
|
100
|
|
|
case 'true': |
|
101
|
|
|
return true; |
|
102
|
|
|
break; |
|
103
|
|
|
case 'false': |
|
104
|
|
|
return false; |
|
105
|
|
|
break; |
|
106
|
|
|
default: |
|
107
|
|
|
return $this->config[$item]; |
|
108
|
|
|
break; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sets one single configuration item. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $key |
|
119
|
|
|
* @param mixed $value |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function set($key, $value) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$query = sprintf("UPDATE %s%s SET config_value = '%s' WHERE config_name = '%s'", |
|
126
|
|
|
PMF_Db::getTablePrefix(), |
|
127
|
|
|
$this->_tableName, |
|
128
|
|
|
$this->getDb()->escape(trim($value)), |
|
129
|
|
|
$this->getDb()->escape(trim($key)) |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
return $this->getDb()->query($query); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Sets the PMF_DB_Driver object. |
|
137
|
|
|
* |
|
138
|
|
|
* @param PMF_DB_Driver $database |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setDb(PMF_DB_Driver $database) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->config['core.database'] = $database; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns the PMF_DB_Driver object. |
|
147
|
|
|
* |
|
148
|
|
|
* @return PMF_DB_Driver |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getDb() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->config['core.database']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Sets the PMF_Instance object. |
|
157
|
|
|
* |
|
158
|
|
|
* @param PMF_Instance $instance |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setInstance(PMF_Instance $instance) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->config['core.instance'] = $instance; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns the PMF_Instance object. |
|
167
|
|
|
* |
|
168
|
|
|
* @return PMF_Instance |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getInstance() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->config['core.instance']; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Sets the Language object. |
|
177
|
|
|
* |
|
178
|
|
|
* @param PMF_Language $language |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setLanguage(PMF_Language $language) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->config['core.language'] = $language; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns the Language object. |
|
187
|
|
|
* |
|
188
|
|
|
* @return PMF_Language |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getLanguage() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->config['core.language']; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Returns the default language. |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getDefaultLanguage() |
|
201
|
|
|
{ |
|
202
|
|
|
return str_replace(['language_', '.php'], '', $this->config['main.language']); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Returns the default URL of the phpMyFAQ installation. |
|
207
|
|
|
* |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getDefaultUrl() |
|
211
|
|
|
{ |
|
212
|
|
|
$defaultUrl = $this->get('main.referenceURL'); |
|
213
|
|
|
|
|
214
|
|
|
if (substr($defaultUrl, -1) !== '/') { |
|
215
|
|
|
return $defaultUrl.'/'; |
|
216
|
|
|
} else { |
|
217
|
|
|
return $defaultUrl; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Sets the PMF_Ldap object. |
|
223
|
|
|
* |
|
224
|
|
|
* @param PMF_Ldap $ldap |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setLdap(PMF_Ldap $ldap) |
|
227
|
|
|
{ |
|
228
|
|
|
$this->config['core.ldap'] = $ldap; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Returns the PMF_Ldap object. |
|
233
|
|
|
* |
|
234
|
|
|
* @return PMF_Ldap |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getLdap() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->config['core.ldap']; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Sets the LDAP configuration. |
|
243
|
|
|
* |
|
244
|
|
|
* @param Array $ldapConfig |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setLdapConfig(Array $ldapConfig) |
|
247
|
|
|
{ |
|
248
|
|
|
// Always add main LDAP server |
|
249
|
|
|
$this->config['core.ldapServer'][0] = [ |
|
250
|
|
|
'ldap_server' => $ldapConfig['ldap_server'], |
|
251
|
|
|
'ldap_port' => $ldapConfig['ldap_port'], |
|
252
|
|
|
'ldap_user' => $ldapConfig['ldap_user'], |
|
253
|
|
|
'ldap_password' => $ldapConfig['ldap_password'], |
|
254
|
|
|
'ldap_base' => $ldapConfig['ldap_base'], |
|
255
|
|
|
]; |
|
256
|
|
|
|
|
257
|
|
|
// Add multiple LDAP servers if enabled |
|
258
|
|
|
if (true === $ldapConfig['ldap_use_multiple_servers']) { |
|
259
|
|
|
$key = 1; |
|
260
|
|
|
while ($key >= 1) { |
|
261
|
|
|
if (isset($ldapConfig[$key])) { |
|
262
|
|
|
$this->config['core.ldapServer'][$key] = $ldapConfig[$key]; |
|
263
|
|
|
++$key; |
|
264
|
|
|
} else { |
|
265
|
|
|
break; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
// Set LDAP configuration |
|
271
|
|
|
$this->config['core.ldapConfig'] = [ |
|
272
|
|
|
'ldap_use_multiple_servers' => $ldapConfig['ldap_use_multiple_servers'], |
|
273
|
|
|
'ldap_mapping' => $ldapConfig['ldap_mapping'], |
|
274
|
|
|
'ldap_use_domain_prefix' => $ldapConfig['ldap_use_domain_prefix'], |
|
275
|
|
|
'ldap_options' => $ldapConfig['ldap_options'], |
|
276
|
|
|
'ldap_use_memberOf' => $ldapConfig['ldap_use_memberOf'], |
|
277
|
|
|
'ldap_use_sasl' => $ldapConfig['ldap_use_sasl'], |
|
278
|
|
|
'ldap_use_anonymous_login' => $ldapConfig['ldap_use_anonymous_login'], |
|
279
|
|
|
]; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Returns the LDAP configuration. |
|
284
|
|
|
* |
|
285
|
|
|
* @return array |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getLdapConfig() |
|
288
|
|
|
{ |
|
289
|
|
|
return isset($this->config['core.ldapConfig']) ? $this->config['core.ldapConfig'] : []; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Returns the LDAP server(s). |
|
294
|
|
|
* |
|
295
|
|
|
* @return array |
|
296
|
|
|
*/ |
|
297
|
|
|
public function getLdapServer() |
|
298
|
|
|
{ |
|
299
|
|
|
return isset($this->config['core.ldapServer']) ? $this->config['core.ldapServer'] : []; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Sets the Elasticsearch client instance. |
|
304
|
|
|
* |
|
305
|
|
|
* @param Elasticsearch\Client $esClient |
|
306
|
|
|
*/ |
|
307
|
|
|
public function setElasticsearch(Elasticsearch\Client $esClient) |
|
308
|
|
|
{ |
|
309
|
|
|
$this->config['core.elasticsearch'] = $esClient; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Returns the Elasticsearch client instance. |
|
314
|
|
|
* |
|
315
|
|
|
* @return Elasticsearch\Client |
|
316
|
|
|
*/ |
|
317
|
|
|
public function getElasticsearch() |
|
318
|
|
|
{ |
|
319
|
|
|
return $this->config['core.elasticsearch']; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Sets the Elasticsearch configuration. |
|
324
|
|
|
* |
|
325
|
|
|
* @param array $data |
|
326
|
|
|
*/ |
|
327
|
|
|
public function setElasticsearchConfig(Array $data) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->config['core.elasticsearchConfig'] = $data; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Returns the Elasticsearch configuration. |
|
334
|
|
|
* |
|
335
|
|
|
* @return array |
|
336
|
|
|
*/ |
|
337
|
|
|
public function getElasticsearchConfig() |
|
338
|
|
|
{ |
|
339
|
|
|
return isset($this->config['core.elasticsearchConfig']) ? $this->config['core.elasticsearchConfig'] : []; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Adds a configuration item for the database. |
|
344
|
|
|
* |
|
345
|
|
|
* @param string $name |
|
346
|
|
|
* @param mixed $value |
|
347
|
|
|
* |
|
348
|
|
|
* @return bool |
|
349
|
|
|
*/ |
|
350
|
|
View Code Duplication |
public function add($name, $value) |
|
351
|
|
|
{ |
|
352
|
|
|
$insert = sprintf( |
|
353
|
|
|
"INSERT INTO |
|
354
|
|
|
%s%s |
|
355
|
|
|
VALUES |
|
356
|
|
|
('%s', '%s')", |
|
357
|
|
|
PMF_Db::getTablePrefix(), |
|
358
|
|
|
$this->_tableName, |
|
359
|
|
|
$this->getDb()->escape(trim($name)), |
|
360
|
|
|
$this->getDb()->escape(trim($value)) |
|
361
|
|
|
); |
|
362
|
|
|
|
|
363
|
|
|
return $this->getDb()->query($insert); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Deletes a configuration item for the database. |
|
368
|
|
|
* |
|
369
|
|
|
* @param string $name |
|
370
|
|
|
* |
|
371
|
|
|
* @return bool |
|
372
|
|
|
*/ |
|
373
|
|
View Code Duplication |
public function delete($name) |
|
374
|
|
|
{ |
|
375
|
|
|
$delete = sprintf( |
|
376
|
|
|
"DELETE FROM |
|
377
|
|
|
%s%s |
|
378
|
|
|
WHERE |
|
379
|
|
|
config_name = '%s'", |
|
380
|
|
|
PMF_Db::getTablePrefix(), |
|
381
|
|
|
$this->_tableName, |
|
382
|
|
|
$this->getDb()->escape(trim($name)) |
|
383
|
|
|
); |
|
384
|
|
|
|
|
385
|
|
|
return $this->getDb()->query($delete); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* Updates all configuration items. |
|
390
|
|
|
* |
|
391
|
|
|
* @param array $newConfigs Array with new configuration values |
|
392
|
|
|
* |
|
393
|
|
|
* @return bool |
|
394
|
|
|
*/ |
|
395
|
|
|
public function update(Array $newConfigs) |
|
396
|
|
|
{ |
|
397
|
|
|
$runtimeConfigs = [ |
|
398
|
|
|
'core.database', // PMF_DB_Driver |
|
399
|
|
|
'core.instance', // PMF_Instance |
|
400
|
|
|
'core.language', // Language |
|
401
|
|
|
'core.ldap', // PMF_Ldap |
|
402
|
|
|
'core.ldapConfig', // $PMF_LDAP |
|
403
|
|
|
'core.elasticsearch', // Elasticsearch\Client |
|
404
|
|
|
'core.elasticsearchConfig' // $PMF_ES |
|
405
|
|
|
]; |
|
406
|
|
|
|
|
407
|
|
|
if (is_array($newConfigs)) { |
|
408
|
|
|
foreach ($newConfigs as $name => $value) { |
|
409
|
|
|
if ($name != 'main.phpMyFAQToken' && |
|
410
|
|
|
!in_array($name, $runtimeConfigs) |
|
411
|
|
|
) { |
|
412
|
|
|
$update = sprintf(" |
|
413
|
|
|
UPDATE |
|
414
|
|
|
%s%s |
|
415
|
|
|
SET |
|
416
|
|
|
config_value = '%s' |
|
417
|
|
|
WHERE |
|
418
|
|
|
config_name = '%s'", |
|
419
|
|
|
PMF_Db::getTablePrefix(), |
|
420
|
|
|
$this->_tableName, |
|
421
|
|
|
$this->getDb()->escape(trim($value)), |
|
422
|
|
|
$name |
|
423
|
|
|
); |
|
424
|
|
|
|
|
425
|
|
|
$this->getDb()->query($update); |
|
426
|
|
|
if (isset($this->config[$name])) { |
|
427
|
|
|
unset($this->config[$name]); |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
return true; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
return false; |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Returns all sorting possibilities for FAQ records. |
|
440
|
|
|
* |
|
441
|
|
|
* @param string $current |
|
442
|
|
|
* |
|
443
|
|
|
* @return string |
|
444
|
|
|
*/ |
|
445
|
|
|
public static function sortingOptions($current) |
|
446
|
|
|
{ |
|
447
|
|
|
global $PMF_LANG; |
|
448
|
|
|
|
|
449
|
|
|
$options = ['id', 'thema', 'visits', 'updated', 'author']; |
|
450
|
|
|
$output = ''; |
|
451
|
|
|
|
|
452
|
|
|
foreach ($options as $value) { |
|
453
|
|
|
printf('<option value="%s"%s>%s</option>', |
|
454
|
|
|
$value, |
|
455
|
|
|
($value == $current) ? ' selected' : '', |
|
456
|
|
|
$PMF_LANG['ad_conf_order_'.$value]); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
return $output; |
|
460
|
|
|
} |
|
461
|
|
|
} |
|
462
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.