Code

Updated class_plugin.inc
[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       // Clean ACL string, we do not want any spaces or lines breaks here. 
91       $plug['ACL'] = trim($plug['ACL']);
93       // Clean ACL string, we do not want any spaces or lines breaks here.
94       $acl = trim($plug['ACL']);
95       if(preg_match("/,/",$acl)){
96           $acls = explode(",",$acl);
97       }else{
98           $acls = array($acl);
99       }
100       foreach($acls as $key => $aclEntry){
101           $aclEntry = trim($aclEntry);
102           $tmp = preg_replace("/[^a-z0-9\/:]/i","",$aclEntry);
104           // Check if cleaned 'acl' tag doesn't match the configured one from the gosa.conf.
105           // Display a notification to tell the user that there is something wrong.
106           if($tmp != $aclEntry){
107               trigger_error("Please check acl='{$aclEntry}' tag for plugin '{$plug['CLASS']}' in your gosa.conf, it contains invalid characters!" );
108           }
109           $acls[$key] = $tmp;
110       }
111       $plug['ACL'] = implode(',',$acls);
114       // Check class
115       if (!isset($plug['CLASS'])){
116           msg_dialog::display(
117                   _("Configuration error"),
118                   _("The configuration format has changed: please run the setup again!"),
119                   FATAL_ERROR_DIALOG);
120           exit();
121       }
122       if (!plugin_available($plug['CLASS'])){
123           return(FALSE);
124       }
125       if (!$this->check_access($plug['ACL'])){
126           return(FALSE);
127       }
128       $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
129       $this->pluginList[$this->index] = $plug['CLASS'];
130       $this->index++;
131       return(TRUE);
132   }
135   /*! \brief  Check whether we are allowed to modify the given acl or not..
136    *            This function is used to check which plugins are visible.
137    *            
138    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
139    *  @return   Boolean TRUE on success else FALSE
140    */
141   function check_access($aclname)
142   {
143     if (isset($this->silly_cache[$aclname])) {
144       return $this->silly_cache[$aclname];
145     }
147     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
148     $acls_to_check = array();
149     if(preg_match("/,/",$aclname)){
150       $acls_to_check = explode(",",$aclname);
151     }else{
152       $acls_to_check = array($aclname);
153     }
155     foreach($acls_to_check as $acl_to_check){
156     
157       // Remove spaces and line breaks.
158       $acl_to_check = trim($acl_to_check);
159       $acl_to_check = preg_replace("/ /","",$acl_to_check);
160         
162       /* Check if the given acl tag is only valid for self acl entries  
163        *          <plugin acl="users/user:self" class="user"...  
164        */       
165       if(preg_match("/:self$/",$acl_to_check)){
166         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
167         if(strpos($acl_to_check,"/")){
168           if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
169             $this->silly_cache[$aclname]= TRUE;
170             return(TRUE);
171           }
172         }else{
173           if($this->ui->get_category_permissions($this->ui->dn,$acl_to_check,"") != ""){
174             $this->silly_cache[$aclname]= TRUE;
175             return(TRUE);
176           }
177         }
178       }else{
180         // No self acls. Check if we have any acls for the given ACL type 
181         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
182         if(count($deps)){
183           $this->silly_cache[$aclname]= TRUE;
184           return TRUE;
185         }
186       }
187     }
189     $this->silly_cache[$aclname]= FALSE;
190     return (FALSE);
191   }
195   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
196    *             this usually only done once during login.
197    *            -----------------------------------------------------------------
198    *            Do NOT add style changes here manually, use the style.css or 
199    *             if you prefer create your own theme!!
200    *            -----------------------------------------------------------------
201    */
202   function gen_menu()
203   {
204     if ($this->menu == ""){
206       // First load the menu plugins and try to register them in the pluglist
207       //  if this fails for some reason, then remove the plugin from the menu.
208       if(isset($this->config->data['MENU'])){
209         foreach($this->config->data['MENU'] as $section => $plugins){
210           foreach($plugins as $id => $plug){
211             if(!$this->registerPlugin($this->config->data['MENU'][$section][$id])){ 
212               unset($this->config->data['MENU'][$section][$id]); 
213             }
214           }
216           // Remove empty sections 
217           if(count($this->config->data['MENU'][$section]) == 0){ 
218               unset($this->config->data['MENU'][$section]); 
219           } 
220         }
221       }
223       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
225       $cfg= $this->config->data['MENU'];
226       $menu = "\n      <div class='navigation'>";
227       foreach ($cfg as $headline => $plug){
230         if(!count($plug)) continue;
232         $menu.= "\n        <div class='menu'>";
233         $menu.= "\n          <ul>";
234         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
235         $id = 0;
236         foreach ($plug as $info){
238             // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
239             if(isset($disabledPlugins[$info['CLASS']])) continue;
241           // Used to detect the last element in the menu
242           $id ++;
244           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
245           $class= "";
246           if($id == count($plug)) $class=" class='menu-last' ";
247           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
248         }
249         $menu.= "\n          </ul>";
250         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
251         $menu.= "\n        </div>\n";
252         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
253         $menu.= "\n        <div class='v-spacer'></div>\n";
254       }
255       $menu.= "\n      </div>\n";
256       $this->menu = $menu;
258       // Add javascript method to print out warning messages while leaving an unsaved form.
259       // We do it in here to get the string translated.
260       $this->menu .=  
261         "\n      <script language='javascript' type='text/javascript'>".
262         "\n        function openPlugin(id){".
263         "\n          return question(\""._("You are currently editing a database entry. Do you want to discard the changes?")."\",".
264         "\n            \"main.php?plug=\" + id + \"&reset=1\");".
265         "\n        }".
266         "\n      </script>\n"; 
267     }
268   
269     // Use javascript to mark the currently selected plugin.
270     $menu = $this->menu;
271     if(isset($_GET['plug'])){
272       $menu.= 
273      "\n      <script language='javascript' type='text/javascript'>".
274      "\n        if($('plugMenuId_".$_GET['plug']."')){".
275      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
276      "\n        }".
277      "\n      </script>\n";
278     }
280     // Return the generated/cached gosa menu.
281     return ($menu);
282   }
285   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
286    *             login.
287    *            -----------------------------------------------------------------
288    *            Do NOT add style changes here manually, use the style.css or 
289    *             if you prefer create your own theme!!
290    *            -----------------------------------------------------------------
291    */
292   function show_iconmenu()
293   {
294     $add_hr =FALSE;
295     if ($this->iconmenu == ""){
297       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
299       $cfg= $this->config->data['MENU'];
300       foreach ($cfg as $headline => $plug){
301         $col= 0;
303         $this->iconmenu .= "\n        <div class='clear'></div>";
304         if($add_hr){
305           $add_hr = FALSE;
306           $this->iconmenu .= "\n        <hr>";
307         }
308         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
310         foreach ($plug as $info){
312           // Get Plugin info
313           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
315           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
316           if(isset($disabledPlugins[$info['CLASS']])) continue;
318           // Add a seperating row
319           if (($col % 4) == 0){ 
320             $this->iconmenu .= "\n        <div class='clear'></div>";
321           }
323           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
324           $this->iconmenu.= "\n          ".image($icon);
325           $this->iconmenu.= "\n          <div class='dsc'>";
326           $this->iconmenu.= "\n            <h1>{$title}</h1>";
327           $this->iconmenu.= "\n            <p>{$desc}</p>";
328           $this->iconmenu.= "\n          </div>";
329           $this->iconmenu.= "\n        </div>";
330           $col++ ;
331         }
332         $add_hr = TRUE;
333       }
334     }
335     return ($this->iconmenu);
336   }
339   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
340    *             the generated HTML content, so we are not forced to generate it on every 
341    *             page request.
342    *            (See <pathMenu> of your gosa.conf)
343    */
344   function genPathMenu()
345   {
346     if(empty($this->pathMenu)){
348       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
350       // Now load the icon menu and try to register the plugins in the pluglist
351       //  if this fails for some reason, then remove the plugins from the menu.
352       if(isset($this->config->data['SHORTCUTMENU'])){
353         foreach($this->config->data['SHORTCUTMENU'] as $id => $plugin){
354           if(!$this->registerPlugin($this->config->data['SHORTCUTMENU'][$id])){
355             unset($this->config->data['SHORTCUTMENU'][$id]); 
356           } 
357         }
358       }
360       // Now load the path menu and try to register the plugins in the pluglist
361       //  if this fails for some reason, then remove the plugin from the menu.
362       if(isset($this->config->data['PATHMENU'])){
363         foreach($this->config->data['PATHMENU'] as $id => $plugin){
364           if(!$this->registerPlugin($this->config->data['PATHMENU'][$id])){
365             unset($this->config->data['PATHMENU'][$id]); 
366           } 
367         }
368       }
370       $this->pathMenu = 
371           "\n        <div class='plugin-path'>".
372           "\n          <ul class='path-navigation'>";
374       // Check if we've at least one entry defined ih the iconmenu
375       if(isset($this->config->data['SHORTCUTMENU'])){
376           $icfg= &$this->config->data['SHORTCUTMENU'];
377           $rcfg = array_reverse($icfg);
378           foreach($rcfg as $id => $plug){
379               list($index, $title, $desc, $icon, $shortIcon) = $this->getPlugData($plug['CLASS']);
381               // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
382               if(isset($disabledPlugins[$plug['CLASS']])) continue;
384               $cssClass= (!isset($rcfg[$id+1]))? 'left' : 'left right-border';
385               $this->pathMenu.= "            
386                   <li class='{$cssClass}' onClick='openPlugin({$index})' title='{$title}'>".
387                   image(get_template_path($shortIcon))."</li>";
388           }
389       }
391       // Place the navigator part, this will be replaced during runtime.
392       $this->pathMenu .= "\n            %navigator%";
394       // Check if we've at least one entry defined ih the pathmenu
395       if(isset($this->config->data['PATHMENU'])){
396         $cfg= &$this->config->data['PATHMENU'];
397         $rcfg = array_reverse($cfg);
398         foreach($rcfg as $id => $plug){
399           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
401           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
402           if(isset($disabledPlugins[$plug['CLASS']])) continue;
404           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
405         }
406       }
407       $this->pathMenu.= "\n          </ul>";
408       $this->pathMenu.= "\n        </div>";
409     }
411     $menu = pathNavigator::getCurrentPath();
412     return(preg_replace("/%navigator%/", $menu, $this->pathMenu)); 
413   }
414   
416   /*! \brief    Returns additional info for a given class name, like 
417    *             plugin-icon, title, description and the index of the element 
418                  in the pluglist which uses this class.
419    */
420   function getPlugData($class)
421   {
422     global $class_mapping;
423     $vars= get_class_vars($class);
424     $plHeadline= _($vars['plHeadline']);
425     $plDescription= _($vars['plDescription']);
426     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
427     $plShortIcon = (isset($vars['plShortIcon'])) ? $vars['plShortIcon']: "plugin.png";
429     $index= $this->get_index($class);
431     /* Check if class is available. If the class doesn't exists display error symbol
432      *  to avoid that a user clicks on a non existing plugin
433      */
434     if(!$vars){
435       $plHeadline = $plDescription = _("Unknown");
436       $plIcon = "error.png";
437       $index = '';
438     } 
440     // Detect the correct position of the plugin icon
441     if(!preg_match("/\//",$plIcon)){
442       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
443             $class_mapping[$class])."/images/$plIcon");
444     }else{
445       $image = $plIcon; 
446     }
447     // Detect the correct position of the plugin icon
448     if(!preg_match("/\//",$plShortIcon)){
449       $shortImage= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
450             $class_mapping[$class])."/images/$plShortIcon");
451     }else{
452       $shortImage = $plShortIcon; 
453     }
454     return(array($index, $plHeadline, $plDescription, $image, $shortImage));
455   }
458   /*! \brief    Returns the installation path of a plugin.
459    *            e.g. '../plugins/admin/mimetypes'
460    */
461   function get_path($index)
462   {
463     if(!isset($this->dirlist[$index])){
464       return ("");
465     }
466     return ("../".$this->dirlist[$index]);
467   }
470   /*! \brief    Returns the plugins id for a given class.
471    */
472   function get_index($class)
473   {
474     return (array_search($class, $this->pluginList));
475   }
478   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
479    *
480    *  @param  $plug_id  Integer  The ID of the plugin.
481    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
482    */
483   function plugin_access_allowed($plug_id)
484   {
485     return(isset($this->pluginList[$plug_id]));
486   }
489   /*! \brief  Force the menu to be recreated 
490    */
491   function reset_menus()
492   {
493     $this->menu = "";
494     $this->iconmenu ="";
495   }
498   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
499    *            This is just used in the helpviewer.php  
500    */
501   function gen_headlines()
502   {
503     $ret = array();
504     if(count($this->headlines) == 0){
505       foreach($this->config->data['MENU'] as $headline => $plugins){
506         foreach( $plugins as $id => $plug){
507           if (plugin_available($plug['CLASS'])){
508             $attrs = (get_class_vars($plug['CLASS']));
509             $ret[$id]['HEADLINE'] = $headline;
510             $ret[$id]['NAME']     = $attrs['plHeadline'];
511           }
512         }
513       }
514       $this->headlines = $ret;
515     }
516     return($this->headlines);
517   }
519 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
520 ?>