]> wagnertech.de Git - kivitendo-erp.git/blob - bin/mozilla/admin.pl
Maske "Datenbankadministration" auf die Verwendung von HTML-Vorlagen umgestellt.
[kivitendo-erp.git] / bin / mozilla / admin.pl
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) 2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # setup module
31 # add/edit/delete users
32 #
33 #======================================================================
34
35 $menufile = "menu.ini";
36
37 use DBI;
38 use CGI;
39
40 use SL::Form;
41 use SL::User;
42 use SL::Common;
43 use SL::Inifile;
44
45 require "bin/mozilla/common.pl";
46
47 our $cgi = new CGI('');
48
49 $form = new Form;
50 $form->{"root"} = "root login";
51
52 $locale = new Locale $language, "admin";
53
54 # customization
55 if (-f "bin/mozilla/custom_$form->{script}") {
56   eval { require "bin/mozilla/custom_$form->{script}"; };
57   $form->error($@) if ($@);
58 }
59
60 $form->{stylesheet} = "lx-office-erp.css";
61 $form->{favicon}    = "favicon.ico";
62
63 if ($form->{action}) {
64
65
66   $subroutine = $locale->findsub($form->{action});
67
68   if ($subroutine eq 'login') {
69     if ($form->{rpw}) {
70       $form->{rpw} = crypt $form->{rpw}, "ro";
71     }
72   }
73
74   check_password();
75
76   call_sub($subroutine);
77
78 } else {
79
80   # if there are no drivers bail out
81   $form->error($locale->text('No Database Drivers available!'))
82     unless (User->dbdrivers);
83
84   # create memberfile
85   if (!-f $memberfile) {
86     open(FH, ">$memberfile") or $form->error("$memberfile : $!");
87     print FH qq|# SQL-Ledger Accounting members
88
89 [root login]
90 password=
91
92 |;
93     close FH;
94   }
95
96   adminlogin();
97
98 }
99
100 1;
101
102 # end
103
104 sub adminlogin {
105
106   $form->{title} =
107     qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
108
109   $form->header();
110   print $form->parse_html_template('admin/adminlogin');
111 }
112
113 sub login {
114   list_users();
115 }
116
117 sub list_users {
118
119   $form->error($locale->text('File locked!')) if (-f "${memberfile}.LCK");
120
121   open(FH, "$memberfile") or $form->error("$memberfile : $!");
122
123   my %members;
124
125   while (<FH>) {
126     chomp;
127
128     if (/^\[.*\]/) {
129       $login = $_;
130       $login =~ s/(\[|\])//g;
131
132       $members{$login} = { "login" => $login };
133     }
134
135     if (/^([a-z]+)=(.*)/) {
136       $members{$login}->{$1} = $2;
137     }
138   }
139
140   close(FH);
141
142   delete $members{"root login"};
143   map { $_->{templates} =~ s|.*/||; } values %members;
144
145   $form->{title}  = "Lx-Office ERP " . $locale->text('Administration');
146   $form->{LOCKED} = -e "$userspath/nologin";
147   $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
148
149   $form->header();
150   print $form->parse_html_template("admin/list_users");
151 }
152
153 sub add_user {
154
155   $form->{title} =
156       "Lx-Office ERP "
157     . $locale->text('Administration') . " / "
158     . $locale->text('Add User');
159
160   my $myconfig = {
161     "vclimit"      => 200,
162     "countrycode"  => "de",
163     "numberformat" => "1000,00",
164     "dateformat"   => "dd.mm.yy",
165     "stylesheet"   => "lx-office-erp.css",
166     "menustyle"    => "v3",
167   };
168
169   edit_user_form($myconfig);
170 }
171
172 sub edit {
173
174   $form->{title} =
175       "Lx-Office ERP "
176     . $locale->text('Administration') . " / "
177     . $locale->text('Edit User');
178   $form->{edit} = 1;
179
180   $form->isblank("login", $locale->text("The login is missing."));
181
182   # get user
183   my $myconfig = new User "$memberfile", "$form->{login}";
184
185   $myconfig->{signature} =~ s/\\n/\r\n/g;
186   $myconfig->{address}   =~ s/\\n/\r\n/g;
187
188   # strip basedir from templates directory
189   $myconfig->{templates} =~ s|.*/||;
190
191   edit_user_form($myconfig);
192 }
193
194 sub edit_user_form {
195   my ($myconfig) = @_;
196
197   my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
198   $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
199
200   my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
201   $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
202
203   %countrycodes = User->country_codes;
204   $form->{ALL_COUNTRYCODES} = [];
205   foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
206     push @{ $form->{ALL_COUNTRYCODES} }, { "value"    => $countrycode,
207                                            "name"     => $countrycodes{$countrycode},
208                                            "selected" => $countrycode eq $myconfig->{countrycode} };
209   }
210
211   # is there a templates basedir
212   if (!-d "$templates") {
213     $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
214   }
215
216   opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $!");
217   my @all     = readdir(TEMPLATEDIR);
218   my @alldir  = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
219   my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
220   closedir TEMPLATEDIR;
221
222   @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
223   @alldir = grep !/^(webpages|\.svn)$/, @alldir;
224
225   @allhtml = reverse grep !/Default/, @allhtml;
226   push @allhtml, 'Default';
227   @allhtml = reverse @allhtml;
228
229   $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
230
231   $lastitem = $allhtml[0];
232   $lastitem =~ s/-.*//g;
233   $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
234   foreach $item (@allhtml) {
235     $item =~ s/-.*//g;
236     next if ($item eq $lastitem);
237
238     push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
239     $lastitem = $item;
240   }
241
242   # css dir has styles that are not intended as general layouts.
243   # reverting to hardcoded list
244   $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
245
246   $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
247
248   map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
249
250   # access control
251   my @acsorder = ();
252   my %acs      = ();
253   my %excl     = ();
254   open(FH, $menufile) or $form->error("$menufile : $!");
255
256   while ($item = <FH>) {
257     next unless $item =~ /\[/;
258     next if $item =~ /\#/;
259
260     $item =~ s/(\[|\])//g;
261     chomp $item;
262
263     my ($level, $menuitem);
264
265     if ($item =~ /--/) {
266       ($level, $menuitem) = split /--/, $item, 2;
267     } else {
268       $level    = $item;
269       $menuitem = $item;
270       push @acsorder, $item;
271     }
272
273     $acs{$level} ||= [];
274     push @{ $acs{$level} }, $menuitem;
275
276   }
277
278   foreach $item (split(/;/, $myconfig->{acs})) {
279     ($key, $value) = split /--/, $item, 2;
280     $excl{$key}{$value} = 1;
281   }
282
283   $form->{ACLS}    = [];
284   $form->{all_acs} = "";
285
286   foreach $key (@acsorder) {
287     my $acl = { "checked" => $form->{login} ? !$excl{$key}->{$key} : 1,
288                 "name"    => "${key}--${key}",
289                 "title"   => $key,
290                 "SUBACLS" => [], };
291     $form->{all_acs} .= "${key}--${key};";
292
293     foreach $item (@{ $acs{$key} }) {
294       next if ($key eq $item);
295
296       my $subacl = { "checked" => $form->{login} ? !$excl{$key}->{$item} : 1,
297                      "name"    => "${key}--${item}",
298                      "title"   => $item };
299       push @{ $acl->{SUBACLS} }, $subacl;
300       $form->{all_acs} .= "${key}--${item};";
301     }
302     push @{ $form->{ACLS} }, $acl;
303   }
304
305   chop $form->{all_acs};
306
307   $form->header();
308   print $form->parse_html_template("admin/edit_user");
309 }
310
311 sub save {
312
313   $form->{dbdriver} = 'Pg';
314
315   # no spaces allowed in login name
316   ($form->{login}) = split / /, $form->{login};
317
318   $form->isblank("login", $locale->text('Login name missing!'));
319
320   # check for duplicates
321   if (!$form->{edit}) {
322     $temp = new User "$memberfile", "$form->{login}";
323
324     if ($temp->{login}) {
325       $form->error("$form->{login} " . $locale->text('is already a member!'));
326     }
327   }
328
329   # no spaces allowed in directories
330   ($form->{newtemplates}) = split / /, $form->{newtemplates};
331
332   if ($form->{newtemplates}) {
333     $form->{templates} = $form->{newtemplates};
334   } else {
335     $form->{templates} =
336       ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
337   }
338
339   # is there a basedir
340   if (!-d "$templates") {
341     $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
342   }
343
344   # add base directory to $form->{templates}
345   $form->{templates} =~ s|.*/||;
346   $form->{templates} =  "$templates/$form->{templates}";
347
348   $myconfig = new User "$memberfile", "$form->{login}";
349
350   # redo acs variable and delete all the acs codes
351   my @acs;
352   foreach $item (split m|;|, $form->{all_acs}) {
353     my $name =  "ACS_${item}";
354     $name    =~ s| |+|g;
355     push @acs, $item if !$form->{$name};
356     delete $form->{$name};
357   }
358   $form->{acs} = join ";", @acs;
359
360   $form->isblank("dbname", $locale->text('Dataset missing!'));
361   $form->isblank("dbuser", $locale->text('Database User missing!'));
362
363   foreach $item (keys %{$form}) {
364     $myconfig->{$item} = $form->{$item};
365   }
366
367   delete $myconfig->{stylesheet};
368   if ($form->{userstylesheet}) {
369     $myconfig->{stylesheet} = $form->{userstylesheet};
370   }
371
372   $myconfig->save_member($memberfile, $userspath);
373
374   if ($webdav) {
375     @webdavdirs =
376       qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
377     foreach $directory (@webdavdirs) {
378       $file = "webdav/" . $directory . "/webdav-user";
379       if ($form->{$directory}) {
380         if (open(HTACCESS, "$file")) {
381           while (<HTACCESS>) {
382             ($login, $password) = split(/:/, $_);
383             if ($login ne $form->{login}) {
384               $newfile .= $_;
385             }
386           }
387           close(HTACCESS);
388         }
389         open(HTACCESS, "> $file") or die "cannot open $file $!\n";
390         $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
391         print(HTACCESS $newfile);
392         close(HTACCESS);
393       } else {
394         $form->{$directory} = 0;
395         if (open(HTACCESS, "$file")) {
396           while (<HTACCESS>) {
397             ($login, $password) = split(/:/, $_);
398             if ($login ne $form->{login}) {
399               $newfile .= $_;
400             }
401           }
402           close(HTACCESS);
403         }
404         open(HTACCESS, "> $file") or die "cannot open $file $!\n";
405         print(HTACCESS $newfile);
406         close(HTACCESS);
407       }
408     }
409   }
410
411   $form->{templates}       =~ s|.*/||;
412   $form->{templates}       =  "${templates}/$form->{templates}";
413   $form->{mastertemplates} =~ s|.*/||;
414
415   # create user template directory and copy master files
416   if (!-d "$form->{templates}") {
417     umask(002);
418
419     if (mkdir "$form->{templates}", oct("771")) {
420
421       umask(007);
422
423       # copy templates to the directory
424       opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $!");
425       @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
426         readdir TEMPLATEDIR;
427       closedir TEMPLATEDIR;
428
429       foreach $file (@templates) {
430         open(TEMP, "$templates/$file")
431           or $form->error("$templates/$file : $!");
432
433         $file =~ s/$form->{mastertemplates}-//;
434         open(NEW, ">$form->{templates}/$file")
435           or $form->error("$form->{templates}/$file : $!");
436
437         while ($line = <TEMP>) {
438           print NEW $line;
439         }
440         close(TEMP);
441         close(NEW);
442       }
443     } else {
444       $form->error("$!: $form->{templates}");
445     }
446   }
447
448   $form->redirect($locale->text('User saved!'));
449
450 }
451
452 sub delete {
453   $form->error($locale->text('File locked!')) if (-f ${memberfile} . LCK);
454   open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $!");
455   close(FH);
456
457   my $members = Inifile->new($memberfile);
458   my $templates = $members->{$form->{login}}->{templates};
459   delete $members->{$form->{login}};
460   $members->write();
461   unlink "${memberfile}.LCK";
462
463   if ($templates) {
464     my $templates_in_use = 0;
465     foreach $login (keys %{ $members }) {
466       next if $login =~ m/^[A-Z]+$/;
467       next if $members->{$login}->{templates} ne $templates;
468       $templates_in_use = 1;
469       last;
470     }
471
472     if (!$templates_in_use && -d $templates) {
473       unlink <$templates/*>;
474       rmdir $templates;
475     }
476   }
477
478   # delete config file for user
479   unlink "$userspath/$form->{login}.conf";
480
481   $form->redirect($locale->text('User deleted!'));
482
483 }
484
485 sub login_name {
486   my $login = shift;
487
488   $login =~ s/\[\]//g;
489   return ($login) ? $login : undef;
490
491 }
492
493 sub get_value {
494   my $line = shift;
495
496   my ($null, $value) = split(/=/, $line, 2);
497
498   # remove comments
499   $value =~ s/\s#.*//g;
500
501   # remove any trailing whitespace
502   $value =~ s/^\s*(.*?)\s*$/$1/;
503
504   $value;
505 }
506
507 sub change_admin_password {
508
509   $form->{title} =
510       qq|Lx-Office ERP |
511     . $locale->text('Administration') . " / "
512     . $locale->text('Change Admin Password');
513
514   $form->header();
515   print $form->parse_html_template("admin/change_admin_password");
516 }
517
518 sub change_password {
519   if ($form->{"password"} ne $form->{"password_again"}) {
520     $form->{title} =
521       qq|Lx-Office ERP |
522       . $locale->text('Administration') . " / "
523       . $locale->text('Change Admin Password');
524
525     $form->header();
526     $form->error($locale->text("The passwords do not match."));
527   }
528
529   $root->{password} = $form->{password};
530
531   $root->{'root login'} = 1;
532   $root->save_member($memberfile);
533
534   $form->{callback} =
535     "$form->{script}?action=list_users&rpw=$root->{password}";
536
537   $form->redirect($locale->text('Password changed!'));
538 }
539
540 sub check_password {
541   $root = new User "$memberfile", $form->{root};
542
543   if (!defined($root->{password}) || ($root->{password} ne $form->{rpw})) {
544     $form->error($locale->text('Incorrect Password!'));
545   }
546
547 }
548
549 sub pg_database_administration {
550
551   $form->{dbdriver} = 'Pg';
552   dbselect_source();
553
554 }
555
556 sub dbselect_source {
557   $form->{dbport}    = '5432';
558   $form->{dbuser}    = 'postgres';
559   $form->{dbdefault} = 'template1';
560   $form->{dbhost}    = 'localhost';
561
562   $form->{title}     = "Lx-Office ERP / " . $locale->text('Database Administration');
563
564   $form->header();
565   print $form->parse_html_template("admin/dbadmin");
566 }
567
568 sub continue {
569   call_sub($form->{"nextsub"});
570 }
571
572 sub update_dataset {
573
574   %needsupdate = User->dbneedsupdate(\%$form);
575
576   $form->{title} =
577       "Lx-Office ERP "
578     . $locale->text('Database Administration') . " / "
579     . $locale->text('Update Dataset');
580
581   $form->header;
582
583   print qq|
584 <body class=admin>
585
586
587 <center>
588 <h2>$form->{title}</h2>
589 |;
590   my $field_id = 0;
591   foreach $key (sort keys %needsupdate) {
592     if ($needsupdate{$key} ne $form->{dbversion}) {
593       $upd .= qq|<input id="$field_id" name="db$key" type="checkbox" value="1" checked> $key\n|;
594       $form->{dbupdate} .= "db$key ";
595       $field_id++;
596     }
597   }
598
599   chop $form->{dbupdate};
600
601   if ($form->{dbupdate}) {
602
603     print qq|
604 <table width=100%>
605 <form method=post action=$form->{script}>
606
607 <input type=hidden name="dbhost"    value="$form->{dbhost}">
608 <input type=hidden name="dbport"    value="$form->{dbport}">
609 <input type=hidden name="dbuser"    value="$form->{dbuser}">
610 <input type=hidden name="dbpasswd"  value="$form->{dbpasswd}">
611 <input type=hidden name="dbdefault" value="$form->{dbdefault}">
612
613 <tr class=listheading>
614   <th>| . $locale->text('The following Datasets need to be updated') . qq|</th>
615 </tr>
616 <tr>
617 <td>
618
619 $upd
620
621 </td>
622 </tr>
623 <tr>
624 <td>
625
626 <input name=dbupdate type=hidden value="$form->{dbupdate}">
627
628 <input name=callback type=hidden value="$form->{script}?action=list_users&rpw=$form->{rpw}">
629
630 <input type=hidden name=rpw value=$form->{rpw}>
631
632 <input type=hidden name=nextsub value=dbupdate>
633
634 <hr size=3 noshade>
635
636 <br>
637 <input type=submit class=submit name=action value="|
638       . $locale->text('Continue') . qq|">
639
640 </td></tr>
641 </table>
642 </form>
643 |;
644
645   } else {
646
647     print $locale->text('All Datasets up to date!');
648
649   }
650
651   print qq|
652
653 </body>
654 </html>
655 |;
656
657 }
658
659 sub dbupdate {
660   $form->{"stylesheet"} = "lx-office-erp.css";
661   $form->{"title"} = $main::locale->text("Dataset upgrade");
662   $form->header();
663   my $dbname =
664     join(" ",
665          map({ s/\s//g; s/^db//; $_; }
666              grep({ $form->{$_} }
667                   split(/\s+/, $form->{"dbupdate"}))));
668   print($form->parse_html_template("dbupgrade/header",
669                                    { "dbname" => $dbname }));
670
671   User->dbupdate(\%$form);
672
673   print qq|
674 <hr>
675
676 | . $locale->text('Dataset updated!') . qq|
677
678 <br>
679
680 <a id="enddatasetupdate" href="admin.pl?action=login&| .
681 join("&", map({ "$_=" . $form->escape($form->{$_}); } qw(rpw))) .
682 qq|">| . $locale->text("Continue") . qq|</a>|;
683
684 }
685
686 sub create_dataset {
687   $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
688
689   $form->{CHARTS} = [];
690
691   opendir SQLDIR, "sql/." or $form - error($!);
692   foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
693     next if ($item eq 'Default-chart.sql');
694     $item =~ s/-chart\.sql//;
695     push @{ $form->{CHARTS} }, { "name"     => $item,
696                                  "selected" => $item eq "Germany-DATEV-SKR03EU" };
697   }
698   closedir SQLDIR;
699
700   my $default_charset = $dbcharset;
701   $default_charset ||= Common::DEFAULT_CHARSET;
702
703   $form->{DBENCODINGS} = [];
704
705   foreach my $encoding (@Common::db_encodings) {
706     push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
707                                       "label"      => $encoding->{label},
708                                       "selected"   => $encoding->{charset} eq $default_charset };
709   }
710
711   $form->{title} =
712       "Lx-Office ERP "
713     . $locale->text('Database Administration') . " / "
714     . $locale->text('Create Dataset');
715
716   $form->header();
717   print $form->parse_html_template("admin/create_dataset");
718 }
719
720 sub dbcreate {
721   $form->isblank("db", $locale->text('Dataset missing!'));
722
723   User->dbcreate(\%$form);
724
725   $form->{title} =
726       "Lx-Office ERP "
727     . $locale->text('Database Administration') . " / "
728     . $locale->text('Create Dataset');
729
730   $form->header();
731   print $form->parse_html_template("admin/dbcreate");
732 }
733
734 sub delete_dataset {
735   @dbsources = User->dbsources_unused(\%$form, $memberfile);
736   $form->error($locale->text('Nothing to delete!')) unless @dbsources;
737
738   $form->{title} =
739       "Lx-Office ERP "
740     . $locale->text('Database Administration') . " / "
741     . $locale->text('Delete Dataset');
742   $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
743
744   $form->header();
745   print $form->parse_html_template("admin/delete_dataset");
746 }
747
748 sub dbdelete {
749
750   if (!$form->{db}) {
751     $form->error($locale->text('No Dataset selected!'));
752   }
753
754   User->dbdelete(\%$form);
755
756   $form->{title} =
757       "Lx-Office ERP "
758     . $locale->text('Database Administration') . " / "
759     . $locale->text('Delete Dataset');
760
761   $form->header();
762   print $form->parse_html_template("admin/dbdelete");
763 }
764
765 sub unlock_system {
766
767   unlink "$userspath/nologin";
768
769   $form->{callback} =
770     "$form->{script}?action=list_users&rpw=$root->{password}";
771
772   $form->redirect($locale->text('Lockfile removed!'));
773
774 }
775
776 sub lock_system {
777
778   open(FH, ">$userspath/nologin")
779     or $form->error($locale->text('Cannot create Lock!'));
780   close(FH);
781
782   $form->{callback} =
783     "$form->{script}?action=list_users&rpw=$root->{password}";
784
785   $form->redirect($locale->text('Lockfile created!'));
786
787 }