xref: /dokuwiki/inc/pluginutils.php (revision d66e3ddfed27523627d33cd17905445d27688afa)
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(){
13*d66e3ddfSchris    global $conf;
14*d66e3ddfSchris
15*d66e3ddfSchris    if (isset($conf['plugin_manager']) && $conf['plugin_manager']) {
16*d66e3ddfSchris        // individual plugin instances of the files swept into one file each
17*d66e3ddfSchris        $dir = "lib/plugins/plugin_";
18*d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'style.css')){
19*d66e3ddfSchris                print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
20*d66e3ddfSchris        }
21*d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'screen.css')){
22*d66e3ddfSchris                print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
23*d66e3ddfSchris        }
24*d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'print.css')){
25*d66e3ddfSchris                print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
26*d66e3ddfSchris        }
27*d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'script.js')){
28*d66e3ddfSchris          print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
29*d66e3ddfSchris        }
30*d66e3ddfSchris    } else {
31*d66e3ddfSchris        // no plugin manager so individual instances of these files for any plugin that uses them
324b15e09dSAndreas Gohr        $plugins = plugin_list();
334b15e09dSAndreas Gohr        foreach ($plugins as $p){
344b15e09dSAndreas Gohr            $dir = "lib/plugins/$p/";
354b15e09dSAndreas Gohr            if(@file_exists(DOKU_INC.$dir.'style.css')){
364b15e09dSAndreas Gohr                print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
374b15e09dSAndreas Gohr            }
384b15e09dSAndreas Gohr            if(@file_exists(DOKU_INC.$dir.'screen.css')){
394b15e09dSAndreas Gohr                print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
404b15e09dSAndreas Gohr            }
414b15e09dSAndreas Gohr            if(@file_exists(DOKU_INC.$dir.'print.css')){
424b15e09dSAndreas Gohr                print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
434b15e09dSAndreas Gohr            }
443600bd52SAndreas Gohr            if(@file_exists(DOKU_INC.$dir.'script.js')){
45e2e0a40fSchris                print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
463600bd52SAndreas Gohr            }
474b15e09dSAndreas Gohr        }
484b15e09dSAndreas Gohr    }
49*d66e3ddfSchris}
504b15e09dSAndreas Gohr
514b15e09dSAndreas Gohr/**
52ee20e7d1Sandi * Returns a list of available plugins of given type
53ee20e7d1Sandi *
544b15e09dSAndreas Gohr * Returns all plugins if no type given
554b15e09dSAndreas Gohr *
56ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
57ee20e7d1Sandi */
584b15e09dSAndreas Gohrfunction plugin_list($type=''){
59ee20e7d1Sandi  $plugins = array();
60ee20e7d1Sandi  if ($dh = opendir(DOKU_PLUGIN)) {
617fb56887Schris    while (false !== ($plugin = readdir($dh))) {
62*d66e3ddfSchris      if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
637fb56887Schris      if (is_file(DOKU_PLUGIN.$plugin)) continue;
64ee20e7d1Sandi
657fb56887Schris      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
667fb56887Schris          $plugins[] = $plugin;
677fb56887Schris      } else {
687fb56887Schris        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
697fb56887Schris          while (false !== ($component = readdir($dp))) {
707fb56887Schris            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
717fb56887Schris            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
727fb56887Schris              $plugins[] = $plugin.'_'.substr($component, 0, -4);
737fb56887Schris            }
747fb56887Schris          }
757fb56887Schris        closedir($dp);
76ee20e7d1Sandi        }
77ee20e7d1Sandi      }
786446f486SEsther Brunner    }
79ee20e7d1Sandi    closedir($dh);
80ee20e7d1Sandi  }
81ee20e7d1Sandi  return $plugins;
82ee20e7d1Sandi}
83ee20e7d1Sandi
84ee20e7d1Sandi/**
85ee20e7d1Sandi * Loads the given plugin and creates an object of it
86ee20e7d1Sandi *
87ee20e7d1Sandi * @author Andreas Gohr <andi@splitbrain.org>
88ee20e7d1Sandi *
89ee20e7d1Sandi * @param  $type string     type of plugin to load
90ee20e7d1Sandi * @param  $name string     name of the plugin to load
914f32716eSAndreas Gohr * @return objectreference  the plugin object or null on failure
92ee20e7d1Sandi */
93a46d0d65SAndreas Gohrfunction &plugin_load($type,$name){
94ee20e7d1Sandi  //we keep all loaded plugins available in global scope for reuse
95ee20e7d1Sandi  global $DOKU_PLUGINS;
96ee20e7d1Sandi
974f32716eSAndreas Gohr
98ee20e7d1Sandi  //plugin already loaded?
99ee20e7d1Sandi  if($DOKU_PLUGINS[$type][$name] != null){
100a46d0d65SAndreas Gohr    return $DOKU_PLUGINS[$type][$name];
101ee20e7d1Sandi  }
102ee20e7d1Sandi
103ee20e7d1Sandi  //try to load the wanted plugin file
104c90b2fb1Schris  if(!@include_once(DOKU_PLUGIN."$name/$type.php")){
1057fb56887Schris    list($plugin, $component) = preg_split("/_/",$name, 2);
106c90b2fb1Schris    if (!$component || !@include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
107a46d0d65SAndreas Gohr        return null;
108ee20e7d1Sandi    }
1097fb56887Schris  }
1107fb56887Schris
111ee20e7d1Sandi  //construct class and instanciate
112ee20e7d1Sandi  $class = $type.'_plugin_'.$name;
113c90b2fb1Schris  if (!class_exists($class)) return null;
114c90b2fb1Schris
115ee20e7d1Sandi  $DOKU_PLUGINS[$type][$name] = new $class;
116a46d0d65SAndreas Gohr  return $DOKU_PLUGINS[$type][$name];
117ee20e7d1Sandi}
118