fe46dbbc372877b6ec50a9ed556f163a39b2de0a
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 __construct(&$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 }
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 if (isset($info['NAME'])){
187 $plHeadline= _($info['NAME']);
188 } else {
189 $plHeadline= $vars['plHeadline'];
190 }
191 $plDescription= $vars['plDescription'];
193 $index= $this->get_index($info['CLASS']);
194 $href= "main.php?plug=$index&reset=1";
196 if(!$vars){
197 $plHeadline = _("Unknown");
198 $plDescription = _("Unknown");
199 $href= "main.php?reset=1";
200 }
202 if ($this->check_access($info['ACL'])){
204 $this->allowed_plugins[$index] = $index;
206 $entries= $entries."<p class=\"menuitem\" ".
207 "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
208 if(session::global_get('js')){
209 $entries.= _($plHeadline)."</p>\n";
210 } else {
211 $entries.= "<a class=\"menuitem\" ".
212 "href=\"$href\">".
213 _($plHeadline)."</a></p>\n";
214 }
216 if(!session::is_set('maxC')){
217 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
218 }
219 }
220 }
222 /* Append to menu */
223 if ($entries != ""){
224 $this->menu.= $menu.$entries;
225 }
226 }
228 }
230 /* Write menu output */
231 return ($this->menu);
232 }
234 function gen_current()
235 {
236 /* Do we have a current value? */
237 if ($this->current == ""){
238 $tmp= array_keys($this->menuparts);
239 $this->current= $tmp[0];
240 }
242 /* Fill current array */
243 $result= "<table width=\"100%\" summary=\"\">";
244 $count= 0;
245 foreach ($this->menuparts[$this->current] as $entry){
246 if ($count == 2){
247 $result.= "</tr>";
248 $count= 0;
249 }
250 if ($count == 0){
251 $result.= "<tr>";
252 }
253 $result.= "<td>$entry</td>";
254 $count++;
255 }
257 /* Add missing cell? */
258 if ($count == 1){
259 $result.= "<td> </td>";
260 }
262 $result.= "</table>";
263 return $result;
264 }
267 function show_iconmenu()
268 {
269 global $class_mapping;
270 if ($this->iconmenu == ""){
271 $cfg= $this->config->data['MENU'];
273 if (isset($this->config->current['ICONSIZE'])){
274 list($x, $y)= explode("x", $this->config->get_cfg_value("iconsize"));
275 $isize= "width=\"$x\" height=\"$y\"";
276 } else {
277 $isize= "";
278 }
280 /* Parse headlines */
281 foreach ($cfg as $headline => $plug){
282 $col= 1;
283 $menu= "<h1 class=\"menuheader\">".
284 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
285 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
286 $entries= "";
288 foreach ($plug as $info){
290 if (!plugin_available($info['CLASS'])){
291 continue;
292 }
294 /* Read information from class variable */
295 $vars= get_class_vars($info['CLASS']);
296 $plHeadline= $vars['plHeadline'];
297 $plDescription= $vars['plDescription'];
298 if (isset($vars['plIcon'])){
299 $plIcon= $vars['plIcon'];
300 } else {
301 $plIcon= "plugin.png";
302 }
304 $index= $this->get_index($info['CLASS']);
306 $href = "main.php?plug=".$index."&reset=1";
308 /* Check if class is available. If the class doesn't exists display error symbol
309 to avoid that a user clicks on a non existing plugin */
310 if(!$vars){
311 $plHeadline = $plDescription = _("Unknown");
312 $info['ICON'] = "error.png";
313 $href="main.php?reset=1";
314 }
317 if ($this->check_access($info['ACL'])){
319 /* Load icon */
320 if (isset($info['ICON'])){
321 $image= get_template_path('images/'.$info['ICON']);
322 } else {
323 if(!preg_match("/\//",$plIcon)){
324 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
325 }else{
326 $image = $plIcon;
327 }
328 }
329 if ($col > 5){
330 $entries= $entries."</tr><tr>";
331 $col = 1;
332 }
333 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
334 ">";
335 if(session::global_get('js')){
336 $entries.= "<img $isize border=0 align=middle src=\"$image".
337 "\" alt=\"*\"> ".
338 _($plHeadline);
339 } else {
340 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
341 "<img $isize border=0 align=middle src=\"$image".
342 "\" alt=\"*\"> ".
343 _($plHeadline)."</a>";
344 }
345 $entries.= "</td>\n";
346 $col++ ;
348 }
349 }
351 /* Append to menu */
352 if ($entries != ""){
353 $this->iconmenu.= $menu.$entries;
355 /* Fill up remaining columns */
356 if ($col != 1){
357 $col--;
358 while ($col % 5){
359 $this->iconmenu= $this->iconmenu.
360 "<td style=\"width:20%\"> </td>\n";
361 $col++;
362 }
363 }
365 /* close table */
366 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
367 }
368 }
370 }
372 /* Write menu output */
373 return ($this->iconmenu);
374 }
376 function get_path($index)
377 {
378 if(!isset($this->dirlist[$index])){
379 return ("");
380 }
381 return ("../".$this->dirlist[$index]);
382 }
384 function get_index($class)
385 {
386 /* Search for plugin index (id), identify entry by path && class */
387 $data = $this->config->data['MENU'];
388 foreach($data as $section => $plugins){
389 foreach($plugins as $key => $plugin) {
390 if($plugin['CLASS'] == $class){
391 return($key);
392 }
393 }
394 }
396 /* Nothing */
397 return (0);
399 }
401 /*! \brief This function checks if we are allowed to view the plugin with the given id
402 @param $plug_id Integer The ID of the plugin.
403 @return Boolean TRUE if we are allowed to view the plugin else FASLE
404 */
405 function plugin_access_allowed($plug_id)
406 {
407 return(isset($this->allowed_plugins[$plug_id]));
408 }
411 /*! \brief Force the menu to be recreated
412 */
413 function reset_menus()
414 {
415 $this->menu = "";
416 $this->iconmenu ="";
417 }
419 }
420 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
421 ?>