Code

Added branches container for old stuff
[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         var $index= 0;
25         var $menu= "";
26         var $iconmenu= "";
27         var $menuparts= array();
28         var $config= NULL;
29         var $dirlist= array();
30         var $ui= NULL;
31         var $current= "";
32         var $info= array();
33         var $headlines = array();
34   var $allowed_plugins = array();
36         function pluglist(&$config, &$ui)
37         {
38                 $this->ui= &$ui;
39                 $this->config= &$config;
41                 /* Create dirlist for all plugins */
42                 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
44                 /* Fill info part of pluglist */
45                 $classes= get_declared_classes();
47                                 foreach ($classes as $cname){
48                                         $cmethods = get_class_methods($cname);
49                                         if (in_array_ics('plInfo',$cmethods)){
50                                                 $this->info[$cname]= call_user_func(array($cname, 'plInfo'));
51                                         }
52                                 }
54                 /* Provide field for 'all' */
55                 $this->info['all']= array();
56                 $this->info['all']['plProvidedAcls']= array();
57                 $this->info['all']['plDescription']= _("All objects in this category");
58                 $this->info['all']['plSelfModify']= FALSE;
59         }
61         function get_plugins($list, &$config)
62         {
63                 global $class_mapping;
65                 /* Error reporting, because I'm getting strange messages in PHP 4.2.x */
66                 if (!isset($config['CLASS'])){
67                         if (is_array($config)){
68                                 foreach ($config as $val){
69                                         $list= $this->get_plugins($list, $val);
70                                 }
71                         }
72                 } else {
73                         if (is_array($config) && isset($class_mapping[$config['CLASS']])){
74                                 $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
75                         } else {
76                                 $list[$this->index++]= "";
77                         }
78                 }
80                 return ($list);
81         }
84         /*! \brief  Check whether we are allowed to modify the given acl or nit..
85                                 This function is used to check which plugins are visible.
86                                 
87                 @param  The acl tag to test, eg.        "users/user:self", "systems", ...
88                 @return Boolean TRUE on success else FALSE
89      */
90         function check_access($aclname)
91         {
92                 /* Split given acl string into an array. 
93                         e.g. "user,systems" => array("users","systems");
94          */
95                 $acls_to_check = array();
96                 if(preg_match("/,/",$aclname)){
97                         $acls_to_check = split(",",$aclname);
98                 }else{
99                         $acls_to_check = array($aclname);
100                 }
102                 foreach($acls_to_check as $acl_to_check){
103                 
104                         /* Check if the given acl tag is only valid for self acl entries  
105                  <plugin acl="users/user:self" class="user"...
106              */ 
107                         if(preg_match("/:self$/",$acl_to_check)){
108                                 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
109                                 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
110                                         return(TRUE);
111                                 }
112                                 return(FALSE);
113                         }else{
114                 
115                                 /* No self acls. Check if we have any acls for the given ACL type 
116                  */
117                                 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
118                                 if(count($deps)) return TRUE;
119                         }
120                 }
121                 return (FALSE);
122         }
125         function gen_headlines()
126         {
127                 $ret = array();
128                 if(count($this->headlines) == 0){
129                         foreach($this->config->data['MENU'] as $headline => $plugins){
130                                 foreach( $plugins as $id => $plug){
131                                         if (plugin_available($plug['CLASS'])){
132                                                 $attrs = (get_class_vars($plug['CLASS']));
133                                                 $ret[$id]['HEADLINE'] = $headline;
134                                                 $ret[$id]['NAME']         = $attrs['plHeadline'];
135                                         }
136                                 }
137                         }
138                         $this->headlines = $ret;
139                 }
140                 return($this->headlines);
141         }
143         function gen_menu()
144         {
145                 if ($this->menu == ""){
146                         $first= TRUE;
147                         $cfg= $this->config->data['MENU'];
149                         /* Parse headlines */
150                         foreach ($cfg as $headline => $plug){
151                                 if ($first){
152                                         $style= "";
153                                         $first= FALSE;
154                                 } else {
155                                         $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
156                                 }
157                                 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
158                                 $entries= "";
159                                 $this->menuparts[_($headline)]= array();
161                                 /* Parse sub-plugins */
162                                 foreach ($plug as $info){
164                                         /* Read information from class variable */
165                                         if (!isset($info['CLASS'])){
166                                                 msg_dialog::display(
167                                                                 _("Configuration error"), 
168                                                                 _("The configuration format has changed. Please re-run setup!"), 
169                                                                 FATAL_ERROR_DIALOG);
170                                                 exit();
171                                         }
172                                         if (!plugin_available($info['CLASS'])){
173                                                 continue;
174                                         }
175                                         $vars= get_class_vars($info['CLASS']);
176                                         $plHeadline= $vars['plHeadline'];
177                                         $plDescription= $vars['plDescription'];
179                                         $index= $this->get_index($info['CLASS']);
180                                         $href= "main.php?plug=$index&amp;reset=1";
182                                         if(!$vars){
183                                                 $plHeadline     = _("Unknown");
184                                                 $plDescription  = _("Unknown");
185                                                 $href= "main.php?reset=1";
186                                         }
188                                         if ($this->check_access($info['ACL'])){
190             $this->allowed_plugins[$index] = $index;
191  
192                                                 $entries= $entries."<p class=\"menuitem\" ".
193                                                         "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
194                                                 if(session::get('js')){
195                                                         $entries.= _($plHeadline)."</p>\n";
196                                                 } else {
197                                                         $entries.= "<a class=\"menuitem\" ".
198                                                         "href=\"$href\">".
199                                                         _($plHeadline)."</a></p>\n";
200                                                 }
202                                                 if(!session::is_set('maxC')){
203                                                         session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
204                                                 }
205                                         }
206                                 }
208                                 /* Append to menu */
209                                 if ($entries != ""){
210                                         $this->menu.= $menu.$entries;
211                                 }
212                         }
214                 }
216                 /* Write menu output */
217                 return ($this->menu);
218         }
220         function gen_current()
221         {
222                 /* Do we have a current value? */
223                 if ($this->current == ""){
224                         $tmp= array_keys($this->menuparts);
225                         $this->current= $tmp[0];
226                 }
228                 /* Fill current array */
229                 $result= "<table width=\"100%\" summary=\"\">";
230                 $count= 0;
231                 foreach ($this->menuparts[$this->current] as $entry){
232                         if ($count == 2){
233                                 $result.= "</tr>";
234                                 $count= 0;
235                         }
236                         if ($count == 0){
237                                 $result.= "<tr>";
238                         }
239                         $result.= "<td>$entry</td>";
240                         $count++;
241                 }
243                 /* Add missing cell? */
244                 if ($count == 1){
245                         $result.= "<td>&nbsp;</td>";
246                 }
248                 $result.= "</table>";
249                 return $result;
250         }
253         function show_iconmenu()
254         {
255                 global $class_mapping;
256                 if ($this->iconmenu == ""){
257                         $cfg= $this->config->data['MENU'];
259                         if (isset($this->config->current['ICONSIZE'])){
260                                 list($x, $y)= split("x", $this->config->get_cfg_value("iconsize"));
261                                 $isize= "width=\"$x\" height=\"$y\"";
262                         } else {
263                                 $isize= "";
264                         }
266                         /* Parse headlines */
267                         foreach ($cfg as $headline => $plug){
268                                 $col= 1;
269                                 $menu= "<h1 class=\"menuheader\">".
270                                         _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
271                                         "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
272                                 $entries= "";
274                                 foreach ($plug as $info){
276                                         if (!plugin_available($info['CLASS'])){
277                                                 continue;
278                                         }
280                                         /* Read information from class variable */
281                                         $vars= get_class_vars($info['CLASS']);
282                                         $plHeadline= $vars['plHeadline'];
283                                         $plDescription= $vars['plDescription'];
284           if (isset($vars['plIcon'])){
285                                         $plIcon= $vars['plIcon'];
286           } else {
287                                         $plIcon= "plugin.png";
288           }
290                                         $index= $this->get_index($info['CLASS']);
292                                         $href = "main.php?plug=".$index."&amp;reset=1";
294                                         /* Check if class is available. If the class doesn't exists display error symbol 
295                                                 to avoid that a user clicks on a non existing plugin  */
296                                         if(!$vars){
297                                                 $plHeadline = $plDescription = _("Unknown");
298                                                 $info['ICON'] = "error.png";
299                                                 $href="main.php?reset=1";
300                                         }
303                                         if ($this->check_access($info['ACL'])){
305                                                 /* Load icon */
306                                                 if (isset($info['ICON'])){
307                                                         $image= get_template_path('images/'.$info['ICON']);
308                                                 } else {
309               if(!preg_match("/\//",$plIcon)){
310                                                         $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
311               }else{
312                 $image = $plIcon; 
313               }
314                                                 }
315                                                 if ($col > 5){
316                                                         $entries= $entries."</tr><tr>";
317                                                         $col = 1;
318                                                 }
319                                                 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
320                                                         ">";
321                                                         if(session::get('js')){
322                                                                 $entries.= "<img $isize border=0 align=middle src=\"$image".
323                                                                         "\" alt=\"*\">&nbsp;".
324                                                                         _($plHeadline);
325                                                         } else {
326                                                                 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
327                                                                         "<img $isize border=0 align=middle src=\"$image".
328                                                                         "\" alt=\"*\">&nbsp;".
329                                                                         _($plHeadline)."</a>";
330                                                         }
331                                                         $entries.= "</td>\n";
332                                                 $col++ ;
334                                         }
335                                 }
337                                 /* Append to menu */
338                                 if ($entries != ""){
339                                         $this->iconmenu.= $menu.$entries;
341                                         /* Fill up remaining columns */
342                                         if ($col != 1){
343                                                 $col--;
344                                                 while ($col % 5){
345                                                         $this->iconmenu= $this->iconmenu.
346                                                                 "<td style=\"width:20%\">&nbsp;</td>\n";
347                                                         $col++;
348                                                 }
349                                         }
351                                         /* close table */
352                                         $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
353                                 }
354                         }
356                 }
358                 /* Write menu output */
359                 return ($this->iconmenu);
360         }
362         function get_path($index)
363         {
364                 if(!isset($this->dirlist[$index])){
365                         return ("");
366                 }
367                 return ("../".$this->dirlist[$index]);
368         }
370         function get_index($class)
371         {
372                 /* Search for plugin index (id), identify entry by path && class */
373                 $data = $this->config->data['MENU'];
374                 foreach($data as $section => $plugins){
375                         foreach($plugins as $key => $plugin)    {
376                                 if($plugin['CLASS'] == $class){
377                                         return($key);
378                                 }
379                         }
380                 }
382                 /* Nothing */
383                 return (0);
385         }
387   /*! \brief  This function checks if we are allowed to view the plugin with the given id 
388       @param  $plug_id  Integer  The ID of the plugin.
389       @return Boolean   TRUE if we are allowed to view the plugin else FASLE
390    */
391   function plugin_access_allowed($plug_id)
392   {
393     return(isset($this->allowed_plugins[$plug_id]));
394   }
397   /*! \brief  Force the menu to be recreated 
398    */
399   function reset_menus()
400   {
401     $this->menu = "";
402     $this->iconmenu ="";
403   }
406 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
407 ?>