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 = split(",",$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($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
114 $this->silly_cache[$aclname]= TRUE;
115 return(TRUE);
116 }
117 $this->silly_cache[$aclname]= FALSE;
118 return(FALSE);
119 }else{
121 /* No self acls. Check if we have any acls for the given ACL type */
122 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
123 if(count($deps)){
124 $this->silly_cache[$aclname]= TRUE;
125 return TRUE;
126 }
127 }
128 }
130 $this->silly_cache[$aclname]= FALSE;
131 return (FALSE);
132 }
135 function gen_headlines()
136 {
137 $ret = array();
138 if(count($this->headlines) == 0){
139 foreach($this->config->data['MENU'] as $headline => $plugins){
140 foreach( $plugins as $id => $plug){
141 if (plugin_available($plug['CLASS'])){
142 $attrs = (get_class_vars($plug['CLASS']));
143 $ret[$id]['HEADLINE'] = $headline;
144 $ret[$id]['NAME'] = $attrs['plHeadline'];
145 }
146 }
147 }
148 $this->headlines = $ret;
149 }
150 return($this->headlines);
151 }
153 function gen_menu()
154 {
155 if ($this->menu == ""){
156 $first= TRUE;
157 $cfg= $this->config->data['MENU'];
159 /* Parse headlines */
160 foreach ($cfg as $headline => $plug){
161 if ($first){
162 $style= "";
163 $first= FALSE;
164 } else {
165 $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
166 }
167 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
168 $entries= "";
169 $this->menuparts[_($headline)]= array();
171 /* Parse sub-plugins */
172 foreach ($plug as $info){
174 /* Read information from class variable */
175 if (!isset($info['CLASS'])){
176 msg_dialog::display(
177 _("Configuration error"),
178 _("The configuration format has changed. Please re-run setup!"),
179 FATAL_ERROR_DIALOG);
180 exit();
181 }
182 if (!plugin_available($info['CLASS'])){
183 continue;
184 }
185 $vars= get_class_vars($info['CLASS']);
186 $plHeadline= $vars['plHeadline'];
187 $plDescription= $vars['plDescription'];
189 $index= $this->get_index($info['CLASS']);
190 $href= "main.php?plug=$index&reset=1";
192 if(!$vars){
193 $plHeadline = _("Unknown");
194 $plDescription = _("Unknown");
195 $href= "main.php?reset=1";
196 }
198 if ($this->check_access($info['ACL'])){
200 $this->allowed_plugins[$index] = $index;
202 $entries= $entries."<p class=\"menuitem\" ".
203 "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
204 if(session::global_get('js')){
205 $entries.= _($plHeadline)."</p>\n";
206 } else {
207 $entries.= "<a class=\"menuitem\" ".
208 "href=\"$href\">".
209 _($plHeadline)."</a></p>\n";
210 }
212 if(!session::is_set('maxC')){
213 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
214 }
215 }
216 }
218 /* Append to menu */
219 if ($entries != ""){
220 $this->menu.= $menu.$entries;
221 }
222 }
224 }
226 /* Write menu output */
227 return ($this->menu);
228 }
230 function gen_current()
231 {
232 /* Do we have a current value? */
233 if ($this->current == ""){
234 $tmp= array_keys($this->menuparts);
235 $this->current= $tmp[0];
236 }
238 /* Fill current array */
239 $result= "<table width=\"100%\" summary=\"\">";
240 $count= 0;
241 foreach ($this->menuparts[$this->current] as $entry){
242 if ($count == 2){
243 $result.= "</tr>";
244 $count= 0;
245 }
246 if ($count == 0){
247 $result.= "<tr>";
248 }
249 $result.= "<td>$entry</td>";
250 $count++;
251 }
253 /* Add missing cell? */
254 if ($count == 1){
255 $result.= "<td> </td>";
256 }
258 $result.= "</table>";
259 return $result;
260 }
263 function show_iconmenu()
264 {
265 global $class_mapping;
266 if ($this->iconmenu == ""){
267 $cfg= $this->config->data['MENU'];
269 if (isset($this->config->current['ICONSIZE'])){
270 list($x, $y)= split("x", $this->config->get_cfg_value("iconsize"));
271 $isize= "width=\"$x\" height=\"$y\"";
272 } else {
273 $isize= "";
274 }
276 /* Parse headlines */
277 foreach ($cfg as $headline => $plug){
278 $col= 1;
279 $menu= "<h1 class=\"menuheader\">".
280 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
281 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
282 $entries= "";
284 foreach ($plug as $info){
286 if (!plugin_available($info['CLASS'])){
287 continue;
288 }
290 /* Read information from class variable */
291 $vars= get_class_vars($info['CLASS']);
292 $plHeadline= $vars['plHeadline'];
293 $plDescription= $vars['plDescription'];
294 if (isset($vars['plIcon'])){
295 $plIcon= $vars['plIcon'];
296 } else {
297 $plIcon= "plugin.png";
298 }
300 $index= $this->get_index($info['CLASS']);
302 $href = "main.php?plug=".$index."&reset=1";
304 /* Check if class is available. If the class doesn't exists display error symbol
305 to avoid that a user clicks on a non existing plugin */
306 if(!$vars){
307 $plHeadline = $plDescription = _("Unknown");
308 $info['ICON'] = "error.png";
309 $href="main.php?reset=1";
310 }
313 if ($this->check_access($info['ACL'])){
315 /* Load icon */
316 if (isset($info['ICON'])){
317 $image= get_template_path('images/'.$info['ICON']);
318 } else {
319 if(!preg_match("/\//",$plIcon)){
320 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
321 }else{
322 $image = $plIcon;
323 }
324 }
325 if ($col > 5){
326 $entries= $entries."</tr><tr>";
327 $col = 1;
328 }
329 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
330 ">";
331 if(session::global_get('js')){
332 $entries.= "<img $isize border=0 align=middle src=\"$image".
333 "\" alt=\"*\"> ".
334 _($plHeadline);
335 } else {
336 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
337 "<img $isize border=0 align=middle src=\"$image".
338 "\" alt=\"*\"> ".
339 _($plHeadline)."</a>";
340 }
341 $entries.= "</td>\n";
342 $col++ ;
344 }
345 }
347 /* Append to menu */
348 if ($entries != ""){
349 $this->iconmenu.= $menu.$entries;
351 /* Fill up remaining columns */
352 if ($col != 1){
353 $col--;
354 while ($col % 5){
355 $this->iconmenu= $this->iconmenu.
356 "<td style=\"width:20%\"> </td>\n";
357 $col++;
358 }
359 }
361 /* close table */
362 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
363 }
364 }
366 }
368 /* Write menu output */
369 return ($this->iconmenu);
370 }
372 function get_path($index)
373 {
374 if(!isset($this->dirlist[$index])){
375 return ("");
376 }
377 return ("../".$this->dirlist[$index]);
378 }
380 function get_index($class)
381 {
382 /* Search for plugin index (id), identify entry by path && class */
383 $data = $this->config->data['MENU'];
384 foreach($data as $section => $plugins){
385 foreach($plugins as $key => $plugin) {
386 if($plugin['CLASS'] == $class){
387 return($key);
388 }
389 }
390 }
392 /* Nothing */
393 return (0);
395 }
397 /*! \brief This function checks if we are allowed to view the plugin with the given id
398 @param $plug_id Integer The ID of the plugin.
399 @return Boolean TRUE if we are allowed to view the plugin else FASLE
400 */
401 function plugin_access_allowed($plug_id)
402 {
403 return(isset($this->allowed_plugins[$plug_id]));
404 }
407 /*! \brief Force the menu to be recreated
408 */
409 function reset_menus()
410 {
411 $this->menu = "";
412 $this->iconmenu ="";
413 }
415 }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>