| 1 | <?php |
| 2 | /* |
| 3 | * Copyright (C) 2003-2004 Polytechnique.org |
| 4 | * http://opensource.polytechnique.org/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | // dependency on PEAR |
| 22 | require_once 'System.php'; |
| 23 | |
| 24 | |
| 25 | /** This class provides a configuration tool for Diogenes plugins. |
| 26 | */ |
| 27 | class Diogenes_Plugin_Editor { |
| 28 | /** Should we show plugins parameters? */ |
| 29 | var $show_params = 1; |
| 30 | |
| 31 | /** Should editing functions be disabled? */ |
| 32 | var $readonly = false; |
| 33 | |
| 34 | /** The alias of the barrel we are working on. */ |
| 35 | var $plug_barrel; |
| 36 | |
| 37 | /** The PID of the page we are working on. */ |
| 38 | var $plug_page; |
| 39 | |
| 40 | /** The write permissions of the page we are working on. */ |
| 41 | var $plug_page_wperms; |
| 42 | |
| 43 | /** Construct a new plugin editor. |
| 44 | * |
| 45 | * @param $plug_barrel |
| 46 | * @param $plug_page |
| 47 | * @param $plug_page_wperms |
| 48 | */ |
| 49 | function Diogenes_Plugin_Editor($plug_barrel, $plug_page, $plug_page_wperms = '') |
| 50 | { |
| 51 | $this->plug_barrel = $plug_barrel; |
| 52 | $this->plug_page = $plug_page; |
| 53 | $this->plug_page_wperms = $plug_page_wperms; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** Run the plugin editor. |
| 58 | * |
| 59 | * @param $page |
| 60 | * @param $outputvar |
| 61 | */ |
| 62 | function run(&$page, $outputvar = '') |
| 63 | { |
| 64 | global $globals; |
| 65 | |
| 66 | $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
| 67 | $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : ''; |
| 68 | |
| 69 | // load all available plugins |
| 70 | $cachefile = $globals->plugins->cacheFile($this->plug_barrel); |
| 71 | |
| 72 | // if the tree cache does not exits try to init it |
| 73 | if(!file_exists($cachefile)) |
| 74 | { |
| 75 | $globals->plugins->compileCache($this->plug_barrel, $page); |
| 76 | } |
| 77 | $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel); |
| 78 | $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page); |
| 79 | |
| 80 | // handle updates |
| 81 | $rebuild_cache = 0; |
| 82 | switch ($action) { |
| 83 | case "move_up": case "move_down": |
| 84 | if ($this->readonly) die("Sorry, this plugin view is read-only."); |
| 85 | |
| 86 | $delta = ($action == "move_down") ? 1 : -1; |
| 87 | $page->info("moving plugin '$target'.."); |
| 88 | $plugcache_a = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $target); |
| 89 | $plug_a =& $globals->plugins->load($plugcache_a['name'], $plugcache_a); |
| 90 | |
| 91 | if (is_object($plug_a) && ($plug_a->active)) { |
| 92 | $old_pos = $plug_a->pos; |
| 93 | $plugcache_b = $globals->plugins->cacheGetAtPos($cache, $this->plug_barrel, $this->plug_page, $old_pos + $delta); |
| 94 | |
| 95 | if (is_array($plugcache_b)) |
| 96 | { |
| 97 | $plug_b =& $globals->plugins->load($plugcache_b['name'], $plugcache_b); |
| 98 | |
| 99 | // swap the current plugin and the next plugin |
| 100 | if (is_object($plug_b) && ($plug_b->active)) |
| 101 | { |
| 102 | $plug_a->toDatabase($this->plug_barrel, $this->plug_page, $old_pos + $delta); |
| 103 | $plug_b->toDatabase($this->plug_barrel, $this->plug_page, $old_pos); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | $rebuild_cache = 1; |
| 108 | break; |
| 109 | |
| 110 | case "update": |
| 111 | if ($this->readonly) die("Sorry, this plugin view is read-only."); |
| 112 | foreach ($available as $plugin => $plugentry) |
| 113 | { |
| 114 | // check we have a valid cache entry |
| 115 | if (!is_array($plugentry)) { |
| 116 | $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'"); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $plug_h =& $globals->plugins->load($plugin, $plugentry); |
| 121 | if (!is_object($plug_h)) { |
| 122 | $page->info("could not load plugin '$plugin' in cache for barrel '{$this->plug_barrel}'"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if ($pos !== false) { |
| 127 | // check the plugin is allowed in the current context |
| 128 | if ($this->plug_barrel and $this->plug_page) { |
| 129 | $wperms = $this->plug_page_wperms; |
| 130 | |
| 131 | // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'.."); |
| 132 | if (!$plug_h->allow_wperms($wperms)) |
| 133 | { |
| 134 | $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!"); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // retrieve parameters from REQUEST |
| 140 | if (isset($_REQUEST[$plug_h->name."_status"])) |
| 141 | { |
| 142 | $plug_h->status = $_REQUEST[$plug_h->name."_status"]; |
| 143 | } |
| 144 | foreach ($plug_h->getParamNames() as $key) |
| 145 | { |
| 146 | if (isset($_REQUEST[$plug_h->name."_".$key])) { |
| 147 | $plug_h->setParamValue($key, $_REQUEST[$plug_h->name."_".$key]); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // write parameters to database |
| 152 | $plug_h->toDatabase($this->plug_barrel, $this->plug_page, $pos); |
| 153 | $rebuild_cache = 1; |
| 154 | } |
| 155 | } |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | // if necessary, rebuild the plugin cache |
| 160 | if ($rebuild_cache) |
| 161 | { |
| 162 | // log this action |
| 163 | if ($this->plug_barrel) |
| 164 | { |
| 165 | if ($this->plug_page) |
| 166 | { |
| 167 | $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page); |
| 168 | } else { |
| 169 | $page->log('barrel_plugins', $this->plug_barrel.":*"); |
| 170 | } |
| 171 | } |
| 172 | // rebuild plugin cache |
| 173 | $globals->plugins->compileCache($this->plug_barrel, $page); |
| 174 | $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel); |
| 175 | $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page); |
| 176 | } |
| 177 | |
| 178 | // get dump of plugins to fill out form |
| 179 | $page->assign('plug_barrel', $this->plug_barrel); |
| 180 | $page->assign('plug_page', $this->plug_page); |
| 181 | $plugs = array(); |
| 182 | |
| 183 | // start by adding the active plugins |
| 184 | foreach ($available as $plugname => $plugcache) |
| 185 | { |
| 186 | if ($plugcache['status'] == PLUG_ACTIVE) |
| 187 | { |
| 188 | $plugentry = $plugcache; |
| 189 | $plugentry['icon'] = $globals->icons->get_action_icon('plugins'); |
| 190 | $type = $plugentry['type']; |
| 191 | if (!empty($plugs[$type])) { |
| 192 | $plugentry['move_up'] = 1; |
| 193 | $last = count($plugs[$type]) - 1; |
| 194 | $plugs[$type][$last]['move_down'] = 1; |
| 195 | } else { |
| 196 | $plugs[$type] = array(); |
| 197 | } |
| 198 | array_push($plugs[$type], $plugentry); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // next we add the inactive plugins |
| 203 | if (!$this->readonly) |
| 204 | { |
| 205 | foreach ($available as $plugname => $plugcache) |
| 206 | { |
| 207 | if ( ($plugcache['status'] == PLUG_AVAILABLE) || |
| 208 | (($plugcache['status'] == PLUG_DISABLED) && !$this->plug_barrel) ) |
| 209 | { |
| 210 | $plugentry = $plugcache; |
| 211 | $plugentry['icon'] = $globals->icons->get_action_icon('plugins'); |
| 212 | $type = $plugentry['type']; |
| 213 | if (empty($plugs[$type])) { |
| 214 | $plugs[$type] = array(); |
| 215 | } |
| 216 | array_push($plugs[$type], $plugentry); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | $page->assign('plugins', $plugs); |
| 222 | |
| 223 | // values |
| 224 | $page->assign('show_params', $this->show_params); |
| 225 | $page->assign('readonly',$this->readonly); |
| 226 | $statusvals = array(0 => 'disabled', '1' => 'available', 2 => 'enabled'); |
| 227 | $page->assign('statusvals', $statusvals); |
| 228 | |
| 229 | // translations |
| 230 | $page->assign('msg_submit', __("Submit")); |
| 231 | $page->assign('msg_plugedit_plugin', __("plugin")); |
| 232 | $page->assign('msg_plugedit_plugins', __("plugins")); |
| 233 | $page->assign('msg_plugedit_description', __("description")); |
| 234 | $page->assign('msg_plugedit_parameters', __("parameters")); |
| 235 | $page->assign('msg_move_up', __("move up")); |
| 236 | $page->assign('msg_move_down', __("move down")); |
| 237 | |
| 238 | // if requested, assign the content to be displayed |
| 239 | if (!empty($outputvar)) { |
| 240 | $page->assign($outputvar, $page->fetch('plugin-editor.tpl')); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** Do not display plugin parameters. |
| 246 | * |
| 247 | * @param $hide boolean |
| 248 | */ |
| 249 | function hide_params($hide) |
| 250 | { |
| 251 | $this->show_params = !$hide; |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | ?> |