Ein paar kleine Uebersetzungen.
[kivitendo-erp.git] / SL / User.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 # user related functions
32 #
33 #=====================================================================
34
35 package User;
36
37 sub new {
38   $main::lxdebug->enter_sub();
39
40   my ($type, $memfile, $login) = @_;
41   my $self = {};
42
43   if ($login ne "") {
44     &error("", "$memfile locked!") if (-f "${memfile}.LCK");
45
46     open(MEMBER, "$memfile") or &error("", "$memfile : $!");
47
48     while (<MEMBER>) {
49       if (/^\[$login\]/) {
50         while (<MEMBER>) {
51           last if /^\[/;
52           next if /^(#|\s)/;
53
54           # remove comments
55           s/\s#.*//g;
56
57           # remove any trailing whitespace
58           s/^\s*(.*?)\s*$/$1/;
59
60           ($key, $value) = split(/=/, $_, 2);
61
62           if (($key eq "stylesheet") && ($value eq "sql-ledger.css")) {
63             $value = "lx-office-erp.css";
64           }
65
66           $self->{$key} = $value;
67         }
68
69         $self->{login} = $login;
70
71         last;
72       }
73     }
74     close MEMBER;
75   }
76
77   $main::lxdebug->leave_sub();
78   bless $self, $type;
79 }
80
81 sub country_codes {
82   $main::lxdebug->enter_sub();
83
84   my %cc       = ();
85   my @language = ();
86
87   # scan the locale directory and read in the LANGUAGE files
88   opendir DIR, "locale";
89
90   my @dir = grep !/(^\.\.?$|\..*)/, readdir DIR;
91
92   foreach my $dir (@dir) {
93     next unless open(FH, "locale/$dir/LANGUAGE");
94     @language = <FH>;
95     close FH;
96
97     $cc{$dir} = "@language";
98   }
99
100   closedir(DIR);
101
102   $main::lxdebug->leave_sub();
103
104   return %cc;
105 }
106
107 sub login {
108   $main::lxdebug->enter_sub();
109
110   my ($self, $form, $userspath) = @_;
111
112   my $rc = -3;
113
114   if ($self->{login}) {
115
116     if ($self->{password}) {
117       $form->{password} = crypt $form->{password},
118         substr($self->{login}, 0, 2);
119       if ($self->{password} ne $form->{password}) {
120         $main::lxdebug->leave_sub();
121         return -1;
122       }
123     }
124
125     unless (-e "$userspath/$self->{login}.conf") {
126       $self->create_config("$userspath/$self->{login}.conf");
127     }
128
129     do "$userspath/$self->{login}.conf";
130     $myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd};
131
132     # check if database is down
133     my $dbh =
134       DBI->connect($myconfig{dbconnect}, $myconfig{dbuser},
135                    $myconfig{dbpasswd})
136       or $self->error(DBI::errstr);
137
138     # we got a connection, check the version
139     my $query = qq|SELECT version FROM defaults|;
140     my $sth   = $dbh->prepare($query);
141     $sth->execute || $form->dberror($query);
142
143     my ($dbversion) = $sth->fetchrow_array;
144     $sth->finish;
145
146     # add login to employee table if it does not exist
147     # no error check for employee table, ignore if it does not exist
148     $query = qq|SELECT e.id FROM employee e WHERE e.login = '$self->{login}'|;
149     $sth   = $dbh->prepare($query);
150     $sth->execute;
151
152     my ($login) = $sth->fetchrow_array;
153     $sth->finish;
154
155     if (!$login) {
156       $query = qq|INSERT INTO employee (login, name, workphone, role)
157                   VALUES ('$self->{login}', '$myconfig{name}',
158                   '$myconfig{tel}', 'user')|;
159       $dbh->do($query);
160     }
161     $dbh->disconnect;
162
163     $rc = 0;
164
165     if (&update_available($myconfig{"dbdriver"}, $dbversion)) {
166
167       # update the tables
168       open FH, ">$userspath/nologin" or die "
169 $!";
170
171       map { $form->{$_} = $myconfig{$_} }
172         qw(dbname dbhost dbport dbdriver dbuser dbpasswd);
173
174       $form->{dbupdate} = "db$myconfig{dbname}";
175       $form->{ $form->{dbupdate} } = 1;
176
177       $form->info(sprintf($main::locale->text("Upgrading Dataset %s..."), $myconfig{dbname}));
178
179       # required for Oracle
180       $form->{dbdefault} = $sid;
181
182       # ignore HUP, QUIT in case the webserver times out
183       $SIG{HUP}  = 'IGNORE';
184       $SIG{QUIT} = 'IGNORE';
185
186       $self->dbupdate($form);
187
188       # remove lock file
189       unlink "$userspath/nologin";
190
191       $form->info($main::locale->text("...done"));
192
193       $rc = -2;
194
195     }
196   }
197
198   $main::lxdebug->leave_sub();
199
200   return $rc;
201 }
202
203 sub dbconnect_vars {
204   $main::lxdebug->enter_sub();
205
206   my ($form, $db) = @_;
207
208   my %dboptions = (
209         'Pg' => { 'yy-mm-dd'   => 'set DateStyle to \'ISO\'',
210                   'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
211                   'mm/dd/yy'   => 'set DateStyle to \'SQL, US\'',
212                   'mm-dd-yy'   => 'set DateStyle to \'POSTGRES, US\'',
213                   'dd/mm/yy'   => 'set DateStyle to \'SQL, EUROPEAN\'',
214                   'dd-mm-yy'   => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
215                   'dd.mm.yy'   => 'set DateStyle to \'GERMAN\''
216         },
217         'Oracle' => {
218           'yy-mm-dd'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
219           'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
220           'mm/dd/yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
221           'mm-dd-yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
222           'dd/mm/yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
223           'dd-mm-yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
224           'dd.mm.yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
225         });
226
227   $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
228
229   if ($form->{dbdriver} eq 'Pg') {
230     $form->{dbconnect} = "dbi:Pg:dbname=$db";
231   }
232
233   if ($form->{dbdriver} eq 'Oracle') {
234     $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
235   }
236
237   if ($form->{dbhost}) {
238     $form->{dbconnect} .= ";host=$form->{dbhost}";
239   }
240   if ($form->{dbport}) {
241     $form->{dbconnect} .= ";port=$form->{dbport}";
242   }
243
244   $main::lxdebug->leave_sub();
245 }
246
247 sub dbdrivers {
248   $main::lxdebug->enter_sub();
249
250   my @drivers = DBI->available_drivers();
251
252   $main::lxdebug->leave_sub();
253
254   return (grep { /(Pg|Oracle)/ } @drivers);
255 }
256
257 sub dbsources {
258   $main::lxdebug->enter_sub();
259
260   my ($self, $form) = @_;
261
262   my @dbsources = ();
263   my ($sth, $query);
264
265   $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
266   $form->{sid} = $form->{dbdefault};
267   &dbconnect_vars($form, $form->{dbdefault});
268
269   my $dbh =
270     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
271     or $form->dberror;
272
273   if ($form->{dbdriver} eq 'Pg') {
274
275     $query = qq|SELECT datname FROM pg_database WHERE NOT ((datname = 'template0') OR (datname = 'template1'))|;
276     $sth   = $dbh->prepare($query);
277     $sth->execute || $form->dberror($query);
278
279     while (my ($db) = $sth->fetchrow_array) {
280
281       if ($form->{only_acc_db}) {
282
283         next if ($db =~ /^template/);
284
285         &dbconnect_vars($form, $db);
286         my $dbh =
287           DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
288           or $form->dberror;
289
290         $query = qq|SELECT p.tablename FROM pg_tables p
291                     WHERE p.tablename = 'defaults'
292                     AND p.tableowner = '$form->{dbuser}'|;
293         my $sth = $dbh->prepare($query);
294         $sth->execute || $form->dberror($query);
295
296         if ($sth->fetchrow_array) {
297           push @dbsources, $db;
298         }
299         $sth->finish;
300         $dbh->disconnect;
301         next;
302       }
303       push @dbsources, $db;
304     }
305   }
306
307   if ($form->{dbdriver} eq 'Oracle') {
308     if ($form->{only_acc_db}) {
309       $query = qq|SELECT o.owner FROM dba_objects o
310                   WHERE o.object_name = 'DEFAULTS'
311                   AND o.object_type = 'TABLE'|;
312     } else {
313       $query = qq|SELECT username FROM dba_users|;
314     }
315
316     $sth = $dbh->prepare($query);
317     $sth->execute || $form->dberror($query);
318
319     while (my ($db) = $sth->fetchrow_array) {
320       push @dbsources, $db;
321     }
322   }
323
324   $sth->finish;
325   $dbh->disconnect;
326
327   $main::lxdebug->leave_sub();
328
329   return @dbsources;
330 }
331
332 sub dbcreate {
333   $main::lxdebug->enter_sub();
334
335   my ($self, $form) = @_;
336
337   my %dbcreate = (
338     'Pg'     => qq|CREATE DATABASE "$form->{db}"|,
339     'Oracle' =>
340       qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
341   );
342
343   $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding};
344
345   $form->{sid} = $form->{dbdefault};
346   &dbconnect_vars($form, $form->{dbdefault});
347   my $dbh =
348     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
349     or $form->dberror;
350   my $query = qq|$dbcreate{$form->{dbdriver}}|;
351   $dbh->do($query) || $form->dberror($query);
352
353   if ($form->{dbdriver} eq 'Oracle') {
354     $query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|;
355     $dbh->do($query) || $form->dberror($query);
356   }
357   $dbh->disconnect;
358
359   # setup variables for the new database
360   if ($form->{dbdriver} eq 'Oracle') {
361     $form->{dbuser}   = $form->{db};
362     $form->{dbpasswd} = $form->{db};
363   }
364
365   &dbconnect_vars($form, $form->{db});
366
367   $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
368     or $form->dberror;
369
370   # create the tables
371   my $filename = qq|sql/lx-office.sql|;
372   $self->process_query($form, $dbh, $filename);
373
374   # load gifi
375   ($filename) = split /_/, $form->{chart};
376   $filename =~ s/_//;
377   $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
378
379   # load chart of accounts
380   $filename = qq|sql/$form->{chart}-chart.sql|;
381   $self->process_query($form, $dbh, $filename);
382
383   $dbh->disconnect;
384
385   $main::lxdebug->leave_sub();
386 }
387
388 # Process a Perl script which updates the database.
389 # If the script returns 1 then the update was successful.
390 # Return code "2" means "needs more interaction; remove
391 # users/nologin and exit".
392 # All other return codes are fatal errors.
393 sub process_perl_script {
394   $main::lxdebug->enter_sub();
395
396   my ($self, $form, $dbh, $filename, $version) = @_;
397
398   open(FH, "$filename") or $form->error("$filename : $!\n");
399   my $contents = join("", <FH>);
400   close(FH);
401
402   $dbh->begin_work();
403
404   my $result = eval($contents);
405
406   if (1 != $result) {
407     $dbh->rollback();
408     $dbh->disconnect();
409   }
410
411   if (!defined($result)) {
412     $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following syntax error:<br>${@}<br>" .
413                    "All changes in that file have been reverted.");
414   } elsif (1 != $result) {
415     unlink("users/nologin") if (2 == $result);
416     exit(0);
417   }
418
419   if ($version) {
420     $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
421   }
422   $dbh->commit();
423
424   $main::lxdebug->leave_sub();
425 }
426
427 sub process_query {
428   $main::lxdebug->enter_sub();
429
430   my ($self, $form, $dbh, $filename, $version) = @_;
431
432   #  return unless (-f $filename);
433
434   open(FH, "$filename") or $form->error("$filename : $!\n");
435   my $query = "";
436   my $sth;
437   my @quote_chars;
438
439   $dbh->begin_work();
440
441   while (<FH>) {
442
443     # Remove DOS and Unix style line endings.
444     chomp;
445
446     # remove comments
447     s/--.*$//;
448
449     for (my $i = 0; $i < length($_); $i++) {
450       my $char = substr($_, $i, 1);
451
452       # Are we inside a string?
453       if (@quote_chars) {
454         if ($char eq $quote_chars[-1]) {
455           pop(@quote_chars);
456         }
457         $query .= $char;
458
459       } else {
460         if (($char eq "'") || ($char eq "\"")) {
461           push(@quote_chars, $char);
462
463         } elsif ($char eq ";") {
464
465           # Query is complete. Send it.
466
467           $sth = $dbh->prepare($query);
468           if (!$sth->execute()) {
469             my $errstr = $dbh->errstr;
470             $sth->finish();
471             $dbh->rollback();
472             $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" .
473                            "The error message was: ${errstr}<br>" .
474                            "All changes in that file have been reverted.");
475           }
476           $sth->finish();
477
478           $char  = "";
479           $query = "";
480         }
481
482         $query .= $char;
483       }
484     }
485   }
486
487   if ($version) {
488     $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
489   }
490   $dbh->commit();
491
492   close FH;
493
494   $main::lxdebug->leave_sub();
495 }
496
497 sub dbdelete {
498   $main::lxdebug->enter_sub();
499
500   my ($self, $form) = @_;
501
502   my %dbdelete = ('Pg'     => qq|DROP DATABASE "$form->{db}"|,
503                   'Oracle' => qq|DROP USER $form->{db} CASCADE|);
504
505   $form->{sid} = $form->{dbdefault};
506   &dbconnect_vars($form, $form->{dbdefault});
507   my $dbh =
508     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
509     or $form->dberror;
510   my $query = qq|$dbdelete{$form->{dbdriver}}|;
511   $dbh->do($query) || $form->dberror($query);
512
513   $dbh->disconnect;
514
515   $main::lxdebug->leave_sub();
516 }
517
518 sub dbsources_unused {
519   $main::lxdebug->enter_sub();
520
521   my ($self, $form, $memfile) = @_;
522
523   my @dbexcl    = ();
524   my @dbsources = ();
525
526   $form->error('File locked!') if (-f "${memfile}.LCK");
527
528   # open members file
529   open(FH, "$memfile") or $form->error("$memfile : $!");
530
531   while (<FH>) {
532     if (/^dbname=/) {
533       my ($null, $item) = split(/=/);
534       push @dbexcl, $item;
535     }
536   }
537
538   close FH;
539
540   $form->{only_acc_db} = 1;
541   my @db = &dbsources("", $form);
542
543   push @dbexcl, $form->{dbdefault};
544
545   foreach $item (@db) {
546     unless (grep /$item$/, @dbexcl) {
547       push @dbsources, $item;
548     }
549   }
550
551   $main::lxdebug->leave_sub();
552
553   return @dbsources;
554 }
555
556 sub dbneedsupdate {
557   $main::lxdebug->enter_sub();
558
559   my ($self, $form) = @_;
560
561   my %dbsources = ();
562   my $query;
563
564   $form->{sid} = $form->{dbdefault};
565   &dbconnect_vars($form, $form->{dbdefault});
566
567   my $dbh =
568     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
569     or $form->dberror;
570
571   if ($form->{dbdriver} eq 'Pg') {
572
573     $query = qq|SELECT d.datname FROM pg_database d, pg_user u
574                 WHERE d.datdba = u.usesysid
575                 AND u.usename = '$form->{dbuser}'|;
576     my $sth = $dbh->prepare($query);
577     $sth->execute || $form->dberror($query);
578
579     while (my ($db) = $sth->fetchrow_array) {
580
581       next if ($db =~ /^template/);
582
583       &dbconnect_vars($form, $db);
584
585       my $dbh =
586         DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
587         or $form->dberror;
588
589       $query = qq|SELECT t.tablename FROM pg_tables t
590                   WHERE t.tablename = 'defaults'|;
591       my $sth = $dbh->prepare($query);
592       $sth->execute || $form->dberror($query);
593
594       if ($sth->fetchrow_array) {
595         $query = qq|SELECT version FROM defaults|;
596         my $sth = $dbh->prepare($query);
597         $sth->execute;
598
599         if (my ($version) = $sth->fetchrow_array) {
600           $dbsources{$db} = $version;
601         }
602         $sth->finish;
603       }
604       $sth->finish;
605       $dbh->disconnect;
606     }
607     $sth->finish;
608   }
609
610   if ($form->{dbdriver} eq 'Oracle') {
611     $query = qq|SELECT o.owner FROM dba_objects o
612                 WHERE o.object_name = 'DEFAULTS'
613                 AND o.object_type = 'TABLE'|;
614
615     $sth = $dbh->prepare($query);
616     $sth->execute || $form->dberror($query);
617
618     while (my ($db) = $sth->fetchrow_array) {
619
620       $form->{dbuser} = $db;
621       &dbconnect_vars($form, $db);
622
623       my $dbh =
624         DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
625         or $form->dberror;
626
627       $query = qq|SELECT version FROM defaults|;
628       my $sth = $dbh->prepare($query);
629       $sth->execute;
630
631       if (my ($version) = $sth->fetchrow_array) {
632         $dbsources{$db} = $version;
633       }
634       $sth->finish;
635       $dbh->disconnect;
636     }
637     $sth->finish;
638   }
639
640   $dbh->disconnect;
641
642   $main::lxdebug->leave_sub();
643
644   return %dbsources;
645 }
646
647 ## LINET
648 sub calc_version {
649   $main::lxdebug->enter_sub(2);
650
651   my (@v, $version, $i);
652
653   @v = split(/\./, $_[0]);
654   while (scalar(@v) < 4) {
655     push(@v, 0);
656   }
657   $version = 0;
658   for ($i = 0; $i < 4; $i++) {
659     $version *= 1000;
660     $version += $v[$i];
661   }
662
663   $main::lxdebug->leave_sub(2);
664   return $version;
665 }
666
667 sub cmp_script_version {
668   my ($a_from, $a_to, $b_from, $b_to);
669   my ($i, $res_a, $res_b);
670   my ($my_a, $my_b) = ($a, $b);
671
672   $my_a =~ s/.*-upgrade-//;
673   $my_a =~ s/.sql$//;
674   $my_b =~ s/.*-upgrade-//;
675   $my_b =~ s/.sql$//;
676   ($my_a_from, $my_a_to) = split(/-/, $my_a);
677   ($my_b_from, $my_b_to) = split(/-/, $my_b);
678
679   $res_a = calc_version($my_a_from);
680   $res_b = calc_version($my_b_from);
681
682   if ($res_a == $res_b) {
683     $res_a = calc_version($my_a_to);
684     $res_b = calc_version($my_b_to);
685   }
686
687   return $res_a <=> $res_b;
688 }
689 ## /LINET
690
691 sub update_available {
692   my ($dbdriver, $cur_version) = @_;
693
694   opendir SQLDIR, "sql/${dbdriver}-upgrade" or &error("", "sql/${dbdriver}-upgrade: $!");
695   my @upgradescripts =
696     grep(/$form->{dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)/, readdir(SQLDIR));
697   closedir SQLDIR;
698
699   return ($#upgradescripts > -1);
700 }
701
702 sub dbupdate {
703   $main::lxdebug->enter_sub();
704
705   my ($self, $form) = @_;
706
707   $form->{sid} = $form->{dbdefault};
708
709   my @upgradescripts = ();
710   my $query;
711   my $rc = -2;
712
713   if ($form->{dbupdate}) {
714
715     # read update scripts into memory
716     opendir SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade" or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
717     ## LINET
718     @upgradescripts =
719       sort(cmp_script_version
720            grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/, readdir(SQLDIR)));
721     ## /LINET
722     closedir SQLDIR;
723   }
724
725   foreach my $db (split / /, $form->{dbupdate}) {
726
727     next unless $form->{$db};
728
729     # strip db from dataset
730     $db =~ s/^db//;
731     &dbconnect_vars($form, $db);
732
733     my $dbh =
734       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
735       or $form->dberror;
736
737     # check version
738     $query = qq|SELECT version FROM defaults|;
739     my $sth = $dbh->prepare($query);
740
741     # no error check, let it fall through
742     $sth->execute;
743
744     my $version = $sth->fetchrow_array;
745     $sth->finish;
746
747     next unless $version;
748
749     ## LINET
750     $version = calc_version($version);
751     ## /LINET
752
753     foreach my $upgradescript (@upgradescripts) {
754       my $a = $upgradescript;
755       $a =~ s/^$form->{dbdriver}-upgrade-|\.(sql|pl)$//g;
756       my $file_type = $1;
757
758       my ($mindb, $maxdb) = split /-/, $a;
759       my $str_maxdb = $maxdb;
760       ## LINET
761       $mindb = calc_version($mindb);
762       $maxdb = calc_version($maxdb);
763       ## /LINET
764
765       next if ($version >= $maxdb);
766
767       # if there is no upgrade script exit
768       last if ($version < $mindb);
769
770       # apply upgrade
771       $main::lxdebug->message(DEBUG2, "Appliying Update $upgradescript");
772       if ($file_type eq "sql") {
773         $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
774       } else {
775         $self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
776       }
777
778       $version = $maxdb;
779
780     }
781
782     $rc = 0;
783     $dbh->disconnect;
784
785   }
786
787   $main::lxdebug->leave_sub();
788
789   return $rc;
790 }
791
792 sub create_config {
793   $main::lxdebug->enter_sub();
794
795   my ($self, $filename) = @_;
796
797   @config = &config_vars;
798
799   open(CONF, ">$filename") or $self->error("$filename : $!");
800
801   # create the config file
802   print CONF qq|# configuration file for $self->{login}
803
804 \%myconfig = (
805 |;
806
807   foreach $key (sort @config) {
808     $self->{$key} =~ s/\'/\\\'/g;
809     print CONF qq|  $key => '$self->{$key}',\n|;
810   }
811
812   print CONF qq|);\n\n|;
813
814   close CONF;
815
816   $main::lxdebug->leave_sub();
817 }
818
819 sub save_member {
820   $main::lxdebug->enter_sub();
821
822   my ($self, $memberfile, $userspath) = @_;
823
824   my $newmember = 1;
825
826   # format dbconnect and dboptions string
827   &dbconnect_vars($self, $self->{dbname});
828
829   $self->error('File locked!') if (-f "${memberfile}.LCK");
830   open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
831   close(FH);
832
833   open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
834
835   @config = <CONF>;
836
837   seek(CONF, 0, 0);
838   truncate(CONF, 0);
839
840   while ($line = shift @config) {
841     if ($line =~ /^\[$self->{login}\]/) {
842       $newmember = 0;
843       last;
844     }
845     print CONF $line;
846   }
847
848   # remove everything up to next login or EOF
849   while ($line = shift @config) {
850     last if ($line =~ /^\[/);
851   }
852
853   # this one is either the next login or EOF
854   print CONF $line;
855
856   while ($line = shift @config) {
857     print CONF $line;
858   }
859
860   print CONF qq|[$self->{login}]\n|;
861
862   if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
863       && $self->{root}) {
864     $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
865     chop $self->{dbpasswd};
866   }
867   if (defined($self->{new_password})) {
868     if ($self->{new_password} ne $self->{old_password}) {
869       $self->{password} = crypt $self->{new_password},
870         substr($self->{login}, 0, 2)
871         if $self->{new_password};
872     }
873   } else {
874     if ($self->{password} ne $self->{old_password}) {
875       $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
876         if $self->{password};
877     }
878   }
879
880   if ($self->{'root login'}) {
881     @config = ("password");
882   } else {
883     @config = &config_vars;
884   }
885
886   # replace \r\n with \n
887   map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
888   foreach $key (sort @config) {
889     print CONF qq|$key=$self->{$key}\n|;
890   }
891
892   print CONF "\n";
893   close CONF;
894   unlink "${memberfile}.LCK";
895
896   # create conf file
897   $self->create_config("$userspath/$self->{login}.conf")
898     unless $self->{'root login'};
899
900   $main::lxdebug->leave_sub();
901 }
902
903 sub config_vars {
904   $main::lxdebug->enter_sub();
905
906   my @conf = qw(acs address admin businessnumber charset company countrycode
907     currency dateformat dbconnect dbdriver dbhost dbport dboptions
908     dbname dbuser dbpasswd email fax name numberformat in_numberformat password
909     printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen
910     anfragen lieferantenbestellungen einkaufsrechnungen steuernummer co_ustid duns menustyle);
911
912   $main::lxdebug->leave_sub();
913
914   return @conf;
915 }
916
917 sub error {
918   $main::lxdebug->enter_sub();
919
920   my ($self, $msg) = @_;
921
922   if ($ENV{HTTP_USER_AGENT}) {
923     print qq|Content-Type: text/html
924
925 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
926
927 <body bgcolor=ffffff>
928
929 <h2><font color=red>Error!</font></h2>
930 <p><b>$msg</b>|;
931
932   }
933
934   die "Error: $msg\n";
935
936   $main::lxdebug->leave_sub();
937 }
938
939 1;
940