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