1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Text\Sanitizer;
13:
14: use Xoops\Core\Yaml;
15:
16: /**
17: * Provide a standard mechanism for a runtime registry for key/value pairs, useful
18: * for attributes and parameters.
19: *
20: * @category Sanitizer
21: * @package Xoops\Core\Text
22: * @author Richard Griffith <richard@geekwright.com>
23: * @copyright 2013-2015 XOOPS Project (http://xoops.org)
24: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25: * @link http://xoops.org
26: */
27: class Configuration extends ConfigurationAbstract
28: {
29: /**
30: * @var string config file with sanitizer prefs
31: */
32: private $sanitizerPrefsFilename = 'var/configs/system_sanitizer_prefs.yml';
33:
34: /**
35: * Get the sanitizer configuration.
36: */
37: public function __construct()
38: {
39: $sanitizerConfiguration = $this->readSanitizerPreferences();
40: parent::__construct($sanitizerConfiguration);
41: }
42:
43: /**
44: * readSanitizerPreferences - read configured sanitizer preferences
45: *
46: * If configuration file does not exist, create it.
47: *
48: * If configurable extensions exist that are not in the configuration
49: * file, add them, and rewrite the configuration file.
50: *
51: * @return array of sanitizer preferences
52: */
53: protected function readSanitizerPreferences()
54: {
55: $xoops = \Xoops::getInstance();
56:
57: $sanitizerPrefs = array();
58:
59: try {
60: $file = $xoops->path($this->sanitizerPrefsFilename);
61: $sanitizerPrefs = Yaml::read($file);
62: } catch (\Exception $e) {
63: $xoops->events()->triggerEvent('core.exception', $e);
64: }
65: if (!is_array($sanitizerPrefs)) {
66: $sanitizerPrefs = array();
67: }
68: $changed = false;
69: $defaultPrefs = new DefaultConfiguration();
70: foreach ($defaultPrefs as $name => $value) {
71: if (!array_key_exists($name, $sanitizerPrefs)) {
72: $sanitizerPrefs[$name] = $defaultPrefs[$name];
73: $changed = true;
74: }
75: }
76: if ($changed) {
77: $this->saveSanitizerPrefrences($sanitizerPrefs);
78: }
79: return $sanitizerPrefs;
80: }
81:
82: /**
83: * saveSanitizerPreferences - record array of sanitizer preferences in config file
84: *
85: * @param array $sanitizerPrefs array of sanitizer preferences to save
86: *
87: * @return void
88: */
89: protected function saveSanitizerPrefrences($sanitizerPrefs)
90: {
91: if (is_array($sanitizerPrefs)) {
92: $xoops = \Xoops::getInstance();
93: try {
94: Yaml::save($sanitizerPrefs, $xoops->path($this->sanitizerPrefsFilename));
95: } catch (\Exception $e) {
96: $xoops->events()->triggerEvent('core.exception', $e);
97: }
98: }
99: }
100: }
101: