Jedes Datenbankupdate wird in einer Transaktion gekapselt. Somit wird ein Upgradescri...
[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 ($form->{dbversion} ne $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("Upgrading Dataset $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("... 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|;
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   # create indices
384   # Indices sind auch in lx-office.sql
385   # $filename = qq|sql/$form->{dbdriver}-indices.sql|;
386   # $self->process_query($form, $dbh, $filename);
387
388   $dbh->disconnect;
389
390   $main::lxdebug->leave_sub();
391 }
392
393 sub process_query {
394   $main::lxdebug->enter_sub();
395
396   my ($self, $form, $dbh, $filename, $version) = @_;
397
398   #  return unless (-f $filename);
399
400   open(FH, "$filename") or $form->error("$filename : $!\n");
401   my $query = "";
402   my $sth;
403   my @quote_chars;
404
405   $dbh->begin_work();
406
407   while (<FH>) {
408
409     # Remove DOS and Unix style line endings.
410     s/[\r\n]//g;
411
412     # don't add comments or empty lines
413     next if /^(--.*|\s+)$/;
414
415     for (my $i = 0; $i < length($_); $i++) {
416       my $char = substr($_, $i, 1);
417
418       # Are we inside a string?
419       if (@quote_chars) {
420         if ($char eq $quote_chars[-1]) {
421           pop(@quote_chars);
422         }
423         $query .= $char;
424
425       } else {
426         if (($char eq "'") || ($char eq "\"")) {
427           push(@quote_chars, $char);
428
429         } elsif ($char eq ";") {
430
431           # Query is complete. Send it.
432
433           $sth = $dbh->prepare($query);
434           if (!$sth->execute()) {
435             $sth->finish();
436             $dbh->rollback();
437             $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" .
438                            "All changes in that file have been reverted.");
439           }
440           $sth->finish();
441
442           $char  = "";
443           $query = "";
444         }
445
446         $query .= $char;
447       }
448     }
449   }
450
451   if ($version) {
452     $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
453   }
454   $dbh->commit();
455
456   close FH;
457
458   $main::lxdebug->leave_sub();
459 }
460
461 sub dbdelete {
462   $main::lxdebug->enter_sub();
463
464   my ($self, $form) = @_;
465
466   my %dbdelete = ('Pg'     => qq|DROP DATABASE "$form->{db}"|,
467                   'Oracle' => qq|DROP USER $form->{db} CASCADE|);
468
469   $form->{sid} = $form->{dbdefault};
470   &dbconnect_vars($form, $form->{dbdefault});
471   my $dbh =
472     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
473     or $form->dberror;
474   my $query = qq|$dbdelete{$form->{dbdriver}}|;
475   $dbh->do($query) || $form->dberror($query);
476
477   $dbh->disconnect;
478
479   $main::lxdebug->leave_sub();
480 }
481
482 sub dbsources_unused {
483   $main::lxdebug->enter_sub();
484
485   my ($self, $form, $memfile) = @_;
486
487   my @dbexcl    = ();
488   my @dbsources = ();
489
490   $form->error('File locked!') if (-f "${memfile}.LCK");
491
492   # open members file
493   open(FH, "$memfile") or $form->error("$memfile : $!");
494
495   while (<FH>) {
496     if (/^dbname=/) {
497       my ($null, $item) = split /=/;
498       push @dbexcl, $item;
499     }
500   }
501
502   close FH;
503
504   $form->{only_acc_db} = 1;
505   my @db = &dbsources("", $form);
506
507   push @dbexcl, $form->{dbdefault};
508
509   foreach $item (@db) {
510     unless (grep /$item$/, @dbexcl) {
511       push @dbsources, $item;
512     }
513   }
514
515   $main::lxdebug->leave_sub();
516
517   return @dbsources;
518 }
519
520 sub dbneedsupdate {
521   $main::lxdebug->enter_sub();
522
523   my ($self, $form) = @_;
524
525   my %dbsources = ();
526   my $query;
527
528   $form->{sid} = $form->{dbdefault};
529   &dbconnect_vars($form, $form->{dbdefault});
530
531   my $dbh =
532     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
533     or $form->dberror;
534
535   if ($form->{dbdriver} eq 'Pg') {
536
537     $query = qq|SELECT d.datname FROM pg_database d, pg_user u
538                 WHERE d.datdba = u.usesysid
539                 AND u.usename = '$form->{dbuser}'|;
540     my $sth = $dbh->prepare($query);
541     $sth->execute || $form->dberror($query);
542
543     while (my ($db) = $sth->fetchrow_array) {
544
545       next if ($db =~ /^template/);
546
547       &dbconnect_vars($form, $db);
548
549       my $dbh =
550         DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
551         or $form->dberror;
552
553       $query = qq|SELECT t.tablename FROM pg_tables t
554                   WHERE t.tablename = 'defaults'|;
555       my $sth = $dbh->prepare($query);
556       $sth->execute || $form->dberror($query);
557
558       if ($sth->fetchrow_array) {
559         $query = qq|SELECT version FROM defaults|;
560         my $sth = $dbh->prepare($query);
561         $sth->execute;
562
563         if (my ($version) = $sth->fetchrow_array) {
564           $dbsources{$db} = $version;
565         }
566         $sth->finish;
567       }
568       $sth->finish;
569       $dbh->disconnect;
570     }
571     $sth->finish;
572   }
573
574   if ($form->{dbdriver} eq 'Oracle') {
575     $query = qq|SELECT o.owner FROM dba_objects o
576                 WHERE o.object_name = 'DEFAULTS'
577                 AND o.object_type = 'TABLE'|;
578
579     $sth = $dbh->prepare($query);
580     $sth->execute || $form->dberror($query);
581
582     while (my ($db) = $sth->fetchrow_array) {
583
584       $form->{dbuser} = $db;
585       &dbconnect_vars($form, $db);
586
587       my $dbh =
588         DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
589         or $form->dberror;
590
591       $query = qq|SELECT version FROM defaults|;
592       my $sth = $dbh->prepare($query);
593       $sth->execute;
594
595       if (my ($version) = $sth->fetchrow_array) {
596         $dbsources{$db} = $version;
597       }
598       $sth->finish;
599       $dbh->disconnect;
600     }
601     $sth->finish;
602   }
603
604   $dbh->disconnect;
605
606   $main::lxdebug->leave_sub();
607
608   return %dbsources;
609 }
610
611 ## LINET
612 sub calc_version {
613   $main::lxdebug->enter_sub();
614
615   my (@v, $version, $i);
616
617   @v = split(/\./, $_[0]);
618   while (scalar(@v) < 4) {
619     push(@v, 0);
620   }
621   $version = 0;
622   for ($i = 0; $i < 4; $i++) {
623     $version *= 1000;
624     $version += $v[$i];
625   }
626
627   $main::lxdebug->leave_sub();
628   return $version;
629 }
630
631 sub cmp_script_version {
632   my ($a_from, $a_to, $b_from, $b_to);
633   my ($i, $res_a, $res_b);
634   my ($my_a, $my_b) = ($a, $b);
635
636   $my_a =~ s/.*-upgrade-//;
637   $my_a =~ s/.sql$//;
638   $my_b =~ s/.*-upgrade-//;
639   $my_b =~ s/.sql$//;
640   ($my_a_from, $my_a_to) = split(/-/, $my_a);
641   ($my_b_from, $my_b_to) = split(/-/, $my_b);
642
643   $res_a = calc_version($my_a_from);
644   $res_b = calc_version($my_b_from);
645
646   if ($res_a == $res_b) {
647     $res_a = calc_version($my_a_to);
648     $res_b = calc_version($my_b_to);
649   }
650
651   return $res_a <=> $res_b;
652 }
653 ## /LINET
654
655 sub dbupdate {
656   $main::lxdebug->enter_sub();
657
658   my ($self, $form) = @_;
659
660   $form->{sid} = $form->{dbdefault};
661
662   my @upgradescripts = ();
663   my $query;
664   my $rc = -2;
665
666   if ($form->{dbupdate}) {
667
668     # read update scripts into memory
669     opendir SQLDIR, "sql/." or &error("", "$!");
670     ## LINET
671     @upgradescripts =
672       sort(cmp_script_version
673            grep(/$form->{dbdriver}-upgrade-.*?\.sql$/, readdir(SQLDIR)));
674     ## /LINET
675     closedir SQLDIR;
676   }
677
678   foreach my $db (split / /, $form->{dbupdate}) {
679
680     next unless $form->{$db};
681
682     # strip db from dataset
683     $db =~ s/^db//;
684     &dbconnect_vars($form, $db);
685
686     my $dbh =
687       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
688       or $form->dberror;
689
690     # check version
691     $query = qq|SELECT version FROM defaults|;
692     my $sth = $dbh->prepare($query);
693
694     # no error check, let it fall through
695     $sth->execute;
696
697     my $version = $sth->fetchrow_array;
698     $sth->finish;
699
700     next unless $version;
701
702     ## LINET
703     $version = calc_version($version);
704     ## /LINET
705
706     foreach my $upgradescript (@upgradescripts) {
707       my $a = $upgradescript;
708       $a =~ s/^$form->{dbdriver}-upgrade-|\.sql$//g;
709
710       my ($mindb, $maxdb) = split /-/, $a;
711       my $str_maxdb = $maxdb;
712       ## LINET
713       $mindb = calc_version($mindb);
714       $maxdb = calc_version($maxdb);
715       ## /LINET
716
717       next if ($version >= $maxdb);
718
719       # if there is no upgrade script exit
720       last if ($version < $mindb);
721
722       # apply upgrade
723       $self->process_query($form, $dbh, "sql/$upgradescript", $str_maxdb);
724
725       $version = $maxdb;
726
727     }
728
729     $rc = 0;
730     $dbh->disconnect;
731
732   }
733
734   $main::lxdebug->leave_sub();
735
736   return $rc;
737 }
738
739 sub create_config {
740   $main::lxdebug->enter_sub();
741
742   my ($self, $filename) = @_;
743
744   @config = &config_vars;
745
746   open(CONF, ">$filename") or $self->error("$filename : $!");
747
748   # create the config file
749   print CONF qq|# configuration file for $self->{login}
750
751 \%myconfig = (
752 |;
753
754   foreach $key (sort @config) {
755     $self->{$key} =~ s/\'/\\\'/g;
756     print CONF qq|  $key => '$self->{$key}',\n|;
757   }
758
759   print CONF qq|);\n\n|;
760
761   close CONF;
762
763   $main::lxdebug->leave_sub();
764 }
765
766 sub save_member {
767   $main::lxdebug->enter_sub();
768
769   my ($self, $memberfile, $userspath) = @_;
770
771   my $newmember = 1;
772
773   # format dbconnect and dboptions string
774   &dbconnect_vars($self, $self->{dbname});
775
776   $self->error('File locked!') if (-f "${memberfile}.LCK");
777   open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
778   close(FH);
779
780   open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
781
782   @config = <CONF>;
783
784   seek(CONF, 0, 0);
785   truncate(CONF, 0);
786
787   while ($line = shift @config) {
788     if ($line =~ /^\[$self->{login}\]/) {
789       $newmember = 0;
790       last;
791     }
792     print CONF $line;
793   }
794
795   # remove everything up to next login or EOF
796   while ($line = shift @config) {
797     last if ($line =~ /^\[/);
798   }
799
800   # this one is either the next login or EOF
801   print CONF $line;
802
803   while ($line = shift @config) {
804     print CONF $line;
805   }
806
807   print CONF qq|[$self->{login}]\n|;
808
809   if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
810       && $self->{root}) {
811     $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
812     chop $self->{dbpasswd};
813   }
814   if (defined($self->{new_password})) {
815     if ($self->{new_password} ne $self->{old_password}) {
816       $self->{password} = crypt $self->{new_password},
817         substr($self->{login}, 0, 2)
818         if $self->{new_password};
819     }
820   } else {
821     if ($self->{password} ne $self->{old_password}) {
822       $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
823         if $self->{password};
824     }
825   }
826
827   if ($self->{'root login'}) {
828     @config = ("password");
829   } else {
830     @config = &config_vars;
831   }
832
833   # replace \r\n with \n
834   map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
835   foreach $key (sort @config) {
836     print CONF qq|$key=$self->{$key}\n|;
837   }
838
839   print CONF "\n";
840   close CONF;
841   unlink "${memberfile}.LCK";
842
843   # create conf file
844   $self->create_config("$userspath/$self->{login}.conf")
845     unless $self->{'root login'};
846
847   $main::lxdebug->leave_sub();
848 }
849
850 sub config_vars {
851   $main::lxdebug->enter_sub();
852
853   my @conf = qw(acs address admin businessnumber charset company countrycode
854     currency dateformat dbconnect dbdriver dbhost dbport dboptions
855     dbname dbuser dbpasswd email fax name numberformat in_numberformat password
856     printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen
857     anfragen lieferantenbestellungen einkaufsrechnungen steuernummer co_ustid duns menustyle);
858
859   $main::lxdebug->leave_sub();
860
861   return @conf;
862 }
863
864 sub error {
865   $main::lxdebug->enter_sub();
866
867   my ($self, $msg) = @_;
868
869   if ($ENV{HTTP_USER_AGENT}) {
870     print qq|Content-Type: text/html
871
872 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
873
874 <body bgcolor=ffffff>
875
876 <h2><font color=red>Error!</font></h2>
877 <p><b>$msg</b>|;
878
879   }
880
881   die "Error: $msg\n";
882
883   $main::lxdebug->leave_sub();
884 }
885
886 1;
887