Code

Added is_harmless
[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 run the setup again!"),
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(strpos($acl_to_check,"/")){
145           if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
146             $this->silly_cache[$aclname]= TRUE;
147             return(TRUE);
148           }
149         }else{
150           if($this->ui->get_category_permissions($this->ui->dn,$acl_to_check,"") != ""){
151             $this->silly_cache[$aclname]= TRUE;
152             return(TRUE);
153           }
154         }
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 == ""){
183       // First load the menu plugins and try to register them in the pluglist
184       //  if this fails for some reason, then remove the plugin from the menu.
185       if(isset($this->config->data['MENU'])){
186         foreach($this->config->data['MENU'] as $section => $plugins){
187           foreach($plugins as $id => $plug){
188             if(!$this->registerPlugin($plug)){ 
189               unset($this->config->data['MENU'][$section][$id]); 
190             }
191           }
192         }
193       }
195       $cfg= $this->config->data['MENU'];
196       $menu = "\n      <div class='navigation'>";
197       foreach ($cfg as $headline => $plug){
199         if(!count($plug)) continue;
201         $menu.= "\n        <div class='menu'>";
202         $menu.= "\n          <ul>";
203         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
204         $id = 0;
205         foreach ($plug as $info){
207           // Used to detect the last element in the menu
208           $id ++;
210           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
211           $class= "";
212           if($id == count($plug)) $class=" class='menu-last' ";
213           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
214         }
215         $menu.= "\n          </ul>";
216         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
217         $menu.= "\n        </div>\n";
218         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
219         $menu.= "\n        <div class='v-spacer'></div>\n";
220       }
221       $menu.= "\n      </div>\n";
222       $this->menu = $menu;
224       // Add javascript method to print out warning messages while leaving an unsaved form.
225       // We do it in here to get the string translated.
226       $this->menu .=  
227         "\n      <script language='javascript' type='text/javascript'>".
228         "\n        function openPlugin(id){".
229         "\n          return question(\""._("You are currently editing a database entry. Do you want to discard the changes?")."\",".
230         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
231         "\n        }".
232         "\n      </script>\n"; 
233     }
234   
235     // Use javascript to mark the currently selected plugin.
236     $menu = $this->menu;
237     if(isset($_GET['plug'])){
238       $menu.= 
239      "\n      <script language='javascript' type='text/javascript'>".
240      "\n        if($('plugMenuId_".$_GET['plug']."')){".
241      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
242      "\n        }".
243      "\n      </script>\n";
244     }
246     // Return the generated/cached gosa menu.
247     return ($menu);
248   }
251   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
252    *             login.
253    *            -----------------------------------------------------------------
254    *            Do NOT add style changes here manually, use the style.css or 
255    *             if you prefer create your own theme!!
256    *            -----------------------------------------------------------------
257    */
258   function show_iconmenu()
259   {
260     $add_hr =FALSE;
261     $this->iconmenu = "";
262     if ($this->iconmenu == ""){
264       $cfg= $this->config->data['MENU'];
265       foreach ($cfg as $headline => $plug){
266         $col= 0;
268         $this->iconmenu .= "\n        <div class='clear'></div>";
269         if($add_hr){
270           $add_hr = FALSE;
271           $this->iconmenu .= "\n        <hr>";
272         }
273         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
275         foreach ($plug as $info){
277           // Get Plugin info
278           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
280           // Add a seperating row
281           if (($col % 4) == 0){ 
282             $this->iconmenu .= "\n        <div class='clear'></div>";
283           }
285           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
286           $this->iconmenu.= "\n          ".image($icon);
287           $this->iconmenu.= "\n          <div class='dsc'>";
288           $this->iconmenu.= "\n            <h1>{$title}</h1>";
289           $this->iconmenu.= "\n            <p>{$desc}</p>";
290           $this->iconmenu.= "\n          </div>";
291           $this->iconmenu.= "\n        </div>";
292           $col++ ;
293         }
294         $add_hr = TRUE;
295       }
296     }
297     return ($this->iconmenu);
298   }
301   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
302    *             the generated HTML content, so we are not forced to generate it on every 
303    *             page request.
304    *            (See <pathMenu> of your gosa.conf)
305    */
306   function genPathMenu()
307   {
308     if(empty($this->pathMenu)){
310       // Now load the path menu and try to register the plugins in the pluglist
311       //  if this fails for some reason, then remove the plugin from the menu.
312       if(isset($this->config->data['PATHMENU'])){
313         foreach($this->config->data['PATHMENU'] as $id => $plugin){
314           if(!$this->registerPlugin($plugin)){
315             unset($this->config->data['PATHMENU'][$id]); 
316           } 
317         }
318       }
320       $this->pathMenu = 
321         "\n        <div class='plugin-path'>".
322         "\n          <ul class='path-navigation'>".
323         "\n            <li class='left' onClick=\"openPlugin('');\" title='"._("Back to main menu")."'>".
324         "\n              ".image(get_template_path("images/home.png")).
325         "\n            </li>".
326         "\n            %navigator%";
328       // Check if we've at least one entry defined ih the pathmenu
329       if(isset($this->config->data['PATHMENU'])){
330         $cfg= &$this->config->data['PATHMENU'];
331         $rcfg = array_reverse($cfg);
332         foreach($rcfg as $id => $plug){
333           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
334           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
335         }
336       }
337       $this->pathMenu.= "\n          </ul>";
338       $this->pathMenu.= "\n        </div>";
339     }
341     $menu = pathNavigator::getCurrentPath();
342     return(preg_replace("/%navigator%/", $menu, $this->pathMenu)); 
343   }
344   
346   /*! \brief    Returns additional info for a given class name, like 
347    *             plugin-icon, title, description and the index of the element 
348                  in the pluglist which uses this class.
349    */
350   function getPlugData($class)
351   {
352     global $class_mapping;
353     $vars= get_class_vars($class);
354     $plHeadline= _($vars['plHeadline']);
355     $plDescription= _($vars['plDescription']);
356     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
357     $index= $this->get_index($class);
359     /* Check if class is available. If the class doesn't exists display error symbol
360      *  to avoid that a user clicks on a non existing plugin
361      */
362     if(!$vars){
363       $plHeadline = $plDescription = _("Unknown");
364       $plIcon = "error.png";
365       $index = '';
366     } 
368     // Detect the correct position of the plugin icon
369     if(!preg_match("/\//",$plIcon)){
370       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
371             $class_mapping[$class])."/images/$plIcon");
372     }else{
373       $image = $plIcon; 
374     }
375     return(array($index, $plHeadline, $plDescription, $image));
376   }
379   /*! \brief    Returns the installation path of a plugin.
380    *            e.g. '../plugins/admin/mimetypes'
381    */
382   function get_path($index)
383   {
384     if(!isset($this->dirlist[$index])){
385       return ("");
386     }
387     return ("../".$this->dirlist[$index]);
388   }
391   /*! \brief    Returns the plugins id for a given class.
392    */
393   function get_index($class)
394   {
395     return (array_search($class, $this->pluginList));
396   }
399   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
400    *
401    *  @param  $plug_id  Integer  The ID of the plugin.
402    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
403    */
404   function plugin_access_allowed($plug_id)
405   {
406     return(isset($this->pluginList[$plug_id]));
407   }
410   /*! \brief  Force the menu to be recreated 
411    */
412   function reset_menus()
413   {
414     $this->menu = "";
415     $this->iconmenu ="";
416   }
419   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
420    *            This is just used in the helpviewer.php  
421    */
422   function gen_headlines()
423   {
424     $ret = array();
425     if(count($this->headlines) == 0){
426       foreach($this->config->data['MENU'] as $headline => $plugins){
427         foreach( $plugins as $id => $plug){
428           if (plugin_available($plug['CLASS'])){
429             $attrs = (get_class_vars($plug['CLASS']));
430             $ret[$id]['HEADLINE'] = $headline;
431             $ret[$id]['NAME']     = $attrs['plHeadline'];
432           }
433         }
434       }
435       $this->headlines = $ret;
436     }
437     return($this->headlines);
438   }
440 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
441 ?>