xref: /dokuwiki/inc/pluginutils.php (revision 03c4aec3c817c51eda2cf5c241f76e3bef585799)
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
9087b3a7fSchris// plugin related constants
10*03c4aec3Schrisif(!defined('DOKU_PLUGIN'))  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11f65bfee1Schris$plugin_types = array('admin','syntax','action');
124b15e09dSAndreas Gohr
134b15e09dSAndreas Gohr/**
14ee20e7d1Sandi * Returns a list of available plugins of given type
15ee20e7d1Sandi *
16087b3a7fSchris * @param $type  string, plugin_type name;
17087b3a7fSchris *               the type of plugin to return,
18087b3a7fSchris *               use empty string for all types
19087b3a7fSchris * @param $all   bool;
20087b3a7fSchris *               false to only return enabled plugins,
21087b3a7fSchris *               true to return both enabled and disabled plugins
22087b3a7fSchris *
23087b3a7fSchris * @return       array of plugin names
244b15e09dSAndreas Gohr *
25ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
26ee20e7d1Sandi */
27087b3a7fSchrisfunction plugin_list($type='',$all=false){
28ee20e7d1Sandi  $plugins = array();
29ee20e7d1Sandi  if ($dh = opendir(DOKU_PLUGIN)) {
307fb56887Schris    while (false !== ($plugin = readdir($dh))) {
31d66e3ddfSchris      if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
327fb56887Schris      if (is_file(DOKU_PLUGIN.$plugin)) continue;
33ee20e7d1Sandi
34087b3a7fSchris			// if required, skip disabled plugins
35087b3a7fSchris			if (!$all && plugin_isdisabled($plugin)) continue;
36087b3a7fSchris
377fb56887Schris      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
387fb56887Schris          $plugins[] = $plugin;
397fb56887Schris      } else {
407fb56887Schris        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
417fb56887Schris          while (false !== ($component = readdir($dp))) {
427fb56887Schris            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
437fb56887Schris            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
447fb56887Schris              $plugins[] = $plugin.'_'.substr($component, 0, -4);
457fb56887Schris            }
467fb56887Schris          }
477fb56887Schris        closedir($dp);
48ee20e7d1Sandi        }
49ee20e7d1Sandi      }
506446f486SEsther Brunner    }
51ee20e7d1Sandi    closedir($dh);
52ee20e7d1Sandi  }
53ee20e7d1Sandi  return $plugins;
54ee20e7d1Sandi}
55ee20e7d1Sandi
56ee20e7d1Sandi/**
57ee20e7d1Sandi * Loads the given plugin and creates an object of it
58ee20e7d1Sandi *
59ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
60ee20e7d1Sandi *
61ee20e7d1Sandi * @param  $type string     type of plugin to load
62ee20e7d1Sandi * @param  $name string     name of the plugin to load
634f32716eSAndreas Gohr * @return objectreference  the plugin object or null on failure
64ee20e7d1Sandi */
65a46d0d65SAndreas Gohrfunction &plugin_load($type,$name){
66ee20e7d1Sandi  //we keep all loaded plugins available in global scope for reuse
67ee20e7d1Sandi  global $DOKU_PLUGINS;
68ee20e7d1Sandi
694f32716eSAndreas Gohr
70ee20e7d1Sandi  //plugin already loaded?
71*03c4aec3Schris  if(!empty($DOKU_PLUGINS[$type][$name])){
72a46d0d65SAndreas Gohr    return $DOKU_PLUGINS[$type][$name];
73ee20e7d1Sandi  }
74ee20e7d1Sandi
75ee20e7d1Sandi  //try to load the wanted plugin file
76da682d4eSEsther Brunner  if (file_exists(DOKU_PLUGIN."$name/$type.php")){
77da682d4eSEsther Brunner    include_once(DOKU_PLUGIN."$name/$type.php");
78da682d4eSEsther Brunner  }else{
797fb56887Schris    list($plugin, $component) = preg_split("/_/",$name, 2);
80da682d4eSEsther Brunner    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
81a46d0d65SAndreas Gohr        return null;
82ee20e7d1Sandi    }
837fb56887Schris  }
847fb56887Schris
85ee20e7d1Sandi  //construct class and instanciate
86ee20e7d1Sandi  $class = $type.'_plugin_'.$name;
87c90b2fb1Schris  if (!class_exists($class)) return null;
88c90b2fb1Schris
89ee20e7d1Sandi  $DOKU_PLUGINS[$type][$name] = new $class;
90a46d0d65SAndreas Gohr  return $DOKU_PLUGINS[$type][$name];
91ee20e7d1Sandi}
92087b3a7fSchris
93087b3a7fSchrisfunction plugin_isdisabled($name) { return @file_exists(DOKU_PLUGIN.$name.'/disabled'); }
94087b3a7fSchrisfunction plugin_enable($name) { return @unlink(DOKU_PLUGIN.$name.'/disabled'); }
95087b3a7fSchrisfunction plugin_disable($name) { return @touch(DOKU_PLUGIN.$name.'/disabled'); }
96