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