Code

2e166125915b6842bbb6238be84e439638955f47
[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     
61     $this->gen_menu();
62   }
65   /*! \brief    Detect plugin installation paths, by walking through the config. 
66    */
67   function get_plugins($list, &$config)
68   {
69     global $class_mapping;
70     if (!isset($config['CLASS'])){
71       if (is_array($config)){
72         foreach ($config as $val){
73           $list= $this->get_plugins($list, $val);
74         }
75       }
76     } else {
77       if (is_array($config) && isset($class_mapping[$config['CLASS']])){
78         $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
79       } else {
80         $list[$this->index++]= "";
81       }
82     }
84     return ($list);
85   }
88   /*! \brief  Check whether we are allowed to modify the given acl or not..
89    *            This function is used to check which plugins are visible.
90    *            
91    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
92    *  @return   Boolean TRUE on success else FALSE
93    */
94   function check_access($aclname)
95   {
96     if (isset($this->silly_cache[$aclname])) {
97       return $this->silly_cache[$aclname];
98     }
100     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
101     $acls_to_check = array();
102     if(preg_match("/,/",$aclname)){
103       $acls_to_check = explode(",",$aclname);
104     }else{
105       $acls_to_check = array($aclname);
106     }
108     foreach($acls_to_check as $acl_to_check){
109       $acl_to_check = trim($acl_to_check);
111       /* Check if the given acl tag is only valid for self acl entries  
112        *          <plugin acl="users/user:self" class="user"...  
113        */       
114       if(preg_match("/:self$/",$acl_to_check)){
115         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
116         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
117           $this->silly_cache[$aclname]= TRUE;
118           return(TRUE);
119         }
120         $this->silly_cache[$aclname]= FALSE;
121         return(FALSE);
122       }else{
124         // No self acls. Check if we have any acls for the given ACL type 
125         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
126         if(count($deps)){
127           $this->silly_cache[$aclname]= TRUE;
128           return TRUE;
129         }
130       }
131     }
133     $this->silly_cache[$aclname]= FALSE;
134     return (FALSE);
135   }
138   /*! \brief    Check the accessibility of the configured plugins.
139    *            We may simply have now permissions to access some plugins 
140    *             but some may be broken or missing!.
141    */
142   function checkMenu()
143   {
144     $cfg= &$this->config->data['MENU'];
145     foreach ($cfg as $headline => $plug){
146       $this->menuparts[_($headline)]= array();
147       foreach ($plug as $id => $info){
148         if (!isset($info['CLASS'])){
149           msg_dialog::display(
150               _("Configuration error"), 
151               _("The configuration format has changed. Please re-run setup!"), 
152               FATAL_ERROR_DIALOG);
153           exit();
154         }
155         if (!plugin_available($info['CLASS'])){
156           unset($cfg[$headline][$id]); 
157           continue; 
158         }
159         if (!$this->check_access($info['ACL'])){
160           unset($cfg[$headline][$id]);
161           continue; 
162         }
163       }
164     }
165     if(!session::is_set('maxC')){
166       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
167     }
168   }
171   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
172    *             this usually only done once during login.
173    *            -----------------------------------------------------------------
174    *            Do NOT add style changes here manually, use the style.css or 
175    *             if you prefer create your own theme!!
176    *            -----------------------------------------------------------------
177    */
178   function gen_menu()
179   {
180     if ($this->menu == ""){
181       $this->checkMenu();
182       $cfg= $this->config->data['MENU'];
183       $menu = "\n<div class='navigation'>";
184       foreach ($cfg as $headline => $plug){
186         if(!count($plug)) continue;
187         $menu.= "\n<div class='menu'>";
188         $menu.= "\n <ul>";
189         $menu.= "\n  <li class='menu-header'>"._($headline)."</li>";
190         $id = 0;
191         foreach ($plug as $info){
192           $id ++;
193           $vars= get_class_vars($info['CLASS']);
194           $plHeadline = _((isset($info['NAME'])) ? $info['NAME'] : $vars['plHeadline']);
195           $plDescription= $vars['plDescription'];
196           $index= $this->get_index($info['CLASS']);
197           $href= "main.php?plug=$index&amp;reset=1";
198           if(!$vars){
199             $plHeadline         = _("Unknown");
200             $plDescription      = _("Unknown");
201             $href= "main.php?reset=1";
202           }
203           $this->allowed_plugins[$index] = $index;
204           $class= "";
205           if($id == count($plug)) $class=" class='menu-last' \n   ";
206           $menu .=  "\n  <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$plHeadline."</li>";
207         }
208         $menu.= "\n </ul>";
209         $menu.= "\n <div></div>\n";
210         $menu.= "\n</div>\n";
211         $menu.= "\n<div></div>\n";
212         $menu.= "\n<div class='v-spacer'></div>\n";
213       }
214       $menu.= "\n</div>";
215       $this->menu = $menu;
217       // Add javascript method to print out warning messages while leaving an unsaved form.
218       // We do it in here to get the string translated.
219       $this->menu .= "
220         <script language='javascript' type='text/javascript'>
221         function openPlugin(id){
222           return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",
223               \"main.php?plug=\" + id + \"&amp;reset=1\");
224         }
225       </script>
226         "; 
227     }
228   
229     // Use javascript to mark the currently selected plugin.
230     $menu = $this->menu;
231     if(isset($_GET['plug'])){
232       $menu.= "
233         <script language='javascript' type='text/javascript'>
234           if($('plugMenuId_".$_GET['plug']."')){
235             $('plugMenuId_".$_GET['plug']."').className= 'current'
236           }
237         </script>
238         "; 
239     }
241     // Return the generated/cached gosa menu.
242     return ($menu);
243   }
246   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
247    *             login.
248    *            -----------------------------------------------------------------
249    *            Do NOT add style changes here manually, use the style.css or 
250    *             if you prefer create your own theme!!
251    *            -----------------------------------------------------------------
252    */
253   function show_iconmenu()
254   {
255     global $class_mapping;
257     $add_hr =FALSE;
258     if ($this->iconmenu == ""){
259       $cfg= $this->config->data['MENU'];
260       foreach ($cfg as $headline => $plug){
261         $col= 0;
263         $this->iconmenu .= "\n  <div class='clear'></div>\n";
264         if($add_hr){
265           $add_hr = FALSE;
266           $this->iconmenu .= "\n  <hr>\n";
267         }
268         $this->iconmenu .= "\n  <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
270         foreach ($plug as $info){
272           // Get Plugin info
273           $vars= get_class_vars($info['CLASS']);
274           $plHeadline= _($vars['plHeadline']);
275           $plDescription= _($vars['plDescription']);
276           $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
277           $index= $this->get_index($info['CLASS']);
279           /* Check if class is available. If the class doesn't exists display error symbol 
280            *  to avoid that a user clicks on a non existing plugin  
281            */
282           if(!$vars){
283             $plHeadline = $plDescription = _("Unknown");
284             $info['ICON'] = "error.png";
285             $index = '';
286           }
288           // Detect the correct position of the plugin icon
289           if(!preg_match("/\//",$plIcon)){
290             $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
291                   $class_mapping[$info['CLASS']])."/images/$plIcon");
292           }else{
293             $image = $plIcon; 
294           }
296           // Add a seperating row
297           if (($col % 4) == 0){ 
298             $this->iconmenu .= "\n  <div class='clear'></div>\n";
299           }
301           $this->iconmenu.= "\n  <div class='container-element' style='width: 25%;' onclick='openPlugin({$index})'> ";
302           $this->iconmenu.= "\n   <div class='icon-menu-item' style='background-image: url({$image});'>";
303           $this->iconmenu.= "\n    <h1>{$plHeadline}</h1>";
304           $this->iconmenu.= "\n    <p>{$plDescription}</p>";
305           $this->iconmenu.= "\n   </div>";
306           $this->iconmenu.= "\n  </div>\n";
307           $col++ ;
308         }
309         $add_hr = TRUE;
310       }
311     }
312     return ($this->iconmenu);
313   }
316   /*! \brief    Returns the installation path of a plugin.
317    *            e.g. '../plugins/admin/mimetypes'
318    */
319   function get_path($index)
320   {
321     if(!isset($this->dirlist[$index])){
322       return ("");
323     }
324     return ("../".$this->dirlist[$index]);
325   }
328   /*! \brief    Returns the plugins id for a given class.
329    */
330   function get_index($class)
331   {
332     /* Search for plugin index (id), identify entry by path && class */
333     $data = $this->config->data['MENU'];
334     foreach($data as $section => $plugins){
335       foreach($plugins as $key => $plugin)    {
336         if($plugin['CLASS'] == $class){
337           return($key);
338         }
339       }
340     }
341     return (0);
342   }
345   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
346    *
347    *  @param  $plug_id  Integer  The ID of the plugin.
348    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
349    */
350   function plugin_access_allowed($plug_id)
351   {
352     return(isset($this->allowed_plugins[$plug_id]));
353   }
356   /*! \brief  Force the menu to be recreated 
357    */
358   function reset_menus()
359   {
360     $this->menu = "";
361     $this->iconmenu ="";
362   }
365   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
366    *            This is just used in the helpviewer.php  
367    */
368   function gen_headlines()
369   {
370     $ret = array();
371     if(count($this->headlines) == 0){
372       foreach($this->config->data['MENU'] as $headline => $plugins){
373         foreach( $plugins as $id => $plug){
374           if (plugin_available($plug['CLASS'])){
375             $attrs = (get_class_vars($plug['CLASS']));
376             $ret[$id]['HEADLINE'] = $headline;
377             $ret[$id]['NAME']     = $attrs['plHeadline'];
378           }
379         }
380       }
381       $this->headlines = $ret;
382     }
383     return($this->headlines);
384   }
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>