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