Source for file plugins.php

Documentation is available at plugins.php

  1. <?php
  2. /**
  3.  * MvBlog -- An open source no-nosense blogtool
  4.  *
  5.  * Copyright (C) 2005-2006, Michiel van Baak
  6.  * Michiel van Baak <mvanbaak@users.sourceforge.net>
  7.  *
  8.  * See http://dev.mvblog.org for more information on MvBlog.
  9.  * That page also provides Bugtrackers, Filereleases etc.
  10.  *
  11.  * This program is free software, distributed under the terms of
  12.  * the GNU General Public License Version 2. See the LICENSE file
  13.  * at the top of the source tree.
  14.  *
  15.  * @package MvBlog
  16.  * @author Michiel van Baak
  17.  * @version 2.0
  18.  * @copyright 2005-2006 Michiel van Baak
  19.  */
  20. /**
  21.  * Class that holds methods to manage plugins etc.
  22.  * @package MvBlog
  23.  */
  24.  
  25.     /* constants */
  26.  
  27.     /* variables */
  28.     /**
  29.      * @var array $plugins Contains all loaded plugin objects
  30.      */
  31.     public $plugins = array();
  32.     /**
  33.      * @var string $plugindir The directory that holds all the plugins
  34.      */
  35.     public $plugindir = "";
  36.  
  37.     /* methods */
  38.     /* __construct {{{ */
  39.     /**
  40.      * Load all plugins into plugins array
  41.      *
  42.      * @param string $plugindir The dir to load
  43.      */
  44.     public function __construct($plugindir&$mvblog{
  45.         /* make sure the dir is set and that it contains sane data */
  46.         if (!strlen($plugindir)) die("Plugindir not given");
  47.         $plugindir str_replace("../../"""$plugindir)// get rid of ../
  48.         $plugindir preg_replace("/^\//si"""$plugindir);   // get rid of .
  49.         /* check if it is there, if not look 1 level up, not more */
  50.         if (!is_dir($plugindir)) {
  51.             $plugindir "../".$plugindir;
  52.         }
  53.         $this->plugindir = $plugindir;
  54.  
  55.         /* read all files and init the objects */
  56.         $handle opendir($this->plugindir);
  57.         while (false !== ($file readdir($handle))) {
  58.             if (strpos($file"."=== 0continue// dont process . .. or hidden files/dirs
  59.             if (is_dir($this->plugindir.$file)) continue// dont process directories
  60.             /* get the filename */
  61.             /* all files should be: pluginname.php */
  62.             $plugin explode("."$file);
  63.             $filename $plugin[0];
  64.             require_once($this->plugindir.$file);
  65.             $this->plugins[$filenamenew $filename($mvblog);
  66.             // FB: 20070304: Eval isn't needed here. Commented out and replaced
  67.             // by the line above. This fixed 'undefined constant' errors.
  68.             //eval("\$this->plugins[".$filename."] = new ".$filename."(\$mvblog);");
  69.         }
  70.         /* sort plugins */
  71.         ksort($this->plugins);
  72.  
  73.     }
  74.     /* }}} */
  75.     /* activate_plugin {{{ */
  76.     /**
  77.      * Activate givin plugin
  78.      * If the plugin is not loaded, do nothing
  79.      *
  80.      * @param string $plugin The plugin name to activate
  81.      * @return bool true on success, false on failure
  82.      */
  83.     public function activate_plugin($plugin{
  84.         if (array_key_exists($plugin$this->plugins)) {
  85.             $this->plugins[$plugin]->active true;
  86.             $this->plugins[$plugin]->activate();
  87.             return true;
  88.         else {
  89.             return false;
  90.         }
  91.     }
  92.     /* }}} */
  93.     /* deactivate_plugin {{{ */
  94.     /**
  95.      * deActivate givin plugin
  96.      * If the plugin is not loaded, do nothing
  97.      *
  98.      * @param string $plugin The plugin name to deactivate
  99.      * @return bool true on success, false on failure
  100.      */
  101.     public function deactivate_plugin($plugin{
  102.         if (array_key_exists($plugin$this->plugins)) {
  103.             $this->plugins[$plugin]->active false;
  104.             $this->plugins[$plugin]->deactivate();
  105.             return true;
  106.         else {
  107.             return false;
  108.         }
  109.     }
  110.     /* }}} */
  111.     /* set_active_plugins {{{ */
  112.     /**
  113.      * Activate plugins using an external array.
  114.      * This can be usefull if you store your activate plugins in a database
  115.      *
  116.      * @param array $active_plugins Array with plugin names to activate
  117.      */
  118.     public function set_active_plugins($active_plugins{
  119.         foreach ($active_plugins as $activate{
  120.             $this->activate_plugin($activate);
  121.         }
  122.     }
  123.     /* }}} */
  124.     /* get_active_plugins {{{ */
  125.     /**
  126.      * Return array of active plugins
  127.      */
  128.     public function get_active_plugins({
  129.         $return array();
  130.         foreach ($this->plugins as $name=>$plugin{
  131.             if ($plugin->active)
  132.                 $return[$name;
  133.         }
  134.         return $return;
  135.     }
  136.     /* }}} */
  137.     /* run_hooks {{{ */
  138.     /**
  139.      * Loop through all the plugins and run functions registered to given hooktype
  140.      */
  141.     public function run_hooks($type$data{
  142.         foreach ($this->plugins as $plugin{
  143.             /* only run when the plugin is active */
  144.             if ($plugin->active{
  145.                 foreach ($plugin->hooks as $hook=>$function{
  146.                     if ($hook == $type{
  147.                         $data = eval("return \$plugin->$function(\$data);");
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.         return $data;
  153.     }
  154.     /* }}} */
  155. }
  156.  
  157. /**
  158.  * Default Plugin class with predefined variables and addHook function.
  159.  * All plugins should extend on this one
  160.  */
  161. Class MvBlog_plugin {
  162.     public $hooks  = array()// de array van alle hooks en functies daaraan gebonden
  163.     public $active = false// standaard is de plugin uitgeschakeld
  164.  
  165.     public function addHook($hook_name$function_name){
  166.         $this->hooks[$hook_name$function_name;
  167.         $this->description htmlspecialchars($this->description);
  168.     }
  169. }
  170.  
  171. /**
  172.  * Plugin interface.
  173.  * A plugin should implement this to assure at least the basics are correct
  174.  */
  175. interface MvBlog_pluginiface {
  176.     public function addHook($hook_name$function_name);
  177.     public function activate();
  178.     public function deactivate();
  179. }
  180. ?>

Documentation generated on Fri, 28 Dec 2007 13:17:42 +0100 by phpDocumentor 1.4.1