Code

We're html5, right?
[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     if(!session::is_set('maxC')){
72       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
73     }
74 //
75 //   // Now generate menu - usually they are cached
76 //   $this->gen_menu();
77 //   $this->show_iconmenu();
78 //   $this->genPathMenu();
79   }
82   /*! \brief    Tries to register a plugin in the pluglist
83    *            Checks existence and ACL for the given plugin.
84    *            Returns true in case of success else false. 
85    */
86   function registerPlugin($plug)
87   {
88     global $class_mapping;
90     if (!isset($plug['CLASS'])){
91       msg_dialog::display(
92           _("Configuration error"),
93           _("The configuration format has changed: please run the setup again!"),
94           FATAL_ERROR_DIALOG);
95       exit();
96     }
97     if (!plugin_available($plug['CLASS'])){
98       return(FALSE);
99     }
100     if (!$this->check_access($plug['ACL'])){
101       return(FALSE);
102     }
103     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
104     $this->pluginList[$this->index] = $plug['CLASS'];
105     $this->index++;
106     return(TRUE);
107   }
110   /*! \brief  Check whether we are allowed to modify the given acl or not..
111    *            This function is used to check which plugins are visible.
112    *            
113    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
114    *  @return   Boolean TRUE on success else FALSE
115    */
116   function check_access($aclname)
117   {
118     if (isset($this->silly_cache[$aclname])) {
119       return $this->silly_cache[$aclname];
120     }
122     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
123     $acls_to_check = array();
124     if(preg_match("/,/",$aclname)){
125       $acls_to_check = explode(",",$aclname);
126     }else{
127       $acls_to_check = array($aclname);
128     }
130     foreach($acls_to_check as $acl_to_check){
131       $acl_to_check = trim($acl_to_check);
133       /* Check if the given acl tag is only valid for self acl entries  
134        *          <plugin acl="users/user:self" class="user"...  
135        */       
136       if(preg_match("/:self$/",$acl_to_check)){
137         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
138         if(strpos($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         }else{
144           if($this->ui->get_category_permissions($this->ui->dn,$acl_to_check,"") != ""){
145             $this->silly_cache[$aclname]= TRUE;
146             return(TRUE);
147           }
148         }
149       }else{
151         // No self acls. Check if we have any acls for the given ACL type 
152         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
153         if(count($deps)){
154           $this->silly_cache[$aclname]= TRUE;
155           return TRUE;
156         }
157       }
158     }
160     $this->silly_cache[$aclname]= FALSE;
161     return (FALSE);
162   }
166   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
167    *             this usually only done once during login.
168    *            -----------------------------------------------------------------
169    *            Do NOT add style changes here manually, use the style.css or 
170    *             if you prefer create your own theme!!
171    *            -----------------------------------------------------------------
172    */
173   function gen_menu()
174   {
175     if ($this->menu == ""){
177       // First load the menu plugins and try to register them in the pluglist
178       //  if this fails for some reason, then remove the plugin from the menu.
179       if(isset($this->config->data['MENU'])){
180         foreach($this->config->data['MENU'] as $section => $plugins){
181           foreach($plugins as $id => $plug){
182             if(!$this->registerPlugin($plug)){ 
183               unset($this->config->data['MENU'][$section][$id]); 
184             }
185           }
186         }
187       }
189       $cfg= $this->config->data['MENU'];
190       $menu = "\n      <div class='navigation'>";
191       foreach ($cfg as $headline => $plug){
193         if(!count($plug)) continue;
195         $menu.= "\n        <div class='menu'>";
196         $menu.= "\n          <ul>";
197         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
198         $id = 0;
199         foreach ($plug as $info){
201           // Used to detect the last element in the menu
202           $id ++;
204           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
205           $class= "";
206           if($id == count($plug)) $class=" class='menu-last' ";
207           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
208         }
209         $menu.= "\n          </ul>";
210         $menu.= "\n          <div style='font-size:0'>&nbsp;</div>";
211         $menu.= "\n        </div>\n";
212         $menu.= "\n        <div style='font-size:0'>&nbsp;</div>";
213         $menu.= "\n        <div class='v-spacer'></div>\n";
214       }
215       $menu.= "\n      </div>\n";
216       $this->menu = $menu;
218       // Add javascript method to print out warning messages while leaving an unsaved form.
219       // We do it in here to get the string translated.
220       $this->menu .=  
221         "\n      <script language='javascript' type='text/javascript'>".
222         "\n        function openPlugin(id){".
223         "\n          return question(\""._("You are currently editing a database entry. Do you want to discard the changes?")."\",".
224         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
225         "\n        }".
226         "\n      </script>\n"; 
227     }
228   
229     // Use javascript to mark the currently selected plugin.
230     $menu = $this->menu;
231     if(isset($_GET['plug'])){
232       $menu.= 
233      "\n      <script language='javascript' type='text/javascript'>".
234      "\n        if($('plugMenuId_".$_GET['plug']."')){".
235      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
236      "\n        }".
237      "\n      </script>\n";
238     }
240     // Return the generated/cached gosa menu.
241     return ($menu);
242   }
245   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
246    *             login.
247    *            -----------------------------------------------------------------
248    *            Do NOT add style changes here manually, use the style.css or 
249    *             if you prefer create your own theme!!
250    *            -----------------------------------------------------------------
251    */
252   function show_iconmenu()
253   {
254     $add_hr =FALSE;
255     $this->iconmenu = "";
256     if ($this->iconmenu == ""){
258       $cfg= $this->config->data['MENU'];
259       foreach ($cfg as $headline => $plug){
260         $col= 0;
262         $this->iconmenu .= "\n        <div class='clear'></div>";
263         if($add_hr){
264           $add_hr = FALSE;
265           $this->iconmenu .= "\n        <hr>";
266         }
267         $this->iconmenu .= "\n        <h3 class='icon-menu-title'>". _($headline)."</h3>";
269         foreach ($plug as $info){
271           // Get Plugin info
272           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
274           // Add a seperating row
275           if (($col % 4) == 0){ 
276             $this->iconmenu .= "\n        <div class='clear'></div>";
277           }
279           $this->iconmenu.= "\n        <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
280           $this->iconmenu.= "\n          ".image($icon);
281           $this->iconmenu.= "\n          <div class='dsc'>";
282           $this->iconmenu.= "\n            <h1>{$title}</h1>";
283           $this->iconmenu.= "\n            <p>{$desc}</p>";
284           $this->iconmenu.= "\n          </div>";
285           $this->iconmenu.= "\n        </div>";
286           $col++ ;
287         }
288         $add_hr = TRUE;
289       }
290     }
291     return ($this->iconmenu);
292   }
295   /*! \brieg    Generates and the path menu (the one on the upper right) and keeps
296    *             the generated HTML content, so we are not forced to generate it on every 
297    *             page request.
298    *            (See <pathMenu> of your gosa.conf)
299    */
300   function genPathMenu()
301   {
302     if(empty($this->pathMenu)){
304       // Now load the path menu and try to register the plugins in the pluglist
305       //  if this fails for some reason, then remove the plugin from the menu.
306       if(isset($this->config->data['PATHMENU'])){
307         foreach($this->config->data['PATHMENU'] as $id => $plugin){
308           if(!$this->registerPlugin($plugin)){
309             unset($this->config->data['PATHMENU'][$id]); 
310           } 
311         }
312       }
314       $this->pathMenu = 
315         "\n        <div class='plugin-path'>".
316         "\n          <ul class='path-navigation'>".
317         "\n            <li class='left' onClick=\"openPlugin('');\" title='"._("Back to main menu")."'>".
318         "\n              ".image(get_template_path("images/home.png")).
319         "\n            </li>".
320         "\n            %navigator%";
322       // Check if we've at least one entry defined ih the pathmenu
323       if(isset($this->config->data['PATHMENU'])){
324         $cfg= &$this->config->data['PATHMENU'];
325         $rcfg = array_reverse($cfg);
326         foreach($rcfg as $id => $plug){
327           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
328           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
329         }
330       }
331       $this->pathMenu.= "\n          </ul>";
332       $this->pathMenu.= "\n        </div>";
333     }
335     $menu = pathNavigator::getCurrentPath();
336     return(preg_replace("/%navigator%/", $menu, $this->pathMenu)); 
337   }
338   
340   /*! \brief    Returns additional info for a given class name, like 
341    *             plugin-icon, title, description and the index of the element 
342                  in the pluglist which uses this class.
343    */
344   function getPlugData($class)
345   {
346     global $class_mapping;
347     $vars= get_class_vars($class);
348     $plHeadline= _($vars['plHeadline']);
349     $plDescription= _($vars['plDescription']);
350     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
351     $index= $this->get_index($class);
353     /* Check if class is available. If the class doesn't exists display error symbol
354      *  to avoid that a user clicks on a non existing plugin
355      */
356     if(!$vars){
357       $plHeadline = $plDescription = _("Unknown");
358       $plIcon = "error.png";
359       $index = '';
360     } 
362     // Detect the correct position of the plugin icon
363     if(!preg_match("/\//",$plIcon)){
364       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
365             $class_mapping[$class])."/images/$plIcon");
366     }else{
367       $image = $plIcon; 
368     }
369     return(array($index, $plHeadline, $plDescription, $image));
370   }
373   /*! \brief    Returns the installation path of a plugin.
374    *            e.g. '../plugins/admin/mimetypes'
375    */
376   function get_path($index)
377   {
378     if(!isset($this->dirlist[$index])){
379       return ("");
380     }
381     return ("../".$this->dirlist[$index]);
382   }
385   /*! \brief    Returns the plugins id for a given class.
386    */
387   function get_index($class)
388   {
389     return (array_search($class, $this->pluginList));
390   }
393   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
394    *
395    *  @param  $plug_id  Integer  The ID of the plugin.
396    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
397    */
398   function plugin_access_allowed($plug_id)
399   {
400     return(isset($this->pluginList[$plug_id]));
401   }
404   /*! \brief  Force the menu to be recreated 
405    */
406   function reset_menus()
407   {
408     $this->menu = "";
409     $this->iconmenu ="";
410   }
413   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
414    *            This is just used in the helpviewer.php  
415    */
416   function gen_headlines()
417   {
418     $ret = array();
419     if(count($this->headlines) == 0){
420       foreach($this->config->data['MENU'] as $headline => $plugins){
421         foreach( $plugins as $id => $plug){
422           if (plugin_available($plug['CLASS'])){
423             $attrs = (get_class_vars($plug['CLASS']));
424             $ret[$id]['HEADLINE'] = $headline;
425             $ret[$id]['NAME']     = $attrs['plHeadline'];
426           }
427         }
428       }
429       $this->headlines = $ret;
430     }
431     return($this->headlines);
432   }
434 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
435 ?>