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\Lists\File;
15:
16: /**
17: * Derive default configuration for all sanitizer extensions
18: *
19: * @category Sanitizer
20: * @package Xoops\Core\Text
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2013-2015 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @link http://xoops.org
25: */
26: class DefaultConfiguration extends ConfigurationAbstract
27: {
28: /**
29: * The system/modules key paths are cleared on module update/install. Since modules
30: * can add extensions, we will store our defaults there so we will pick up changes.
31: *
32: * @var string cache key for defaults
33: */
34: protected $cacheKey = 'system/modules/sanitizer-defaults';
35:
36: /**
37: * Get defaults to initialize
38: */
39: public function __construct()
40: {
41: $xoops = \Xoops::getInstance();
42: //$xoops->cache()->delete($this->cacheKey);
43: //\Xmf\Debug::startTimer('sanitizer-defaults');
44: $sanitizerConfiguration = $xoops->cache()->cacheRead(
45: $this->cacheKey,
46: array($this, 'buildDefaultConfiguration')
47: );
48: parent::__construct($sanitizerConfiguration);
49: //\Xmf\Debug::stopTimer('sanitizer-defaults');
50: }
51:
52: /**
53: * Ask each sanitizer extension for default configuration
54: *
55: * @return array
56: */
57: public function buildDefaultConfiguration()
58: {
59: $this->registerComponent(\Xoops\Core\Text\Sanitizer::getDefaultConfig());
60: $extensions = File::getList(__DIR__ . '/Extensions');
61: foreach ($extensions as $extensionFile) {
62: if (substr($extensionFile, -4) === '.php') {
63: $class = __NAMESPACE__ . '\Extensions\\' . substr($extensionFile, 0, -4);
64: if (is_a($class, 'Xoops\Core\Text\Sanitizer\SanitizerConfigurable', true)) {
65: $this->registerComponent($class::getDefaultConfig());
66: }
67: }
68: }
69:
70: /**
71: * Register any 3rd party extensions
72: *
73: * Listeners will be passed a Configuration object as the single argument, and should
74: * call $arg->registerComponent() to register extensions
75: *
76: * All extensions must implement SanitizerConfigurable, extending either ExtensionAbstract
77: * or FilterAbstract, and MUST autoload
78: *
79: * NB: Extensions and Filters all share the same configuration space, so a 3rd party
80: * extension that has the same short name as system extension will override the system
81: * supplied one.
82: */
83: \Xoops::getInstance()->events()->triggerEvent('core.sanitizer.configuration.defaults', $this);
84:
85: return (array) $this;
86: }
87:
88: /**
89: * Add a component (i.e extension or filter) to the configuration with default values
90: *
91: * @param array $configArray extension configuration
92: */
93: public function registerComponent($configArray)
94: {
95: if (is_array($configArray)) {
96: foreach ($configArray as $key => $config) {
97: if (isset($config['configured_class']) &&
98: is_a($config['configured_class'], 'Xoops\Core\Text\Sanitizer\SanitizerConfigurable', true)
99: ) {
100: $this->set($key, $config);
101: }
102: }
103: }
104: }
105: }
106: