JavaScript-Menü: Der Schlüssel 'action' soll nicht als Parameter in den Link aufgenom...
[kivitendo-erp.git] / SL / Menu.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #=====================================================================
30 #
31 # routines for menu items
32 #
33 #=====================================================================
34
35 package Menu;
36
37 use SL::Auth;
38 use SL::Inifile;
39
40 sub new {
41   $main::lxdebug->enter_sub();
42
43   my ($type, $menufile) = @_;
44
45   my $self    = {};
46   my $inifile = Inifile->new($menufile);
47
48   map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
49
50   bless $self, $type;
51
52   $self->set_access();
53
54   $main::lxdebug->leave_sub();
55
56   return $self;
57 }
58
59 sub menuitem {
60   $main::lxdebug->enter_sub();
61
62   my ($self, $myconfig, $form, $item) = @_;
63
64   my $module = $form->{script};
65   my $action = "section_menu";
66   my $target = "";
67
68   if ($self->{$item}{module}) {
69     $module = $self->{$item}{module};
70   }
71   if ($self->{$item}{action}) {
72     $action = $self->{$item}{action};
73   }
74   if ($self->{$item}{target}) {
75     $target = $self->{$item}{target};
76   }
77
78   my $level = $form->escape($item);
79
80   my $str = qq|<a style="vertical-align:top" href=$module?action=$action&level=$level|;
81
82   my @vars = qw(module action target href);
83
84   if ($self->{$item}{href}) {
85     $str  = qq|<a href=$self->{$item}{href}|;
86     @vars = qw(module target href);
87   }
88
89   map { delete $self->{$item}{$_} } @vars;
90
91   # add other params
92   foreach my $key (keys %{ $self->{$item} }) {
93     $str .= "&" . $form->escape($key, 1) . "=";
94     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
95     $value = $myconfig->{$value} . "/$conf" if ($conf);
96     $str .= $form->escape($value, 1);
97   }
98
99   if ($target) {
100     $str .= qq| target=$target|;
101   }
102
103   $str .= ">";
104
105   $main::lxdebug->leave_sub();
106
107   return $str;
108 }
109
110 sub menuitem_new {
111   $main::lxdebug->enter_sub();
112
113   my ($self, $name, $item) = @_;
114
115   my $form        = $main::form;
116
117   my $module      = $self->{$name}->{module} || $form->{script};
118   my $action      = $self->{$name}->{action};
119
120   $item->{target} = $self->{$name}->{target} || "main_window";
121   $item->{href}   = $self->{$name}->{href}   || "${module}?action=" . $form->escape($action);
122
123   my @vars = qw(module target href);
124   push @vars, 'action' unless ($self->{$name}->{href});
125
126   map { delete $self->{$name}{$_} } @vars;
127
128   # add other params
129   foreach my $key (keys %{ $self->{$name} }) {
130     my ($value, $conf)  = split(m/=/, $self->{$name}->{$key}, 2);
131     $value              = $myconfig->{$value} . "/$conf" if ($conf);
132     $item->{href}      .= "&" . $form->escape($key) . "=" . $form->escape($value);
133   }
134
135   $main::lxdebug->leave_sub();
136 }
137
138 sub menuitem_v3 {
139   $main::lxdebug->enter_sub();
140
141   my ($self, $myconfig, $form, $item, $other) = @_;
142
143   my $module = $form->{script};
144   my $action = "section_menu";
145   my $target = "";
146
147   if ($self->{$item}{module}) {
148     $module = $self->{$item}{module};
149   }
150   if ($self->{$item}{action}) {
151     $action = $self->{$item}{action};
152   }
153   if ($self->{$item}{target}) {
154     $target = $self->{$item}{target};
155   }
156
157   my $level = $form->escape($item);
158
159   my $str = qq|<a href="$module?action=| . $form->escape($action) . qq|&level=| . $form->escape($level);
160
161   my @vars = qw(module action target href);
162
163   if ($self->{$item}{href}) {
164     $str  = qq|<a href=$self->{$item}{href}|;
165     @vars = qw(module target href);
166   }
167
168   map { delete $self->{$item}{$_} } @vars;
169
170   # add other params
171   foreach my $key (keys %{ $self->{$item} }) {
172     $str .= "&" . $form->escape($key, 1) . "=";
173     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
174     $value = $myconfig->{$value} . "/$conf" if ($conf);
175     $str .= $form->escape($value, 1);
176   }
177
178   $str .= '"';
179
180   if ($target) {
181     $str .= qq| target="| . $form->quote($target) . qq|"|;
182   }
183
184   if ($other) {
185     foreach my $key (keys(%{$other})) {
186       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
187     }
188   }
189
190   $str .= ">";
191
192   $main::lxdebug->leave_sub();
193
194   return $str;
195 }
196
197 sub menuitem_XML {
198   $main::lxdebug->enter_sub();
199
200   my ($self, $myconfig, $form, $item, $other) = @_;
201
202   my $module = $form->{script};
203   my $action = "section_menu";
204   my $target = "";
205
206   if ($self->{$item}{module}) {
207     $module = $self->{$item}{module};
208   }
209   if ($self->{$item}{action}) {
210     $action = $self->{$item}{action};
211   }
212   if ($self->{$item}{target}) {
213     $target = $self->{$item}{target};
214   }
215
216   my $level = $form->escape($item);
217
218   my $str = qq| link="$module?action=| . $form->escape($action) .
219     qq|&amp;level=| . $form->escape($level);
220
221   my @vars = qw(module action target href);
222
223   if ($self->{$item}{href}) {
224     $str  = qq| link=$self->{$item}{href}|;
225     @vars = qw(module target href);
226   }
227
228   map { delete $self->{$item}{$_} } @vars;
229
230   # add other params
231   foreach my $key (keys %{ $self->{$item} }) {
232     $str .= "&amp;" . $form->escape($key, 1) . "=";
233     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
234     $value = $myconfig->{$value} . "/$conf" if ($conf);
235     $str .= $form->escape($value, 1);
236   }
237
238   $str .= '"';
239
240
241
242   if ($other) {
243     foreach my $key (keys(%{$other})) {
244       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
245     }
246   }
247
248
249   $main::lxdebug->leave_sub();
250
251   return $str;
252 }
253
254 sub access_control {
255   $main::lxdebug->enter_sub(2);
256
257   my ($self, $myconfig, $menulevel) = @_;
258
259   my @menu = ();
260
261   if ($menulevel eq "") {
262     @menu = grep { !/--/ } @{ $self->{ORDER} };
263   } else {
264     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
265   }
266
267   my @a    = split(/;/, $myconfig->{acs});
268   my $excl = ();
269
270   # remove --AR, --AP from array
271   grep { ($a, $b) = split(/--/); s/--$a$//; } @a;
272
273   map { $excl{$_} = 1 } @a;
274
275   @a = ();
276   map { push @a, $_ unless $excl{$_} } (@menu);
277
278   $main::lxdebug->leave_sub(2);
279
280   return @a;
281 }
282
283 sub generate_acl {
284   my ($self, $menulevel, $hash) = @_;
285
286   my @items = $self->access_control(\%main::myconfig, $menulevel);
287
288   $menulevel =~ s/[^A-Za-z_\/\.\+\-]/_/g;
289   $hash->{"access_" . lc($menulevel)} = 1 if ($menulevel);
290
291   foreach my $item (@items) {
292     $self->generate_acl($item, $hash); #unless ($menulevel);
293   }
294 }
295
296 sub parse_access_string {
297   my $self   = shift;
298   my $key    = shift;
299   my $access = shift;
300
301   my @stack;
302   my $cur_ary = [];
303
304   push @stack, $cur_ary;
305
306   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
307     my $token = $1;
308     substr($access, 0, length($1)) = "";
309
310     next if ($token =~ /\s/);
311
312     if ($token eq "(") {
313       my $new_cur_ary = [];
314       push @stack, $new_cur_ary;
315       push @{$cur_ary}, $new_cur_ary;
316       $cur_ary = $new_cur_ary;
317
318     } elsif ($token eq ")") {
319       pop @stack;
320       if (!@stack) {
321         $main::form->error("Error in menu.ini for entry ${key}: missing '('");
322       }
323       $cur_ary = $stack[-1];
324
325     } elsif (($token eq "|") || ($token eq "&")) {
326       push @{$cur_ary}, $token;
327
328     } else {
329       push @{$cur_ary}, $main::auth->check_right($main::form->{login}, $token, 1);
330     }
331   }
332
333   if ($access) {
334     $main::form->error("Error in menu.ini for entry ${name}: unrecognized token at the start of '$access'\n");
335   }
336
337   if (1 < scalar @stack) {
338     $main::form->error("Error in menu.ini for entry ${name}: Missing ')'\n");
339   }
340
341   return SL::Auth::evaluate_rights_ary($stack[0]);
342 }
343
344 sub set_access {
345   my $self = shift;
346
347   my $key;
348
349   foreach $key (@{ $self->{ORDER} }) {
350     my $entry = $self->{$key};
351
352     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
353     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
354     $entry->{NUM_VISIBLE_CHILDREN} = 0;
355
356     if ($key =~ m/--/) {
357       my $parent = $key;
358       substr($parent, rindex($parent, '--')) = '';
359       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
360     }
361
362     $entry->{VISIBLE} = $entry->{GRANTED};
363   }
364
365   foreach $key (reverse @{ $self->{ORDER} }) {
366     my $entry = $self->{$key};
367
368     if ($entry->{IS_MENU}) {
369       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
370     }
371
372     next if (($key !~ m/--/) || !$entry->{VISIBLE});
373
374     my $parent = $key;
375     substr($parent, rindex($parent, '--')) = '';
376     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
377   }
378
379 #   $self->dump_visible();
380
381   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
382
383   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
384 }
385
386 sub dump_visible {
387   my $self = shift;
388   foreach my $key (@{ $self->{ORDER} }) {
389     my $entry = $self->{$key};
390     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
391   }
392 }
393
394 1;
395