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