501ea37b8d3c67fa5cf87e87a5d0938679dae114
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>\n";
217 $menu.= "\n<div class='v-spacer'></div>\n";
218 }
219 $menu.= "\n</div>";
220 $this->menu = $menu;
221 }
223 /* Write menu output */
224 return ($this->menu);
225 }
227 function gen_current()
228 {
229 /* Do we have a current value? */
230 if ($this->current == ""){
231 $tmp= array_keys($this->menuparts);
232 $this->current= $tmp[0];
233 }
235 /* Fill current array */
236 $result= "<ul>";
237 $count= 0;
238 foreach ($this->menuparts[$this->current] as $entry){
239 if ($count == 2){
240 $result.= "</li>";
241 $count= 0;
242 }
243 if ($count == 0){
244 $result.= "<li>";
245 }
246 $result.= "$entry";
247 $count++;
248 }
250 /* Add missing cell? */
251 if ($count == 1){
252 $result.= " ";
253 }
255 $result.= "</ul>";
256 return $result;
257 }
260 function show_iconmenu()
261 {
262 global $class_mapping;
263 if ($this->iconmenu == ""){
264 $cfg= $this->config->data['MENU'];
266 if (isset($this->config->current['ICONSIZE'])){
267 list($x, $y)= explode("x", $this->config->get_cfg_value("iconsize"));
268 $isize= "width=\"$x\" height=\"$y\"";
269 } else {
270 $isize= "";
271 }
273 /* Parse headlines */
274 foreach ($cfg as $headline => $plug){
275 $col= 1;
276 $menu= "<h1 class=\"menuheader\">".
277 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
278 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
279 $entries= "";
281 foreach ($plug as $info){
283 if (!plugin_available($info['CLASS'])){
284 continue;
285 }
287 /* Read information from class variable */
288 $vars= get_class_vars($info['CLASS']);
289 $plHeadline= $vars['plHeadline'];
290 $plDescription= $vars['plDescription'];
291 if (isset($vars['plIcon'])){
292 $plIcon= $vars['plIcon'];
293 } else {
294 $plIcon= "plugin.png";
295 }
297 $index= $this->get_index($info['CLASS']);
299 $href = "main.php?plug=".$index."&reset=1";
301 /* Check if class is available. If the class doesn't exists display error symbol
302 to avoid that a user clicks on a non existing plugin */
303 if(!$vars){
304 $plHeadline = $plDescription = _("Unknown");
305 $info['ICON'] = "error.png";
306 $href="main.php?reset=1";
307 }
310 if ($this->check_access($info['ACL'])){
312 /* Load icon */
313 if (isset($info['ICON'])){
314 $image= get_template_path('images/'.$info['ICON']);
315 } else {
316 if(!preg_match("/\//",$plIcon)){
317 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
318 }else{
319 $image = $plIcon;
320 }
321 }
322 if ($col > 5){
323 $entries= $entries."</tr><tr>";
324 $col = 1;
325 }
326 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
327 ">";
328 if(session::global_get('js')){
329 $entries.= "<img $isize border=0 align=middle src=\"$image".
330 "\" alt=\"*\"> ".
331 _($plHeadline);
332 } else {
333 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
334 "<img $isize border=0 align=middle src=\"$image".
335 "\" alt=\"*\"> ".
336 _($plHeadline)."</a>";
337 }
338 $entries.= "</td>\n";
339 $col++ ;
341 }
342 }
344 /* Append to menu */
345 if ($entries != ""){
346 $this->iconmenu.= $menu.$entries;
348 /* Fill up remaining columns */
349 if ($col != 1){
350 $col--;
351 while ($col % 5){
352 $this->iconmenu= $this->iconmenu.
353 "<td style=\"width:20%\"> </td>\n";
354 $col++;
355 }
356 }
358 /* close table */
359 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
360 }
361 }
363 }
365 /* Write menu output */
366 return ($this->iconmenu);
367 }
369 function get_path($index)
370 {
371 if(!isset($this->dirlist[$index])){
372 return ("");
373 }
374 return ("../".$this->dirlist[$index]);
375 }
377 function get_index($class)
378 {
379 /* Search for plugin index (id), identify entry by path && class */
380 $data = $this->config->data['MENU'];
381 foreach($data as $section => $plugins){
382 foreach($plugins as $key => $plugin) {
383 if($plugin['CLASS'] == $class){
384 return($key);
385 }
386 }
387 }
389 /* Nothing */
390 return (0);
392 }
394 /*! \brief This function checks if we are allowed to view the plugin with the given id
395 @param $plug_id Integer The ID of the plugin.
396 @return Boolean TRUE if we are allowed to view the plugin else FASLE
397 */
398 function plugin_access_allowed($plug_id)
399 {
400 return(isset($this->allowed_plugins[$plug_id]));
401 }
404 /*! \brief Force the menu to be recreated
405 */
406 function reset_menus()
407 {
408 $this->menu = "";
409 $this->iconmenu ="";
410 }
412 }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>