Skip to content
Snippets Groups Projects
pluginutils.php 3.13 KiB
Newer Older
andi's avatar
andi committed
<?php
/**
 * Utilities for handling plugins
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

Andreas Gohr's avatar
Andreas Gohr committed
 * prints needed HTML to include plugin CSS and JS files
Andreas Gohr's avatar
Andreas Gohr committed
function plugin_printCSSJS(){
  $plugins = plugin_list();
  foreach ($plugins as $p){
    $dir = "lib/plugins/$p/";
        if(@file_exists(DOKU_INC.$dir.'style.css')){
            print '  <link rel="stylesheet" type="text/css" href="'.DOKU_BASE.$dir.'style.css" />'."\n";
        if(@file_exists(DOKU_INC.$dir.'screen.css')){
            print '  <link rel="stylesheet" media="screen" type="text/css" href="'.DOKU_BASE.$dir.'screen.css" />'."\n";
        if(@file_exists(DOKU_INC.$dir.'print.css')){
            print '  <link rel="stylesheet" media="print" type="text/css" href="'.DOKU_BASE.$dir.'print.css" />'."\n";
Andreas Gohr's avatar
Andreas Gohr committed
    }
        if(@file_exists(DOKU_INC.$dir.'script.js')){
      print '  <script type="text/javascript" language="javascript" charset="utf-8" src="'.DOKU_BASE.$dir.'script.js"></script>'."\n";
andi's avatar
andi committed
/**
 * Returns a list of available plugins of given type
 *
 * Returns all plugins if no type given
 *
andi's avatar
andi committed
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function plugin_list($type=''){
andi's avatar
andi committed
  $plugins = array();
  if ($dh = opendir(DOKU_PLUGIN)) {
    while (false !== ($plugin = readdir($dh))) {
      if ($plugin == '.' || $plugin == '..') continue;
      if (is_file(DOKU_PLUGIN.$plugin)) continue;
      if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
          $plugins[] = $plugin;
      } else {
        if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
          while (false !== ($component = readdir($dp))) {
            if ($component == '.' || $component == '..' || strtolower(substr($component, -4)) != ".php") continue;
            if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
              $plugins[] = $plugin.'_'.substr($component, 0, -4);
            }
          }
        }
        closedir($dp);
andi's avatar
andi committed
      }
    }
    closedir($dh);
  }
  return $plugins;
}

/**
 * Loads the given plugin and creates an object of it
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 *
 * @param  $type string     type of plugin to load
 * @param  $name string     name of the plugin to load
 * @return object         the plugin object or null on failure
andi's avatar
andi committed
 */
function &plugin_load($type,$name){
andi's avatar
andi committed
  //we keep all loaded plugins available in global scope for reuse
  global $DOKU_PLUGINS;

    //plugin already loaded?
    if($DOKU_PLUGINS[$type][$name] != null){
        return $DOKU_PLUGINS[$type][$name];
    }
andi's avatar
andi committed

  //try to load the wanted plugin file
  if(!@include_once(DOKU_PLUGIN."$name/$type.php")){
    list($plugin, $component) = preg_split("/_/",$name, 2);
    if (!$component || !@include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
        return null;
    }
  global $plugin_investigate_pluginorder;
  if (!isset($plugin_investigate_pluginorder)) $plugin_investigate_pluginorder = array();
  $plugin_investigate_pluginorder[] = $name;

andi's avatar
andi committed
  //construct class and instanciate
  $class = $type.'_plugin_'.$name;
  $DOKU_PLUGINS[$type][$name] = new $class;
  return $DOKU_PLUGINS[$type][$name];
andi's avatar
andi committed
}