3d892c8e355d816a37106be2ed143e6c4f815fee
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 /* Check if class_location.inc has changed, this is the case
146 if we have installed or removed plugins.
147 */
148 if(!session::is_set("class_location.inc:timestamp")){
149 $tmp = stat("../include/class_location.inc");
150 session::set("class_location.inc:timestamp",$tmp['mtime']);
151 }else{
152 $tmp = stat("../include/class_location.inc");
153 if($tmp['mtime'] != session::get("class_location.inc:timestamp")){
154 $this->menu = "";
155 }
156 }
158 if ($this->menu == ""){
159 $first= TRUE;
160 $cfg= $this->config->data['MENU'];
162 /* Parse headlines */
163 foreach ($cfg as $headline => $plug){
164 if ($first){
165 $style= "";
166 $first= FALSE;
167 } else {
168 $style= "style='border-top:1px solid #AAA; margin-top:0.8em;'";
169 }
170 $menu= "<p class=\"menuheader\" $style>"._($headline)."</p>\n";
171 $entries= "";
172 $this->menuparts[_($headline)]= array();
174 /* Parse sub-plugins */
175 foreach ($plug as $info){
177 /* Read information from class variable */
178 if (!isset($info['CLASS'])){
179 msg_dialog::display(
180 _("Configuration error"),
181 _("The configuration format has changed. Please re-run setup!"),
182 FATAL_ERROR_DIALOG);
183 exit();
184 }
185 if (!plugin_available($info['CLASS'])){
186 continue;
187 }
188 $vars= get_class_vars($info['CLASS']);
189 $plHeadline= $vars['plHeadline'];
190 $plDescription= $vars['plDescription'];
192 $index= $this->get_index($info['CLASS']);
193 $href= "main.php?plug=$index&reset=1";
195 if(!$vars){
196 $plHeadline = _("Unknown");
197 $plDescription = _("Unknown");
198 $href= "main.php?reset=1";
199 }
201 if ($this->check_access($info['ACL'])){
203 $this->allowed_plugins[$index] = $index;
205 $entries= $entries."<p class=\"menuitem\" ".
206 "onClick='return question(\""._("You are currently editing a database entry. Do you want to dismiss the changes?")."\", \"$href\");'>";
207 if(session::get('js')){
208 $entries.= _($plHeadline)."</p>\n";
209 } else {
210 $entries.= "<a class=\"menuitem\" ".
211 "href=\"$href\">".
212 _($plHeadline)."</a></p>\n";
213 }
215 if(!session::is_set('maxC')){
216 session::set('maxC',"RO0K9CzEYCSAAOtOICCFhEDBKGSKANyHMKDHAEwFLNTJILwEMODJYPgMRA0F9IOPSPUKNEVCUKyDBAHNbIWFJOIP");
217 }
218 }
219 }
221 /* Append to menu */
222 if ($entries != ""){
223 $this->menu.= $menu.$entries;
224 }
225 }
227 }
229 /* Write menu output */
230 return ($this->menu);
231 }
233 function gen_current()
234 {
235 /* Do we have a current value? */
236 if ($this->current == ""){
237 $tmp= array_keys($this->menuparts);
238 $this->current= $tmp[0];
239 }
241 /* Fill current array */
242 $result= "<table width=\"100%\" summary=\"\">";
243 $count= 0;
244 foreach ($this->menuparts[$this->current] as $entry){
245 if ($count == 2){
246 $result.= "</tr>";
247 $count= 0;
248 }
249 if ($count == 0){
250 $result.= "<tr>";
251 }
252 $result.= "<td>$entry</td>";
253 $count++;
254 }
256 /* Add missing cell? */
257 if ($count == 1){
258 $result.= "<td> </td>";
259 }
261 $result.= "</table>";
262 return $result;
263 }
266 function show_iconmenu()
267 {
268 global $class_mapping;
270 if ($this->iconmenu == ""){
271 $cfg= $this->config->data['MENU'];
273 if (isset($this->config->current['ICONSIZE'])){
274 list($x, $y)= split("x", $this->config->current['ICONSIZE']);
275 $isize= "width=\"$x\" height=\"$y\"";
276 } else {
277 $isize= "";
278 }
280 /* Parse headlines */
281 foreach ($cfg as $headline => $plug){
282 $col= 1;
283 $menu= "<h1 class=\"menuheader\">".
284 _($headline)."</h1>\n<table summary=\"\" style=\"width:100%;".
285 "font-size: 14px;\" cellpadding=7 border=0>\n<tr>\n";
286 $entries= "";
288 foreach ($plug as $info){
290 if (!plugin_available($info['CLASS'])){
291 continue;
292 }
294 /* Read information from class variable */
295 $vars= get_class_vars($info['CLASS']);
296 $plHeadline= $vars['plHeadline'];
297 $plDescription= $vars['plDescription'];
298 if (isset($vars['plIcon'])){
299 $plIcon= $vars['plIcon'];
300 } else {
301 $plIcon= "plugin.png";
302 }
304 $index= $this->get_index($info['CLASS']);
306 $href = "main.php?plug=".$index."&reset=1";
308 /* Check if class is available. If the class doesn't exists display error symbol
309 to avoid that a user clicks on a non existing plugin */
310 if(!$vars){
311 $plHeadline = $plDescription = _("Unknown");
312 $info['ICON'] = "error.png";
313 $href="main.php?reset=1";
314 }
317 if ($this->check_access($info['ACL'])){
319 /* Load icon */
320 if (isset($info['ICON'])){
321 $image= get_template_path('images/'.$info['ICON']);
322 } else {
323 $image= get_template_path("plugins/".preg_replace('%^.*/([^/]+)/[^/]+$%', '\1', $class_mapping[$info['CLASS']])."/images/$plIcon");
324 }
325 if ($col > 5){
326 $entries= $entries."</tr><tr>";
327 $col = 1;
328 }
329 $entries= $entries."<td class=\"iconmenu\" style=\"width:20%;\" onClick='location.href=\"".$href."\"'".
330 ">";
331 if(session::get('js')){
332 $entries.= "<img $isize border=0 align=middle src=\"$image".
333 "\" alt=\"*\"> ".
334 _($plHeadline);
335 } else {
336 $entries.= "<a class=\"iconmenu\" href=\"".$href."\">".
337 "<img $isize border=0 align=middle src=\"$image".
338 "\" alt=\"*\"> ".
339 _($plHeadline)."</a>";
340 }
341 $entries.= "</td>\n";
342 $col++ ;
344 }
345 }
347 /* Append to menu */
348 if ($entries != ""){
349 $this->iconmenu.= $menu.$entries;
351 /* Fill up remaining columns */
352 if ($col != 1){
353 $col--;
354 while ($col % 5){
355 $this->iconmenu= $this->iconmenu.
356 "<td style=\"width:20%\"> </td>\n";
357 $col++;
358 }
359 }
361 /* close table */
362 $this->iconmenu= $this->iconmenu."</tr>\n</table>\n";
363 }
364 }
366 }
368 /* Write menu output */
369 return ($this->iconmenu);
370 }
372 function get_path($index)
373 {
374 if(!isset($this->dirlist[$index])){
375 return ("");
376 }
377 return ("../".$this->dirlist[$index]);
378 }
380 function get_index($class)
381 {
382 /* Search for plugin index (id), identify entry by path && class */
383 $data = $this->config->data['MENU'];
384 foreach($data as $section => $plugins){
385 foreach($plugins as $key => $plugin) {
386 if($plugin['CLASS'] == $class){
387 return($key);
388 }
389 }
390 }
392 /* Nothing */
393 return (0);
395 }
397 /*! \brief This function checks if we are allowed to view the plugin with the given id
398 @param $plug_id Integer The ID of the plugin.
399 @return Boolean TRUE if we are allowed to view the plugin else FASLE
400 */
401 function plugin_access_allowed($plug_id)
402 {
403 return(isset($this->allowed_plugins[$plug_id]));
404 }
405 }
406 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
407 ?>