xref: /dokuwiki/inc/pluginutils.php (revision 1ef8efdac923215cecaf114a3bedcb96fca97781)
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
10/**
11 * Returns a list of available plugins of given type
12 *
13 * Returns all plugins if no type given
14 *
15 * @author Andreas Gohr <andi@splitbrain.org>
16 */
17function plugin_list($type=''){
18  $plugins = array();
19  if ($dh = opendir(DOKU_PLUGIN)) {
20    while (false !== ($plugin = readdir($dh))) {
21      if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
22      if (is_file(DOKU_PLUGIN.$plugin)) continue;
23
24      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
25          $plugins[] = $plugin;
26      } else {
27        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
28          while (false !== ($component = readdir($dp))) {
29            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
30            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
31              $plugins[] = $plugin.'_'.substr($component, 0, -4);
32            }
33          }
34        closedir($dp);
35        }
36      }
37    }
38    closedir($dh);
39  }
40  return $plugins;
41}
42
43/**
44 * Loads the given plugin and creates an object of it
45 *
46 * @author Andreas Gohr <andi@splitbrain.org>
47 *
48 * @param  $type string     type of plugin to load
49 * @param  $name string     name of the plugin to load
50 * @return objectreference  the plugin object or null on failure
51 */
52function &plugin_load($type,$name){
53  //we keep all loaded plugins available in global scope for reuse
54  global $DOKU_PLUGINS;
55
56
57  //plugin already loaded?
58  if($DOKU_PLUGINS[$type][$name] != null){
59    return $DOKU_PLUGINS[$type][$name];
60  }
61
62  //try to load the wanted plugin file
63  if (file_exists(DOKU_PLUGIN."$name/$type.php")){
64    include_once(DOKU_PLUGIN."$name/$type.php");
65  }else{
66    list($plugin, $component) = preg_split("/_/",$name, 2);
67    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
68        return null;
69    }
70  }
71
72  //construct class and instanciate
73  $class = $type.'_plugin_'.$name;
74  if (!class_exists($class)) return null;
75
76  $DOKU_PLUGINS[$type][$name] = new $class;
77  return $DOKU_PLUGINS[$type][$name];
78}
79