xref: /dokuwiki/inc/pluginutils.php (revision c90b2fb183974c22359b17366599543007f0eea3)
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        closedir($dp);
56        }
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 objectreference  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
78  //plugin already loaded?
79  if($DOKU_PLUGINS[$type][$name] != null){
80    return $DOKU_PLUGINS[$type][$name];
81  }
82
83  //try to load the wanted plugin file
84  if(!@include_once(DOKU_PLUGIN."$name/$type.php")){
85    list($plugin, $component) = preg_split("/_/",$name, 2);
86    if (!$component || !@include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
87        return null;
88    }
89  }
90
91  //construct class and instanciate
92  $class = $type.'_plugin_'.$name;
93  if (!class_exists($class)) return null;
94
95  $DOKU_PLUGINS[$type][$name] = new $class;
96  return $DOKU_PLUGINS[$type][$name];
97}
98