Code

Updated generateLdif method
[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           }
215         }
216       }
218       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
220       $cfg= $this->config->data['MENU'];
221       $menu = "\n      <div class='navigation'>";
222       foreach ($cfg as $headline => $plug){
225         if(!count($plug)) continue;
227         $menu.= "\n        <div class='menu'>";
228         $menu.= "\n          <ul>";
229         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
230         $id = 0;
231         foreach ($plug as $info){
233             // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
234             if(isset($disabledPlugins[$info['CLASS']])) continue;
236           // Used to detect the last element in the menu
237           $id ++;
239           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
240           $class= "";
241           if($id == count($plug)) $class=" class='menu-last' ";
242           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
243         }
244         $menu.= "\n          </ul>";
245         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
246         $menu.= "\n        </div>\n";
247         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
248         $menu.= "\n        <div class='v-spacer'></div>\n";
249       }
250       $menu.= "\n      </div>\n";
251       $this->menu = $menu;
253       // Add javascript method to print out warning messages while leaving an unsaved form.
254       // We do it in here to get the string translated.
255       $this->menu .=  
256         "\n      <script language='javascript' type='text/javascript'>".
257         "\n        function openPlugin(id){".
258         "\n          return question(\""._("You are currently editing a database entry. Do you want to discard the changes?")."\",".
259         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
260         "\n        }".
261         "\n      </script>\n"; 
262     }
263   
264     // Use javascript to mark the currently selected plugin.
265     $menu = $this->menu;
266     if(isset($_GET['plug'])){
267       $menu.= 
268      "\n      <script language='javascript' type='text/javascript'>".
269      "\n        if($('plugMenuId_".$_GET['plug']."')){".
270      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
271      "\n        }".
272      "\n      </script>\n";
273     }
275     // Return the generated/cached gosa menu.
276     return ($menu);
277   }
280   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
281    *             login.
282    *            -----------------------------------------------------------------
283    *            Do NOT add style changes here manually, use the style.css or 
284    *             if you prefer create your own theme!!
285    *            -----------------------------------------------------------------
286    */
287   function show_iconmenu()
288   {
289     $add_hr =FALSE;
290     if ($this->iconmenu == ""){
292       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
294       $cfg= $this->config->data['MENU'];
295       foreach ($cfg as $headline => $plug){
296         $col= 0;
298         $this->iconmenu .= "\n        <div class='clear'></div>";
299         if($add_hr){
300           $add_hr = FALSE;
301           $this->iconmenu .= "\n        <hr>";
302         }
303         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
305         foreach ($plug as $info){
307           // Get Plugin info
308           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
310           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
311           if(isset($disabledPlugins[$info['CLASS']])) continue;
313           // Add a seperating row
314           if (($col % 4) == 0){ 
315             $this->iconmenu .= "\n        <div class='clear'></div>";
316           }
318           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
319           $this->iconmenu.= "\n          ".image($icon);
320           $this->iconmenu.= "\n          <div class='dsc'>";
321           $this->iconmenu.= "\n            <h1>{$title}</h1>";
322           $this->iconmenu.= "\n            <p>{$desc}</p>";
323           $this->iconmenu.= "\n          </div>";
324           $this->iconmenu.= "\n        </div>";
325           $col++ ;
326         }
327         $add_hr = TRUE;
328       }
329     }
330     return ($this->iconmenu);
331   }
334   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
335    *             the generated HTML content, so we are not forced to generate it on every 
336    *             page request.
337    *            (See <pathMenu> of your gosa.conf)
338    */
339   function genPathMenu()
340   {
341     if(empty($this->pathMenu)){
343       $disabledPlugins = $this->config->configRegistry->getDisabledPlugins();
345       // Now load the icon menu and try to register the plugins in the pluglist
346       //  if this fails for some reason, then remove the plugins from the menu.
347       if(isset($this->config->data['SHORTCUTMENU'])){
348         foreach($this->config->data['SHORTCUTMENU'] as $id => $plugin){
349           if(!$this->registerPlugin($this->config->data['SHORTCUTMENU'][$id])){
350             unset($this->config->data['SHORTCUTMENU'][$id]); 
351           } 
352         }
353       }
355       // Now load the path menu and try to register the plugins in the pluglist
356       //  if this fails for some reason, then remove the plugin from the menu.
357       if(isset($this->config->data['PATHMENU'])){
358         foreach($this->config->data['PATHMENU'] as $id => $plugin){
359           if(!$this->registerPlugin($this->config->data['PATHMENU'][$id])){
360             unset($this->config->data['PATHMENU'][$id]); 
361           } 
362         }
363       }
365       $this->pathMenu = 
366           "\n        <div class='plugin-path'>".
367           "\n          <ul class='path-navigation'>";
369       // Check if we've at least one entry defined ih the iconmenu
370       if(isset($this->config->data['SHORTCUTMENU'])){
371           $icfg= &$this->config->data['SHORTCUTMENU'];
372           $rcfg = array_reverse($icfg);
373           foreach($rcfg as $id => $plug){
374               list($index, $title, $desc, $icon, $shortIcon) = $this->getPlugData($plug['CLASS']);
376               // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
377               if(isset($disabledPlugins[$plug['CLASS']])) continue;
379               $cssClass= (!isset($rcfg[$id+1]))? 'left' : 'left right-border';
380               $this->pathMenu.= "            
381                   <li class='{$cssClass}' onClick='openPlugin({$index})' title='{$title}'>".
382                   image(get_template_path($shortIcon))."</li>";
383           }
384       }
386       // Place the navigator part, this will be replaced during runtime.
387       $this->pathMenu .= "\n            %navigator%";
389       // Check if we've at least one entry defined ih the pathmenu
390       if(isset($this->config->data['PATHMENU'])){
391         $cfg= &$this->config->data['PATHMENU'];
392         $rcfg = array_reverse($cfg);
393         foreach($rcfg as $id => $plug){
394           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
396           // The Plugin has been deactivated for some reason, perhabs a missing ldap schema.
397           if(isset($disabledPlugins[$plug['CLASS']])) continue;
399           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
400         }
401       }
402       $this->pathMenu.= "\n          </ul>";
403       $this->pathMenu.= "\n        </div>";
404     }
406     $menu = pathNavigator::getCurrentPath();
407     return(preg_replace("/%navigator%/", $menu, $this->pathMenu)); 
408   }
409   
411   /*! \brief    Returns additional info for a given class name, like 
412    *             plugin-icon, title, description and the index of the element 
413                  in the pluglist which uses this class.
414    */
415   function getPlugData($class)
416   {
417     global $class_mapping;
418     $vars= get_class_vars($class);
419     $plHeadline= _($vars['plHeadline']);
420     $plDescription= _($vars['plDescription']);
421     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
422     $plShortIcon = (isset($vars['plShortIcon'])) ? $vars['plShortIcon']: "plugin.png";
424     $index= $this->get_index($class);
426     /* Check if class is available. If the class doesn't exists display error symbol
427      *  to avoid that a user clicks on a non existing plugin
428      */
429     if(!$vars){
430       $plHeadline = $plDescription = _("Unknown");
431       $plIcon = "error.png";
432       $index = '';
433     } 
435     // Detect the correct position of the plugin icon
436     if(!preg_match("/\//",$plIcon)){
437       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
438             $class_mapping[$class])."/images/$plIcon");
439     }else{
440       $image = $plIcon; 
441     }
442     // Detect the correct position of the plugin icon
443     if(!preg_match("/\//",$plShortIcon)){
444       $shortImage= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
445             $class_mapping[$class])."/images/$plShortIcon");
446     }else{
447       $shortImage = $plShortIcon; 
448     }
449     return(array($index, $plHeadline, $plDescription, $image, $shortImage));
450   }
453   /*! \brief    Returns the installation path of a plugin.
454    *            e.g. '../plugins/admin/mimetypes'
455    */
456   function get_path($index)
457   {
458     if(!isset($this->dirlist[$index])){
459       return ("");
460     }
461     return ("../".$this->dirlist[$index]);
462   }
465   /*! \brief    Returns the plugins id for a given class.
466    */
467   function get_index($class)
468   {
469     return (array_search($class, $this->pluginList));
470   }
473   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
474    *
475    *  @param  $plug_id  Integer  The ID of the plugin.
476    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
477    */
478   function plugin_access_allowed($plug_id)
479   {
480     return(isset($this->pluginList[$plug_id]));
481   }
484   /*! \brief  Force the menu to be recreated 
485    */
486   function reset_menus()
487   {
488     $this->menu = "";
489     $this->iconmenu ="";
490   }
493   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
494    *            This is just used in the helpviewer.php  
495    */
496   function gen_headlines()
497   {
498     $ret = array();
499     if(count($this->headlines) == 0){
500       foreach($this->config->data['MENU'] as $headline => $plugins){
501         foreach( $plugins as $id => $plug){
502           if (plugin_available($plug['CLASS'])){
503             $attrs = (get_class_vars($plug['CLASS']));
504             $ret[$id]['HEADLINE'] = $headline;
505             $ret[$id]['NAME']     = $attrs['plHeadline'];
506           }
507         }
508       }
509       $this->headlines = $ret;
510     }
511     return($this->headlines);
512   }
514 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
515 ?>