Code

Comments again
[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();
37   // Remember stuff in here.
38   var $silly_cache= array();
40   // The pluglist keeps track of all registered plugins.
41   // (Registered plugins are those we are able to access)
42   var $pluginList = array();
44   // Some cache vars to avoid regenration of the menus.   
45   var $pathMenu = "";
46   var $menu= "";
47   var $iconmenu= "";
49   function pluglist(&$config, &$ui)
50   {
51     $this->ui= &$ui;
52     $this->config= &$config;
53     $this->loadPluginList();
54   }
56   function loadPluginList()
57   {
58     $this->pluginList = array();
60     // First load the menu plugins and try to register them in the pluglist
61     //  if this fails for some reason, then remove the plugin from the menu.
62     if(isset($this->config->data['MENU'])){
63       foreach($this->config->data['MENU'] as $section => $plugins){
64         foreach($plugins as $id => $plug){
65           if(!$this->registerPlugin($plug)){ 
66             unset($this->config->data['MENU'][$section][$id]); 
67           }
68         }
69       }
70     }
71     
72     // Now load the path menu and try to register the plugins in the pluglist
73     //  if this fails for some reason, then remove the plugin from the menu.
74     if(isset($this->config->data['PATHMENU'])){
75       foreach($this->config->data['PATHMENU'] as $id => $plugin){
76         if(!$this->registerPlugin($plugin)){
77           unset($this->config->data['PATHMENU'][$id]); 
78         } 
79       }
80     }
82     if(!session::is_set('maxC')){
83       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
84     }
86     // Now generate menu - usually they are cached
87     $this->gen_menu();
88     $this->show_iconmenu();
89     $this->genPathMenu();
90   }
93   /*! \brief    Tries to register a plugin in the pluglist
94    *            Checks existence and ACL for the given plugin.
95    *            Returns true in case of success else false. 
96    */
97   function registerPlugin($plug)
98   {
99     global $class_mapping;
101     if (!isset($plug['CLASS'])){
102       msg_dialog::display(
103           _("Configuration error"),
104           _("The configuration format has changed. Please re-run setup!"),
105           FATAL_ERROR_DIALOG);
106       exit();
107     }
108     if (!plugin_available($plug['CLASS'])){
109       return(FALSE);
110     }
111     if (!$this->check_access($plug['ACL'])){
112       return(FALSE);
113     }
114     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
115     $this->pluginList[$this->index] = $plug['CLASS'];
116     $this->index++;
117     return(TRUE);
118   }
121   /*! \brief  Check whether we are allowed to modify the given acl or not..
122    *            This function is used to check which plugins are visible.
123    *            
124    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
125    *  @return   Boolean TRUE on success else FALSE
126    */
127   function check_access($aclname)
128   {
129     if (isset($this->silly_cache[$aclname])) {
130       return $this->silly_cache[$aclname];
131     }
133     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
134     $acls_to_check = array();
135     if(preg_match("/,/",$aclname)){
136       $acls_to_check = explode(",",$aclname);
137     }else{
138       $acls_to_check = array($aclname);
139     }
141     foreach($acls_to_check as $acl_to_check){
142       $acl_to_check = trim($acl_to_check);
144       /* Check if the given acl tag is only valid for self acl entries  
145        *          <plugin acl="users/user:self" class="user"...  
146        */       
147       if(preg_match("/:self$/",$acl_to_check)){
148         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
149         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
150           $this->silly_cache[$aclname]= TRUE;
151           return(TRUE);
152         }
153         $this->silly_cache[$aclname]= FALSE;
154         return(FALSE);
155       }else{
157         // No self acls. Check if we have any acls for the given ACL type 
158         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
159         if(count($deps)){
160           $this->silly_cache[$aclname]= TRUE;
161           return TRUE;
162         }
163       }
164     }
166     $this->silly_cache[$aclname]= FALSE;
167     return (FALSE);
168   }
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       $cfg= $this->config->data['MENU'];
183       $menu = "\n      <div class='navigation'>";
184       foreach ($cfg as $headline => $plug){
186         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){
194           // Used to detect the last element in the menu
195           $id ++;
197           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
198           $class= "";
199           if($id == count($plug)) $class=" class='menu-last' ";
200           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
201         }
202         $menu.= "\n          </ul>";
203         $menu.= "\n          <div></div>";
204         $menu.= "\n        </div>\n";
205         $menu.= "\n        <div></div>";
206         $menu.= "\n        <div class='v-spacer'></div>\n";
207       }
208       $menu.= "\n      </div>\n";
209       $this->menu = $menu;
211       // Add javascript method to print out warning messages while leaving an unsaved form.
212       // We do it in here to get the string translated.
213       $this->menu .=  
214         "\n      <script language='javascript' type='text/javascript'>".
215         "\n        function openPlugin(id){".
216         "\n          return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
217         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
218         "\n        }".
219         "\n      </script>\n"; 
220     }
221   
222     // Use javascript to mark the currently selected plugin.
223     $menu = $this->menu;
224     if(isset($_GET['plug'])){
225       $menu.= 
226      "\n      <script language='javascript' type='text/javascript'>".
227      "\n        if($('plugMenuId_".$_GET['plug']."')){".
228      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
229      "\n        }".
230      "\n      </script>\n";
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     $this->iconmenu = "";
249     if ($this->iconmenu == ""){
250       $cfg= $this->config->data['MENU'];
251       foreach ($cfg as $headline => $plug){
252         $col= 0;
254         $this->iconmenu .= "\n        <div class='clear'></div>";
255         if($add_hr){
256           $add_hr = FALSE;
257           $this->iconmenu .= "\n        <hr>";
258         }
259         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
261         foreach ($plug as $info){
263           // Get Plugin info
264           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
266           // Add a seperating row
267           if (($col % 4) == 0){ 
268             $this->iconmenu .= "\n        <div class='clear'></div>";
269           }
271           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
272           $this->iconmenu.= "\n          ".image($icon);
273           $this->iconmenu.= "\n          <div class='dsc'>";
274           $this->iconmenu.= "\n            <h1>{$title}</h1>";
275           $this->iconmenu.= "\n            <p>{$desc}</p>";
276           $this->iconmenu.= "\n          </div>";
277           $this->iconmenu.= "\n        </div>";
278           $col++ ;
279         }
280         $add_hr = TRUE;
281       }
282     }
283     return ($this->iconmenu);
284   }
287   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
288    *             the generated HTML content, so we are not forced to generate it on every 
289    *             page request.
290    *            (See <pathMenu> of your gosa.conf)
291    */
292   function genPathMenu()
293   {
294     if(empty($this->pathMenu)){
295       $this->pathMenu = 
296         "\n        <div class='plugin-path'>".
297         "\n          <ul class='path-navigation'>".
298         "\n            <li class='left right-border' onClick=\"openPlugin('');\">".
299         "\n              <div class='nav-home'></div>".
300         "\n            </li>".
301         "\n            <li class='left'>Welcome to GOsa</li>";
303       // Check if we've at least one entry defined ih the pathmenu
304       if(isset($this->config->data['PATHMENU'])){
305         $cfg= &$this->config->data['PATHMENU'];
306         $rcfg = array_reverse($cfg);
307         foreach($rcfg as $id => $plug){
308           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
309           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
310         }
311       }
312       $this->pathMenu.= "\n          </ul>";
313       $this->pathMenu.= "\n        </div>";
314     } 
315     return($this->pathMenu); 
316   }
317   
319   /*! \brief    Returns additional info for a given class name, like 
320    *             plugin-icon, title, description and the index of the element 
321                  in the pluglist which uses this class.
322    */
323   function getPlugData($class)
324   {
325     global $class_mapping;
326     $vars= get_class_vars($class);
327     $plHeadline= _($vars['plHeadline']);
328     $plDescription= _($vars['plDescription']);
329     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
330     $index= $this->get_index($class);
332     /* Check if class is available. If the class doesn't exists display error symbol
333      *  to avoid that a user clicks on a non existing plugin
334      */
335     if(!$vars){
336       $plHeadline = $plDescription = _("Unknown");
337       $plIcon = "error.png";
338       $index = '';
339     } 
341     // Detect the correct position of the plugin icon
342     if(!preg_match("/\//",$plIcon)){
343       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
344             $class_mapping[$class])."/images/$plIcon");
345     }else{
346       $image = $plIcon; 
347     }
348     return(array($index, $plHeadline, $plDescription, $image));
349   }
352   /*! \brief    Returns the installation path of a plugin.
353    *            e.g. '../plugins/admin/mimetypes'
354    */
355   function get_path($index)
356   {
357     if(!isset($this->dirlist[$index])){
358       return ("");
359     }
360     return ("../".$this->dirlist[$index]);
361   }
364   /*! \brief    Returns the plugins id for a given class.
365    */
366   function get_index($class)
367   {
368     return (array_search($class, $this->pluginList));
369   }
372   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
373    *
374    *  @param  $plug_id  Integer  The ID of the plugin.
375    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
376    */
377   function plugin_access_allowed($plug_id)
378   {
379     return(isset($this->pluginList[$plug_id]));
380   }
383   /*! \brief  Force the menu to be recreated 
384    */
385   function reset_menus()
386   {
387     $this->menu = "";
388     $this->iconmenu ="";
389   }
392   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
393    *            This is just used in the helpviewer.php  
394    */
395   function gen_headlines()
396   {
397     $ret = array();
398     if(count($this->headlines) == 0){
399       foreach($this->config->data['MENU'] as $headline => $plugins){
400         foreach( $plugins as $id => $plug){
401           if (plugin_available($plug['CLASS'])){
402             $attrs = (get_class_vars($plug['CLASS']));
403             $ret[$id]['HEADLINE'] = $headline;
404             $ret[$id]['NAME']     = $attrs['plHeadline'];
405           }
406         }
407       }
408       $this->headlines = $ret;
409     }
410     return($this->headlines);
411   }
413 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
414 ?>