update Debian packaging
[diogenes.git] / include / Plugin / Editor.php
CommitLineData
6855525e
JL
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
22require_once 'System.php';
23
24
25/** This class provides a configuration tool for Diogenes plugins.
26 */
27class 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 }
56aefc1e
JL
55
56
6855525e
JL
57 /** Run the plugin editor.
58 *
59 * @param $page
60 * @param $outputvar
61 */
62 function run(&$page, $outputvar = '')
63 {
64 global $globals;
56aefc1e 65
6855525e
JL
66 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
67 $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : '';
56aefc1e 68
56aefc1e 69 // if the tree cache does not exits try to init it
16e8fac2 70 $cachefile = $globals->plugins->cacheFile($this->plug_barrel);
56aefc1e
JL
71 if(!file_exists($cachefile))
72 {
73 $globals->plugins->compileCache($this->plug_barrel, $page);
74 }
6855525e 75 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
16e8fac2 76 $available = $globals->plugins->cachedVisible($cache, $this->plug_barrel, $this->plug_page);
56aefc1e 77
6855525e 78 // handle updates
56aefc1e 79 $rebuild_cache = 0;
6855525e
JL
80 switch ($action) {
81 case "move_up": case "move_down":
82 if ($this->readonly) die("Sorry, this plugin view is read-only.");
56aefc1e 83
6855525e 84 $delta = ($action == "move_down") ? 1 : -1;
56aefc1e 85 $page->info("moving plugin '$target'..");
6855525e 86 $plugcache_a = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $target);
56aefc1e
JL
87 $plug_a =& $globals->plugins->load($plugcache_a['name'], $plugcache_a);
88
6855525e
JL
89 if (is_object($plug_a) && ($plug_a->active)) {
90 $old_pos = $plug_a->pos;
6855525e 91 $plugcache_b = $globals->plugins->cacheGetAtPos($cache, $this->plug_barrel, $this->plug_page, $old_pos + $delta);
56aefc1e 92
6855525e
JL
93 if (is_array($plugcache_b))
94 {
56aefc1e
JL
95 $plug_b =& $globals->plugins->load($plugcache_b['name'], $plugcache_b);
96
6855525e
JL
97 // swap the current plugin and the next plugin
98 if (is_object($plug_b) && ($plug_b->active))
99 {
56aefc1e
JL
100 $plug_a->toDatabase($this->plug_barrel, $this->plug_page, $old_pos + $delta);
101 $plug_b->toDatabase($this->plug_barrel, $this->plug_page, $old_pos);
102 }
6855525e 103 }
56aefc1e
JL
104 }
105 $rebuild_cache = 1;
6855525e 106 break;
56aefc1e 107
6855525e
JL
108 case "update":
109 if ($this->readonly) die("Sorry, this plugin view is read-only.");
56aefc1e
JL
110 foreach ($available as $plugin => $plugentry)
111 {
6855525e
JL
112 // check we have a valid cache entry
113 if (!is_array($plugentry)) {
56aefc1e 114 $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
6855525e
JL
115 return;
116 }
56aefc1e
JL
117
118 $plug_h =& $globals->plugins->load($plugin, $plugentry);
119 if (!is_object($plug_h)) {
120 $page->info("could not load plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
121 return;
122 }
123
124 if ($pos !== false) {
125 // check the plugin is allowed in the current context
126 if ($this->plug_barrel and $this->plug_page) {
127 $wperms = $this->plug_page_wperms;
128
129 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
130 if (!$plug_h->allow_wperms($wperms))
6855525e 131 {
56aefc1e
JL
132 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
133 break;
6855525e 134 }
56aefc1e
JL
135 }
136
137 // retrieve parameters from REQUEST
138 if (isset($_REQUEST[$plug_h->name."_status"]))
139 {
140 $plug_h->status = $_REQUEST[$plug_h->name."_status"];
141 }
142 foreach ($plug_h->getParamNames() as $key)
143 {
144 if (isset($_REQUEST[$plug_h->name."_".$key])) {
145 $plug_h->setParamValue($key, $_REQUEST[$plug_h->name."_".$key]);
146 }
147 }
148
149 // write parameters to database
150 $plug_h->toDatabase($this->plug_barrel, $this->plug_page, $pos);
151 $rebuild_cache = 1;
6855525e
JL
152 }
153 }
56aefc1e
JL
154 break;
155 }
156
157 // if necessary, rebuild the plugin cache
158 if ($rebuild_cache)
159 {
6855525e
JL
160 // log this action
161 if ($this->plug_barrel)
162 {
163 if ($this->plug_page)
164 {
56aefc1e 165 $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page);
6855525e 166 } else {
56aefc1e 167 $page->log('barrel_plugins', $this->plug_barrel.":*");
6855525e
JL
168 }
169 }
56aefc1e
JL
170 // rebuild plugin cache
171 $globals->plugins->compileCache($this->plug_barrel, $page);
172 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
16e8fac2 173 $available = $globals->plugins->cachedVisible($cache, $this->plug_barrel, $this->plug_page);
6855525e
JL
174 }
175
176 // get dump of plugins to fill out form
177 $page->assign('plug_barrel', $this->plug_barrel);
178 $page->assign('plug_page', $this->plug_page);
16e8fac2
JL
179 $ro_plugs = array();
180 $rw_plugs_on = array();
181 $rw_plugs_off = array();
6855525e
JL
182
183 // start by adding the active plugins
56aefc1e 184 foreach ($available as $plugname => $plugcache)
6855525e 185 {
16e8fac2
JL
186 $plugentry = $plugcache;
187 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
188 $type = $plugentry['type'];
189
190 if ($plugentry['status'] & PLUG_LOCK)
6855525e 191 {
16e8fac2
JL
192 $o_plugs =& $ro_plugs;
193 $plugentry['readonly'] = 1;
194 } else {
195 if ($plugentry['status'] & PLUG_ACTIVE)
196 $o_plugs =& $rw_plugs_on;
197 else
198 $o_plugs =& $rw_plugs_off;
56aefc1e 199 }
56aefc1e 200
16e8fac2
JL
201 if (!empty($o_plugs[$type])) {
202 $plugentry['move_up'] = 1;
203 $last = count($o_plugs[$type]) - 1;
204 $o_plugs[$type][$last]['move_down'] = 1;
205 } else {
206 $o_plugs[$type] = array();
6855525e 207 }
16e8fac2 208 array_push($o_plugs[$type], $plugentry);
6855525e 209 }
56aefc1e 210
16e8fac2
JL
211 // next we add the inactive plugins
212 $plugs = array_merge_recursive($rw_plugs_on, $rw_plugs_off);
6855525e 213 $page->assign('plugins', $plugs);
cbe74a8e
JL
214 // debugging
215 foreach ($plugs as $p_type => $p_entries)
216 {
217 $globals->plugins->log = array_merge($globals->plugins->log, $p_entries);
218 }
56aefc1e 219
6855525e
JL
220 // values
221 $page->assign('show_params', $this->show_params);
222 $page->assign('readonly',$this->readonly);
16e8fac2 223 $statusvals = array(0 => 'off', 1 => 'on', 2 => 'off (lock)', 3 => 'on (lock)');
56aefc1e 224 $page->assign('statusvals', $statusvals);
6855525e
JL
225
226 // translations
227 $page->assign('msg_submit', __("Submit"));
228 $page->assign('msg_plugedit_plugin', __("plugin"));
229 $page->assign('msg_plugedit_plugins', __("plugins"));
230 $page->assign('msg_plugedit_description', __("description"));
231 $page->assign('msg_plugedit_parameters', __("parameters"));
232 $page->assign('msg_move_up', __("move up"));
233 $page->assign('msg_move_down', __("move down"));
56aefc1e 234
6855525e
JL
235 // if requested, assign the content to be displayed
236 if (!empty($outputvar)) {
237 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
238 }
239 }
240
241
242 /** Do not display plugin parameters.
243 *
244 * @param $hide boolean
245 */
246 function hide_params($hide)
247 {
248 $this->show_params = !$hide;
249 }
250
251}
252
253?>