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