add an RSS feed to describe a barrel's recent changes
[diogenes.git] / include / Plugin / Editor.php
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 define('MODE_NORMAL', 0);
25 define('MODE_ROOT', 1);
26 define('MODE_READONLY', 2);
27 define('MODE_NOPARAMS', 4);
28
29 /** This class provides a configuration tool for Diogenes plugins.
30 */
31 class Diogenes_Plugin_Editor {
32 /** The alias of the barrel we are working on. */
33 var $plug_barrel;
34
35 /** The PID of the page we are working on. */
36 var $plug_page;
37
38 /** The write permissions of the page we are working on. */
39 var $plug_page_wperms;
40
41 /** The plugin editor mode */
42 var $mode = MODE_NORMAL;
43
44 /** Construct a new plugin editor.
45 *
46 * @param $plug_barrel
47 * @param $plug_page
48 * @param $plug_page_wperms
49 */
50 function Diogenes_Plugin_Editor($plug_barrel, $plug_page, $plug_page_wperms = '')
51 {
52 $this->plug_barrel = $plug_barrel;
53 $this->plug_page = $plug_page;
54 $this->plug_page_wperms = $plug_page_wperms;
55 }
56
57
58 /** Run the plugin editor.
59 *
60 * @param $page
61 * @param $outputvar
62 */
63 function run(&$page, $outputvar = '')
64 {
65 global $globals;
66
67 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
68 $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : '';
69
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))
73 {
74 $globals->plugins->compileCache($this->plug_barrel, $page);
75 }
76 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
77 $available = $globals->plugins->cacheGetVisible($cache, $this->plug_barrel, $this->plug_page);
78
79 // handle updates
80 $rebuild_cache = 0;
81 switch ($action) {
82 case "update":
83 if ($this->mode & MODE_READONLY) die("Sorry, this plugin view is read-only.");
84 foreach ($available as $plugin => $plugentry)
85 {
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}'");
89 return;
90 }
91
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}'");
95 return;
96 }
97
98 if ($pos !== false) {
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;
102
103 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
104 if (!$plug_h->allow_wperms($wperms))
105 {
106 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
107 break;
108 }
109 }
110
111 // retrieve parameters from REQUEST
112 if (isset($_REQUEST[$plug_h->name."_status"]))
113 {
114 $plug_h->status = $_REQUEST[$plug_h->name."_status"];
115 }
116 foreach ($plug_h->getParamNames() as $key)
117 {
118 if (isset($_REQUEST[$plug_h->name."_".$key])) {
119 $plug_h->setParamValue($key, $_REQUEST[$plug_h->name."_".$key]);
120 }
121 }
122
123 // write parameters to database
124 if ($plug_h->status & PLUG_SET)
125 {
126 $plug_h->toDatabase($this->plug_barrel, $this->plug_page, $pos);
127 } else {
128 $plug_h->eraseParameters($this->plug_barrel, $his->plug_page);
129 }
130 $rebuild_cache = 1;
131 }
132 }
133 break;
134 }
135
136 // if necessary, rebuild the plugin cache
137 if ($rebuild_cache)
138 {
139 // log this action
140 if ($this->plug_barrel)
141 {
142 if ($this->plug_page)
143 {
144 $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page);
145 } else {
146 $page->log('barrel_plugins', $this->plug_barrel.":*");
147 }
148 }
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);
153 }
154
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);
158 $ro_plugs = array();
159 $rw_plugs_on = array();
160 $rw_plugs_off = array();
161
162 /* run over the plugins and split plugins into the following categories:
163 * - read-only,
164 * - read-write/active
165 * - read-write/inactive
166 */
167 foreach ($available as $plugname => $plugcache)
168 {
169 $plugentry = $plugcache;
170 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
171 $type = $plugentry['type'];
172
173 // FIXME : the test below needs to be refined
174 if (!($this->mode & MODE_ROOT) && ($plugentry['status'] & PLUG_LOCK))
175 {
176 $o_plugs =& $ro_plugs;
177 $plugentry['readonly'] = 1;
178 } else {
179 if ($plugentry['status'] & PLUG_ACTIVE)
180 $o_plugs =& $rw_plugs_on;
181 else
182 $o_plugs =& $rw_plugs_off;
183 }
184
185 if (empty($o_plugs[$type])) {
186 $o_plugs[$type] = array();
187 }
188 array_push($o_plugs[$type], $plugentry);
189 }
190
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);
195
196 // debugging
197 foreach ($plugs as $p_type => $p_entries)
198 {
199 $globals->plugins->log = array_merge($globals->plugins->log, $p_entries);
200 }
201
202 // values
203 $page->assign('show_params', !($this->mode & MODE_NOPARAMS));
204 $page->assign('readonly', ($this->mode & MODE_READONLY));
205 $statusvals = array(
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)',
211 );
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]);
216 }
217 $page->assign('statusvals', $statusvals);
218 $page->assign('rwstatusvals', $rwstatusvals);
219
220 // translations
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"));
226
227 // if requested, assign the content to be displayed
228 if (!empty($outputvar)) {
229 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
230 }
231 }
232
233 /** Set the editor mode.
234 */
235 function set_mode($mode)
236 {
237 $this->mode = $mode;
238 }
239 }
240
241 ?>