Code

I really start hating those @ in front of function calls... makes debugging very...
[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();
36   // Remember stuff in here.
37   var $silly_cache= array();
39   // The pluglist keeps track of all registered plugins.
40   // (Registered plugins are those we are able to access)
41   var $pluginList = array();
43   // Some cache vars to avoid regenration of the menus.   
44   var $pathMenu = "";
45   var $menu= "";
46   var $iconmenu= "";
48   // Classes plInfo list
49   var $info = array();
51   function pluglist(&$config, &$ui)
52   {
53     $this->ui= &$ui;
54     $this->config= &$config;
55     $this->loadPluginList();
56   }
58   function loadPluginList()
59   {
60     $this->pluginList = array();
62                 /* Fill info part of pluglist */
63                 $classes= get_declared_classes();
64                 foreach ($classes as $cname){
65                         $cmethods = get_class_methods($cname);
66                         if (in_array_ics('plInfo',$cmethods)){
67                                 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
68                         }
69                 }
71                 /* Provide field for 'all' */
72                 $this->info['all']= array();
73                 $this->info['all']['plProvidedAcls']= array();
74                 $this->info['all']['plDescription']= _("All objects in this category");
75                 $this->info['all']['plSelfModify']= FALSE;
77     if(!session::is_set('maxC')){
78       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
79     }
80 //
81 //   // Now generate menu - usually they are cached
82 //   $this->gen_menu();
83 //   $this->show_iconmenu();
84 //   $this->genPathMenu();
85   }
88   /*! \brief    Tries to register a plugin in the pluglist
89    *            Checks existence and ACL for the given plugin.
90    *            Returns true in case of success else false. 
91    */
92   function registerPlugin($plug)
93   {
94     global $class_mapping;
96     if (!isset($plug['CLASS'])){
97       msg_dialog::display(
98           _("Configuration error"),
99           _("The configuration format has changed. Please re-run setup!"),
100           FATAL_ERROR_DIALOG);
101       exit();
102     }
103     if (!plugin_available($plug['CLASS'])){
104       return(FALSE);
105     }
106     if (!$this->check_access($plug['ACL'])){
107       return(FALSE);
108     }
109     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
110     $this->pluginList[$this->index] = $plug['CLASS'];
111     $this->index++;
112     return(TRUE);
113   }
116   /*! \brief  Check whether we are allowed to modify the given acl or not..
117    *            This function is used to check which plugins are visible.
118    *            
119    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
120    *  @return   Boolean TRUE on success else FALSE
121    */
122   function check_access($aclname)
123   {
124     if (isset($this->silly_cache[$aclname])) {
125       return $this->silly_cache[$aclname];
126     }
128     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
129     $acls_to_check = array();
130     if(preg_match("/,/",$aclname)){
131       $acls_to_check = explode(",",$aclname);
132     }else{
133       $acls_to_check = array($aclname);
134     }
136     foreach($acls_to_check as $acl_to_check){
137       $acl_to_check = trim($acl_to_check);
139       /* Check if the given acl tag is only valid for self acl entries  
140        *          <plugin acl="users/user:self" class="user"...  
141        */       
142       if(preg_match("/:self$/",$acl_to_check)){
143         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
144         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
145           $this->silly_cache[$aclname]= TRUE;
146           return(TRUE);
147         }
148         $this->silly_cache[$aclname]= FALSE;
149         return(FALSE);
150       }else{
152         // No self acls. Check if we have any acls for the given ACL type 
153         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
154         if(count($deps)){
155           $this->silly_cache[$aclname]= TRUE;
156           return TRUE;
157         }
158       }
159     }
161     $this->silly_cache[$aclname]= FALSE;
162     return (FALSE);
163   }
167   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
168    *             this usually only done once during login.
169    *            -----------------------------------------------------------------
170    *            Do NOT add style changes here manually, use the style.css or 
171    *             if you prefer create your own theme!!
172    *            -----------------------------------------------------------------
173    */
174   function gen_menu()
175   {
176     if ($this->menu == ""){
178       // First load the menu plugins and try to register them in the pluglist
179       //  if this fails for some reason, then remove the plugin from the menu.
180       if(isset($this->config->data['MENU'])){
181         foreach($this->config->data['MENU'] as $section => $plugins){
182           foreach($plugins as $id => $plug){
183             if(!$this->registerPlugin($plug)){ 
184               unset($this->config->data['MENU'][$section][$id]); 
185             }
186           }
187         }
188       }
190       $cfg= $this->config->data['MENU'];
191       $menu = "\n      <div class='navigation'>";
192       foreach ($cfg as $headline => $plug){
194         if(!count($plug)) continue;
196         $menu.= "\n        <div class='menu'>";
197         $menu.= "\n          <ul>";
198         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
199         $id = 0;
200         foreach ($plug as $info){
202           // Used to detect the last element in the menu
203           $id ++;
205           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
206           $class= "";
207           if($id == count($plug)) $class=" class='menu-last' ";
208           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
209         }
210         $menu.= "\n          </ul>";
211         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
212         $menu.= "\n        </div>\n";
213         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
214         $menu.= "\n        <div class='v-spacer'></div>\n";
215       }
216       $menu.= "\n      </div>\n";
217       $this->menu = $menu;
219       // Add javascript method to print out warning messages while leaving an unsaved form.
220       // We do it in here to get the string translated.
221       $this->menu .=  
222         "\n      <script language='javascript' type='text/javascript'>".
223         "\n        function openPlugin(id){".
224         "\n          return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
225         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
226         "\n        }".
227         "\n      </script>\n"; 
228     }
229   
230     // Use javascript to mark the currently selected plugin.
231     $menu = $this->menu;
232     if(isset($_GET['plug'])){
233       $menu.= 
234      "\n      <script language='javascript' type='text/javascript'>".
235      "\n        if($('plugMenuId_".$_GET['plug']."')){".
236      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
237      "\n        }".
238      "\n      </script>\n";
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     $add_hr =FALSE;
256     $this->iconmenu = "";
257     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>";
264         if($add_hr){
265           $add_hr = FALSE;
266           $this->iconmenu .= "\n        <hr>";
267         }
268         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
270         foreach ($plug as $info){
272           // Get Plugin info
273           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
275           // Add a seperating row
276           if (($col % 4) == 0){ 
277             $this->iconmenu .= "\n        <div class='clear'></div>";
278           }
280           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
281           $this->iconmenu.= "\n          ".image($icon);
282           $this->iconmenu.= "\n          <div class='dsc'>";
283           $this->iconmenu.= "\n            <h1>{$title}</h1>";
284           $this->iconmenu.= "\n            <p>{$desc}</p>";
285           $this->iconmenu.= "\n          </div>";
286           $this->iconmenu.= "\n        </div>";
287           $col++ ;
288         }
289         $add_hr = TRUE;
290       }
291     }
292     return ($this->iconmenu);
293   }
296   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
297    *             the generated HTML content, so we are not forced to generate it on every 
298    *             page request.
299    *            (See <pathMenu> of your gosa.conf)
300    */
301   function genPathMenu()
302   {
303     if(empty($this->pathMenu)){
305       // Now load the path menu and try to register the plugins in the pluglist
306       //  if this fails for some reason, then remove the plugin from the menu.
307       if(isset($this->config->data['PATHMENU'])){
308         foreach($this->config->data['PATHMENU'] as $id => $plugin){
309           if(!$this->registerPlugin($plugin)){
310             unset($this->config->data['PATHMENU'][$id]); 
311           } 
312         }
313       }
315       $this->pathMenu = 
316         "\n        <div class='plugin-path'>".
317         "\n          <ul class='path-navigation'>".
318         "\n            <li class='left right-border' onClick=\"openPlugin('');\">".
319         "\n              <div class='nav-home'></div>".
320         "\n            </li>".
321         "\n            <li class='left'>Welcome to GOsa</li>";
323       // Check if we've at least one entry defined ih the pathmenu
324       if(isset($this->config->data['PATHMENU'])){
325         $cfg= &$this->config->data['PATHMENU'];
326         $rcfg = array_reverse($cfg);
327         foreach($rcfg as $id => $plug){
328           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
329           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
330         }
331       }
332       $this->pathMenu.= "\n          </ul>";
333       $this->pathMenu.= "\n        </div>";
334     } 
335     return($this->pathMenu); 
336   }
337   
339   /*! \brief    Returns additional info for a given class name, like 
340    *             plugin-icon, title, description and the index of the element 
341                  in the pluglist which uses this class.
342    */
343   function getPlugData($class)
344   {
345     global $class_mapping;
346     $vars= get_class_vars($class);
347     $plHeadline= _($vars['plHeadline']);
348     $plDescription= _($vars['plDescription']);
349     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
350     $index= $this->get_index($class);
352     /* Check if class is available. If the class doesn't exists display error symbol
353      *  to avoid that a user clicks on a non existing plugin
354      */
355     if(!$vars){
356       $plHeadline = $plDescription = _("Unknown");
357       $plIcon = "error.png";
358       $index = '';
359     } 
361     // Detect the correct position of the plugin icon
362     if(!preg_match("/\//",$plIcon)){
363       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
364             $class_mapping[$class])."/images/$plIcon");
365     }else{
366       $image = $plIcon; 
367     }
368     return(array($index, $plHeadline, $plDescription, $image));
369   }
372   /*! \brief    Returns the installation path of a plugin.
373    *            e.g. '../plugins/admin/mimetypes'
374    */
375   function get_path($index)
376   {
377     if(!isset($this->dirlist[$index])){
378       return ("");
379     }
380     return ("../".$this->dirlist[$index]);
381   }
384   /*! \brief    Returns the plugins id for a given class.
385    */
386   function get_index($class)
387   {
388     return (array_search($class, $this->pluginList));
389   }
392   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
393    *
394    *  @param  $plug_id  Integer  The ID of the plugin.
395    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
396    */
397   function plugin_access_allowed($plug_id)
398   {
399     return(isset($this->pluginList[$plug_id]));
400   }
403   /*! \brief  Force the menu to be recreated 
404    */
405   function reset_menus()
406   {
407     $this->menu = "";
408     $this->iconmenu ="";
409   }
412   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
413    *            This is just used in the helpviewer.php  
414    */
415   function gen_headlines()
416   {
417     $ret = array();
418     if(count($this->headlines) == 0){
419       foreach($this->config->data['MENU'] as $headline => $plugins){
420         foreach( $plugins as $id => $plug){
421           if (plugin_available($plug['CLASS'])){
422             $attrs = (get_class_vars($plug['CLASS']));
423             $ret[$id]['HEADLINE'] = $headline;
424             $ret[$id]['NAME']     = $attrs['plHeadline'];
425           }
426         }
427       }
428       $this->headlines = $ret;
429     }
430     return($this->headlines);
431   }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>