xref: /dokuwiki/inc/pluginutils.php (revision 78c7c8c9da23cd3799beecd1da873261238af12b)
1<?php
2/**
3 * Utilities for handling plugins
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9// plugin related constants
10$plugin_types = array('admin','syntax');
11
12/**
13 * Returns a list of available plugins of given type
14 *
15 * @param $type  string, plugin_type name;
16 *               the type of plugin to return,
17 *               use empty string for all types
18 * @param $all   bool;
19 *               false to only return enabled plugins,
20 *               true to return both enabled and disabled plugins
21 *
22 * @return       array of plugin names
23 *
24 * @author Andreas Gohr <andi@splitbrain.org>
25 */
26function plugin_list($type='',$all=false){
27  $plugins = array();
28  if ($dh = opendir(DOKU_PLUGIN)) {
29    while (false !== ($plugin = readdir($dh))) {
30      if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
31      if (is_file(DOKU_PLUGIN.$plugin)) continue;
32
33			// if required, skip disabled plugins
34			if (!$all && plugin_isdisabled($plugin)) continue;
35
36      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
37          $plugins[] = $plugin;
38      } else {
39        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
40          while (false !== ($component = readdir($dp))) {
41            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
42            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
43              $plugins[] = $plugin.'_'.substr($component, 0, -4);
44            }
45          }
46        closedir($dp);
47        }
48      }
49    }
50    closedir($dh);
51  }
52  return $plugins;
53}
54
55/**
56 * Loads the given plugin and creates an object of it
57 *
58 * @author Andreas Gohr <andi@splitbrain.org>
59 *
60 * @param  $type string     type of plugin to load
61 * @param  $name string     name of the plugin to load
62 * @return objectreference  the plugin object or null on failure
63 */
64function &plugin_load($type,$name){
65  //we keep all loaded plugins available in global scope for reuse
66  global $DOKU_PLUGINS;
67
68
69  //plugin already loaded?
70  if($DOKU_PLUGINS[$type][$name] != null){
71    return $DOKU_PLUGINS[$type][$name];
72  }
73
74  //try to load the wanted plugin file
75  if (file_exists(DOKU_PLUGIN."$name/$type.php")){
76    include_once(DOKU_PLUGIN."$name/$type.php");
77  }else{
78    list($plugin, $component) = preg_split("/_/",$name, 2);
79    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
80        return null;
81    }
82  }
83
84  //construct class and instanciate
85  $class = $type.'_plugin_'.$name;
86  if (!class_exists($class)) return null;
87
88  $DOKU_PLUGINS[$type][$name] = new $class;
89  return $DOKU_PLUGINS[$type][$name];
90}
91
92function plugin_isdisabled($name) { return @file_exists(DOKU_PLUGIN.$name.'/disabled'); }
93function plugin_enable($name) { return @unlink(DOKU_PLUGIN.$name.'/disabled'); }
94function plugin_disable($name) { return @touch(DOKU_PLUGIN.$name.'/disabled'); }
95