xref: /dokuwiki/inc/pluginutils.php (revision 36df6fa3069d55db3c35724c4f465bde9c50b53a)
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 files
11 */
12function plugin_printCSS(){
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	}
26}
27
28/**
29 * Returns a list of available plugins of given type
30 *
31 * Returns all plugins if no type given
32 *
33 * @author Andreas Gohr <andi@splitbrain.org>
34 */
35function plugin_list($type=''){
36  $plugins = array();
37  if ($dh = opendir(DOKU_PLUGIN)) {
38    while (false !== ($file = readdir($dh))) {
39      if ($file == '.' || $file == '..') continue;
40      if (is_file(DOKU_PLUGIN.$file)) continue;
41
42	    if ($type=='' ||   @file_exists(DOKU_PLUGIN.$file.'/'.$type.'.php')){
43  	    $plugins[] = $file;
44      }
45    }
46    closedir($dh);
47  }
48  return $plugins;
49}
50
51/**
52 * Loads the given plugin and creates an object of it
53 *
54 * @author Andreas Gohr <andi@splitbrain.org>
55 *
56 * @param  $type string 	type of plugin to load
57 * @param  $name string 	name of the plugin to load
58 * @return object         the plugin object or null on failure
59 */
60function &plugin_load($type,$name){
61  //we keep all loaded plugins available in global scope for reuse
62  global $DOKU_PLUGINS;
63
64	//plugin already loaded?
65	if($DOKU_PLUGINS[$type][$name] != null){
66		return $DOKU_PLUGINS[$type][$name];
67	}
68
69  //try to load the wanted plugin file
70  if(!include_once(DOKU_PLUGIN.$name.'/'.$type.'.php')){
71    return null;
72  }
73
74  //construct class and instanciate
75  $class = $type.'_plugin_'.$name;
76  $DOKU_PLUGINS[$type][$name] = new $class;
77  return $DOKU_PLUGINS[$type][$name];
78}
79
80
81