Code

Updated config and pluglist to supprt the new shortCut menu
[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 {
25   // The id of the last registered plugin id 
26   var $index= 0;
28   var $config= NULL;
29   var $ui= NULL;
31   // Contains a list of the paths for all registered plugins.
32   var $dirlist= array();
34   // Seems to be used in the help menu to generate an overview.
35   var $headlines = array();
36   // Remember stuff in here.
37   var $silly_cache= array();
39   // The pluglist keeps track of all registered plugins.
40   // (Registered plugins are those we are able to access)
41   var $pluginList = array();
43   // Some cache vars to avoid regenration of the menus.   
44   var $pathMenu = "";
45   var $menu= "";
46   var $iconmenu= "";
48   // Classes plInfo list
49   var $info = array();
51   function pluglist(&$config, &$ui)
52   {
53     $this->ui= &$ui;
54     $this->config= &$config;
55     $this->loadPluginList();
56   }
58   function loadPluginList()
59   {
60     $this->pluginList = array();
62                 /* Fill info part of pluglist */
63                 $classes= get_declared_classes();
64                 foreach ($classes as $cname){
65                         $cmethods = get_class_methods($cname);
66                         if (in_array_ics('plInfo',$cmethods)){
67                                 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
68                         }
69                 }
71     if(!session::is_set('maxC')){
72       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
73     }
74 //
75 //   // Now generate menu - usually they are cached
76 //   $this->gen_menu();
77 //   $this->show_iconmenu();
78 //   $this->genPathMenu();
79   }
82   /*! \brief    Tries to register a plugin in the pluglist
83    *            Checks existence and ACL for the given plugin.
84    *            Returns true in case of success else false. 
85    */
86   function registerPlugin($plug)
87   {
88     global $class_mapping;
90     if (!isset($plug['CLASS'])){
91       msg_dialog::display(
92           _("Configuration error"),
93           _("The configuration format has changed: please run the setup again!"),
94           FATAL_ERROR_DIALOG);
95       exit();
96     }
97     if (!plugin_available($plug['CLASS'])){
98       return(FALSE);
99     }
100     if (!$this->check_access($plug['ACL'])){
101       return(FALSE);
102     }
103     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
104     $this->pluginList[$this->index] = $plug['CLASS'];
105     $this->index++;
106     return(TRUE);
107   }
110   /*! \brief  Check whether we are allowed to modify the given acl or not..
111    *            This function is used to check which plugins are visible.
112    *            
113    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
114    *  @return   Boolean TRUE on success else FALSE
115    */
116   function check_access($aclname)
117   {
118     if (isset($this->silly_cache[$aclname])) {
119       return $this->silly_cache[$aclname];
120     }
122     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
123     $acls_to_check = array();
124     if(preg_match("/,/",$aclname)){
125       $acls_to_check = explode(",",$aclname);
126     }else{
127       $acls_to_check = array($aclname);
128     }
130     foreach($acls_to_check as $acl_to_check){
131       $acl_to_check = trim($acl_to_check);
133       /* Check if the given acl tag is only valid for self acl entries  
134        *          <plugin acl="users/user:self" class="user"...  
135        */       
136       if(preg_match("/:self$/",$acl_to_check)){
137         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
138         if(strpos($acl_to_check,"/")){
139           if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
140             $this->silly_cache[$aclname]= TRUE;
141             return(TRUE);
142           }
143         }else{
144           if($this->ui->get_category_permissions($this->ui->dn,$acl_to_check,"") != ""){
145             $this->silly_cache[$aclname]= TRUE;
146             return(TRUE);
147           }
148         }
149       }else{
151         // No self acls. Check if we have any acls for the given ACL type 
152         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
153         if(count($deps)){
154           $this->silly_cache[$aclname]= TRUE;
155           return TRUE;
156         }
157       }
158     }
160     $this->silly_cache[$aclname]= FALSE;
161     return (FALSE);
162   }
166   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
167    *             this usually only done once during login.
168    *            -----------------------------------------------------------------
169    *            Do NOT add style changes here manually, use the style.css or 
170    *             if you prefer create your own theme!!
171    *            -----------------------------------------------------------------
172    */
173   function gen_menu()
174   {
175     if ($this->menu == ""){
177       // First load the menu plugins and try to register them in the pluglist
178       //  if this fails for some reason, then remove the plugin from the menu.
179       if(isset($this->config->data['MENU'])){
180         foreach($this->config->data['MENU'] as $section => $plugins){
181           foreach($plugins as $id => $plug){
182             if(!$this->registerPlugin($plug)){ 
183               unset($this->config->data['MENU'][$section][$id]); 
184             }
185           }
186         }
187       }
189       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
191       $cfg= $this->config->data['MENU'];
192       $menu = "\n      <div class='navigation'>";
193       foreach ($cfg as $headline => $plug){
196         if(!count($plug)) continue;
198         $menu.= "\n        <div class='menu'>";
199         $menu.= "\n          <ul>";
200         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
201         $id = 0;
202         foreach ($plug as $info){
204             // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
205             if(isset($disabledPlugins[$info['CLASS']])) continue;
207           // Used to detect the last element in the menu
208           $id ++;
210           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
211           $class= "";
212           if($id == count($plug)) $class=" class='menu-last' ";
213           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
214         }
215         $menu.= "\n          </ul>";
216         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
217         $menu.= "\n        </div>\n";
218         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
219         $menu.= "\n        <div class='v-spacer'></div>\n";
220       }
221       $menu.= "\n      </div>\n";
222       $this->menu = $menu;
224       // Add javascript method to print out warning messages while leaving an unsaved form.
225       // We do it in here to get the string translated.
226       $this->menu .=  
227         "\n      <script language='javascript' type='text/javascript'>".
228         "\n        function openPlugin(id){".
229         "\n          return question(\""._("You are currently editing a database entry. Do you want to discard the changes?")."\",".
230         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
231         "\n        }".
232         "\n      </script>\n"; 
233     }
234   
235     // Use javascript to mark the currently selected plugin.
236     $menu = $this->menu;
237     if(isset($_GET['plug'])){
238       $menu.= 
239      "\n      <script language='javascript' type='text/javascript'>".
240      "\n        if($('plugMenuId_".$_GET['plug']."')){".
241      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
242      "\n        }".
243      "\n      </script>\n";
244     }
246     // Return the generated/cached gosa menu.
247     return ($menu);
248   }
251   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
252    *             login.
253    *            -----------------------------------------------------------------
254    *            Do NOT add style changes here manually, use the style.css or 
255    *             if you prefer create your own theme!!
256    *            -----------------------------------------------------------------
257    */
258   function show_iconmenu()
259   {
260     $add_hr =FALSE;
261     if ($this->iconmenu == ""){
263       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
265       $cfg= $this->config->data['MENU'];
266       foreach ($cfg as $headline => $plug){
267         $col= 0;
269         $this->iconmenu .= "\n        <div class='clear'></div>";
270         if($add_hr){
271           $add_hr = FALSE;
272           $this->iconmenu .= "\n        <hr>";
273         }
274         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
276         foreach ($plug as $info){
278           // Get Plugin info
279           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
281           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
282           if(isset($disabledPlugins[$info['CLASS']])) continue;
284           // Add a seperating row
285           if (($col % 4) == 0){ 
286             $this->iconmenu .= "\n        <div class='clear'></div>";
287           }
289           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
290           $this->iconmenu.= "\n          ".image($icon);
291           $this->iconmenu.= "\n          <div class='dsc'>";
292           $this->iconmenu.= "\n            <h1>{$title}</h1>";
293           $this->iconmenu.= "\n            <p>{$desc}</p>";
294           $this->iconmenu.= "\n          </div>";
295           $this->iconmenu.= "\n        </div>";
296           $col++ ;
297         }
298         $add_hr = TRUE;
299       }
300     }
301     return ($this->iconmenu);
302   }
305   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
306    *             the generated HTML content, so we are not forced to generate it on every 
307    *             page request.
308    *            (See <pathMenu> of your gosa.conf)
309    */
310   function genPathMenu()
311   {
312     if(empty($this->pathMenu)){
314       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
316       // Now load the icon menu and try to register the plugins in the pluglist
317       //  if this fails for some reason, then remove the plugins from the menu.
318       if(isset($this->config->data['SHORTCUTMENU'])){
319         foreach($this->config->data['SHORTCUTMENU'] as $id => $plugin){
320           if(!$this->registerPlugin($plugin)){
321             unset($this->config->data['SHORTCUTMENU'][$id]); 
322           } 
323         }
324       }
326       // Now load the path menu and try to register the plugins in the pluglist
327       //  if this fails for some reason, then remove the plugin from the menu.
328       if(isset($this->config->data['PATHMENU'])){
329         foreach($this->config->data['PATHMENU'] as $id => $plugin){
330           if(!$this->registerPlugin($plugin)){
331             unset($this->config->data['PATHMENU'][$id]); 
332           } 
333         }
334       }
336       $this->pathMenu = 
337           "\n        <div class='plugin-path'>".
338           "\n          <ul class='path-navigation'>";
340       // Check if we've at least one entry defined ih the iconmenu
341       if(isset($this->config->data['SHORTCUTMENU'])){
342           $icfg= &$this->config->data['SHORTCUTMENU'];
343           $rcfg = array_reverse($icfg);
344           foreach($rcfg as $id => $plug){
345               list($index, $title, $desc, $icon, $shortIcon) = $this->getPlugData($plug['CLASS']);
347               // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
348               if(isset($disabledPlugins[$plug['CLASS']])) continue;
350               $cssClass= (!isset($rcfg[$id+1]))? 'left' : 'left right-border';
351               $this->pathMenu.= "            
352                   <li class='{$cssClass}' onClick='openPlugin({$index})' title='{$title}'>".
353                   image(get_template_path($shortIcon))."</li>";
354           }
355       }
357       // Place the navigator part, this will be replaced during runtime.
358       $this->pathMenu .= "\n            %navigator%";
360       // Check if we've at least one entry defined ih the pathmenu
361       if(isset($this->config->data['PATHMENU'])){
362         $cfg= &$this->config->data['PATHMENU'];
363         $rcfg = array_reverse($cfg);
364         foreach($rcfg as $id => $plug){
365           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
367           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
368           if(isset($disabledPlugins[$plug['CLASS']])) continue;
370           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
371         }
372       }
373       $this->pathMenu.= "\n          </ul>";
374       $this->pathMenu.= "\n        </div>";
375     }
377     $menu = pathNavigator::getCurrentPath();
378     return(preg_replace("/%navigator%/", $menu, $this->pathMenu)); 
379   }
380   
382   /*! \brief    Returns additional info for a given class name, like 
383    *             plugin-icon, title, description and the index of the element 
384                  in the pluglist which uses this class.
385    */
386   function getPlugData($class)
387   {
388     global $class_mapping;
389     $vars= get_class_vars($class);
390     $plHeadline= _($vars['plHeadline']);
391     $plDescription= _($vars['plDescription']);
392     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
393     $plShortIcon = (isset($vars['plShortIcon'])) ? $vars['plShortIcon']: "plugin.png";
395     $index= $this->get_index($class);
397     /* Check if class is available. If the class doesn't exists display error symbol
398      *  to avoid that a user clicks on a non existing plugin
399      */
400     if(!$vars){
401       $plHeadline = $plDescription = _("Unknown");
402       $plIcon = "error.png";
403       $index = '';
404     } 
406     // Detect the correct position of the plugin icon
407     if(!preg_match("/\//",$plIcon)){
408       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
409             $class_mapping[$class])."/images/$plIcon");
410     }else{
411       $image = $plIcon; 
412     }
413     // Detect the correct position of the plugin icon
414     if(!preg_match("/\//",$plShortIcon)){
415       $shortImage= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
416             $class_mapping[$class])."/images/$plShortIcon");
417     }else{
418       $shortImage = $plShortIcon; 
419     }
420     return(array($index, $plHeadline, $plDescription, $image, $shortImage));
421   }
424   /*! \brief    Returns the installation path of a plugin.
425    *            e.g. '../plugins/admin/mimetypes'
426    */
427   function get_path($index)
428   {
429     if(!isset($this->dirlist[$index])){
430       return ("");
431     }
432     return ("../".$this->dirlist[$index]);
433   }
436   /*! \brief    Returns the plugins id for a given class.
437    */
438   function get_index($class)
439   {
440     return (array_search($class, $this->pluginList));
441   }
444   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
445    *
446    *  @param  $plug_id  Integer  The ID of the plugin.
447    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
448    */
449   function plugin_access_allowed($plug_id)
450   {
451     return(isset($this->pluginList[$plug_id]));
452   }
455   /*! \brief  Force the menu to be recreated 
456    */
457   function reset_menus()
458   {
459     $this->menu = "";
460     $this->iconmenu ="";
461   }
464   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
465    *            This is just used in the helpviewer.php  
466    */
467   function gen_headlines()
468   {
469     $ret = array();
470     if(count($this->headlines) == 0){
471       foreach($this->config->data['MENU'] as $headline => $plugins){
472         foreach( $plugins as $id => $plug){
473           if (plugin_available($plug['CLASS'])){
474             $attrs = (get_class_vars($plug['CLASS']));
475             $ret[$id]['HEADLINE'] = $headline;
476             $ret[$id]['NAME']     = $attrs['plHeadline'];
477           }
478         }
479       }
480       $this->headlines = $ret;
481     }
482     return($this->headlines);
483   }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>