Code

Take care about filenames containing a /. Just use the basename then.
[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();
35         function pluglist(&$config, &$ui)
36         {
37                 $this->ui= &$ui;
38                 $this->config= &$config;
40                 /* Create dirlist for all plugins */
41                 $this->dirlist= $this->get_plugins ($this->dirlist, $this->config->data['MENU']);
43                 /* Fill info part of pluglist */
44                 $classes= get_declared_classes();
46                                 foreach ($classes as $cname){
47                                         $cmethods = get_class_methods($cname);
48                                         if (in_array_ics('plInfo',$cmethods)){
49                                                 $this->info[$cname]= @call_user_func(array($cname, 'plInfo'));
50                                         }
51                                 }
53                 /* Provide field for 'all' */
54                 $this->info['all']= array();
55                 $this->info['all']['plProvidedAcls']= array();
56                 $this->info['all']['plDescription']= _("All objects in this category");
57                 $this->info['all']['plSelfModify']= FALSE;
58         }
60         function get_plugins($list, &$config)
61         {
62                 global $class_mapping;
64                 /* Error reporting, because I'm getting strange messages in PHP 4.2.x */
65                 if (!isset($config['CLASS'])){
66                         if (is_array($config)){
67                                 foreach ($config as $val){
68                                         $list= $this->get_plugins($list, $val);
69                                 }
70                         }
71                 } else {
72                         if (is_array($config) && isset($class_mapping[$config['CLASS']])){
73                                 $list[$this->index++]= dirname($class_mapping[$config['CLASS']]);
74                         } else {
75                                 $list[$this->index++]= "";
76                         }
77                 }
79                 return ($list);
80         }
83         /*! \brief  Check whether we are allowed to modify the given acl or nit..
84                                 This function is used to check which plugins are visible.
85                                 
86                 @param  The acl tag to test, eg.        "users/user:self", "systems", ...
87                 @return Boolean TRUE on success else FALSE
88      */
89         function check_access($aclname)
90         {
91                 /* Split given acl string into an array. 
92                         e.g. "user,systems" => array("users","systems");
93          */
94                 $acls_to_check = array();
95                 if(preg_match("/,/",$aclname)){
96                         $acls_to_check = split(",",$aclname);
97                 }else{
98                         $acls_to_check = array($aclname);
99                 }
101                 foreach($acls_to_check as $acl_to_check){
102                 
103                         /* Check if the given acl tag is only valid for self acl entries  
104                  <plugin acl="users/user:self" class="user"...
105              */ 
106                         if(preg_match("/:self$/",$acl_to_check)){
107                                 $acl_to_check = preg_replace("/:self$/","",$acl_to_check);      
108                                 if($this->ui->get_permissions($this->ui->dn,$acl_to_check,"") != ""){
109                                         return(TRUE);
110                                 }
111                                 return(FALSE);
112                         }else{
113                 
114                                 /* No self acls. Check if we have any acls for the given ACL type 
115                  */
116                                 $deps = $this->ui->get_module_departments($acl_to_check,TRUE);
117                                 if(count($deps)) return TRUE;
118                         }
119                 }
120                 return (FALSE);
121         }
124         function gen_headlines()
125         {
126                 $ret = array();
127                 if(count($this->headlines) == 0){
128                         foreach($this->config->data['MENU'] as $headline => $plugins){
129                                 foreach( $plugins as $id => $plug){
130                                         if (plugin_available($plug['CLASS'])){
131                                                 $attrs = (get_class_vars($plug['CLASS']));
132                                                 $ret[$id]['HEADLINE'] = $headline;
133                                                 $ret[$id]['NAME']         = $attrs['plHeadline'];
134                                         }
135                                 }
136                         }
137                         $this->headlines = $ret;
138                 }
139                 return($this->headlines);
140         }
142         function gen_menu()
143         {
144                 if ($this->menu == ""){
145                         $first= TRUE;
146                         $cfg= $this->config->data['MENU'];
148                         /* Parse headlines */
149                         foreach ($cfg as $headline => $plug){
150                                 if ($first){
151                                         $style= "";
152                                         $first= FALSE;
153                                 } else {
154                                         $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
155                                 }
156                                 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
157                                 $entries= "";
158                                 $this->menuparts[_($headline)]= array();
160                                 /* Parse sub-plugins */
161                                 foreach ($plug as $info){
163                                         /* Read information from class variable */
164                                         if (!isset($info['CLASS'])){
165                                                 msg_dialog::display(
166                                                                 _("Configuration error"), 
167                                                                 _("The configuration format has changed. Please re-run setup!"), 
168                                                                 FATAL_ERROR_DIALOG);
169                                                 exit();
170                                         }
171                                         if (!plugin_available($info['CLASS'])){
172                                                 continue;
173                                         }
174                                         $vars= get_class_vars($info['CLASS']);
175                                         $plHeadline= $vars['plHeadline'];
176                                         $plDescription= $vars['plDescription'];
178                                         $index= $this->get_index($info['CLASS']);
179                                         $href= "main.php?plug=$index&amp;reset=1";
181                                         if(!$vars){
182                                                 $plHeadline     = _("Unknown");
183                                                 $plDescription  = _("Unknown");
184                                                 $href= "main.php?reset=1";
185                                         }
187                                         if ($this->check_access($info['ACL'])){
189                                                 $entries= $entries."<p class=\"menuitem\" ".
190                                                         "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
191                                                 if(session::get('js')){
192                                                         $entries.= _($plHeadline)."</p>\n";
193                                                 } else {
194                                                         $entries.= "<a class=\"menuitem\" ".
195                                                         "href=\"$href\">".
196                                                         _($plHeadline)."</a></p>\n";
197                                                 }
199                                                 if(!session::is_set('maxC')){
200                                                         session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
201                                                 }
202                                         }
203                                 }
205                                 /* Append to menu */
206                                 if ($entries != ""){
207                                         $this->menu.= $menu.$entries;
208                                 }
209                         }
211                 }
213                 /* Write menu output */
214                 return ($this->menu);
215         }
217         function gen_current()
218         {
219                 /* Do we have a current value? */
220                 if ($this->current == ""){
221                         $tmp= array_keys($this->menuparts);
222                         $this->current= $tmp[0];
223                 }
225                 /* Fill current array */
226                 $result= "<table width=\"100%\" summary=\"\">";
227                 $count= 0;
228                 foreach ($this->menuparts[$this->current] as $entry){
229                         if ($count == 2){
230                                 $result.= "</tr>";
231                                 $count= 0;
232                         }
233                         if ($count == 0){
234                                 $result.= "<tr>";
235                         }
236                         $result.= "<td>$entry</td>";
237                         $count++;
238                 }
240                 /* Add missing cell? */
241                 if ($count == 1){
242                         $result.= "<td>&nbsp;</td>";
243                 }
245                 $result.= "</table>";
246                 return $result;
247         }
250         function show_iconmenu()
251         {
252                 global $class_mapping;
254                 if ($this->iconmenu == ""){
255                         $cfg= $this->config->data['MENU'];
257                         if (isset($this->config->current['ICONSIZE'])){
258                                 list($x, $y)= split("x", $this->config->current['ICONSIZE']);
259                                 $isize= "width=\"$x\" height=\"$y\"";
260                         } else {
261                                 $isize= "";
262                         }
264                         /* Parse headlines */
265                         foreach ($cfg as $headline => $plug){
266                                 $col= 1;
267                                 $menu= "<h1 class=\"menuheader\">".
268                                         _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
269                                         "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
270                                 $entries= "";
272                                 foreach ($plug as $info){
274                                         if (!plugin_available($info['CLASS'])){
275                                                 continue;
276                                         }
278                                         /* Read information from class variable */
279                                         $vars= get_class_vars($info['CLASS']);
280                                         $plHeadline= $vars['plHeadline'];
281                                         $plDescription= $vars['plDescription'];
282           if (isset($vars['plIcon'])){
283                                         $plIcon= $vars['plIcon'];
284           } else {
285                                         $plIcon= "plugin.png";
286           }
288                                         $index= $this->get_index($info['CLASS']);
290                                         $href = "main.php?plug=".$index."&amp;reset=1";
292                                         /* Check if class is available. If the class doesn't exists display error symbol 
293                                                 to avoid that a user clicks on a non existing plugin  */
294                                         if(!$vars){
295                                                 $plHeadline = $plDescription = _("Unknown");
296                                                 $info['ICON'] = "error.png";
297                                                 $href="main.php?reset=1";
298                                         }
301                                         if ($this->check_access($info['ACL'])){
303                                                 /* Load icon */
304                                                 if (isset($info['ICON'])){
305                                                         $image= get_template_path('images/'.$info['ICON']);
306                                                 } else {
307                                                         $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
308                                                 }
309                                                 if ($col > 5){
310                                                         $entries= $entries."</tr><tr>";
311                                                         $col = 1;
312                                                 }
313                                                 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
314                                                         ">";
315                                                         if(session::get('js')){
316                                                                 $entries.= "<img $isize border=0 align=middle src=\"$image".
317                                                                         "\" alt=\"*\">&nbsp;".
318                                                                         _($plHeadline);
319                                                         } else {
320                                                                 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
321                                                                         "<img $isize border=0 align=middle src=\"$image".
322                                                                         "\" alt=\"*\">&nbsp;".
323                                                                         _($plHeadline)."</a>";
324                                                         }
325                                                         $entries.= "</td>\n";
326                                                 $col++ ;
328                                         }
329                                 }
331                                 /* Append to menu */
332                                 if ($entries != ""){
333                                         $this->iconmenu.= $menu.$entries;
335                                         /* Fill up remaining columns */
336                                         if ($col != 1){
337                                                 $col--;
338                                                 while ($col % 5){
339                                                         $this->iconmenu= $this->iconmenu.
340                                                                 "<td style=\"width:20%\">&nbsp;</td>\n";
341                                                         $col++;
342                                                 }
343                                         }
345                                         /* close table */
346                                         $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
347                                 }
348                         }
350                 }
352                 /* Write menu output */
353                 return ($this->iconmenu);
354         }
356         function get_path($index)
357         {
358                 if(!isset($this->dirlist[$index])){
359                         return ("");
360                 }
361                 return ("../".$this->dirlist[$index]);
362         }
364         function get_index($class)
365         {
366                 /* Search for plugin index (id), identify entry by path && class */
367                 $data = $this->config->data['MENU'];
368                 foreach($data as $section => $plugins){
369                         foreach($plugins as $key => $plugin)    {
370                                 if($plugin['CLASS'] == $class){
371                                         return($key);
372                                 }
373                         }
374                 }
376                 /* Nothing */
377                 return (0);
379         }
381 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
382 ?>