3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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
22 require_once 'System.php';
24 define('MODE_NORMAL', 0);
25 define('MODE_ROOT', 1);
26 define('MODE_READONLY', 2);
27 define('MODE_NOPARAMS', 4);
29 /** This class provides a configuration tool for Diogenes plugins.
31 class Diogenes_Plugin_Editor
{
32 /** The alias of the barrel we are working on. */
35 /** The PID of the page we are working on. */
38 /** The write permissions of the page we are working on. */
39 var $plug_page_wperms;
41 /** The plugin editor mode */
42 var $mode = MODE_NORMAL
;
44 /** Construct a new plugin editor.
48 * @param $plug_page_wperms
50 function Diogenes_Plugin_Editor($plug_barrel, $plug_page, $plug_page_wperms = '')
52 $this->plug_barrel
= $plug_barrel;
53 $this->plug_page
= $plug_page;
54 $this->plug_page_wperms
= $plug_page_wperms;
58 /** Run the plugin editor.
63 function run(&$page, $outputvar = '')
67 $action = isset($_REQUEST['action']) ?
$_REQUEST['action'] : '';
68 $target = isset($_REQUEST['plug_target']) ?
$_REQUEST['plug_target'] : '';
70 // if the tree cache does not exits try to init it
71 $cachefile = $globals->plugins
->cacheFile($this->plug_barrel
);
72 if(!file_exists($cachefile))
74 $globals->plugins
->compileCache($this->plug_barrel
, $page);
76 $cache = $globals->plugins
->readCache($cachefile, $this->plug_barrel
);
77 $available = $globals->plugins
->cacheGetVisible($cache, $this->plug_barrel
, $this->plug_page
);
83 if ($this->mode
& MODE_READONLY
) die("Sorry, this plugin view is read-only.");
84 foreach ($available as $plugin => $plugentry)
86 // check we have a valid cache entry
87 if (!is_array($plugentry)) {
88 $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
92 $plug_h =& $globals->plugins
->load($plugin, $plugentry);
93 if (!is_object($plug_h)) {
94 $page->info("could not load plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
99 // check the plugin is allowed in the current context
100 if ($this->plug_barrel
and $this->plug_page
) {
101 $wperms = $this->plug_page_wperms
;
103 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
104 if (!$plug_h->allow_wperms($wperms))
106 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
111 // retrieve parameters from REQUEST
112 if (isset($_REQUEST[$plug_h->name
."_status"]))
114 $plug_h->status
= $_REQUEST[$plug_h->name
."_status"];
116 foreach ($plug_h->getParamNames() as $key)
118 if (isset($_REQUEST[$plug_h->name
."_".$key])) {
119 $plug_h->setParamValue($key, $_REQUEST[$plug_h->name
."_".$key]);
123 // write parameters to database
124 if ($plug_h->status
& PLUG_SET
)
126 $plug_h->toDatabase($this->plug_barrel
, $this->plug_page
, $pos);
128 $plug_h->eraseParameters($this->plug_barrel
, $his->plug_page
);
136 // if necessary, rebuild the plugin cache
140 if ($this->plug_barrel
)
142 if ($this->plug_page
)
144 $page->log('page_plugins', $this->plug_barrel
.":".$this->plug_page
);
146 $page->log('barrel_plugins', $this->plug_barrel
.":*");
149 // rebuild plugin cache
150 $globals->plugins
->compileCache($this->plug_barrel
, $page);
151 $cache = $globals->plugins
->readCache($cachefile, $this->plug_barrel
);
152 $available = $globals->plugins
->cacheGetVisible($cache, $this->plug_barrel
, $this->plug_page
);
155 // get dump of plugins to fill out form
156 $page->assign('plug_barrel', $this->plug_barrel
);
157 $page->assign('plug_page', $this->plug_page
);
159 $rw_plugs_on = array();
160 $rw_plugs_off = array();
162 /* run over the plugins and split plugins into the following categories:
164 * - read-write/active
165 * - read-write/inactive
167 foreach ($available as $plugname => $plugcache)
169 $plugentry = $plugcache;
170 $plugentry['icon'] = $globals->icons
->get_action_icon('plugins');
171 $type = $plugentry['type'];
173 // FIXME : the test below needs to be refined
174 if (!($this->mode
& MODE_ROOT
) && ($plugentry['status'] & PLUG_LOCK
))
176 $o_plugs =& $ro_plugs;
177 $plugentry['readonly'] = 1;
179 if ($plugentry['status'] & PLUG_ACTIVE
)
180 $o_plugs =& $rw_plugs_on;
182 $o_plugs =& $rw_plugs_off;
185 if (empty($o_plugs[$type])) {
186 $o_plugs[$type] = array();
188 array_push($o_plugs[$type], $plugentry);
191 // merge the different plugin categories into a global list
192 $plugs = array_merge_recursive($rw_plugs_on, $rw_plugs_off);
193 $plugs = array_merge_recursive($plugs, $ro_plugs);
194 $page->assign('plugins', $plugs);
197 foreach ($plugs as $p_type => $p_entries)
199 $globals->plugins
->log
= array_merge($globals->plugins
->log
, $p_entries);
203 $page->assign('show_params', !($this->mode
& MODE_NOPARAMS
));
204 $page->assign('readonly', ($this->mode
& MODE_READONLY
));
206 PLUG_DISABLED
=> 'default',
207 PLUG_SET | PLUG_DISABLED
=> 'off',
208 PLUG_SET | PLUG_ACTIVE
=> 'on',
209 PLUG_SET | PLUG_DISABLED | PLUG_LOCK
=> 'off (lock)',
210 PLUG_SET | PLUG_ACTIVE | PLUG_LOCK
=> 'on (lock)',
212 $rwstatusvals = $statusvals;
213 if (!($this->mode
& MODE_ROOT
)) {
214 unset($rwstatusvals[PLUG_SET | PLUG_DISABLED | PLUG_LOCK
]);
215 unset($rwstatusvals[PLUG_SET | PLUG_ACTIVE | PLUG_LOCK
]);
217 $page->assign('statusvals', $statusvals);
218 $page->assign('rwstatusvals', $rwstatusvals);
221 $page->assign('msg_submit', __("Submit"));
222 $page->assign('msg_plugedit_plugin', __("plugin"));
223 $page->assign('msg_plugedit_plugins', __("plugins"));
224 $page->assign('msg_plugedit_description', __("description"));
225 $page->assign('msg_plugedit_parameters', __("parameters"));
227 // if requested, assign the content to be displayed
228 if (!empty($outputvar)) {
229 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
233 /** Set the editor mode.
235 function set_mode($mode)