Code

Added additional comments.
[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   var $index= 0;
26   var $config= NULL;
27   var $dirlist= array();
28   var $ui= NULL;
29   var $info= array();
30   var $headlines = array();
31   var $silly_cache= array();
33   var $pluginList = array();
35   var $pathMenu = "";
36   var $menu= "";
37   var $iconmenu= "";
39   function pluglist(&$config, &$ui)
40   {
41     $this->ui= &$ui;
42     $this->config= &$config;
43     $this->loadPluginList();
44   }
46   function loadPluginList()
47   {
48     $this->pluginList = array();
50     // First load the menu plugins and try to register them in the pluglist
51     //  if this fails for some reason, then remove the plugin from the menu.
52     if(isset($this->config->data['MENU'])){
53       foreach($this->config->data['MENU'] as $section => $plugins){
54         foreach($plugins as $id => $plug){
55           if(!$this->registerPlugin($plug)){ 
56             unset($this->config->data['MENU'][$section][$id]); 
57           }
58         }
59       }
60     }
61     
62     // Now load the path menu and try to register the plugins in the pluglist
63     //  if this fails for some reason, then remove the plugin from the menu.
64     if(isset($this->config->data['PATHMENU'])){
65       foreach($this->config->data['PATHMENU'] as $id => $plugin){
66         if(!$this->registerPlugin($plugin)){
67           unset($this->config->data['PATHMENU'][$id]); 
68         } 
69       }
70     }
72     if(!session::is_set('maxC')){
73       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
74     }
76     // Now generate menu - usually they are cached
77     $this->gen_menu();
78     $this->show_iconmenu();
79     $this->genPathMenu();
80   }
83   /*! \brief    Tries to register a plugin in the pluglist
84    *            Checks existence and ACL for the given plugin.
85    *            Returns true in case of success else false. 
86    */
87   function registerPlugin($plug)
88   {
89     global $class_mapping;
91     if (!isset($plug['CLASS'])){
92       msg_dialog::display(
93           _("Configuration error"),
94           _("The configuration format has changed. Please re-run setup!"),
95           FATAL_ERROR_DIALOG);
96       exit();
97     }
98     if (!plugin_available($plug['CLASS'])){
99       return(FALSE);
100     }
101     if (!$this->check_access($plug['ACL'])){
102       return(FALSE);
103     }
104     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
105     $this->pluginList[$this->index] = $plug['CLASS'];
106     $this->index++;
107     return(TRUE);
108   }
111   /*! \brief  Check whether we are allowed to modify the given acl or not..
112    *            This function is used to check which plugins are visible.
113    *            
114    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
115    *  @return   Boolean TRUE on success else FALSE
116    */
117   function check_access($aclname)
118   {
119     if (isset($this->silly_cache[$aclname])) {
120       return $this->silly_cache[$aclname];
121     }
123     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
124     $acls_to_check = array();
125     if(preg_match("/,/",$aclname)){
126       $acls_to_check = explode(",",$aclname);
127     }else{
128       $acls_to_check = array($aclname);
129     }
131     foreach($acls_to_check as $acl_to_check){
132       $acl_to_check = trim($acl_to_check);
134       /* Check if the given acl tag is only valid for self acl entries  
135        *          <plugin acl="users/user:self" class="user"...  
136        */       
137       if(preg_match("/:self$/",$acl_to_check)){
138         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
139         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
140           $this->silly_cache[$aclname]= TRUE;
141           return(TRUE);
142         }
143         $this->silly_cache[$aclname]= FALSE;
144         return(FALSE);
145       }else{
147         // No self acls. Check if we have any acls for the given ACL type 
148         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
149         if(count($deps)){
150           $this->silly_cache[$aclname]= TRUE;
151           return TRUE;
152         }
153       }
154     }
156     $this->silly_cache[$aclname]= FALSE;
157     return (FALSE);
158   }
162   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
163    *             this usually only done once during login.
164    *            -----------------------------------------------------------------
165    *            Do NOT add style changes here manually, use the style.css or 
166    *             if you prefer create your own theme!!
167    *            -----------------------------------------------------------------
168    */
169   function gen_menu()
170   {
171     if ($this->menu == ""){
172       $cfg= $this->config->data['MENU'];
173       $menu = "\n      <div class='navigation'>";
174       foreach ($cfg as $headline => $plug){
176         if(!count($plug)) continue;
178         $menu.= "\n        <div class='menu'>";
179         $menu.= "\n          <ul>";
180         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
181         $id = 0;
182         foreach ($plug as $info){
184           // Used to detect the last element in the menu
185           $id ++;
187           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
188           $class= "";
189           if($id == count($plug)) $class=" class='menu-last' ";
190           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
191         }
192         $menu.= "\n          </ul>";
193         $menu.= "\n          <div></div>";
194         $menu.= "\n        </div>\n";
195         $menu.= "\n        <div></div>";
196         $menu.= "\n        <div class='v-spacer'></div>\n";
197       }
198       $menu.= "\n      </div>\n";
199       $this->menu = $menu;
201       // Add javascript method to print out warning messages while leaving an unsaved form.
202       // We do it in here to get the string translated.
203       $this->menu .=  
204         "\n      <script language='javascript' type='text/javascript'>".
205         "\n        function openPlugin(id){".
206         "\n          return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
207         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
208         "\n        }".
209         "\n      </script>\n"; 
210     }
211   
212     // Use javascript to mark the currently selected plugin.
213     $menu = $this->menu;
214     if(isset($_GET['plug'])){
215       $menu.= 
216      "\n      <script language='javascript' type='text/javascript'>".
217      "\n        if($('plugMenuId_".$_GET['plug']."')){".
218      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
219      "\n        }".
220      "\n      </script>\n";
221     }
223     // Return the generated/cached gosa menu.
224     return ($menu);
225   }
228   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
229    *             login.
230    *            -----------------------------------------------------------------
231    *            Do NOT add style changes here manually, use the style.css or 
232    *             if you prefer create your own theme!!
233    *            -----------------------------------------------------------------
234    */
235   function show_iconmenu()
236   {
237     $add_hr =FALSE;
238     $this->iconmenu = "";
239     if ($this->iconmenu == ""){
240       $cfg= $this->config->data['MENU'];
241       foreach ($cfg as $headline => $plug){
242         $col= 0;
244         $this->iconmenu .= "\n        <div class='clear'></div>";
245         if($add_hr){
246           $add_hr = FALSE;
247           $this->iconmenu .= "\n        <hr>";
248         }
249         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
251         foreach ($plug as $info){
253           // Get Plugin info
254           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
256           // Add a seperating row
257           if (($col % 4) == 0){ 
258             $this->iconmenu .= "\n        <div class='clear'></div>";
259           }
261           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
262           $this->iconmenu.= "\n          ".image($icon);
263           $this->iconmenu.= "\n          <div class='dsc'>";
264           $this->iconmenu.= "\n            <h1>{$title}</h1>";
265           $this->iconmenu.= "\n            <p>{$desc}</p>";
266           $this->iconmenu.= "\n          </div>";
267           $this->iconmenu.= "\n        </div>";
268           $col++ ;
269         }
270         $add_hr = TRUE;
271       }
272     }
273     return ($this->iconmenu);
274   }
277   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
278    *             the generated HTML content, so we are not forced to generate it on every 
279    *             page request.
280    *            (See <pathMenu> of your gosa.conf)
281    */
282   function genPathMenu()
283   {
284     if(empty($this->pathMenu)){
285       $this->pathMenu = 
286         "\n        <div class='plugin-path'>".
287         "\n          <ul class='path-navigation'>".
288         "\n            <li class='left right-border' onClick=\"openPlugin('');\">".
289         "\n              <div class='nav-home'></div>".
290         "\n            </li>".
291         "\n            <li class='left'>Welcome to GOsa</li>";
293       // Check if we've at least one entry defined ih the pathmenu
294       if(isset($this->config->data['PATHMENU'])){
295         $cfg= &$this->config->data['PATHMENU'];
296         $rcfg = array_reverse($cfg);
297         foreach($rcfg as $id => $plug){
298           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
299           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
300         }
301       }
302       $this->pathMenu.= "\n          </ul>";
303       $this->pathMenu.= "\n        </div>";
304     } 
305     return($this->pathMenu); 
306   }
307   
309   /*! \brief    Returns additional info for a given class name, like 
310    *             plugin-icon, title, description and the index of the element 
311                  in the pluglist which uses this class.
312    */
313   function getPlugData($class)
314   {
315     global $class_mapping;
316     $vars= get_class_vars($class);
317     $plHeadline= _($vars['plHeadline']);
318     $plDescription= _($vars['plDescription']);
319     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
320     $index= $this->get_index($class);
322     /* Check if class is available. If the class doesn't exists display error symbol
323      *  to avoid that a user clicks on a non existing plugin
324      */
325     if(!$vars){
326       $plHeadline = $plDescription = _("Unknown");
327       $plIcon = "error.png";
328       $index = '';
329     } 
331     // Detect the correct position of the plugin icon
332     if(!preg_match("/\//",$plIcon)){
333       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
334             $class_mapping[$class])."/images/$plIcon");
335     }else{
336       $image = $plIcon; 
337     }
338     return(array($index, $plHeadline, $plDescription, $image));
339   }
342   /*! \brief    Returns the installation path of a plugin.
343    *            e.g. '../plugins/admin/mimetypes'
344    */
345   function get_path($index)
346   {
347     if(!isset($this->dirlist[$index])){
348       return ("");
349     }
350     return ("../".$this->dirlist[$index]);
351   }
354   /*! \brief    Returns the plugins id for a given class.
355    */
356   function get_index($class)
357   {
358     return (array_search($class, $this->pluginList));
359   }
362   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
363    *
364    *  @param  $plug_id  Integer  The ID of the plugin.
365    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
366    */
367   function plugin_access_allowed($plug_id)
368   {
369     return(isset($this->pluginList[$plug_id]));
370   }
373   /*! \brief  Force the menu to be recreated 
374    */
375   function reset_menus()
376   {
377     $this->menu = "";
378     $this->iconmenu ="";
379   }
382   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
383    *            This is just used in the helpviewer.php  
384    */
385   function gen_headlines()
386   {
387     $ret = array();
388     if(count($this->headlines) == 0){
389       foreach($this->config->data['MENU'] as $headline => $plugins){
390         foreach( $plugins as $id => $plug){
391           if (plugin_available($plug['CLASS'])){
392             $attrs = (get_class_vars($plug['CLASS']));
393             $ret[$id]['HEADLINE'] = $headline;
394             $ret[$id]['NAME']     = $attrs['plHeadline'];
395           }
396         }
397       }
398       $this->headlines = $ret;
399     }
400     return($this->headlines);
401   }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>