Code

Updated ACL - acls ;)
[gosa.git] / gosa-core / include / class_pluglist.inc
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();
36         function pluglist(&$config, &$ui)
37         {
38                 $this->ui= &$ui;
39                 $this->config= &$config;
41                 /* Create dirlist for all plugins */
42                 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
44                 /* Fill info part of pluglist */
45                 $classes= get_declared_classes();
47                                 foreach ($classes as $cname){
48                                         $cmethods = get_class_methods($cname);
49                                         if (in_array_ics('plInfo',$cmethods)){
50                                                 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
51                                         }
52                                 }
54                 /* Provide field for 'all' */
55                 $this->info['all']= array();
56                 $this->info['all']['plProvidedAcls']= array();
57                 $this->info['all']['plDescription']= _("All objects in this category");
58                 $this->info['all']['plSelfModify']= FALSE;
59         }
61         function get_plugins($list, &$config)
62         {
63                 global $class_mapping;
65                 /* Error reporting, because I'm getting strange messages in PHP 4.2.x */
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 nit..
85                                 This function is used to check which plugins are visible.
86                                 
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                 /* Split given acl string into an array. 
93                         e.g. "user,systems" => array("users","systems");
94          */
95                 $acls_to_check = array();
96                 if(preg_match("/,/",$aclname)){
97                         $acls_to_check = split(",",$aclname);
98                 }else{
99                         $acls_to_check = array($aclname);
100                 }
102                 foreach($acls_to_check as $acl_to_check){
103       $acl_to_check = trim($acl_to_check);
104                 
105                         /* Check if the given acl tag is only valid for self acl entries  
106                  <plugin acl="users/user:self" class="user"...
107              */ 
108                         if(preg_match("/:self$/",$acl_to_check)){
109                                 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
110                                 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
111                                         return(TRUE);
112                                 }
113                                 return(FALSE);
114                         }else{
115                 
116                                 /* No self acls. Check if we have any acls for the given ACL type 
117                  */
118                                 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
119                                 if(count($deps)) return TRUE;
120                         }
121                 }
122                 return (FALSE);
123         }
126         function gen_headlines()
127         {
128                 $ret = array();
129                 if(count($this->headlines) == 0){
130                         foreach($this->config->data['MENU'] as $headline => $plugins){
131                                 foreach( $plugins as $id => $plug){
132                                         if (plugin_available($plug['CLASS'])){
133                                                 $attrs = (get_class_vars($plug['CLASS']));
134                                                 $ret[$id]['HEADLINE'] = $headline;
135                                                 $ret[$id]['NAME']         = $attrs['plHeadline'];
136                                         }
137                                 }
138                         }
139                         $this->headlines = $ret;
140                 }
141                 return($this->headlines);
142         }
144         function gen_menu()
145         {
146                 if ($this->menu == ""){
147                         $first= TRUE;
148                         $cfg= $this->config->data['MENU'];
150                         /* Parse headlines */
151                         foreach ($cfg as $headline => $plug){
152                                 if ($first){
153                                         $style= "";
154                                         $first= FALSE;
155                                 } else {
156                                         $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
157                                 }
158                                 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
159                                 $entries= "";
160                                 $this->menuparts[_($headline)]= array();
162                                 /* Parse sub-plugins */
163                                 foreach ($plug as $info){
165                                         /* Read information from class variable */
166                                         if (!isset($info['CLASS'])){
167                                                 msg_dialog::display(
168                                                                 _("Configuration error"), 
169                                                                 _("The configuration format has changed. Please re-run setup!"), 
170                                                                 FATAL_ERROR_DIALOG);
171                                                 exit();
172                                         }
173                                         if (!plugin_available($info['CLASS'])){
174                                                 continue;
175                                         }
176                                         $vars= get_class_vars($info['CLASS']);
177                                         $plHeadline= $vars['plHeadline'];
178                                         $plDescription= $vars['plDescription'];
180                                         $index= $this->get_index($info['CLASS']);
181                                         $href= "main.php?plug=$index&amp;reset=1";
183                                         if(!$vars){
184                                                 $plHeadline     = _("Unknown");
185                                                 $plDescription  = _("Unknown");
186                                                 $href= "main.php?reset=1";
187                                         }
189                                         if ($this->check_access($info['ACL'])){
191             $this->allowed_plugins[$index] = $index;
192  
193                                                 $entries= $entries."<p class=\"menuitem\" ".
194                                                         "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
195                                                 if(session::get('js')){
196                                                         $entries.= _($plHeadline)."</p>\n";
197                                                 } else {
198                                                         $entries.= "<a class=\"menuitem\" ".
199                                                         "href=\"$href\">".
200                                                         _($plHeadline)."</a></p>\n";
201                                                 }
203                                                 if(!session::is_set('maxC')){
204                                                         session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
205                                                 }
206                                         }
207                                 }
209                                 /* Append to menu */
210                                 if ($entries != ""){
211                                         $this->menu.= $menu.$entries;
212                                 }
213                         }
215                 }
217                 /* Write menu output */
218                 return ($this->menu);
219         }
221         function gen_current()
222         {
223                 /* Do we have a current value? */
224                 if ($this->current == ""){
225                         $tmp= array_keys($this->menuparts);
226                         $this->current= $tmp[0];
227                 }
229                 /* Fill current array */
230                 $result= "<table width=\"100%\" summary=\"\">";
231                 $count= 0;
232                 foreach ($this->menuparts[$this->current] as $entry){
233                         if ($count == 2){
234                                 $result.= "</tr>";
235                                 $count= 0;
236                         }
237                         if ($count == 0){
238                                 $result.= "<tr>";
239                         }
240                         $result.= "<td>$entry</td>";
241                         $count++;
242                 }
244                 /* Add missing cell? */
245                 if ($count == 1){
246                         $result.= "<td>&nbsp;</td>";
247                 }
249                 $result.= "</table>";
250                 return $result;
251         }
254         function show_iconmenu()
255         {
256                 global $class_mapping;
257                 if ($this->iconmenu == ""){
258                         $cfg= $this->config->data['MENU'];
260                         if (isset($this->config->current['ICONSIZE'])){
261                                 list($x, $y)= split("x", $this->config->get_cfg_value("iconsize"));
262                                 $isize= "width=\"$x\" height=\"$y\"";
263                         } else {
264                                 $isize= "";
265                         }
267                         /* Parse headlines */
268                         foreach ($cfg as $headline => $plug){
269                                 $col= 1;
270                                 $menu= "<h1 class=\"menuheader\">".
271                                         _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
272                                         "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
273                                 $entries= "";
275                                 foreach ($plug as $info){
277                                         if (!plugin_available($info['CLASS'])){
278                                                 continue;
279                                         }
281                                         /* Read information from class variable */
282                                         $vars= get_class_vars($info['CLASS']);
283                                         $plHeadline= $vars['plHeadline'];
284                                         $plDescription= $vars['plDescription'];
285           if (isset($vars['plIcon'])){
286                                         $plIcon= $vars['plIcon'];
287           } else {
288                                         $plIcon= "plugin.png";
289           }
291                                         $index= $this->get_index($info['CLASS']);
293                                         $href = "main.php?plug=".$index."&amp;reset=1";
295                                         /* Check if class is available. If the class doesn't exists display error symbol 
296                                                 to avoid that a user clicks on a non existing plugin  */
297                                         if(!$vars){
298                                                 $plHeadline = $plDescription = _("Unknown");
299                                                 $info['ICON'] = "error.png";
300                                                 $href="main.php?reset=1";
301                                         }
304                                         if ($this->check_access($info['ACL'])){
306                                                 /* Load icon */
307                                                 if (isset($info['ICON'])){
308                                                         $image= get_template_path('images/'.$info['ICON']);
309                                                 } else {
310               if(!preg_match("/\//",$plIcon)){
311                                                         $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
312               }else{
313                 $image = $plIcon; 
314               }
315                                                 }
316                                                 if ($col > 5){
317                                                         $entries= $entries."</tr><tr>";
318                                                         $col = 1;
319                                                 }
320                                                 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
321                                                         ">";
322                                                         if(session::get('js')){
323                                                                 $entries.= "<img $isize border=0 align=middle src=\"$image".
324                                                                         "\" alt=\"*\">&nbsp;".
325                                                                         _($plHeadline);
326                                                         } else {
327                                                                 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
328                                                                         "<img $isize border=0 align=middle src=\"$image".
329                                                                         "\" alt=\"*\">&nbsp;".
330                                                                         _($plHeadline)."</a>";
331                                                         }
332                                                         $entries.= "</td>\n";
333                                                 $col++ ;
335                                         }
336                                 }
338                                 /* Append to menu */
339                                 if ($entries != ""){
340                                         $this->iconmenu.= $menu.$entries;
342                                         /* Fill up remaining columns */
343                                         if ($col != 1){
344                                                 $col--;
345                                                 while ($col % 5){
346                                                         $this->iconmenu= $this->iconmenu.
347                                                                 "<td style=\"width:20%\">&nbsp;</td>\n";
348                                                         $col++;
349                                                 }
350                                         }
352                                         /* close table */
353                                         $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
354                                 }
355                         }
357                 }
359                 /* Write menu output */
360                 return ($this->iconmenu);
361         }
363         function get_path($index)
364         {
365                 if(!isset($this->dirlist[$index])){
366                         return ("");
367                 }
368                 return ("../".$this->dirlist[$index]);
369         }
371         function get_index($class)
372         {
373                 /* Search for plugin index (id), identify entry by path && class */
374                 $data = $this->config->data['MENU'];
375                 foreach($data as $section => $plugins){
376                         foreach($plugins as $key => $plugin)    {
377                                 if($plugin['CLASS'] == $class){
378                                         return($key);
379                                 }
380                         }
381                 }
383                 /* Nothing */
384                 return (0);
386         }
388   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
389       @param  $plug_id  Integer  The ID of the plugin.
390       @return Boolean   TRUE if we are allowed to view the plugin else FASLE
391    */
392   function plugin_access_allowed($plug_id)
393   {
394     return(isset($this->allowed_plugins[$plug_id]));
395   }
398   /*! \brief  Force the menu to be recreated 
399    */
400   function reset_menus()
401   {
402     $this->menu = "";
403     $this->iconmenu ="";
404   }
407 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
408 ?>