Code

81f9a8d9f5894775d52d9acbc907d0fb22ed32e9
[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']);
44     $this->dirlist= array_merge($this->dirlist,$this->get_plugins ($this->dirlist, $this->config->data['MENU']));
46     // Detect installed plugins and their configuration, to be able to restrict access later.
47     $classes= get_declared_classes();
48     foreach ($classes as $cname){
49       $cmethods = get_class_methods($cname);
50       if (in_array_ics('plInfo',$cmethods)){
51         $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
52       }
53     }
55     // Reserve a special ACL will allows us to display plugins/addons whenever a user 
56     //  is able to login into gosa. E.g. some kind of welcome page. 
57     $this->info['all']= array();
58     $this->info['all']['plProvidedAcls']= array();
59     $this->info['all']['plDescription']= _("All objects in this category");
60     $this->info['all']['plSelfModify']= FALSE;
61     
62     $this->gen_menu();
63   }
66   /*! \brief    Detect plugin installation paths, by walking through the config. 
67    */
68   function get_plugins($list, &$config)
69   {
70     global $class_mapping;
71     if (!isset($config['CLASS'])){
72       if (is_array($config)){
73         foreach ($config as $val){
74           $list= $this->get_plugins($list, $val);
75         }
76       }
77     } else {
78       if (is_array($config) && isset($class_mapping[$config['CLASS']])){
79         $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
80       } else {
81         $list[$this->index++]= "";
82       }
83     }
85     return ($list);
86   }
89   /*! \brief  Check whether we are allowed to modify the given acl or not..
90    *            This function is used to check which plugins are visible.
91    *            
92    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
93    *  @return   Boolean TRUE on success else FALSE
94    */
95   function check_access($aclname)
96   {
97     if (isset($this->silly_cache[$aclname])) {
98       return $this->silly_cache[$aclname];
99     }
101     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
102     $acls_to_check = array();
103     if(preg_match("/,/",$aclname)){
104       $acls_to_check = explode(",",$aclname);
105     }else{
106       $acls_to_check = array($aclname);
107     }
109     foreach($acls_to_check as $acl_to_check){
110       $acl_to_check = trim($acl_to_check);
112       /* Check if the given acl tag is only valid for self acl entries  
113        *          <plugin acl="users/user:self" class="user"...  
114        */       
115       if(preg_match("/:self$/",$acl_to_check)){
116         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
117         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
118           $this->silly_cache[$aclname]= TRUE;
119           return(TRUE);
120         }
121         $this->silly_cache[$aclname]= FALSE;
122         return(FALSE);
123       }else{
125         // No self acls. Check if we have any acls for the given ACL type 
126         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
127         if(count($deps)){
128           $this->silly_cache[$aclname]= TRUE;
129           return TRUE;
130         }
131       }
132     }
134     $this->silly_cache[$aclname]= FALSE;
135     return (FALSE);
136   }
139   /*! \brief    Check the accessibility of the configured plugins.
140    *            We may simply have now permissions to access some plugins 
141    *             but some may be broken or missing!.
142    */
143   function checkMenu()
144   {
145     $cfg= &$this->config->data['MENU'];
146     foreach ($cfg as $headline => $plug){
147       $this->menuparts[_($headline)]= array();
148       foreach ($plug as $id => $info){
149         if (!isset($info['CLASS'])){
150           msg_dialog::display(
151               _("Configuration error"), 
152               _("The configuration format has changed. Please re-run setup!"), 
153               FATAL_ERROR_DIALOG);
154           exit();
155         }
156         if (!plugin_available($info['CLASS'])){
157           unset($cfg[$headline][$id]); 
158           continue; 
159         }
160         if (!$this->check_access($info['ACL'])){
161           unset($cfg[$headline][$id]);
162           continue; 
163         }
164       }
165     }
166     if(!session::is_set('maxC')){
167       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
168     }
169   }
172   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
173    *             this usually only done once during login.
174    *            -----------------------------------------------------------------
175    *            Do NOT add style changes here manually, use the style.css or 
176    *             if you prefer create your own theme!!
177    *            -----------------------------------------------------------------
178    */
179   function gen_menu()
180   {
181     if ($this->menu == ""){
182       $this->checkMenu();
183       $cfg= $this->config->data['MENU'];
184       $menu = "\n<div class='navigation'>";
185       foreach ($cfg as $headline => $plug){
187         if(!count($plug)) continue;
188         $menu.= "\n<div class='menu'>";
189         $menu.= "\n <ul>";
190         $menu.= "\n  <li class='menu-header'>"._($headline)."</li>";
191         $id = 0;
192         foreach ($plug as $info){
193           $id ++;
194           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
195           $this->allowed_plugins[$index] = $index;
196           $class= "";
197           if($id == count($plug)) $class=" class='menu-last' \n   ";
198           $menu .=  "\n  <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
199         }
200         $menu.= "\n </ul>";
201         $menu.= "\n <div></div>\n";
202         $menu.= "\n</div>\n";
203         $menu.= "\n<div></div>\n";
204         $menu.= "\n<div class='v-spacer'></div>\n";
205       }
206       $menu.= "\n</div>";
207       $this->menu = $menu;
209       // Add javascript method to print out warning messages while leaving an unsaved form.
210       // We do it in here to get the string translated.
211       $this->menu .= "
212         <script language='javascript' type='text/javascript'>
213         function openPlugin(id){
214           return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
215               \"main.php?plug=\" + id + \"&amp;reset=1\");
216         }
217       </script>
218         "; 
219     }
220   
221     // Use javascript to mark the currently selected plugin.
222     $menu = $this->menu;
223     if(isset($_GET['plug'])){
224       $menu.= "
225         <script language='javascript' type='text/javascript'>
226           if($('plugMenuId_".$_GET['plug']."')){
227             $('plugMenuId_".$_GET['plug']."').className= 'current'
228           }
229         </script>
230         "; 
231     }
233     // Return the generated/cached gosa menu.
234     return ($menu);
235   }
238   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
239    *             login.
240    *            -----------------------------------------------------------------
241    *            Do NOT add style changes here manually, use the style.css or 
242    *             if you prefer create your own theme!!
243    *            -----------------------------------------------------------------
244    */
245   function show_iconmenu()
246   {
247     $add_hr =FALSE;
248     if ($this->iconmenu == ""){
249       $cfg= $this->config->data['MENU'];
250       foreach ($cfg as $headline => $plug){
251         $col= 0;
253         $this->iconmenu .= "\n  <div class='clear'></div>\n";
254         if($add_hr){
255           $add_hr = FALSE;
256           $this->iconmenu .= "\n  <hr>\n";
257         }
258         $this->iconmenu .= "\n  <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
260         foreach ($plug as $info){
262           // Get Plugin info
263           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
265           // Add a seperating row
266           if (($col % 4) == 0){ 
267             $this->iconmenu .= "\n  <div class='clear'></div>\n";
268           }
270           $this->iconmenu.= "\n  <div class='container-element' style='width: 25%;' onclick='openPlugin({$index})'> ";
271           $this->iconmenu.= "\n   <div class='icon-menu-item' style='background-image: url({$icon});'>";
272           $this->iconmenu.= "\n    <h1>{$title}</h1>";
273           $this->iconmenu.= "\n    <p>{$desc}</p>";
274           $this->iconmenu.= "\n   </div>";
275           $this->iconmenu.= "\n  </div>\n";
276           $col++ ;
277         }
278         $add_hr = TRUE;
279       }
280     }
281     return ($this->iconmenu);
282   }
285   function genPathMenu()
286   {
287     if(1 | empty($this->pathMenu)){
289       $this->pathMenu = 
290         "\n    <div class='plugin-path'>".
291         "\n     <ul class='path-navigation'>".
292         "\n      <li class='left right-border' onClick=\"openPlugin('');\">".
293         "\n       <div class='nav-home'></div>".
294         "\n      </li>".
295         "\n      <li class='left'>Welcome to GOsa</li>";
297       if(isset($this->config->data['PATHMENU'])){
298         $cfg= &$this->config->data['PATHMENU'];
299         $rcfg = array_reverse($cfg);
300         foreach($rcfg as $id => $plug){
302           // Do not add inaccessible plugins
303           if (!$this->check_access($plug['ACL'])){
304             unset($rcfg[$id]);
305             continue; 
306           }
308           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
309           $this->allowed_plugins[$index] = $index;
310           $this->pathMenu.= "\n     <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
311         }
312       }
313       $this->pathMenu.= "\n    </ul>";
314       $this->pathMenu.= "\n    </div>";
315     } 
316     return($this->pathMenu); 
317   }
318   
320   function getPlugData($class)
321   {
322     global $class_mapping;
323     $vars= get_class_vars($class);
324     $plHeadline= _($vars['plHeadline']);
325     $plDescription= _($vars['plDescription']);
326     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
327     $index= $this->get_index($class);
329     /* Check if class is available. If the class doesn't exists display error symbol
330      *  to avoid that a user clicks on a non existing plugin
331      */
332     if(!$vars){
333       $plHeadline = $plDescription = _("Unknown");
334       $plIcon = "error.png";
335       $index = '';
336     } 
338     // Detect the correct position of the plugin icon
339     if(!preg_match("/\//",$plIcon)){
340       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
341             $class_mapping[$class])."/images/$plIcon");
342     }else{
343       $image = $plIcon; 
344     }
345     return(array($index, $plHeadline, $plDescription, $image));
346   }
349   /*! \brief    Returns the installation path of a plugin.
350    *            e.g. '../plugins/admin/mimetypes'
351    */
352   function get_path($index)
353   {
354     if(!isset($this->dirlist[$index])){
355       return ("");
356     }
357     return ("../".$this->dirlist[$index]);
358   }
361   /*! \brief    Returns the plugins id for a given class.
362    */
363   function get_index($class)
364   {
365     /* Search for plugin index (id), identify entry by path && class */
366     $data = $this->config->data['MENU'];
367     foreach($data as $section => $plugins){
368       foreach($plugins as $key => $plugin)    {
369         if($plugin['CLASS'] == $class){
370           return($key);
371         }
372       }
373     }
374     return (0);
375   }
378   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
379    *
380    *  @param  $plug_id  Integer  The ID of the plugin.
381    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
382    */
383   function plugin_access_allowed($plug_id)
384   {
385     return(isset($this->allowed_plugins[$plug_id]));
386   }
389   /*! \brief  Force the menu to be recreated 
390    */
391   function reset_menus()
392   {
393     $this->menu = "";
394     $this->iconmenu ="";
395   }
398   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
399    *            This is just used in the helpviewer.php  
400    */
401   function gen_headlines()
402   {
403     $ret = array();
404     if(count($this->headlines) == 0){
405       foreach($this->config->data['MENU'] as $headline => $plugins){
406         foreach( $plugins as $id => $plug){
407           if (plugin_available($plug['CLASS'])){
408             $attrs = (get_class_vars($plug['CLASS']));
409             $ret[$id]['HEADLINE'] = $headline;
410             $ret[$id]['NAME']     = $attrs['plHeadline'];
411           }
412         }
413       }
414       $this->headlines = $ret;
415     }
416     return($this->headlines);
417   }
419 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
420 ?>