1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class pluglist {
24 var $index= 0;
25 var $menu= "";
26 var $iconmenu= "";
27 var $menuparts= array();
28 var $config= NULL;
29 var $dirlist= array();
30 var $ui= NULL;
31 var $current= "";
32 var $info= array();
33 var $headlines = array();
34 var $allowed_plugins = array();
35 var $silly_cache= array();
37 function pluglist(&$config, &$ui)
38 {
39 $this->ui= &$ui;
40 $this->config= &$config;
42 /* Create dirlist for all plugins */
43 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
45 /* Fill info part of pluglist */
46 $classes= get_declared_classes();
48 foreach ($classes as $cname){
49 $cmethods = get_class_methods($cname);
50 if (in_array_ics('plInfo',$cmethods)){
51 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
52 }
53 }
55 /* Provide field for 'all' */
56 $this->info['all']= array();
57 $this->info['all']['plProvidedAcls']= array();
58 $this->info['all']['plDescription']= _("All objects in this category");
59 $this->info['all']['plSelfModify']= FALSE;
60 }
62 function get_plugins($list, &$config)
63 {
64 global $class_mapping;
66 if (!isset($config['CLASS'])){
67 if (is_array($config)){
68 foreach ($config as $val){
69 $list= $this->get_plugins($list, $val);
70 }
71 }
72 } else {
73 if (is_array($config) && isset($class_mapping[$config['CLASS']])){
74 $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
75 } else {
76 $list[$this->index++]= "";
77 }
78 }
80 return ($list);
81 }
84 /*! \brief Check whether we are allowed to modify the given acl or not..
85 This function is used to check which plugins are visible.
87 @param The acl tag to test, eg. "users/user:self", "systems", ...
88 @return Boolean TRUE on success else FALSE
89 */
90 function check_access($aclname)
91 {
92 if (isset($this->silly_cache[$aclname])) {
93 return $this->silly_cache[$aclname];
94 }
96 /* Split given acl string into an array.
97 e.g. "user,systems" => array("users","systems");
98 */
99 $acls_to_check = array();
100 if(preg_match("/,/",$aclname)){
101 $acls_to_check = explode(",",$aclname);
102 }else{
103 $acls_to_check = array($aclname);
104 }
106 foreach($acls_to_check as $acl_to_check){
107 $acl_to_check = trim($acl_to_check);
109 /* Check if the given acl tag is only valid for self acl entries
110 <plugin acl="users/user:self" class="user"... */
111 if(preg_match("/:self$/",$acl_to_check)){
112 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);
113 if(strpos($acl_to_check, '/')){
114 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
115 $this->silly_cache[$aclname]= TRUE;
116 return(TRUE);
117 }
118 }else{
119 if($this->ui->get_category_permissions($this->ui->dn,$acl_to_check,"") != ""){
120 $this->silly_cache[$aclname]= TRUE;
121 return(TRUE);
122 }
123 }
124 }else{
126 /* No self acls. Check if we have any acls for the given ACL type */
127 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
128 if(count($deps)){
129 $this->silly_cache[$aclname]= TRUE;
130 return TRUE;
131 }
132 }
133 }
135 $this->silly_cache[$aclname]= FALSE;
136 return (FALSE);
137 }
140 function gen_headlines()
141 {
142 $ret = array();
143 if(count($this->headlines) == 0){
144 foreach($this->config->data['MENU'] as $headline => $plugins){
145 foreach( $plugins as $id => $plug){
146 if (plugin_available($plug['CLASS'])){
147 $attrs = (get_class_vars($plug['CLASS']));
148 $ret[$id]['HEADLINE'] = $headline;
149 $ret[$id]['NAME'] = $attrs['plHeadline'];
150 }
151 }
152 }
153 $this->headlines = $ret;
154 }
155 return($this->headlines);
156 }
158 function gen_menu()
159 {
160 if ($this->menu == ""){
161 $first= TRUE;
162 $cfg= $this->config->data['MENU'];
164 /* Parse headlines */
165 foreach ($cfg as $headline => $plug){
166 if ($first){
167 $style= "";
168 $first= FALSE;
169 } else {
170 $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
171 }
172 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
173 $entries= "";
174 $this->menuparts[_($headline)]= array();
176 /* Parse sub-plugins */
177 foreach ($plug as $info){
179 /* Read information from class variable */
180 if (!isset($info['CLASS'])){
181 msg_dialog::display(
182 _("Configuration error"),
183 _("The configuration format has changed. Please re-run setup!"),
184 FATAL_ERROR_DIALOG);
185 exit();
186 }
187 if (!plugin_available($info['CLASS'])){
188 continue;
189 }
190 $vars= get_class_vars($info['CLASS']);
191 if (isset($info['NAME'])){
192 $plHeadline= _($info['NAME']);
193 } else {
194 $plHeadline= $vars['plHeadline'];
195 }
196 $plDescription= $vars['plDescription'];
198 $index= $this->get_index($info['CLASS']);
199 $href= "main.php?plug=$index&reset=1";
201 if(!$vars){
202 $plHeadline = _("Unknown");
203 $plDescription = _("Unknown");
204 $href= "main.php?reset=1";
205 }
207 if ($this->check_access($info['ACL'])){
209 $this->allowed_plugins[$index] = $index;
211 $entries= $entries."<p class=\"menuitem\" ".
212 "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
213 if(session::global_get('js')){
214 $entries.= _($plHeadline)."</p>\n";
215 } else {
216 $entries.= "<a class=\"menuitem\" ".
217 "href=\"$href\">".
218 _($plHeadline)."</a></p>\n";
219 }
221 if(!session::is_set('maxC')){
222 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
223 }
224 }
225 }
227 /* Append to menu */
228 if ($entries != ""){
229 $this->menu.= $menu.$entries;
230 }
231 }
233 }
235 /* Write menu output */
236 return ($this->menu);
237 }
239 function gen_current()
240 {
241 /* Do we have a current value? */
242 if ($this->current == ""){
243 $tmp= array_keys($this->menuparts);
244 $this->current= $tmp[0];
245 }
247 /* Fill current array */
248 $result= "<table width=\"100%\" summary=\"\">";
249 $count= 0;
250 foreach ($this->menuparts[$this->current] as $entry){
251 if ($count == 2){
252 $result.= "</tr>";
253 $count= 0;
254 }
255 if ($count == 0){
256 $result.= "<tr>";
257 }
258 $result.= "<td>$entry</td>";
259 $count++;
260 }
262 /* Add missing cell? */
263 if ($count == 1){
264 $result.= "<td> </td>";
265 }
267 $result.= "</table>";
268 return $result;
269 }
272 function show_iconmenu()
273 {
274 global $class_mapping;
275 if ($this->iconmenu == ""){
276 $cfg= $this->config->data['MENU'];
278 if (isset($this->config->current['ICONSIZE'])){
279 list($x, $y)= explode("x", $this->config->get_cfg_value("iconsize"));
280 $isize= "width=\"$x\" height=\"$y\"";
281 } else {
282 $isize= "";
283 }
285 /* Parse headlines */
286 foreach ($cfg as $headline => $plug){
287 $col= 1;
288 $menu= "<h1 class=\"menuheader\">".
289 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
290 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
291 $entries= "";
293 foreach ($plug as $info){
295 if (!plugin_available($info['CLASS'])){
296 continue;
297 }
299 /* Read information from class variable */
300 $vars= get_class_vars($info['CLASS']);
301 $plHeadline= $vars['plHeadline'];
302 $plDescription= $vars['plDescription'];
303 if (isset($vars['plIcon'])){
304 $plIcon= $vars['plIcon'];
305 } else {
306 $plIcon= "plugin.png";
307 }
309 $index= $this->get_index($info['CLASS']);
311 $href = "main.php?plug=".$index."&reset=1";
313 /* Check if class is available. If the class doesn't exists display error symbol
314 to avoid that a user clicks on a non existing plugin */
315 if(!$vars){
316 $plHeadline = $plDescription = _("Unknown");
317 $info['ICON'] = "error.png";
318 $href="main.php?reset=1";
319 }
322 if ($this->check_access($info['ACL'])){
324 /* Load icon */
325 if (isset($info['ICON'])){
326 $image= get_template_path('images/'.$info['ICON']);
327 } else {
328 if(!preg_match("/\//",$plIcon)){
329 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
330 }else{
331 $image = $plIcon;
332 }
333 }
334 if ($col > 5){
335 $entries= $entries."</tr><tr>";
336 $col = 1;
337 }
338 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
339 ">";
340 if(session::global_get('js')){
341 $entries.= "<img $isize border=0 align=middle src=\"$image".
342 "\" alt=\"*\"> ".
343 _($plHeadline);
344 } else {
345 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
346 "<img $isize border=0 align=middle src=\"$image".
347 "\" alt=\"*\"> ".
348 _($plHeadline)."</a>";
349 }
350 $entries.= "</td>\n";
351 $col++ ;
353 }
354 }
356 /* Append to menu */
357 if ($entries != ""){
358 $this->iconmenu.= $menu.$entries;
360 /* Fill up remaining columns */
361 if ($col != 1){
362 $col--;
363 while ($col % 5){
364 $this->iconmenu= $this->iconmenu.
365 "<td style=\"width:20%\"> </td>\n";
366 $col++;
367 }
368 }
370 /* close table */
371 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
372 }
373 }
375 }
377 /* Write menu output */
378 return ($this->iconmenu);
379 }
381 function get_path($index)
382 {
383 if(!isset($this->dirlist[$index])){
384 return ("");
385 }
386 return ("../".$this->dirlist[$index]);
387 }
389 function get_index($class)
390 {
391 /* Search for plugin index (id), identify entry by path && class */
392 $data = $this->config->data['MENU'];
393 foreach($data as $section => $plugins){
394 foreach($plugins as $key => $plugin) {
395 if($plugin['CLASS'] == $class){
396 return($key);
397 }
398 }
399 }
401 /* Nothing */
402 return (0);
404 }
406 /*! \brief This function checks if we are allowed to view the plugin with the given id
407 @param $plug_id Integer The ID of the plugin.
408 @return Boolean TRUE if we are allowed to view the plugin else FASLE
409 */
410 function plugin_access_allowed($plug_id)
411 {
412 return(isset($this->allowed_plugins[$plug_id]));
413 }
416 /*! \brief Force the menu to be recreated
417 */
418 function reset_menus()
419 {
420 $this->menu = "";
421 $this->iconmenu ="";
422 }
424 }
425 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
426 ?>