Code

9166656b5df3bd153c4477e2ecf7404fcf0cdb94
[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 Menu Plugins 
51     if(isset($this->config->data['MENU'])){
52       foreach($this->config->data['MENU'] as $section => $plugins){
53         foreach($plugins as $id => $plug){
54           if(!$this->registerPlugin($plug)){ 
55             unset($this->config->data['MENU'][$section][$id]); 
56           }
57         }
58       }
59     }
60     
61     // Now register pathMenu plugins
62     if(isset($this->config->data['PATHMENU'])){
63       foreach($this->config->data['PATHMENU'] as $id => $plugin){
64         if(!$this->registerPlugin($plugin)){
65           unset($this->config->data['PATHMENU'][$id]); 
66         } 
67       }
68     }
70     if(!session::is_set('maxC')){
71       session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
72     }
73     $this->gen_menu();
74     $this->show_iconmenu();
75     $this->genPathMenu();
76   }
79   function registerPlugin($plug)
80   {
81     global $class_mapping;
83     if (!isset($plug['CLASS'])){
84       msg_dialog::display(
85           _("Configuration error"),
86           _("The configuration format has changed. Please re-run setup!"),
87           FATAL_ERROR_DIALOG);
88       exit();
89     }
90     if (!plugin_available($plug['CLASS'])){
91       return(FALSE);
92     }
93     if (!$this->check_access($plug['ACL'])){
94       return(FALSE);
95     }
96     $this->dirlist[$this->index] = dirname($class_mapping[$plug['CLASS']]);
97     $this->pluginList[$this->index] = $plug['CLASS'];
98     $this->index++;
99     return(TRUE);
100   }
103   /*! \brief  Check whether we are allowed to modify the given acl or not..
104    *            This function is used to check which plugins are visible.
105    *            
106    *  @param    The acl tag to check for, eg.   "users/user:self", "systems", ...
107    *  @return   Boolean TRUE on success else FALSE
108    */
109   function check_access($aclname)
110   {
111     if (isset($this->silly_cache[$aclname])) {
112       return $this->silly_cache[$aclname];
113     }
115     // Split given acl string into an array. e.g. "user,systems" => array("users","systems");
116     $acls_to_check = array();
117     if(preg_match("/,/",$aclname)){
118       $acls_to_check = explode(",",$aclname);
119     }else{
120       $acls_to_check = array($aclname);
121     }
123     foreach($acls_to_check as $acl_to_check){
124       $acl_to_check = trim($acl_to_check);
126       /* Check if the given acl tag is only valid for self acl entries  
127        *          <plugin acl="users/user:self" class="user"...  
128        */       
129       if(preg_match("/:self$/",$acl_to_check)){
130         $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
131         if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
132           $this->silly_cache[$aclname]= TRUE;
133           return(TRUE);
134         }
135         $this->silly_cache[$aclname]= FALSE;
136         return(FALSE);
137       }else{
139         // No self acls. Check if we have any acls for the given ACL type 
140         $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
141         if(count($deps)){
142           $this->silly_cache[$aclname]= TRUE;
143           return TRUE;
144         }
145       }
146     }
148     $this->silly_cache[$aclname]= FALSE;
149     return (FALSE);
150   }
154   /*! \brief    Generate the GOsa Main-Menu here (The menu on the left), 
155    *             this usually only done once during login.
156    *            -----------------------------------------------------------------
157    *            Do NOT add style changes here manually, use the style.css or 
158    *             if you prefer create your own theme!!
159    *            -----------------------------------------------------------------
160    */
161   function gen_menu()
162   {
163     if (1 || $this->menu == ""){
164       $cfg= $this->config->data['MENU'];
165       $menu = "\n      <div class='navigation'>";
166       foreach ($cfg as $headline => $plug){
168         if(!count($plug)) continue;
170         $menu.= "\n        <div class='menu'>";
171         $menu.= "\n          <ul>";
172         $menu.= "\n            <li class='menu-header'>"._($headline)."</li>";
173         $id = 0;
174         foreach ($plug as $info){
176           // Used to detect the last element in the menu
177           $id ++;
179           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
180           $class= "";
181           if($id == count($plug)) $class=" class='menu-last' ";
182           $menu .=  "\n            <li id='plugMenuId_{$index}' $class onClick='return openPlugin({$index});'>".$title."</li>";
183         }
184         $menu.= "\n          </ul>";
185         $menu.= "\n          <div></div>";
186         $menu.= "\n        </div>\n";
187         $menu.= "\n        <div></div>";
188         $menu.= "\n        <div class='v-spacer'></div>\n";
189       }
190       $menu.= "\n      </div>\n";
191       $this->menu = $menu;
193       // Add javascript method to print out warning messages while leaving an unsaved form.
194       // We do it in here to get the string translated.
195       $this->menu .=  
196         "\n      <script language='javascript' type='text/javascript'>".
197         "\n        function openPlugin(id){".
198         "\n          return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\",".
199         "\n            \"main.php?plug=\" + id + \"&amp;reset=1\");".
200         "\n        }".
201         "\n      </script>\n"; 
202     }
203   
204     // Use javascript to mark the currently selected plugin.
205     $menu = $this->menu;
206     if(isset($_GET['plug'])){
207       $menu.= 
208      "\n      <script language='javascript' type='text/javascript'>".
209      "\n        if($('plugMenuId_".$_GET['plug']."')){".
210      "\n          $('plugMenuId_".$_GET['plug']."').className= 'current'".
211      "\n        }".
212      "\n      </script>\n";
213     }
215     // Return the generated/cached gosa menu.
216     return ($menu);
217   }
220   /*! \brief    Generate the GOsa Icon-Menu here, this usually only done once during
221    *             login.
222    *            -----------------------------------------------------------------
223    *            Do NOT add style changes here manually, use the style.css or 
224    *             if you prefer create your own theme!!
225    *            -----------------------------------------------------------------
226    */
227   function show_iconmenu()
228   {
229     $add_hr =FALSE;
230     if ($this->iconmenu == ""){
231       $cfg= $this->config->data['MENU'];
232       foreach ($cfg as $headline => $plug){
233         $col= 0;
235         $this->iconmenu .= "\n  <div class='clear'></div>\n";
236         if($add_hr){
237           $add_hr = FALSE;
238           $this->iconmenu .= "\n  <hr>\n";
239         }
240         $this->iconmenu .= "\n  <h3 class='icon-menu-title'>". _($headline)."</h3>\n";
242         foreach ($plug as $info){
244           // Get Plugin info
245           list($index, $title, $desc, $icon) = $this->getPlugData($info['CLASS']);
247           // Add a seperating row
248           if (($col % 4) == 0){ 
249             $this->iconmenu .= "\n  <div class='clear'></div>\n";
250           }
253           $this->iconmenu.= "\n  <div class='icon-menu-item' style='width: 25%;' onclick='openPlugin({$index})'>";
254           $this->iconmenu.= "\n   ".image($icon);
255           $this->iconmenu.= "\n   <div class='dsc'>";
256           $this->iconmenu.= "\n    <h1>{$title}</h1>";
257           $this->iconmenu.= "\n    <p>{$desc}</p>";
258           $this->iconmenu.= "\n   </div>";
259           $this->iconmenu.= "\n  </div>";
261           $col++ ;
262         }
263         $add_hr = TRUE;
264       }
265     }
266     return ($this->iconmenu);
267   }
270   function genPathMenu()
271   {
272     if(1 || empty($this->pathMenu)){
273       $this->pathMenu = 
274         "\n        <div class='plugin-path'>".
275         "\n          <ul class='path-navigation'>".
276         "\n            <li class='left right-border' onClick=\"openPlugin('');\">".
277         "\n              <div class='nav-home'></div>".
278         "\n            </li>".
279         "\n            <li class='left'>Welcome to GOsa</li>";
281       if(isset($this->config->data['PATHMENU'])){
282         $cfg= &$this->config->data['PATHMENU'];
283         $rcfg = array_reverse($cfg);
284         foreach($rcfg as $id => $plug){
285           list($index, $title, $desc, $icon) = $this->getPlugData($plug['CLASS']);
286           $this->pathMenu.= "\n            <li class='right left-border' onClick='openPlugin({$index})'>{$title}</li>";
287         }
288       }
289       $this->pathMenu.= "\n          </ul>";
290       $this->pathMenu.= "\n        </div>";
291     } 
292     return($this->pathMenu); 
293   }
294   
296   function getPlugData($class)
297   {
298     global $class_mapping;
299     $vars= get_class_vars($class);
300     $plHeadline= _($vars['plHeadline']);
301     $plDescription= _($vars['plDescription']);
302     $plIcon = (isset($vars['plIcon'])) ? $vars['plIcon']: "plugin.png";
303     $index= $this->get_index($class);
305     /* Check if class is available. If the class doesn't exists display error symbol
306      *  to avoid that a user clicks on a non existing plugin
307      */
308     if(!$vars){
309       $plHeadline = $plDescription = _("Unknown");
310       $plIcon = "error.png";
311       $index = '';
312     } 
314     // Detect the correct position of the plugin icon
315     if(!preg_match("/\//",$plIcon)){
316       $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', 
317             $class_mapping[$class])."/images/$plIcon");
318     }else{
319       $image = $plIcon; 
320     }
321     return(array($index, $plHeadline, $plDescription, $image));
322   }
325   /*! \brief    Returns the installation path of a plugin.
326    *            e.g. '../plugins/admin/mimetypes'
327    */
328   function get_path($index)
329   {
330     if(!isset($this->dirlist[$index])){
331       return ("");
332     }
333     return ("../".$this->dirlist[$index]);
334   }
337   /*! \brief    Returns the plugins id for a given class.
338    */
339   function get_index($class)
340   {
341     return (array_search($class, $this->pluginList));
342   }
345   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
346    *
347    *  @param  $plug_id  Integer  The ID of the plugin.
348    *  @return Boolean   TRUE if we are allowed to view the plugin else FASLE
349    */
350   function plugin_access_allowed($plug_id)
351   {
352     return(isset($this->pluginList[$plug_id]));
353   }
356   /*! \brief  Force the menu to be recreated 
357    */
358   function reset_menus()
359   {
360     $this->menu = "";
361     $this->iconmenu ="";
362   }
365   /*! \brief    Generates an array containing plugin names (headlines) and theirs ids.
366    *            This is just used in the helpviewer.php  
367    */
368   function gen_headlines()
369   {
370     $ret = array();
371     if(count($this->headlines) == 0){
372       foreach($this->config->data['MENU'] as $headline => $plugins){
373         foreach( $plugins as $id => $plug){
374           if (plugin_available($plug['CLASS'])){
375             $attrs = (get_class_vars($plug['CLASS']));
376             $ret[$id]['HEADLINE'] = $headline;
377             $ret[$id]['NAME']     = $attrs['plHeadline'];
378           }
379         }
380       }
381       $this->headlines = $ret;
382     }
383     return($this->headlines);
384   }
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>