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