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