xref: /dokuwiki/inc/pluginutils.php (revision 087b3a7fd4d81528ea23786bb2aaa707b3b4de17)
1ee20e7d1Sandi<?php
2ee20e7d1Sandi/**
3ee20e7d1Sandi * Utilities for handling plugins
4ee20e7d1Sandi *
5ee20e7d1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee20e7d1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7ee20e7d1Sandi */
8ee20e7d1Sandi
9*087b3a7fSchris// plugin related constants
10*087b3a7fSchris$plugin_types = array('admin','syntax');
114b15e09dSAndreas Gohr
124b15e09dSAndreas Gohr/**
13ee20e7d1Sandi * Returns a list of available plugins of given type
14ee20e7d1Sandi *
15*087b3a7fSchris * @param $type  string, plugin_type name;
16*087b3a7fSchris *               the type of plugin to return,
17*087b3a7fSchris *               use empty string for all types
18*087b3a7fSchris * @param $all   bool;
19*087b3a7fSchris *               false to only return enabled plugins,
20*087b3a7fSchris *               true to return both enabled and disabled plugins
21*087b3a7fSchris *
22*087b3a7fSchris * @return       array of plugin names
234b15e09dSAndreas Gohr *
24ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
25ee20e7d1Sandi */
26*087b3a7fSchrisfunction plugin_list($type='',$all=false){
27ee20e7d1Sandi  $plugins = array();
28ee20e7d1Sandi  if ($dh = opendir(DOKU_PLUGIN)) {
297fb56887Schris    while (false !== ($plugin = readdir($dh))) {
30d66e3ddfSchris      if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
317fb56887Schris      if (is_file(DOKU_PLUGIN.$plugin)) continue;
32ee20e7d1Sandi
33*087b3a7fSchris			// if required, skip disabled plugins
34*087b3a7fSchris			if (!$all && plugin_isdisabled($plugin)) continue;
35*087b3a7fSchris
367fb56887Schris      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
377fb56887Schris          $plugins[] = $plugin;
387fb56887Schris      } else {
397fb56887Schris        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
407fb56887Schris          while (false !== ($component = readdir($dp))) {
417fb56887Schris            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
427fb56887Schris            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
437fb56887Schris              $plugins[] = $plugin.'_'.substr($component, 0, -4);
447fb56887Schris            }
457fb56887Schris          }
467fb56887Schris        closedir($dp);
47ee20e7d1Sandi        }
48ee20e7d1Sandi      }
496446f486SEsther Brunner    }
50ee20e7d1Sandi    closedir($dh);
51ee20e7d1Sandi  }
52ee20e7d1Sandi  return $plugins;
53ee20e7d1Sandi}
54ee20e7d1Sandi
55ee20e7d1Sandi/**
56ee20e7d1Sandi * Loads the given plugin and creates an object of it
57ee20e7d1Sandi *
58ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
59ee20e7d1Sandi *
60ee20e7d1Sandi * @param  $type string     type of plugin to load
61ee20e7d1Sandi * @param  $name string     name of the plugin to load
624f32716eSAndreas Gohr * @return objectreference  the plugin object or null on failure
63ee20e7d1Sandi */
64a46d0d65SAndreas Gohrfunction &plugin_load($type,$name){
65ee20e7d1Sandi  //we keep all loaded plugins available in global scope for reuse
66ee20e7d1Sandi  global $DOKU_PLUGINS;
67ee20e7d1Sandi
684f32716eSAndreas Gohr
69ee20e7d1Sandi  //plugin already loaded?
70ee20e7d1Sandi  if($DOKU_PLUGINS[$type][$name] != null){
71a46d0d65SAndreas Gohr    return $DOKU_PLUGINS[$type][$name];
72ee20e7d1Sandi  }
73ee20e7d1Sandi
74ee20e7d1Sandi  //try to load the wanted plugin file
75da682d4eSEsther Brunner  if (file_exists(DOKU_PLUGIN."$name/$type.php")){
76da682d4eSEsther Brunner    include_once(DOKU_PLUGIN."$name/$type.php");
77da682d4eSEsther Brunner  }else{
787fb56887Schris    list($plugin, $component) = preg_split("/_/",$name, 2);
79da682d4eSEsther Brunner    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
80a46d0d65SAndreas Gohr        return null;
81ee20e7d1Sandi    }
827fb56887Schris  }
837fb56887Schris
84ee20e7d1Sandi  //construct class and instanciate
85ee20e7d1Sandi  $class = $type.'_plugin_'.$name;
86c90b2fb1Schris  if (!class_exists($class)) return null;
87c90b2fb1Schris
88ee20e7d1Sandi  $DOKU_PLUGINS[$type][$name] = new $class;
89a46d0d65SAndreas Gohr  return $DOKU_PLUGINS[$type][$name];
90ee20e7d1Sandi}
91*087b3a7fSchris
92*087b3a7fSchrisfunction plugin_isdisabled($name) { return @file_exists(DOKU_PLUGIN.$name.'/disabled'); }
93*087b3a7fSchrisfunction plugin_enable($name) { return @unlink(DOKU_PLUGIN.$name.'/disabled'); }
94*087b3a7fSchrisfunction plugin_disable($name) { return @touch(DOKU_PLUGIN.$name.'/disabled'); }
95