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