xref: /dokuwiki/inc/pluginutils.php (revision 4f32716ed3cd44b3827d2a778211d2516344f77b)
1ee20e7d1Sandi<?php
2ee20e7d1Sandi/**
3ee20e7d1Sandi * Utilities for handling plugins
4ee20e7d1Sandi *
5ee20e7d1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee20e7d1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7ee20e7d1Sandi */
8ee20e7d1Sandi
9ee20e7d1Sandi/**
103600bd52SAndreas Gohr * prints needed HTML to include plugin CSS and JS files
114b15e09dSAndreas Gohr */
123600bd52SAndreas Gohrfunction plugin_printCSSJS(){
134b15e09dSAndreas Gohr  $plugins = plugin_list();
144b15e09dSAndreas Gohr  foreach ($plugins as $p){
154b15e09dSAndreas Gohr    $dir = "lib/plugins/$p/";
164b15e09dSAndreas Gohr        if(@file_exists(DOKU_INC.$dir.'style.css')){
174b15e09dSAndreas Gohr            print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
184b15e09dSAndreas Gohr    }
194b15e09dSAndreas Gohr        if(@file_exists(DOKU_INC.$dir.'screen.css')){
204b15e09dSAndreas Gohr            print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
214b15e09dSAndreas Gohr    }
224b15e09dSAndreas Gohr        if(@file_exists(DOKU_INC.$dir.'print.css')){
234b15e09dSAndreas Gohr            print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
244b15e09dSAndreas Gohr    }
253600bd52SAndreas Gohr        if(@file_exists(DOKU_INC.$dir.'script.js')){
26e2e0a40fSchris      print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
273600bd52SAndreas Gohr    }
284b15e09dSAndreas Gohr    }
294b15e09dSAndreas Gohr}
304b15e09dSAndreas Gohr
314b15e09dSAndreas Gohr/**
32ee20e7d1Sandi * Returns a list of available plugins of given type
33ee20e7d1Sandi *
344b15e09dSAndreas Gohr * Returns all plugins if no type given
354b15e09dSAndreas Gohr *
36ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
37ee20e7d1Sandi */
384b15e09dSAndreas Gohrfunction plugin_list($type=''){
39ee20e7d1Sandi  $plugins = array();
40ee20e7d1Sandi  if ($dh = opendir(DOKU_PLUGIN)) {
417fb56887Schris    while (false !== ($plugin = readdir($dh))) {
427fb56887Schris      if ($plugin == '.' || $plugin == '..') continue;
437fb56887Schris      if (is_file(DOKU_PLUGIN.$plugin)) continue;
44ee20e7d1Sandi
457fb56887Schris      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
467fb56887Schris          $plugins[] = $plugin;
477fb56887Schris      } else {
487fb56887Schris        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
497fb56887Schris          while (false !== ($component = readdir($dp))) {
507fb56887Schris            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
517fb56887Schris            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
527fb56887Schris              $plugins[] = $plugin.'_'.substr($component, 0, -4);
537fb56887Schris            }
547fb56887Schris          }
557fb56887Schris        closedir($dp);
56ee20e7d1Sandi        }
57ee20e7d1Sandi      }
586446f486SEsther Brunner    }
59ee20e7d1Sandi    closedir($dh);
60ee20e7d1Sandi  }
61ee20e7d1Sandi  return $plugins;
62ee20e7d1Sandi}
63ee20e7d1Sandi
64ee20e7d1Sandi/**
65ee20e7d1Sandi * Loads the given plugin and creates an object of it
66ee20e7d1Sandi *
67ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
68ee20e7d1Sandi *
69ee20e7d1Sandi * @param  $type string     type of plugin to load
70ee20e7d1Sandi * @param  $name string     name of the plugin to load
71*4f32716eSAndreas Gohr * @return objectreference  the plugin object or null on failure
72ee20e7d1Sandi */
73a46d0d65SAndreas Gohrfunction &plugin_load($type,$name){
74ee20e7d1Sandi  //we keep all loaded plugins available in global scope for reuse
75ee20e7d1Sandi  global $DOKU_PLUGINS;
76ee20e7d1Sandi
77*4f32716eSAndreas Gohr
78ee20e7d1Sandi  //plugin already loaded?
79ee20e7d1Sandi  if($DOKU_PLUGINS[$type][$name] != null){
80a46d0d65SAndreas Gohr    return $DOKU_PLUGINS[$type][$name];
81ee20e7d1Sandi  }
82ee20e7d1Sandi
83ee20e7d1Sandi  //try to load the wanted plugin file
84*4f32716eSAndreas Gohr  if(!include_once(DOKU_PLUGIN."$name/$type.php")){
857fb56887Schris    list($plugin, $component) = preg_split("/_/",$name, 2);
86*4f32716eSAndreas Gohr    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
87a46d0d65SAndreas Gohr        return null;
88ee20e7d1Sandi    }
897fb56887Schris  }
907fb56887Schris
91*4f32716eSAndreas Gohr/* FIXME: chris: what's this for?
927fb56887Schris  global $plugin_investigate_pluginorder;
937fb56887Schris  if (!isset($plugin_investigate_pluginorder)) $plugin_investigate_pluginorder = array();
947fb56887Schris  $plugin_investigate_pluginorder[] = $name;
95*4f32716eSAndreas Gohr*/
96ee20e7d1Sandi
97ee20e7d1Sandi  //construct class and instanciate
98ee20e7d1Sandi  $class = $type.'_plugin_'.$name;
99ee20e7d1Sandi  $DOKU_PLUGINS[$type][$name] = new $class;
100a46d0d65SAndreas Gohr  return $DOKU_PLUGINS[$type][$name];
101ee20e7d1Sandi}
102