Code

Cleaned up pluglist a bit.
[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();
35         var $silly_cache= array();
37         function pluglist(&$config, &$ui)
38         {
39                 $this->ui= &$ui;
40                 $this->config= &$config;
42                 // Get list of plugin paths, this allows us to open the plugins main.inc if available.
43                 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
45                 // Detect installed plugins and their configuration, to be able to restrict access later.
46                 $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                 // Reserve a special ACL will allows us to display plugins/addons whenever a user 
55     //  is able to login into gosa. E.g. some kind of welcome page. 
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         }
63   /*! \brief    Detect plugin installation paths, by walking through the config. 
64    */
65         function get_plugins($list, &$config)
66         {
67                 global $class_mapping;
68                 if (!isset($config['CLASS'])){
69                         if (is_array($config)){
70                                 foreach ($config as $val){
71                                         $list= $this->get_plugins($list, $val);
72                                 }
73                         }
74                 } else {
75                         if (is_array($config) && isset($class_mapping[$config['CLASS']])){
76                                 $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
77                         } else {
78                                 $list[$this->index++]= "";
79                         }
80                 }
82                 return ($list);
83         }
86   /*! \brief  Check whether we are allowed to modify the given acl or not..
87    *            This function is used to check which plugins are visible.
88    *            
89    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
90    *  @return   Boolean TRUE on success else FALSE
91    */
92         function check_access($aclname)
93         {
94                 if (isset($this->silly_cache[$aclname])) {
95                         return $this->silly_cache[$aclname];
96                 }
98                 // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
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);
108                 
109       /* Check if the given acl tag is only valid for self acl entries  
110        *          <plugin acl="users/user:self" class="user"...  
111        */       
112                         if(preg_match("/:self$/",$acl_to_check)){
113                                 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
114                                 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
115                                         $this->silly_cache[$aclname]= TRUE;
116                                         return(TRUE);
117                                 }
118                                 $this->silly_cache[$aclname]= FALSE;
119                                 return(FALSE);
120                         }else{
121                 
122                                 // No self acls. Check if we have any acls for the given ACL type 
123                                 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
124                                 if(count($deps)){
125                                         $this->silly_cache[$aclname]= TRUE;
126                                         return TRUE;
127                                 }
128                         }
129                 }
131                 $this->silly_cache[$aclname]= FALSE;
132                 return (FALSE);
133         }
136   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
137    *            This is just used in the helpviewer.php  
138    */
139         function gen_headlines()
140         {
141                 $ret = array();
142                 if(count($this->headlines) == 0){
143                         foreach($this->config->data['MENU'] as $headline => $plugins){
144                                 foreach( $plugins as $id => $plug){
145                                         if (plugin_available($plug['CLASS'])){
146                                                 $attrs = (get_class_vars($plug['CLASS']));
147                                                 $ret[$id]['HEADLINE'] = $headline;
148                                                 $ret[$id]['NAME']         = $attrs['plHeadline'];
149                                         }
150                                 }
151                         }
152                         $this->headlines = $ret;
153                 }
154                 return($this->headlines);
155         }
158   /*! \brief    Check the accessibility of the configured plugins.
159    *            We may simply have now permissions to access some plugins 
160    *             but some may be broken or missing!.
161    */
162   function checkMenu()
163   {
164           $cfg= &$this->config->data['MENU'];
165     foreach ($cfg as $headline => $plug){
166                   $this->menuparts[_($headline)]= array();
167       foreach ($plug as $id => $info){
168         if (!isset($info['CLASS'])){
169           msg_dialog::display(
170               _("Configuration error"), 
171               _("The configuration format has changed. Please re-run setup!"), 
172               FATAL_ERROR_DIALOG);
173           exit();
174         }
175         if (!plugin_available($info['CLASS'])){
176           unset($cfg[$headline][$id]); 
177           continue; 
178         }
179                           if (!$this->check_access($info['ACL'])){
180           unset($cfg[$headline][$id]);
181           continue; 
182         }
183       }
184     }
185     if(!session::is_set('maxC')){
186       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
187     }
188   }
191   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
192    *             this usually only done once during login.
193    *            -----------------------------------------------------------------
194    *            Do NOT add style changes here manually, use the style.css or 
195    *             if you prefer create your own theme!!
196    *            -----------------------------------------------------------------
197    */
198   function gen_menu()
199   {
200     if ($this->menu == ""){
201       $cfg= $this->config->data['MENU'];
202       $this->checkMenu();
203       $cfg= $this->config->data['MENU'];
204       $menu = "\n<div class='navigation'>";
205       foreach ($cfg as $headline => $plug){
207         if(!count($plug)) continue;
208         $menu.= "\n<div class='menu'>";
209         $menu.= "\n <ul>";
210         $menu.= "\n  <li class='menu-header'>"._($headline)."</li>";
211         $id = 0;
212         foreach ($plug as $info){
213           $id ++;
214           $vars= get_class_vars($info['CLASS']);
215           $plHeadline = _((isset($info['NAME'])) ? $info['NAME'] : $vars['plHeadline']);
216           $plDescription= $vars['plDescription'];
217           $index= $this->get_index($info['CLASS']);
218           $href= "main.php?plug=$index&amp;reset=1";
219           if(!$vars){
220             $plHeadline         = _("Unknown");
221             $plDescription      = _("Unknown");
222             $href= "main.php?reset=1";
223           }
224           $this->allowed_plugins[$index] = $index;
225           $class= "";
226           if($id == count($plug)) $class=" class='menu-last' \n   ";
227           $menu .=  "\n  <li $class onClick='return question(\"".
228             _("You are currently editing a database entry. Do you want to dismiss the changes?")."\", 
229             \"$href\");'>".$plHeadline."</li>";
230         }
231         $menu.= "\n </ul>";
232         $menu.= "\n <div></div>\n";
233         $menu.= "\n</div>\n";
234         $menu.= "\n<div></div>\n";
235         $menu.= "\n<div class='v-spacer'></div>\n";
236       }
237       $menu.= "\n</div>";
238       $this->menu = $menu;
239     }
241     // Return the generated/cached gosa menu.
242     return ($this->menu);
243   }
246   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
247    *             login.
248    *            -----------------------------------------------------------------
249    *            Do NOT add style changes here manually, use the style.css or 
250    *             if you prefer create your own theme!!
251    *            -----------------------------------------------------------------
252    */
253         function show_iconmenu()
254         {
255                 global $class_mapping;
256                 if ($this->iconmenu == ""){
257                         $cfg= $this->config->data['MENU'];
259                         if (isset($this->config->current['ICONSIZE'])){
260                                 list($x, $y)= explode("x", $this->config->get_cfg_value("iconsize"));
261                                 $isize= "width=\"$x\" height=\"$y\"";
262                         } else {
263                                 $isize= "";
264                         }
266                         /* Parse headlines */
267                         foreach ($cfg as $headline => $plug){
268                                 $col= 1;
269                                 $menu= "<h1 class=\"menuheader\">".
270                                         _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
271                                         "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
272                                 $entries= "";
274                                 foreach ($plug as $info){
276                                         if (!plugin_available($info['CLASS'])){
277                                                 continue;
278                                         }
280                                         /* Read information from class variable */
281                                         $vars= get_class_vars($info['CLASS']);
282                                         $plHeadline= $vars['plHeadline'];
283                                         $plDescription= $vars['plDescription'];
284           if (isset($vars['plIcon'])){
285                                         $plIcon= $vars['plIcon'];
286           } else {
287                                         $plIcon= "plugin.png";
288           }
290                                         $index= $this->get_index($info['CLASS']);
292                                         $href = "main.php?plug=".$index."&amp;reset=1";
294                                         /* Check if class is available. If the class doesn't exists display error symbol 
295                                                 to avoid that a user clicks on a non existing plugin  */
296                                         if(!$vars){
297                                                 $plHeadline = $plDescription = _("Unknown");
298                                                 $info['ICON'] = "error.png";
299                                                 $href="main.php?reset=1";
300                                         }
303                                         if ($this->check_access($info['ACL'])){
305                                                 /* Load icon */
306                                                 if (isset($info['ICON'])){
307                                                         $image= get_template_path('images/'.$info['ICON']);
308                                                 } else {
309               if(!preg_match("/\//",$plIcon)){
310                                                         $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
311               }else{
312                 $image = $plIcon; 
313               }
314                                                 }
315                                                 if ($col > 5){
316                                                         $entries= $entries."</tr><tr>";
317                                                         $col = 1;
318                                                 }
319                                                 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
320                                                         ">";
321                                                         if(session::global_get('js')){
322                                                                 $entries.= "<img $isize border=0 align=middle src=\"$image".
323                                                                         "\" alt=\"*\">&nbsp;".
324                                                                         _($plHeadline);
325                                                         } else {
326                                                                 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
327                                                                         "<img $isize border=0 align=middle src=\"$image".
328                                                                         "\" alt=\"*\">&nbsp;".
329                                                                         _($plHeadline)."</a>";
330                                                         }
331                                                         $entries.= "</td>\n";
332                                                 $col++ ;
334                                         }
335                                 }
337                                 /* Append to menu */
338                                 if ($entries != ""){
339                                         $this->iconmenu.= $menu.$entries;
341                                         /* Fill up remaining columns */
342                                         if ($col != 1){
343                                                 $col--;
344                                                 while ($col % 5){
345                                                         $this->iconmenu= $this->iconmenu.
346                                                                 "<td style=\"width:20%\">&nbsp;</td>\n";
347                                                         $col++;
348                                                 }
349                                         }
351                                         /* close table */
352                                         $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
353                                 }
354                         }
356                 }
358                 /* Write menu output */
359                 return ($this->iconmenu);
360         }
363   /*! \brief    Returns the installation path of a plugin.
364    *            e.g. '../plugins/admin/mimetypes'
365    */
366         function get_path($index)
367         {
368                 if(!isset($this->dirlist[$index])){
369                         return ("");
370                 }
371     echo "../".$this->dirlist[$index];
372                 return ("../".$this->dirlist[$index]);
373         }
376   /*! \brief    Returns the plugins id for a given class.
377    */
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     }
389     return (0);
390         }
393   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
394    *
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   }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>