xref: /dokuwiki/inc/pluginutils.php (revision 00a7b5ad8c432fcfb4f394ab300d802a0fe4c35c)
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 * prints needed HTML to include plugin CSS and JS files
11 */
12function plugin_printCSSJS(){
13    global $conf;
14
15    if (isset($conf['pluginmanager']) && $conf['pluginmanager']) {
16        // individual plugin instances of the files swept into one file each
17        $dir = "lib/plugins/plugin_";
18            if(@file_exists(DOKU_INC.$dir.'style.css')){
19                print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
20        }
21            if(@file_exists(DOKU_INC.$dir.'screen.css')){
22                print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
23        }
24            if(@file_exists(DOKU_INC.$dir.'print.css')){
25                print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
26        }
27            if(@file_exists(DOKU_INC.$dir.'script.js')){
28          print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
29        }
30    } else {
31        // no plugin manager so individual instances of these files for any plugin that uses them
32        $plugins = plugin_list();
33        foreach ($plugins as $p){
34            $dir = "lib/plugins/$p/";
35            if(@file_exists(DOKU_INC.$dir.'style.css')){
36                print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
37            }
38            if(@file_exists(DOKU_INC.$dir.'screen.css')){
39                print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
40            }
41            if(@file_exists(DOKU_INC.$dir.'print.css')){
42                print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
43            }
44            if(@file_exists(DOKU_INC.$dir.'script.js')){
45                print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
46            }
47        }
48    }
49}
50
51/**
52 * Returns a list of available plugins of given type
53 *
54 * Returns all plugins if no type given
55 *
56 * @author Andreas Gohr <andi@splitbrain.org>
57 */
58function plugin_list($type=''){
59  $plugins = array();
60  if ($dh = opendir(DOKU_PLUGIN)) {
61    while (false !== ($plugin = readdir($dh))) {
62      if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
63      if (is_file(DOKU_PLUGIN.$plugin)) continue;
64
65      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
66          $plugins[] = $plugin;
67      } else {
68        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
69          while (false !== ($component = readdir($dp))) {
70            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
71            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
72              $plugins[] = $plugin.'_'.substr($component, 0, -4);
73            }
74          }
75        closedir($dp);
76        }
77      }
78    }
79    closedir($dh);
80  }
81  return $plugins;
82}
83
84/**
85 * Loads the given plugin and creates an object of it
86 *
87 * @author Andreas Gohr <andi@splitbrain.org>
88 *
89 * @param  $type string     type of plugin to load
90 * @param  $name string     name of the plugin to load
91 * @return objectreference  the plugin object or null on failure
92 */
93function &plugin_load($type,$name){
94  //we keep all loaded plugins available in global scope for reuse
95  global $DOKU_PLUGINS;
96
97
98  //plugin already loaded?
99  if($DOKU_PLUGINS[$type][$name] != null){
100    return $DOKU_PLUGINS[$type][$name];
101  }
102
103  //try to load the wanted plugin file
104  if (file_exists(DOKU_PLUGIN."$name/$type.php")){
105    include_once(DOKU_PLUGIN."$name/$type.php");
106  }else{
107    list($plugin, $component) = preg_split("/_/",$name, 2);
108    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
109        return null;
110    }
111  }
112
113  //construct class and instanciate
114  $class = $type.'_plugin_'.$name;
115  if (!class_exists($class)) return null;
116
117  $DOKU_PLUGINS[$type][$name] = new $class;
118  return $DOKU_PLUGINS[$type][$name];
119}
120