5c13da2b0bb9fe77433775c5013dbd3a21dab89e
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($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 }
154 function checkMenu()
155 {
156 $cfg= &$this->config->data['MENU'];
157 foreach ($cfg as $headline => $plug){
158 $this->menuparts[_($headline)]= array();
159 foreach ($plug as $id => $info){
160 if (!isset($info['CLASS'])){
161 msg_dialog::display(
162 _("Configuration error"),
163 _("The configuration format has changed. Please re-run setup!"),
164 FATAL_ERROR_DIALOG);
165 exit();
166 }
167 if (!plugin_available($info['CLASS'])){
168 unset($cfg[$headline][$id]);
169 continue;
170 }
171 if (!$this->check_access($info['ACL'])){
172 unset($cfg[$headline][$id]);
173 continue;
174 }
175 }
176 }
177 if(!session::is_set('maxC')){
178 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
179 }
180 }
182 function gen_menu()
183 {
184 if ($this->menu == ""){
185 $cfg= $this->config->data['MENU'];
186 $this->checkMenu();
187 $cfg= $this->config->data['MENU'];
188 $menu = "\n<div class='navigation'>";
189 foreach ($cfg as $headline => $plug){
191 if(!count($plug)) continue;
192 $menu.= "\n<div class='menu'>";
193 $menu.= "\n <ul>";
194 $menu.= "\n <li class='menu-header'>".$headline."</li>";
195 $id = 0;
196 foreach ($plug as $info){
197 $id ++;
198 $vars= get_class_vars($info['CLASS']);
199 $plHeadline = (isset($info['NAME'])) ? $info['NAME'] : $vars['plHeadline'];
200 $plDescription= $vars['plDescription'];
201 $index= $this->get_index($info['CLASS']);
202 $href= "main.php?plug=$index&reset=1";
203 if(!$vars){
204 $plHeadline = _("Unknown");
205 $plDescription = _("Unknown");
206 $href= "main.php?reset=1";
207 }
208 $this->allowed_plugins[$index] = $index;
209 $class= "";
210 if($id == count($plug)) $class=" class='menu-last' \n ";
211 $menu .= "\n <li $class onClick='return question(\"".
212 _("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
213 \"$href\");'>".$plHeadline."</li>";
214 }
215 $menu.= "\n </ul>";
216 $menu.= "\n <div></div>\n";
217 $menu.= "\n</div>\n";
218 $menu.= "\n<div class='v-spacer'></div>\n";
219 }
220 $menu.= "\n</div>";
221 $this->menu = $menu;
222 }
224 /* Write menu output */
225 return ($this->menu);
226 }
228 function gen_current()
229 {
230 /* Do we have a current value? */
231 if ($this->current == ""){
232 $tmp= array_keys($this->menuparts);
233 $this->current= $tmp[0];
234 }
236 /* Fill current array */
237 $result= "<ul>";
238 $count= 0;
239 foreach ($this->menuparts[$this->current] as $entry){
240 if ($count == 2){
241 $result.= "</li>";
242 $count= 0;
243 }
244 if ($count == 0){
245 $result.= "<li>";
246 }
247 $result.= "$entry";
248 $count++;
249 }
251 /* Add missing cell? */
252 if ($count == 1){
253 $result.= " ";
254 }
256 $result.= "</ul>";
257 return $result;
258 }
261 function show_iconmenu()
262 {
263 global $class_mapping;
264 if ($this->iconmenu == ""){
265 $cfg= $this->config->data['MENU'];
267 if (isset($this->config->current['ICONSIZE'])){
268 list($x, $y)= explode("x", $this->config->get_cfg_value("iconsize"));
269 $isize= "width=\"$x\" height=\"$y\"";
270 } else {
271 $isize= "";
272 }
274 /* Parse headlines */
275 foreach ($cfg as $headline => $plug){
276 $col= 1;
277 $menu= "<h1 class=\"menuheader\">".
278 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
279 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
280 $entries= "";
282 foreach ($plug as $info){
284 if (!plugin_available($info['CLASS'])){
285 continue;
286 }
288 /* Read information from class variable */
289 $vars= get_class_vars($info['CLASS']);
290 $plHeadline= $vars['plHeadline'];
291 $plDescription= $vars['plDescription'];
292 if (isset($vars['plIcon'])){
293 $plIcon= $vars['plIcon'];
294 } else {
295 $plIcon= "plugin.png";
296 }
298 $index= $this->get_index($info['CLASS']);
300 $href = "main.php?plug=".$index."&reset=1";
302 /* Check if class is available. If the class doesn't exists display error symbol
303 to avoid that a user clicks on a non existing plugin */
304 if(!$vars){
305 $plHeadline = $plDescription = _("Unknown");
306 $info['ICON'] = "error.png";
307 $href="main.php?reset=1";
308 }
311 if ($this->check_access($info['ACL'])){
313 /* Load icon */
314 if (isset($info['ICON'])){
315 $image= get_template_path('images/'.$info['ICON']);
316 } else {
317 if(!preg_match("/\//",$plIcon)){
318 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
319 }else{
320 $image = $plIcon;
321 }
322 }
323 if ($col > 5){
324 $entries= $entries."</tr><tr>";
325 $col = 1;
326 }
327 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
328 ">";
329 if(session::global_get('js')){
330 $entries.= "<img $isize border=0 align=middle src=\"$image".
331 "\" alt=\"*\"> ".
332 _($plHeadline);
333 } else {
334 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
335 "<img $isize border=0 align=middle src=\"$image".
336 "\" alt=\"*\"> ".
337 _($plHeadline)."</a>";
338 }
339 $entries.= "</td>\n";
340 $col++ ;
342 }
343 }
345 /* Append to menu */
346 if ($entries != ""){
347 $this->iconmenu.= $menu.$entries;
349 /* Fill up remaining columns */
350 if ($col != 1){
351 $col--;
352 while ($col % 5){
353 $this->iconmenu= $this->iconmenu.
354 "<td style=\"width:20%\"> </td>\n";
355 $col++;
356 }
357 }
359 /* close table */
360 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
361 }
362 }
364 }
366 /* Write menu output */
367 return ($this->iconmenu);
368 }
370 function get_path($index)
371 {
372 if(!isset($this->dirlist[$index])){
373 return ("");
374 }
375 return ("../".$this->dirlist[$index]);
376 }
378 function get_index($class)
379 {
380 /* Search for plugin index (id), identify entry by path && class */
381 $data = $this->config->data['MENU'];
382 foreach($data as $section => $plugins){
383 foreach($plugins as $key => $plugin) {
384 if($plugin['CLASS'] == $class){
385 return($key);
386 }
387 }
388 }
390 /* Nothing */
391 return (0);
393 }
395 /*! \brief This function checks if we are allowed to view the plugin with the given id
396 @param $plug_id Integer The ID of the plugin.
397 @return Boolean TRUE if we are allowed to view the plugin else FASLE
398 */
399 function plugin_access_allowed($plug_id)
400 {
401 return(isset($this->allowed_plugins[$plug_id]));
402 }
405 /*! \brief Force the menu to be recreated
406 */
407 function reset_menus()
408 {
409 $this->menu = "";
410 $this->iconmenu ="";
411 }
413 }
414 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
415 ?>