more work on the plugins system
[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
7aff44b2
JL
24define('MODE_NORMAL', 0);
25define('MODE_ROOT', 1);
26define('MODE_READONLY', 2);
27define('MODE_NOPARAMS', 4);
6855525e
JL
28
29/** This class provides a configuration tool for Diogenes plugins.
30 */
31class Diogenes_Plugin_Editor {
6855525e
JL
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;
7aff44b2
JL
40
41 /** The plugin editor mode */
42 var $mode = MODE_NORMAL;
6855525e
JL
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 }
56aefc1e
JL
56
57
6855525e
JL
58 /** Run the plugin editor.
59 *
60 * @param $page
61 * @param $outputvar
62 */
63 function run(&$page, $outputvar = '')
64 {
65 global $globals;
56aefc1e 66
6855525e
JL
67 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
68 $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : '';
56aefc1e 69
56aefc1e 70 // if the tree cache does not exits try to init it
16e8fac2 71 $cachefile = $globals->plugins->cacheFile($this->plug_barrel);
56aefc1e
JL
72 if(!file_exists($cachefile))
73 {
74 $globals->plugins->compileCache($this->plug_barrel, $page);
75 }
6855525e 76 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
7aff44b2 77 $available = $globals->plugins->cacheGetVisible($cache, $this->plug_barrel, $this->plug_page);
56aefc1e 78
6855525e 79 // handle updates
56aefc1e 80 $rebuild_cache = 0;
6855525e 81 switch ($action) {
6855525e 82 case "update":
7aff44b2 83 if ($this->mode & MODE_READONLY) die("Sorry, this plugin view is read-only.");
56aefc1e
JL
84 foreach ($available as $plugin => $plugentry)
85 {
6855525e
JL
86 // check we have a valid cache entry
87 if (!is_array($plugentry)) {
56aefc1e 88 $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
6855525e
JL
89 return;
90 }
56aefc1e
JL
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))
6855525e 105 {
56aefc1e
JL
106 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
107 break;
6855525e 108 }
56aefc1e
JL
109 }
110
111 // retrieve parameters from REQUEST
112 if (isset($_REQUEST[$plug_h->name."_status"]))
7aff44b2 113 {
56aefc1e
JL
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
7aff44b2
JL
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 }
56aefc1e 130 $rebuild_cache = 1;
6855525e
JL
131 }
132 }
56aefc1e
JL
133 break;
134 }
135
136 // if necessary, rebuild the plugin cache
137 if ($rebuild_cache)
138 {
6855525e
JL
139 // log this action
140 if ($this->plug_barrel)
141 {
142 if ($this->plug_page)
143 {
56aefc1e 144 $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page);
6855525e 145 } else {
56aefc1e 146 $page->log('barrel_plugins', $this->plug_barrel.":*");
6855525e
JL
147 }
148 }
56aefc1e
JL
149 // rebuild plugin cache
150 $globals->plugins->compileCache($this->plug_barrel, $page);
151 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
7aff44b2 152 $available = $globals->plugins->cacheGetVisible($cache, $this->plug_barrel, $this->plug_page);
6855525e
JL
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);
16e8fac2
JL
158 $ro_plugs = array();
159 $rw_plugs_on = array();
160 $rw_plugs_off = array();
6855525e 161
7aff44b2
JL
162 /* run over the plugins and split plugins into the following categories:
163 * - read-only,
164 * - read-write/active
165 * - read-write/inactive
166 */
56aefc1e 167 foreach ($available as $plugname => $plugcache)
6855525e 168 {
16e8fac2
JL
169 $plugentry = $plugcache;
170 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
171 $type = $plugentry['type'];
172
7aff44b2
JL
173 // FIXME : the test below needs to be refined
174 if (!($this->mode & MODE_ROOT) && ($plugentry['status'] & PLUG_LOCK))
6855525e 175 {
16e8fac2
JL
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;
56aefc1e 183 }
56aefc1e 184
7aff44b2 185 if (empty($o_plugs[$type])) {
16e8fac2 186 $o_plugs[$type] = array();
6855525e 187 }
16e8fac2 188 array_push($o_plugs[$type], $plugentry);
6855525e 189 }
56aefc1e 190
7aff44b2 191 // merge the different plugin categories into a global list
16e8fac2 192 $plugs = array_merge_recursive($rw_plugs_on, $rw_plugs_off);
7aff44b2 193 $plugs = array_merge_recursive($plugs, $ro_plugs);
6855525e 194 $page->assign('plugins', $plugs);
7aff44b2 195
cbe74a8e
JL
196 // debugging
197 foreach ($plugs as $p_type => $p_entries)
198 {
199 $globals->plugins->log = array_merge($globals->plugins->log, $p_entries);
200 }
56aefc1e 201
6855525e 202 // values
7aff44b2
JL
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 }
56aefc1e 217 $page->assign('statusvals', $statusvals);
7aff44b2 218 $page->assign('rwstatusvals', $rwstatusvals);
6855525e 219
7aff44b2 220 // translations
6855525e 221 $page->assign('msg_submit', __("Submit"));
7aff44b2 222 $page->assign('msg_plugedit_plugin', __("plugin"));
6855525e
JL
223 $page->assign('msg_plugedit_plugins', __("plugins"));
224 $page->assign('msg_plugedit_description', __("description"));
225 $page->assign('msg_plugedit_parameters', __("parameters"));
56aefc1e 226
6855525e
JL
227 // if requested, assign the content to be displayed
228 if (!empty($outputvar)) {
229 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
230 }
231 }
7aff44b2
JL
232
233 /** Set the editor mode.
6855525e 234 */
7aff44b2 235 function set_mode($mode)
6855525e 236 {
7aff44b2 237 $this->mode = $mode;
6855525e 238 }
6855525e
JL
239}
240
241?>