8c47f218a97be8d9c53e26e529363e69a9588056
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.
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){
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{
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&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;
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> </td>";
246 }
248 $result.= "</table>";
249 return $result;
250 }
253 function show_iconmenu()
254 {
255 global $class_mapping;
257 if ($this->iconmenu == ""){
258 $cfg= $this->config->data['MENU'];
260 if (isset($this->config->current['ICONSIZE'])){
261 list($x, $y)= split("x", $this->config->current['ICONSIZE']);
262 $isize= "width=\"$x\" height=\"$y\"";
263 } else {
264 $isize= "";
265 }
267 /* Parse headlines */
268 foreach ($cfg as $headline => $plug){
269 $col= 1;
270 $menu= "<h1 class=\"menuheader\">".
271 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
272 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
273 $entries= "";
275 foreach ($plug as $info){
277 if (!plugin_available($info['CLASS'])){
278 continue;
279 }
281 /* Read information from class variable */
282 $vars= get_class_vars($info['CLASS']);
283 $plHeadline= $vars['plHeadline'];
284 $plDescription= $vars['plDescription'];
285 if (isset($vars['plIcon'])){
286 $plIcon= $vars['plIcon'];
287 } else {
288 $plIcon= "plugin.png";
289 }
291 $index= $this->get_index($info['CLASS']);
293 $href = "main.php?plug=".$index."&reset=1";
295 /* Check if class is available. If the class doesn't exists display error symbol
296 to avoid that a user clicks on a non existing plugin */
297 if(!$vars){
298 $plHeadline = $plDescription = _("Unknown");
299 $info['ICON'] = "error.png";
300 $href="main.php?reset=1";
301 }
304 if ($this->check_access($info['ACL'])){
306 /* Load icon */
307 if (isset($info['ICON'])){
308 $image= get_template_path('images/'.$info['ICON']);
309 } else {
310 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
311 }
312 if ($col > 5){
313 $entries= $entries."</tr><tr>";
314 $col = 1;
315 }
316 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
317 ">";
318 if(session::get('js')){
319 $entries.= "<img $isize border=0 align=middle src=\"$image".
320 "\" alt=\"*\"> ".
321 _($plHeadline);
322 } else {
323 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
324 "<img $isize border=0 align=middle src=\"$image".
325 "\" alt=\"*\"> ".
326 _($plHeadline)."</a>";
327 }
328 $entries.= "</td>\n";
329 $col++ ;
331 }
332 }
334 /* Append to menu */
335 if ($entries != ""){
336 $this->iconmenu.= $menu.$entries;
338 /* Fill up remaining columns */
339 if ($col != 1){
340 $col--;
341 while ($col % 5){
342 $this->iconmenu= $this->iconmenu.
343 "<td style=\"width:20%\"> </td>\n";
344 $col++;
345 }
346 }
348 /* close table */
349 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
350 }
351 }
353 }
355 /* Write menu output */
356 return ($this->iconmenu);
357 }
359 function get_path($index)
360 {
361 if(!isset($this->dirlist[$index])){
362 return ("");
363 }
364 return ("../".$this->dirlist[$index]);
365 }
367 function get_index($class)
368 {
369 /* Search for plugin index (id), identify entry by path && class */
370 $data = $this->config->data['MENU'];
371 foreach($data as $section => $plugins){
372 foreach($plugins as $key => $plugin) {
373 if($plugin['CLASS'] == $class){
374 return($key);
375 }
376 }
377 }
379 /* Nothing */
380 return (0);
382 }
384 /*! \brief This function checks if we are allowed to view the plugin with the given id
385 @param $plug_id Integer The ID of the plugin.
386 @return Boolean TRUE if we are allowed to view the plugin else FASLE
387 */
388 function plugin_access_allowed($plug_id)
389 {
390 return(isset($this->allowed_plugins[$plug_id]));
391 }
392 }
393 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
394 ?>