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