xref: /dokuwiki/inc/Extension/PluginController.php (revision bff2c9d24314e25b31ceb53d51de76d678a0a4dc)
1<?php
2
3namespace dokuwiki\Extension;
4
5use dokuwiki\ErrorHandler;
6
7/**
8 * Class to encapsulate access to dokuwiki plugins
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Christopher Smith <chris@jalakai.co.uk>
12 */
13class PluginController
14{
15    /** @var array the types of plugins DokuWiki supports */
16    const PLUGIN_TYPES = ['auth', 'admin', 'syntax', 'action', 'renderer', 'helper', 'remote', 'cli'];
17
18    protected $listByType = [];
19    /** @var array all installed plugins and their enabled state [plugin=>enabled] */
20    protected $masterList = [];
21    protected $pluginCascade = ['default' => [], 'local' => [], 'protected' => []];
22    protected $lastLocalConfigFile = '';
23
24    /**
25     * Populates the master list of plugins
26     */
27    public function __construct()
28    {
29        $this->loadConfig();
30        $this->populateMasterList();
31    }
32
33    /**
34     * Returns a list of available plugins of given type
35     *
36     * @param $type  string, plugin_type name;
37     *               the type of plugin to return,
38     *               use empty string for all types
39     * @param $all   bool;
40     *               false to only return enabled plugins,
41     *               true to return both enabled and disabled plugins
42     *
43     * @return       array of
44     *                  - plugin names when $type = ''
45     *                  - or plugin component names when a $type is given
46     *
47     * @author Andreas Gohr <andi@splitbrain.org>
48     */
49    public function getList($type = '', $all = false)
50    {
51
52        // request the complete list
53        if (!$type) {
54            return $all ? array_keys($this->masterList) : array_keys(array_filter($this->masterList));
55        }
56
57        if (!isset($this->listByType[$type]['enabled'])) {
58            $this->listByType[$type]['enabled'] = $this->getListByType($type, true);
59        }
60        if ($all && !isset($this->listByType[$type]['disabled'])) {
61            $this->listByType[$type]['disabled'] = $this->getListByType($type, false);
62        }
63
64        return $all
65            ? array_merge($this->listByType[$type]['enabled'], $this->listByType[$type]['disabled'])
66            : $this->listByType[$type]['enabled'];
67    }
68
69    /**
70     * Loads the given plugin and creates an object of it
71     *
72     * @param  $type     string type of plugin to load
73     * @param  $name     string name of the plugin to load
74     * @param  $new      bool   true to return a new instance of the plugin, false to use an already loaded instance
75     * @param  $disabled bool   true to load even disabled plugins
76     * @return PluginInterface|null  the plugin object or null on failure
77     * @author Andreas Gohr <andi@splitbrain.org>
78     *
79     */
80    public function load($type, $name, $new = false, $disabled = false)
81    {
82
83        //we keep all loaded plugins available in global scope for reuse
84        global $DOKU_PLUGINS;
85
86        [$plugin, ] = $this->splitName($name);
87
88        // check if disabled
89        if (!$disabled && !$this->isEnabled($plugin)) {
90            return null;
91        }
92
93        $class = $type . '_plugin_' . $name;
94
95        try {
96            //plugin already loaded?
97            if (!empty($DOKU_PLUGINS[$type][$name])) {
98                if ($new || !$DOKU_PLUGINS[$type][$name]->isSingleton()) {
99
100                    return class_exists($class, true) ? new $class : null;
101                }
102
103                return $DOKU_PLUGINS[$type][$name];
104            }
105
106            //construct class and instantiate
107            if (!class_exists($class, true)) {
108                # the plugin might be in the wrong directory
109                $inf = confToHash(DOKU_PLUGIN . "$plugin/plugin.info.txt");
110                if ($inf['base'] && $inf['base'] != $plugin) {
111                    msg(
112                        sprintf(
113                            "Plugin installed incorrectly. Rename plugin directory '%s' to '%s'.",
114                            hsc($plugin),
115                            hsc(
116                                $inf['base']
117                            )
118                        ), -1
119                    );
120                } elseif (preg_match('/^' . DOKU_PLUGIN_NAME_REGEX . '$/', $plugin) !== 1) {
121                    msg(sprintf(
122                        'Plugin name \'%s\' is not a valid plugin name, only the characters a-z and 0-9 are allowed. ' .
123                        'Maybe the plugin has been installed in the wrong directory?', hsc($plugin)
124                    ), -1);
125                }
126                return null;
127            }
128            $DOKU_PLUGINS[$type][$name] = new $class;
129
130        } catch (\Throwable $e) {
131            ErrorHandler::showExceptionMsg($e, sprintf('Failed to load plugin %s', $plugin));
132            return null;
133        }
134
135        return $DOKU_PLUGINS[$type][$name];
136    }
137
138    /**
139     * Whether plugin is disabled
140     *
141     * @param string $plugin name of plugin
142     * @return bool  true disabled, false enabled
143     * @deprecated in favor of the more sensible isEnabled where the return value matches the enabled state
144     */
145    public function isDisabled($plugin)
146    {
147        dbg_deprecated('isEnabled()');
148        return !$this->isEnabled($plugin);
149    }
150
151    /**
152     * Check whether plugin is disabled
153     *
154     * @param string $plugin name of plugin
155     * @return bool  true enabled, false disabled
156     */
157    public function isEnabled($plugin)
158    {
159        return !empty($this->masterList[$plugin]);
160    }
161
162    /**
163     * Disable the plugin
164     *
165     * @param string $plugin name of plugin
166     * @return bool  true saving succeed, false saving failed
167     */
168    public function disable($plugin)
169    {
170        if (array_key_exists($plugin, $this->pluginCascade['protected'])) return false;
171        $this->masterList[$plugin] = 0;
172        return $this->saveList();
173    }
174
175    /**
176     * Enable the plugin
177     *
178     * @param string $plugin name of plugin
179     * @return bool  true saving succeed, false saving failed
180     */
181    public function enable($plugin)
182    {
183        if (array_key_exists($plugin, $this->pluginCascade['protected'])) return false;
184        $this->masterList[$plugin] = 1;
185        return $this->saveList();
186    }
187
188    /**
189     * Returns cascade of the config files
190     *
191     * @return array with arrays of plugin configs
192     */
193    public function getCascade()
194    {
195        return $this->pluginCascade;
196    }
197
198    /**
199     * Read all installed plugins and their current enabled state
200     */
201    protected function populateMasterList()
202    {
203        if ($dh = @opendir(DOKU_PLUGIN)) {
204            $all_plugins = [];
205            while (false !== ($plugin = readdir($dh))) {
206                if ($plugin[0] === '.') continue;               // skip hidden entries
207                if (is_file(DOKU_PLUGIN . $plugin)) continue;    // skip files, we're only interested in directories
208
209                if (array_key_exists($plugin, $this->masterList) && $this->masterList[$plugin] == 0) {
210                    $all_plugins[$plugin] = 0;
211
212                } elseif (array_key_exists($plugin, $this->masterList) && $this->masterList[$plugin] == 1) {
213                    $all_plugins[$plugin] = 1;
214                } else {
215                    $all_plugins[$plugin] = 1;
216                }
217            }
218            $this->masterList = $all_plugins;
219            if (!file_exists($this->lastLocalConfigFile)) {
220                $this->saveList(true);
221            }
222        }
223    }
224
225    /**
226     * Includes the plugin config $files
227     * and returns the entries of the $plugins array set in these files
228     *
229     * @param array $files list of files to include, latter overrides previous
230     * @return array with entries of the $plugins arrays of the included files
231     */
232    protected function checkRequire($files)
233    {
234        $plugins = [];
235        foreach ($files as $file) {
236            if (file_exists($file)) {
237                include_once($file);
238            }
239        }
240        return $plugins;
241    }
242
243    /**
244     * Save the current list of plugins
245     *
246     * @param bool $forceSave ;
247     *              false to save only when config changed
248     *              true to always save
249     * @return bool  true saving succeed, false saving failed
250     */
251    protected function saveList($forceSave = false)
252    {
253        global $conf;
254
255        if (empty($this->masterList)) return false;
256
257        // Rebuild list of local settings
258        $local_plugins = $this->rebuildLocal();
259        if ($local_plugins != $this->pluginCascade['local'] || $forceSave) {
260            $file = $this->lastLocalConfigFile;
261            $out = "<?php\n/*\n * Local plugin enable/disable settings\n" .
262                " * Auto-generated through plugin/extension manager\n *\n" .
263                " * NOTE: Plugins will not be added to this file unless there " .
264                "is a need to override a default setting. Plugins are\n" .
265                " *       enabled by default.\n */\n";
266            foreach ($local_plugins as $plugin => $value) {
267                $out .= "\$plugins['$plugin'] = $value;\n";
268            }
269            // backup current file (remove any existing backup)
270            if (file_exists($file)) {
271                $backup = $file . '.bak';
272                if (file_exists($backup)) @unlink($backup);
273                if (!@copy($file, $backup)) return false;
274                if ($conf['fperm']) chmod($backup, $conf['fperm']);
275            }
276            //check if can open for writing, else restore
277            return io_saveFile($file, $out);
278        }
279        return false;
280    }
281
282    /**
283     * Rebuild the set of local plugins
284     *
285     * @return array array of plugins to be saved in end($config_cascade['plugins']['local'])
286     */
287    protected function rebuildLocal()
288    {
289        //assign to local variable to avoid overwriting
290        $backup = $this->masterList;
291        //Can't do anything about protected one so rule them out completely
292        $local_default = array_diff_key($backup, $this->pluginCascade['protected']);
293        //Diff between local+default and default
294        //gives us the ones we need to check and save
295        $diffed_ones = array_diff_key($local_default, $this->pluginCascade['default']);
296        //The ones which we are sure of (list of 0s not in default)
297        $sure_plugins = array_filter($diffed_ones, [$this, 'negate']);
298        //the ones in need of diff
299        $conflicts = array_diff_key($local_default, $diffed_ones);
300        //The final list
301        return array_merge($sure_plugins, array_diff_assoc($conflicts, $this->pluginCascade['default']));
302    }
303
304    /**
305     * Build the list of plugins and cascade
306     *
307     */
308    protected function loadConfig()
309    {
310        global $config_cascade;
311        foreach (['default', 'protected'] as $type) {
312            if (array_key_exists($type, $config_cascade['plugins'])) {
313                $this->pluginCascade[$type] = $this->checkRequire($config_cascade['plugins'][$type]);
314            }
315        }
316        $local = $config_cascade['plugins']['local'];
317        $this->lastLocalConfigFile = array_pop($local);
318        $this->pluginCascade['local'] = $this->checkRequire([$this->lastLocalConfigFile]);
319        $this->pluginCascade['default'] = array_merge(
320            $this->pluginCascade['default'],
321            $this->checkRequire($local)
322        );
323        $this->masterList = array_merge(
324            $this->pluginCascade['default'],
325            $this->pluginCascade['local'],
326            $this->pluginCascade['protected']
327        );
328    }
329
330    /**
331     * Returns a list of available plugin components of given type
332     *
333     * @param string $type plugin_type name; the type of plugin to return,
334     * @param bool $enabled true to return enabled plugins,
335     *                          false to return disabled plugins
336     * @return array of plugin components of requested type
337     */
338    protected function getListByType($type, $enabled)
339    {
340        $master_list = $enabled
341            ? array_keys(array_filter($this->masterList))
342            : array_keys(array_filter($this->masterList, [$this, 'negate']));
343        $plugins = [];
344
345        foreach ($master_list as $plugin) {
346
347            if (file_exists(DOKU_PLUGIN . "$plugin/$type.php")) {
348                $plugins[] = $plugin;
349                continue;
350            }
351
352            $typedir = DOKU_PLUGIN . "$plugin/$type/";
353            if (is_dir($typedir)) {
354                if ($dp = opendir($typedir)) {
355                    while (false !== ($component = readdir($dp))) {
356                        if (strpos($component, '.') === 0 || strtolower(substr($component, -4)) !== '.php') continue;
357                        if (is_file($typedir . $component)) {
358                            $plugins[] = $plugin . '_' . substr($component, 0, -4);
359                        }
360                    }
361                    closedir($dp);
362                }
363            }
364
365        }//foreach
366
367        return $plugins;
368    }
369
370    /**
371     * Split name in a plugin name and a component name
372     *
373     * @param string $name
374     * @return array with
375     *              - plugin name
376     *              - and component name when available, otherwise empty string
377     */
378    protected function splitName($name)
379    {
380        if (!isset($this->masterList[$name])) {
381            return sexplode('_', $name, 2, '');
382        }
383
384        return [$name, ''];
385    }
386
387    /**
388     * Returns inverse boolean value of the input
389     *
390     * @param mixed $input
391     * @return bool inversed boolean value of input
392     */
393    protected function negate($input)
394    {
395        return !(bool)$input;
396    }
397}
398