Code

Fixed labeling
[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                 /* Provide field for 'all' */
72                 $this->info['all']= array();
73                 $this->info['all']['plProvidedAcls']= array();
74                 $this->info['all']['plDescription']= _("All objects in this category");
75                 $this->info['all']['plSelfModify']= FALSE;
77     // First load the menu plugins and try to register them in the pluglist
78     //  if this fails for some reason, then remove the plugin from the menu.
79     if(isset($this->config->data['MENU'])){
80       foreach($this->config->data['MENU'] as $section => $plugins){
81         foreach($plugins as $id => $plug){
82           if(!$this->registerPlugin($plug)){ 
83             unset($this->config->data['MENU'][$section][$id]); 
84           }
85         }
86       }
87     }
88     
89     // Now load the path menu and try to register the plugins in the pluglist
90     //  if this fails for some reason, then remove the plugin from the menu.
91     if(isset($this->config->data['PATHMENU'])){
92       foreach($this->config->data['PATHMENU'] as $id => $plugin){
93         if(!$this->registerPlugin($plugin)){
94           unset($this->config->data['PATHMENU'][$id]); 
95         } 
96       }
97     }
99     if(!session::is_set('maxC')){
100       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
101     }
103     // Now generate menu - usually they are cached
104     $this->gen_menu();
105     $this->show_iconmenu();
106     $this->genPathMenu();
107   }
110   /*! \brief    Tries to register a plugin in the pluglist
111    *            Checks existence and ACL for the given plugin.
112    *            Returns true in case of success else false. 
113    */
114   function registerPlugin($plug)
115   {
116     global $class_mapping;
118     if (!isset($plug['CLASS'])){
119       msg_dialog::display(
120           _("Configuration error"),
121           _("The configuration format has changed. Please re-run setup!"),
122           FATAL_ERROR_DIALOG);
123       exit();
124     }
125     if (!plugin_available($plug['CLASS'])){
126       return(FALSE);
127     }
128     if (!$this->check_access($plug['ACL'])){
129       return(FALSE);
130     }
131     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
132     $this->pluginList[$this->index] = $plug['CLASS'];
133     $this->index++;
134     return(TRUE);
135   }
138   /*! \brief  Check whether we are allowed to modify the given acl or not..
139    *            This function is used to check which plugins are visible.
140    *            
141    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
142    *  @return   Boolean TRUE on success else FALSE
143    */
144   function check_access($aclname)
145   {
146     if (isset($this->silly_cache[$aclname])) {
147       return $this->silly_cache[$aclname];
148     }
150     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
151     $acls_to_check = array();
152     if(preg_match("/,/",$aclname)){
153       $acls_to_check = explode(",",$aclname);
154     }else{
155       $acls_to_check = array($aclname);
156     }
158     foreach($acls_to_check as $acl_to_check){
159       $acl_to_check = trim($acl_to_check);
161       /* Check if the given acl tag is only valid for self acl entries  
162        *          <plugin acl="users/user:self" class="user"...  
163        */       
164       if(preg_match("/:self$/",$acl_to_check)){
165         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
166         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
167           $this->silly_cache[$aclname]= TRUE;
168           return(TRUE);
169         }
170         $this->silly_cache[$aclname]= FALSE;
171         return(FALSE);
172       }else{
174         // No self acls. Check if we have any acls for the given ACL type 
175         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
176         if(count($deps)){
177           $this->silly_cache[$aclname]= TRUE;
178           return TRUE;
179         }
180       }
181     }
183     $this->silly_cache[$aclname]= FALSE;
184     return (FALSE);
185   }
189   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
190    *             this usually only done once during login.
191    *            -----------------------------------------------------------------
192    *            Do NOT add style changes here manually, use the style.css or 
193    *             if you prefer create your own theme!!
194    *            -----------------------------------------------------------------
195    */
196   function gen_menu()
197   {
198     if ($this->menu == ""){
199       $cfg= $this->config->data['MENU'];
200       $menu = "\n      <div class='navigation'>";
201       foreach ($cfg as $headline => $plug){
203         if(!count($plug)) continue;
205         $menu.= "\n        <div class='menu'>";
206         $menu.= "\n          <ul>";
207         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
208         $id = 0;
209         foreach ($plug as $info){
211           // Used to detect the last element in the menu
212           $id ++;
214           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
215           $class= "";
216           if($id == count($plug)) $class=" class='menu-last' ";
217           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
218         }
219         $menu.= "\n          </ul>";
220         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
221         $menu.= "\n        </div>\n";
222         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
223         $menu.= "\n        <div class='v-spacer'></div>\n";
224       }
225       $menu.= "\n      </div>\n";
226       $this->menu = $menu;
228       // Add javascript method to print out warning messages while leaving an unsaved form.
229       // We do it in here to get the string translated.
230       $this->menu .=  
231         "\n      <script language='javascript' type='text/javascript'>".
232         "\n        function openPlugin(id){".
233         "\n          return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
234         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
235         "\n        }".
236         "\n      </script>\n"; 
237     }
238   
239     // Use javascript to mark the currently selected plugin.
240     $menu = $this->menu;
241     if(isset($_GET['plug'])){
242       $menu.= 
243      "\n      <script language='javascript' type='text/javascript'>".
244      "\n        if($('plugMenuId_".$_GET['plug']."')){".
245      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
246      "\n        }".
247      "\n      </script>\n";
248     }
250     // Return the generated/cached gosa menu.
251     return ($menu);
252   }
255   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
256    *             login.
257    *            -----------------------------------------------------------------
258    *            Do NOT add style changes here manually, use the style.css or 
259    *             if you prefer create your own theme!!
260    *            -----------------------------------------------------------------
261    */
262   function show_iconmenu()
263   {
264     $add_hr =FALSE;
265     $this->iconmenu = "";
266     if ($this->iconmenu == ""){
267       $cfg= $this->config->data['MENU'];
268       foreach ($cfg as $headline => $plug){
269         $col= 0;
271         $this->iconmenu .= "\n        <div class='clear'></div>";
272         if($add_hr){
273           $add_hr = FALSE;
274           $this->iconmenu .= "\n        <hr>";
275         }
276         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
278         foreach ($plug as $info){
280           // Get Plugin info
281           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
283           // Add a seperating row
284           if (($col % 4) == 0){ 
285             $this->iconmenu .= "\n        <div class='clear'></div>";
286           }
288           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
289           $this->iconmenu.= "\n          ".image($icon);
290           $this->iconmenu.= "\n          <div class='dsc'>";
291           $this->iconmenu.= "\n            <h1>{$title}</h1>";
292           $this->iconmenu.= "\n            <p>{$desc}</p>";
293           $this->iconmenu.= "\n          </div>";
294           $this->iconmenu.= "\n        </div>";
295           $col++ ;
296         }
297         $add_hr = TRUE;
298       }
299     }
300     return ($this->iconmenu);
301   }
304   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
305    *             the generated HTML content, so we are not forced to generate it on every 
306    *             page request.
307    *            (See <pathMenu> of your gosa.conf)
308    */
309   function genPathMenu()
310   {
311     if(empty($this->pathMenu)){
312       $this->pathMenu = 
313         "\n        <div class='plugin-path'>".
314         "\n          <ul class='path-navigation'>".
315         "\n            <li class='left right-border' onClick=\"openPlugin('');\">".
316         "\n              <div class='nav-home'></div>".
317         "\n            </li>".
318         "\n            <li class='left'>Welcome to GOsa</li>";
320       // Check if we've at least one entry defined ih the pathmenu
321       if(isset($this->config->data['PATHMENU'])){
322         $cfg= &$this->config->data['PATHMENU'];
323         $rcfg = array_reverse($cfg);
324         foreach($rcfg as $id => $plug){
325           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
326           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
327         }
328       }
329       $this->pathMenu.= "\n          </ul>";
330       $this->pathMenu.= "\n        </div>";
331     } 
332     return($this->pathMenu); 
333   }
334   
336   /*! \brief    Returns additional info for a given class name, like 
337    *             plugin-icon, title, description and the index of the element 
338                  in the pluglist which uses this class.
339    */
340   function getPlugData($class)
341   {
342     global $class_mapping;
343     $vars= get_class_vars($class);
344     $plHeadline= _($vars['plHeadline']);
345     $plDescription= _($vars['plDescription']);
346     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
347     $index= $this->get_index($class);
349     /* Check if class is available. If the class doesn't exists display error symbol
350      *  to avoid that a user clicks on a non existing plugin
351      */
352     if(!$vars){
353       $plHeadline = $plDescription = _("Unknown");
354       $plIcon = "error.png";
355       $index = '';
356     } 
358     // Detect the correct position of the plugin icon
359     if(!preg_match("/\//",$plIcon)){
360       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
361             $class_mapping[$class])."/images/$plIcon");
362     }else{
363       $image = $plIcon; 
364     }
365     return(array($index, $plHeadline, $plDescription, $image));
366   }
369   /*! \brief    Returns the installation path of a plugin.
370    *            e.g. '../plugins/admin/mimetypes'
371    */
372   function get_path($index)
373   {
374     if(!isset($this->dirlist[$index])){
375       return ("");
376     }
377     return ("../".$this->dirlist[$index]);
378   }
381   /*! \brief    Returns the plugins id for a given class.
382    */
383   function get_index($class)
384   {
385     return (array_search($class, $this->pluginList));
386   }
389   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
390    *
391    *  @param  $plug_id  Integer  The ID of the plugin.
392    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
393    */
394   function plugin_access_allowed($plug_id)
395   {
396     return(isset($this->pluginList[$plug_id]));
397   }
400   /*! \brief  Force the menu to be recreated 
401    */
402   function reset_menus()
403   {
404     $this->menu = "";
405     $this->iconmenu ="";
406   }
409   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
410    *            This is just used in the helpviewer.php  
411    */
412   function gen_headlines()
413   {
414     $ret = array();
415     if(count($this->headlines) == 0){
416       foreach($this->config->data['MENU'] as $headline => $plugins){
417         foreach( $plugins as $id => $plug){
418           if (plugin_available($plug['CLASS'])){
419             $attrs = (get_class_vars($plug['CLASS']));
420             $ret[$id]['HEADLINE'] = $headline;
421             $ret[$id]['NAME']     = $attrs['plHeadline'];
422           }
423         }
424       }
425       $this->headlines = $ret;
426     }
427     return($this->headlines);
428   }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>