xref: /dokuwiki/inc/pluginutils.php (revision da682d4efe15ba703873b4989e0fdb895dd3f140)
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(){
13d66e3ddfSchris    global $conf;
14d66e3ddfSchris
15521f0997Schris    if (isset($conf['pluginmanager']) && $conf['pluginmanager']) {
16d66e3ddfSchris        // individual plugin instances of the files swept into one file each
17d66e3ddfSchris        $dir = "lib/plugins/plugin_";
18d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'style.css')){
19d66e3ddfSchris                print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
20d66e3ddfSchris        }
21d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'screen.css')){
22d66e3ddfSchris                print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
23d66e3ddfSchris        }
24d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'print.css')){
25d66e3ddfSchris                print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
26d66e3ddfSchris        }
27d66e3ddfSchris            if(@file_exists(DOKU_INC.$dir.'script.js')){
28d66e3ddfSchris          print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
29d66e3ddfSchris        }
30d66e3ddfSchris    } else {
31d66e3ddfSchris        // 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    }
49d66e3ddfSchris}
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))) {
62d66e3ddfSchris      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
104*da682d4eSEsther Brunner  if (file_exists(DOKU_PLUGIN."$name/$type.php")){
105*da682d4eSEsther Brunner    include_once(DOKU_PLUGIN."$name/$type.php");
106*da682d4eSEsther Brunner  }else{
1077fb56887Schris    list($plugin, $component) = preg_split("/_/",$name, 2);
108*da682d4eSEsther Brunner    if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
109a46d0d65SAndreas Gohr        return null;
110ee20e7d1Sandi    }
1117fb56887Schris  }
1127fb56887Schris
113ee20e7d1Sandi  //construct class and instanciate
114ee20e7d1Sandi  $class = $type.'_plugin_'.$name;
115c90b2fb1Schris  if (!class_exists($class)) return null;
116c90b2fb1Schris
117ee20e7d1Sandi  $DOKU_PLUGINS[$type][$name] = new $class;
118a46d0d65SAndreas Gohr  return $DOKU_PLUGINS[$type][$name];
119ee20e7d1Sandi}
120