Code

Updated framework to mark currently selected plugin
[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);
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{
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    Check the accessibility of the configured plugins.
137    *            We may simply have now permissions to access some plugins 
138    *             but some may be broken or missing!.
139    */
140   function checkMenu()
141   {
142     $cfg= &$this->config->data['MENU'];
143     foreach ($cfg as $headline => $plug){
144       $this->menuparts[_($headline)]= array();
145       foreach ($plug as $id => $info){
146         if (!isset($info['CLASS'])){
147           msg_dialog::display(
148               _("Configuration error"), 
149               _("The configuration format has changed. Please re-run setup!"), 
150               FATAL_ERROR_DIALOG);
151           exit();
152         }
153         if (!plugin_available($info['CLASS'])){
154           unset($cfg[$headline][$id]); 
155           continue; 
156         }
157         if (!$this->check_access($info['ACL'])){
158           unset($cfg[$headline][$id]);
159           continue; 
160         }
161       }
162     }
163     if(!session::is_set('maxC')){
164       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
165     }
166   }
169   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
170    *             this usually only done once during login.
171    *            -----------------------------------------------------------------
172    *            Do NOT add style changes here manually, use the style.css or 
173    *             if you prefer create your own theme!!
174    *            -----------------------------------------------------------------
175    */
176   function gen_menu()
177   {
178     if ($this->menu == ""){
179       $this->checkMenu();
180       $cfg= $this->config->data['MENU'];
181       $menu = "\n<div class='navigation'>";
182       foreach ($cfg as $headline => $plug){
184         if(!count($plug)) continue;
185         $menu.= "\n<div class='menu'>";
186         $menu.= "\n <ul>";
187         $menu.= "\n  <li class='menu-header'>"._($headline)."</li>";
188         $id = 0;
189         foreach ($plug as $info){
190           $id ++;
191           $vars= get_class_vars($info['CLASS']);
192           $plHeadline = _((isset($info['NAME'])) ? $info['NAME'] : $vars['plHeadline']);
193           $plDescription= $vars['plDescription'];
194           $index= $this->get_index($info['CLASS']);
195           $href= "main.php?plug=$index&amp;reset=1";
196           if(!$vars){
197             $plHeadline         = _("Unknown");
198             $plDescription      = _("Unknown");
199             $href= "main.php?reset=1";
200           }
201           $this->allowed_plugins[$index] = $index;
202           $class= "";
203           if($id == count($plug)) $class=" class='menu-last' \n   ";
204           $menu .=  "\n  <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$plHeadline."</li>";
205         }
206         $menu.= "\n </ul>";
207         $menu.= "\n <div></div>\n";
208         $menu.= "\n</div>\n";
209         $menu.= "\n<div></div>\n";
210         $menu.= "\n<div class='v-spacer'></div>\n";
211       }
212       $menu.= "\n</div>";
213       $this->menu = $menu;
215       // Add javascript method to print out warning messages while leaving an unsaved form.
216       // We do it in here to get the string translated.
217       $this->menu .= "
218         <script language='javascript' type='text/javascript'>
219         function openPlugin(id){
220           return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
221               \"main.php?plug=\" + id + \"&amp;reset=1\");
222         }
223       </script>
224         "; 
225     }
226   
227     // Use javascript to mark the currently selected plugin.
228     if(isset($_GET['plug'])){
229       $menu = $this->menu."
230         <script language='javascript' type='text/javascript'>
231           $('plugMenuId_".$_GET['plug']."').className= 'current'
232         </script>
233         "; 
234     }
236     // Return the generated/cached gosa menu.
237     return ($menu);
238   }
241   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
242    *             login.
243    *            -----------------------------------------------------------------
244    *            Do NOT add style changes here manually, use the style.css or 
245    *             if you prefer create your own theme!!
246    *            -----------------------------------------------------------------
247    */
248   function show_iconmenu()
249   {
250     global $class_mapping;
252     $add_hr =FALSE;
253     if ($this->iconmenu == ""){
254       $cfg= $this->config->data['MENU'];
255       foreach ($cfg as $headline => $plug){
256         $col= 0;
258         $this->iconmenu .= "\n  <div class='clear'></div>\n";
259         if($add_hr){
260           $add_hr = FALSE;
261           $this->iconmenu .= "\n  <hr>\n";
262         }
263         $this->iconmenu .= "\n  <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
265         foreach ($plug as $info){
267           // Get Plugin info
268           $vars= get_class_vars($info['CLASS']);
269           $plHeadline= _($vars['plHeadline']);
270           $plDescription= _($vars['plDescription']);
271           $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
272           $index= $this->get_index($info['CLASS']);
274           /* Check if class is available. If the class doesn't exists display error symbol 
275            *  to avoid that a user clicks on a non existing plugin  
276            */
277           if(!$vars){
278             $plHeadline = $plDescription = _("Unknown");
279             $info['ICON'] = "error.png";
280             $index = '';
281           }
283           // Detect the correct position of the plugin icon
284           if(!preg_match("/\//",$plIcon)){
285             $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
286                   $class_mapping[$info['CLASS']])."/images/$plIcon");
287           }else{
288             $image = $plIcon; 
289           }
291           // Add a seperating row
292           if (($col % 4) == 0){ 
293             $this->iconmenu .= "\n  <div class='clear'></div>\n";
294           }
296           $this->iconmenu.= "\n  <div class='container-element' style='width: 25%;' onclick='openPlugin({$index})'> ";
297           $this->iconmenu.= "\n   <div class='icon-menu-item' style='background-image: url({$image});'>";
298           $this->iconmenu.= "\n    <h1>{$plHeadline}</h1>";
299           $this->iconmenu.= "\n    <p>{$plDescription}</p>";
300           $this->iconmenu.= "\n   </div>";
301           $this->iconmenu.= "\n  </div>\n";
302           $col++ ;
303         }
304         $add_hr = TRUE;
305       }
306     }
307     return ($this->iconmenu);
308   }
311   /*! \brief    Returns the installation path of a plugin.
312    *            e.g. '../plugins/admin/mimetypes'
313    */
314   function get_path($index)
315   {
316     if(!isset($this->dirlist[$index])){
317       return ("");
318     }
319     return ("../".$this->dirlist[$index]);
320   }
323   /*! \brief    Returns the plugins id for a given class.
324    */
325   function get_index($class)
326   {
327     /* Search for plugin index (id), identify entry by path && class */
328     $data = $this->config->data['MENU'];
329     foreach($data as $section => $plugins){
330       foreach($plugins as $key => $plugin)    {
331         if($plugin['CLASS'] == $class){
332           return($key);
333         }
334       }
335     }
336     return (0);
337   }
340   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
341    *
342    *  @param  $plug_id  Integer  The ID of the plugin.
343    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
344    */
345   function plugin_access_allowed($plug_id)
346   {
347     return(isset($this->allowed_plugins[$plug_id]));
348   }
351   /*! \brief  Force the menu to be recreated 
352    */
353   function reset_menus()
354   {
355     $this->menu = "";
356     $this->iconmenu ="";
357   }
360   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
361    *            This is just used in the helpviewer.php  
362    */
363   function gen_headlines()
364   {
365     $ret = array();
366     if(count($this->headlines) == 0){
367       foreach($this->config->data['MENU'] as $headline => $plugins){
368         foreach( $plugins as $id => $plug){
369           if (plugin_available($plug['CLASS'])){
370             $attrs = (get_class_vars($plug['CLASS']));
371             $ret[$id]['HEADLINE'] = $headline;
372             $ret[$id]['NAME']     = $attrs['plHeadline'];
373           }
374         }
375       }
376       $this->headlines = $ret;
377     }
378     return($this->headlines);
379   }
381 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
382 ?>