1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  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.
 
  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 #=====================================================================
 
  31 # user related functions
 
  33 #=====================================================================
 
  49   $main::lxdebug->enter_sub();
 
  51   my ($type, $login) = @_;
 
  56     my %user_data = $main::auth->read_user($login);
 
  57     map { $self->{$_} = $user_data{$_} } keys %user_data;
 
  60   $main::lxdebug->leave_sub();
 
  66   $main::lxdebug->enter_sub();
 
  73   # scan the locale directory and read in the LANGUAGE files
 
  74   opendir(DIR, "locale");
 
  76   my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
 
  78   foreach my $dir (@dir) {
 
  79     next unless open(FH, "locale/$dir/LANGUAGE");
 
  83     $cc{$dir} = "@language";
 
  88   $main::lxdebug->leave_sub();
 
  94   $main::lxdebug->enter_sub();
 
  96   my ($self, $form) = @_;
 
 103   if ($self->{login}) {
 
 104     my %myconfig = $main::auth->read_user($self->{login});
 
 106     # check if database is down
 
 108       DBI->connect($myconfig{dbconnect}, $myconfig{dbuser},
 
 110       or $self->error(DBI::errstr);
 
 112     # we got a connection, check the version
 
 113     my $query = qq|SELECT version FROM defaults|;
 
 114     my $sth   = $dbh->prepare($query);
 
 115     $sth->execute || $form->dberror($query);
 
 117     my ($dbversion) = $sth->fetchrow_array;
 
 120     $self->create_employee_entry($form, $dbh, \%myconfig);
 
 122     $self->create_schema_info_table($form, $dbh);
 
 129       parse_dbupdate_controls($form, $myconfig{"dbdriver"});
 
 131     map({ $form->{$_} = $myconfig{$_} }
 
 132         qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect dateformat));
 
 134     if (update_available($myconfig{"dbdriver"}, $dbversion) ||
 
 135         update2_available($form, $controls)) {
 
 137       $form->{"stylesheet"} = "lx-office-erp.css";
 
 138       $form->{"title"} = $main::locale->text("Dataset upgrade");
 
 140       print $form->parse_html_template("dbupgrade/header");
 
 142       $form->{dbupdate} = "db$myconfig{dbname}";
 
 143       $form->{ $form->{dbupdate} } = 1;
 
 145       if ($form->{"show_dbupdate_warning"}) {
 
 146         print $form->parse_html_template("dbupgrade/warning");
 
 151       if (!open(FH, ">$main::userspath/nologin")) {
 
 152         $form->show_generic_error($main::locale->text('A temporary file could not be created. ' .
 
 153                                                       'Please verify that the directory "#1" is writeable by the webserver.',
 
 158       # required for Oracle
 
 159       $form->{dbdefault} = $sid;
 
 161       # ignore HUP, QUIT in case the webserver times out
 
 162       $SIG{HUP}  = 'IGNORE';
 
 163       $SIG{QUIT} = 'IGNORE';
 
 165       $self->dbupdate($form);
 
 166       $self->dbupdate2($form, $controls);
 
 171       unlink("$main::userspath/nologin");
 
 174         $self->{"menustyle"} eq "v3" ? "menuv3.pl" :
 
 175         $self->{"menustyle"} eq "neu" ? "menunew.pl" :
 
 176         $self->{"menustyle"} eq "js" ? "menujs.pl" :
 
 177         $self->{"menustyle"} eq "xml" ? "menuXML.pl" :
 
 180       print $form->parse_html_template("dbupgrade/footer", { "menufile" => $menufile });
 
 187   $main::lxdebug->leave_sub();
 
 193   $main::lxdebug->enter_sub();
 
 195   my ($form, $db) = @_;
 
 198         'Pg' => { 'yy-mm-dd'   => 'set DateStyle to \'ISO\'',
 
 199                   'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
 
 200                   'mm/dd/yy'   => 'set DateStyle to \'SQL, US\'',
 
 201                   'mm-dd-yy'   => 'set DateStyle to \'POSTGRES, US\'',
 
 202                   'dd/mm/yy'   => 'set DateStyle to \'SQL, EUROPEAN\'',
 
 203                   'dd-mm-yy'   => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
 
 204                   'dd.mm.yy'   => 'set DateStyle to \'GERMAN\''
 
 207           'yy-mm-dd'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
 
 208           'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
 
 209           'mm/dd/yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
 
 210           'mm-dd-yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
 
 211           'dd/mm/yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
 
 212           'dd-mm-yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
 
 213           'dd.mm.yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
 
 216   $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
 
 218   if ($form->{dbdriver} eq 'Pg') {
 
 219     $form->{dbconnect} = "dbi:Pg:dbname=$db";
 
 222   if ($form->{dbdriver} eq 'Oracle') {
 
 223     $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
 
 226   if ($form->{dbhost}) {
 
 227     $form->{dbconnect} .= ";host=$form->{dbhost}";
 
 229   if ($form->{dbport}) {
 
 230     $form->{dbconnect} .= ";port=$form->{dbport}";
 
 233   $main::lxdebug->leave_sub();
 
 237   $main::lxdebug->enter_sub();
 
 239   my @drivers = DBI->available_drivers();
 
 241   $main::lxdebug->leave_sub();
 
 243   return (grep { /(Pg|Oracle)/ } @drivers);
 
 247   $main::lxdebug->enter_sub();
 
 249   my ($self, $form) = @_;
 
 254   $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
 
 255   $form->{sid} = $form->{dbdefault};
 
 256   &dbconnect_vars($form, $form->{dbdefault});
 
 259     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 262   if ($form->{dbdriver} eq 'Pg') {
 
 264       qq|SELECT datname FROM pg_database | .
 
 265       qq|WHERE NOT datname IN ('template0', 'template1')|;
 
 266     $sth = $dbh->prepare($query);
 
 267     $sth->execute() || $form->dberror($query);
 
 269     while (my ($db) = $sth->fetchrow_array) {
 
 271       if ($form->{only_acc_db}) {
 
 273         next if ($db =~ /^template/);
 
 275         &dbconnect_vars($form, $db);
 
 277           DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 281           qq|SELECT tablename FROM pg_tables | .
 
 282           qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
 
 283         my $sth = $dbh->prepare($query);
 
 284         $sth->execute($form->{dbuser}) ||
 
 285           $form->dberror($query . " ($form->{dbuser})");
 
 287         if ($sth->fetchrow_array) {
 
 288           push(@dbsources, $db);
 
 294       push(@dbsources, $db);
 
 298   if ($form->{dbdriver} eq 'Oracle') {
 
 299     if ($form->{only_acc_db}) {
 
 301         qq|SELECT owner FROM dba_objects | .
 
 302         qq|WHERE object_name = 'DEFAULTS' AND object_type = 'TABLE'|;
 
 304       $query = qq|SELECT username FROM dba_users|;
 
 307     $sth = $dbh->prepare($query);
 
 308     $sth->execute || $form->dberror($query);
 
 310     while (my ($db) = $sth->fetchrow_array) {
 
 311       push(@dbsources, $db);
 
 318   $main::lxdebug->leave_sub();
 
 323 sub dbclusterencoding {
 
 324   $main::lxdebug->enter_sub();
 
 326   my ($self, $form) = @_;
 
 328   $form->{dbdefault} ||= $form->{dbuser};
 
 330   dbconnect_vars($form, $form->{dbdefault});
 
 332   my $dbh                = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) || $form->dberror();
 
 333   my $query              = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
 
 334   my ($cluster_encoding) = $dbh->selectrow_array($query);
 
 337   $main::lxdebug->leave_sub();
 
 339   return $cluster_encoding;
 
 343   $main::lxdebug->enter_sub();
 
 345   my ($self, $form) = @_;
 
 347   $form->{sid} = $form->{dbdefault};
 
 348   &dbconnect_vars($form, $form->{dbdefault});
 
 350     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 352   $form->{db} =~ s/\"//g;
 
 354     'Pg'     => qq|CREATE DATABASE "$form->{db}"|,
 
 356     qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS | .
 
 357     qq|TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
 
 364   push(@{$dboptions{"Pg"}}, "ENCODING = " . $dbh->quote($form->{"encoding"}))
 
 365     if ($form->{"encoding"});
 
 366   if ($form->{"dbdefault"}) {
 
 367     my $dbdefault = $form->{"dbdefault"};
 
 368     $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
 
 369     push(@{$dboptions{"Pg"}}, "TEMPLATE = $dbdefault");
 
 372   my $query = $dbcreate{$form->{dbdriver}};
 
 373   $query .= " WITH " . join(" ", @{$dboptions{"Pg"}}) if (@{$dboptions{"Pg"}});
 
 375   # Ignore errors if the database exists.
 
 378   if ($form->{dbdriver} eq 'Oracle') {
 
 379     $query = qq|GRANT CONNECT, RESOURCE TO "$form->{db}"|;
 
 380     do_query($form, $dbh, $query);
 
 384   # setup variables for the new database
 
 385   if ($form->{dbdriver} eq 'Oracle') {
 
 386     $form->{dbuser}   = $form->{db};
 
 387     $form->{dbpasswd} = $form->{db};
 
 390   &dbconnect_vars($form, $form->{db});
 
 392   $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 395   my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
 
 396   $db_charset ||= Common::DEFAULT_CHARSET;
 
 399   $self->process_query($form, $dbh, "sql/lx-office.sql", undef, $db_charset);
 
 401   # load chart of accounts
 
 402   $self->process_query($form, $dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
 
 404   $query = "UPDATE defaults SET coa = ?";
 
 405   do_query($form, $dbh, $query, $form->{chart});
 
 409   $main::lxdebug->leave_sub();
 
 412 # Process a Perl script which updates the database.
 
 413 # If the script returns 1 then the update was successful.
 
 414 # Return code "2" means "needs more interaction; remove
 
 415 # users/nologin and exit".
 
 416 # All other return codes are fatal errors.
 
 417 sub process_perl_script {
 
 418   $main::lxdebug->enter_sub();
 
 420   my ($self, $form, $dbh, $filename, $version_or_control, $db_charset) = @_;
 
 422   my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
 
 424   my $file_charset = Common::DEFAULT_CHARSET;
 
 426   if (ref($version_or_control) eq "HASH") {
 
 427     $file_charset = $version_or_control->{charset};
 
 432       next if !/^--\s*\@charset:\s*(.+)/;
 
 436     $fh->seek(0, SEEK_SET);
 
 439   my $contents = join "", <$fh>;
 
 442   $db_charset ||= Common::DEFAULT_CHARSET;
 
 444   my $iconv = SL::Iconv::get_converter($file_charset, $db_charset);
 
 448   my %dbup_myconfig = ();
 
 449   map({ $dbup_myconfig{$_} = $form->{$_}; }
 
 450       qw(dbname dbuser dbpasswd dbhost dbport dbconnect));
 
 452   my $nls_file = $filename;
 
 453   $nls_file =~ s|.*/||;
 
 454   $nls_file =~ s|.pl$||;
 
 455   my $dbup_locale = Locale->new($main::language, $nls_file);
 
 457   my $result = eval($contents);
 
 464   if (!defined($result)) {
 
 465     print $form->parse_html_template("dbupgrade/error",
 
 466                                      { "file"  => $filename,
 
 469   } elsif (1 != $result) {
 
 470     unlink("users/nologin") if (2 == $result);
 
 474   if (ref($version_or_control) eq "HASH") {
 
 475     $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" .
 
 476              $dbh->quote($version_or_control->{"tag"}) . ", " .
 
 477              $dbh->quote($form->{"login"}) . ")");
 
 478   } elsif ($version_or_control) {
 
 479     $dbh->do("UPDATE defaults SET version = " .
 
 480              $dbh->quote($version_or_control));
 
 484   $main::lxdebug->leave_sub();
 
 488   $main::lxdebug->enter_sub();
 
 490   my ($self, $form, $dbh, $filename, $version_or_control, $db_charset) = @_;
 
 492   my $fh = IO::File->new($filename, "r") or $form->error("$filename : $!\n");
 
 497   my $file_charset = Common::DEFAULT_CHARSET;
 
 500     next if !/^--\s*\@charset:\s*(.+)/;
 
 504   $fh->seek(0, SEEK_SET);
 
 506   $db_charset ||= Common::DEFAULT_CHARSET;
 
 511     $_ = SL::Iconv::convert($file_charset, $db_charset, $_);
 
 513     # Remove DOS and Unix style line endings.
 
 519     for (my $i = 0; $i < length($_); $i++) {
 
 520       my $char = substr($_, $i, 1);
 
 522       # Are we inside a string?
 
 524         if ($char eq $quote_chars[-1]) {
 
 530         if (($char eq "'") || ($char eq "\"")) {
 
 531           push(@quote_chars, $char);
 
 533         } elsif ($char eq ";") {
 
 535           # Query is complete. Send it.
 
 537           $sth = $dbh->prepare($query);
 
 538           if (!$sth->execute()) {
 
 539             my $errstr = $dbh->errstr;
 
 542             $form->dberror("The database update/creation did not succeed. " .
 
 543                            "The file ${filename} containing the following " .
 
 544                            "query failed:<br>${query}<br>" .
 
 545                            "The error message was: ${errstr}<br>" .
 
 546                            "All changes in that file have been reverted.");
 
 558     # Insert a space at the end of each line so that queries split
 
 559     # over multiple lines work properly.
 
 561       $query .= @quote_chars ? "\n" : ' ';
 
 565   if (ref($version_or_control) eq "HASH") {
 
 566     $dbh->do("INSERT INTO schema_info (tag, login) VALUES (" .
 
 567              $dbh->quote($version_or_control->{"tag"}) . ", " .
 
 568              $dbh->quote($form->{"login"}) . ")");
 
 569   } elsif ($version_or_control) {
 
 570     $dbh->do("UPDATE defaults SET version = " .
 
 571              $dbh->quote($version_or_control));
 
 577   $main::lxdebug->leave_sub();
 
 581   $main::lxdebug->enter_sub();
 
 583   my ($self, $form) = @_;
 
 584   $form->{db} =~ s/\"//g;
 
 585   my %dbdelete = ('Pg'     => qq|DROP DATABASE "$form->{db}"|,
 
 586                   'Oracle' => qq|DROP USER "$form->{db}" CASCADE|);
 
 588   $form->{sid} = $form->{dbdefault};
 
 589   &dbconnect_vars($form, $form->{dbdefault});
 
 591     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 593   my $query = $dbdelete{$form->{dbdriver}};
 
 594   do_query($form, $dbh, $query);
 
 598   $main::lxdebug->leave_sub();
 
 601 sub dbsources_unused {
 
 602   $main::lxdebug->enter_sub();
 
 604   my ($self, $form) = @_;
 
 606   $form->{only_acc_db} = 1;
 
 608   my %members = $main::auth->read_all_users();
 
 609   my %dbexcl  = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
 
 611   $dbexcl{$form->{dbdefault}}             = 1;
 
 612   $dbexcl{$main::auth->{DB_config}->{db}} = 1;
 
 614   my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
 
 616   $main::lxdebug->leave_sub();
 
 622   $main::lxdebug->enter_sub();
 
 624   my ($self, $form) = @_;
 
 626   my %members  = $main::auth->read_all_users();
 
 627   my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
 
 629   my ($query, $sth, %dbs_needing_updates);
 
 631   foreach my $login (grep /[a-z]/, keys %members) {
 
 632     my $member = $members{$login};
 
 634     map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
 
 635     dbconnect_vars($form, $form->{dbname});
 
 637     my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
 
 643     $query = qq|SELECT version FROM defaults|;
 
 644     $sth = prepare_query($form, $dbh, $query);
 
 645     if ($sth->execute()) {
 
 646       ($version) = $sth->fetchrow_array();
 
 651     next unless $version;
 
 653     if (update_available($form->{dbdriver}, $version) || update2_available($form, $controls)) {
 
 655       map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
 
 656       $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
 
 660   $main::lxdebug->leave_sub();
 
 662   return values %dbs_needing_updates;
 
 666   $main::lxdebug->enter_sub(2);
 
 668   my (@v, $version, $i);
 
 670   @v = split(/\./, $_[0]);
 
 671   while (scalar(@v) < 4) {
 
 675   for ($i = 0; $i < 4; $i++) {
 
 680   $main::lxdebug->leave_sub(2);
 
 684 sub cmp_script_version {
 
 685   my ($a_from, $a_to, $b_from, $b_to);
 
 686   my ($i, $res_a, $res_b);
 
 687   my ($my_a, $my_b) = ($a, $b);
 
 689   $my_a =~ s/.*-upgrade-//;
 
 691   $my_b =~ s/.*-upgrade-//;
 
 693   my ($my_a_from, $my_a_to) = split(/-/, $my_a);
 
 694   my ($my_b_from, $my_b_to) = split(/-/, $my_b);
 
 696   $res_a = calc_version($my_a_from);
 
 697   $res_b = calc_version($my_b_from);
 
 699   if ($res_a == $res_b) {
 
 700     $res_a = calc_version($my_a_to);
 
 701     $res_b = calc_version($my_b_to);
 
 704   return $res_a <=> $res_b;
 
 707 sub update_available {
 
 708   my ($dbdriver, $cur_version) = @_;
 
 712   opendir SQLDIR, "sql/${dbdriver}-upgrade" || error("", "sql/${dbdriver}-upgrade: $!");
 
 713   my @upgradescripts = grep /${dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)$/, readdir SQLDIR;
 
 716   return ($#upgradescripts > -1);
 
 719 sub create_schema_info_table {
 
 720   $main::lxdebug->enter_sub();
 
 722   my ($self, $form, $dbh) = @_;
 
 724   my $query = "SELECT tag FROM schema_info LIMIT 1";
 
 725   if (!$dbh->do($query)) {
 
 728       qq|CREATE TABLE schema_info (| .
 
 731       qq|  itime timestamp DEFAULT now(), | .
 
 732       qq|  PRIMARY KEY (tag))|;
 
 733     $dbh->do($query) || $form->dberror($query);
 
 736   $main::lxdebug->leave_sub();
 
 740   $main::lxdebug->enter_sub();
 
 742   my ($self, $form) = @_;
 
 746   $form->{sid} = $form->{dbdefault};
 
 748   my @upgradescripts = ();
 
 752   if ($form->{dbupdate}) {
 
 754     # read update scripts into memory
 
 755     opendir(SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade")
 
 756       or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
 
 758       sort(cmp_script_version
 
 759            grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/,
 
 764   my $db_charset = $main::dbcharset;
 
 765   $db_charset ||= Common::DEFAULT_CHARSET;
 
 767   foreach my $db (split(/ /, $form->{dbupdate})) {
 
 769     next unless $form->{$db};
 
 771     # strip db from dataset
 
 773     &dbconnect_vars($form, $db);
 
 776       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 779     $dbh->do($form->{dboptions}) if ($form->{dboptions});
 
 782     $query = qq|SELECT version FROM defaults|;
 
 783     my ($version) = selectrow_query($form, $dbh, $query);
 
 785     next unless $version;
 
 787     $version = calc_version($version);
 
 789     foreach my $upgradescript (@upgradescripts) {
 
 790       my $a = $upgradescript;
 
 791       $a =~ s/^\Q$form->{dbdriver}\E-upgrade-|\.(sql|pl)$//g;
 
 794       my ($mindb, $maxdb) = split /-/, $a;
 
 795       my $str_maxdb = $maxdb;
 
 796       $mindb = calc_version($mindb);
 
 797       $maxdb = calc_version($maxdb);
 
 799       next if ($version >= $maxdb);
 
 801       # if there is no upgrade script exit
 
 802       last if ($version < $mindb);
 
 805       $main::lxdebug->message(LXDebug::DEBUG2, "Applying Update $upgradescript");
 
 806       if ($file_type eq "sql") {
 
 807         $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} .
 
 808                              "-upgrade/$upgradescript", $str_maxdb, $db_charset);
 
 810         $self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} .
 
 811                                    "-upgrade/$upgradescript", $str_maxdb, $db_charset);
 
 823   $main::lxdebug->leave_sub();
 
 829   $main::lxdebug->enter_sub();
 
 831   my ($self, $form, $controls) = @_;
 
 833   $form->{sid} = $form->{dbdefault};
 
 835   my @upgradescripts = ();
 
 836   my ($query, $sth, $tag);
 
 839   @upgradescripts = sort_dbupdate_controls($controls);
 
 841   my $db_charset = $main::dbcharset;
 
 842   $db_charset ||= Common::DEFAULT_CHARSET;
 
 844   foreach my $db (split / /, $form->{dbupdate}) {
 
 846     next unless $form->{$db};
 
 848     # strip db from dataset
 
 850     &dbconnect_vars($form, $db);
 
 853       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 856     $dbh->do($form->{dboptions}) if ($form->{dboptions});
 
 858     map({ $_->{"applied"} = 0; } @upgradescripts);
 
 860     $self->create_schema_info_table($form, $dbh);
 
 862     $query = qq|SELECT tag FROM schema_info|;
 
 863     $sth = $dbh->prepare($query);
 
 864     $sth->execute() || $form->dberror($query);
 
 865     while (($tag) = $sth->fetchrow_array()) {
 
 866       $controls->{$tag}->{"applied"} = 1 if (defined($controls->{$tag}));
 
 871     foreach (@upgradescripts) {
 
 872       if (!$_->{"applied"}) {
 
 878     next if ($all_applied);
 
 880     foreach my $control (@upgradescripts) {
 
 881       next if ($control->{"applied"});
 
 883       $control->{description} = SL::Iconv::convert($control->{charset}, $db_charset, $control->{description});
 
 885       $control->{"file"} =~ /\.(sql|pl)$/;
 
 889       $main::lxdebug->message(LXDebug::DEBUG2, "Applying Update $control->{file}");
 
 890       print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
 
 892       if ($file_type eq "sql") {
 
 893         $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} .
 
 894                              "-upgrade2/$control->{file}", $control, $db_charset);
 
 896         $self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} .
 
 897                                    "-upgrade2/$control->{file}", $control, $db_charset);
 
 906   $main::lxdebug->leave_sub();
 
 911 sub update2_available {
 
 912   $main::lxdebug->enter_sub();
 
 914   my ($form, $controls) = @_;
 
 916   map({ $_->{"applied"} = 0; } values(%{$controls}));
 
 918   dbconnect_vars($form, $form->{"dbname"});
 
 921     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}) ||
 
 924   my ($query, $tag, $sth);
 
 926   $query = qq|SELECT tag FROM schema_info|;
 
 927   $sth = $dbh->prepare($query);
 
 928   if ($sth->execute()) {
 
 929     while (($tag) = $sth->fetchrow_array()) {
 
 930       $controls->{$tag}->{"applied"} = 1 if (defined($controls->{$tag}));
 
 936   map({ $main::lxdebug->leave_sub() and return 1 if (!$_->{"applied"}) }
 
 937       values(%{$controls}));
 
 939   $main::lxdebug->leave_sub();
 
 944   $main::lxdebug->enter_sub();
 
 947   my $form   = \%main::form;
 
 949   # format dbconnect and dboptions string
 
 950   dbconnect_vars($self, $self->{dbname});
 
 952   map { $self->{$_} =~ s/\r//g; } qw(address signature);
 
 954   $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
 
 956   my $dbh = DBI->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd});
 
 958     $self->create_employee_entry($form, $dbh, $self, 1);
 
 962   $main::lxdebug->leave_sub();
 
 965 sub create_employee_entry {
 
 966   $main::lxdebug->enter_sub();
 
 971   my $myconfig        = shift;
 
 972   my $update_existing = shift;
 
 974   if (!does_table_exist($dbh, 'employee')) {
 
 975     $main::lxdebug->leave_sub();
 
 979   # add login to employee table if it does not exist
 
 980   # no error check for employee table, ignore if it does not exist
 
 981   my ($id)  = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
 
 984     my $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
 
 985     do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
 
 987   } elsif ($update_existing) {
 
 988     my $query = qq|UPDATE employee SET name = ?, workphone = ?, role = 'user' WHERE id = ?|;
 
 989     do_query($form, $dbh, $query, $myconfig->{name}, $myconfig->{tel}, $id);
 
 992   $main::lxdebug->leave_sub();
 
 996   $main::lxdebug->enter_sub();
 
 998   my @conf = qw(address admin businessnumber company countrycode
 
 999     currency dateformat dbconnect dbdriver dbhost dbport dboptions
 
1000     dbname dbuser dbpasswd email fax name numberformat password
 
1001     printer role sid signature stylesheet tel templates vclimit angebote
 
1002     bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen
 
1003     taxnumber co_ustid duns menustyle template_format default_media
 
1004     default_printer_id copies show_form_details favorites
 
1005     pdonumber sdonumber);
 
1007   $main::lxdebug->leave_sub();
 
1013   $main::lxdebug->enter_sub();
 
1015   my ($self, $msg) = @_;
 
1017   $main::lxdebug->show_backtrace();
 
1019   if ($ENV{HTTP_USER_AGENT}) {
 
1020     print qq|Content-Type: text/html
 
1022 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
 
1024 <body bgcolor=ffffff>
 
1026 <h2><font color=red>Error!</font></h2>
 
1031   die "Error: $msg\n";
 
1033   $main::lxdebug->leave_sub();