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 #=====================================================================
 
  41 use SL::DB::AuthClient;
 
  48 use SL::System::InstallationLock;
 
  49 use SL::DefaultManager;
 
  53 use constant LOGIN_OK                      =>  0;
 
  54 use constant LOGIN_BASIC_TABLES_MISSING    => -1;
 
  55 use constant LOGIN_DBUPDATE_AVAILABLE      => -2;
 
  56 use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3;
 
  57 use constant LOGIN_GENERAL_ERROR           => -4;
 
  60   $main::lxdebug->enter_sub();
 
  62   my ($type, %params) = @_;
 
  66   if ($params{id} || $params{login}) {
 
  67     my %user_data = $main::auth->read_user(%params);
 
  68     map { $self->{$_} = $user_data{$_} } keys %user_data;
 
  71   $main::lxdebug->leave_sub();
 
  77   $main::lxdebug->enter_sub();
 
  84   # scan the locale directory and read in the LANGUAGE files
 
  85   opendir(DIR, "locale");
 
  87   my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
 
  89   foreach my $dir (@dir) {
 
  90     next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
 
  94     $cc{$dir} = "@language";
 
  99   $main::lxdebug->leave_sub();
 
 105   my ($self, $form) = @_;
 
 107   return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
 
 109   my %myconfig = $main::auth->read_user(login => $self->{login});
 
 111   # Auth DB upgrades available?
 
 112   my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
 
 113   return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
 
 115   # check if database is down
 
 116   my $dbh = $form->dbconnect_noauto;
 
 118   # we got a connection, check the version
 
 119   my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
 
 122     return LOGIN_BASIC_TABLES_MISSING();
 
 125   $self->create_schema_info_table($form, $dbh);
 
 127   my $dbupdater        = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
 
 128   my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
 
 131   if (!@unapplied_scripts) {
 
 132     SL::DB::Manager::Employee->update_entries_for_authorized_users;
 
 136   # Store the fact that we're applying database upgrades at the
 
 137   # moment. That way functions called from the layout modules that may
 
 138   # require updated tables can chose only to use basic features.
 
 139   $::request->applying_database_upgrades(1);
 
 141   $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
 
 142   $form->{$_} = $myconfig{$_}         for qw(datestyle);
 
 144   $form->{"title"} = $main::locale->text("Dataset upgrade");
 
 145   $form->header(no_layout => $form->{no_layout});
 
 146   print $form->parse_html_template("dbupgrade/header");
 
 148   $form->{dbupdate} = "db" . $::auth->client->{dbname};
 
 150   if ($form->{"show_dbupdate_warning"}) {
 
 151     print $form->parse_html_template("dbupgrade/warning", { unapplied_scripts => \@unapplied_scripts });
 
 152     $::dispatcher->end_request;
 
 156   SL::System::InstallationLock->lock;
 
 158   # ignore HUP, QUIT in case the webserver times out
 
 159   $SIG{HUP}  = 'IGNORE';
 
 160   $SIG{QUIT} = 'IGNORE';
 
 162   $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
 
 164   # If $self->dbupdate2 returns than this means all upgrade scripts
 
 165   # have been applied successfully, none required user
 
 166   # interaction. Otherwise the deeper layers would have called
 
 167   # $::dispatcher->end_request already, and return would not have returned to
 
 168   # us. Therefore we can now use RDBO instances because their supposed
 
 169   # table structures do match the actual structures. So let's ensure
 
 170   # that the "employee" table contains the appropriate entries for all
 
 171   # users authorized for the current client.
 
 172   SL::DB::Manager::Employee->update_entries_for_authorized_users;
 
 174   SL::System::InstallationLock->unlock;
 
 176   print $form->parse_html_template("dbupgrade/footer");
 
 178   return LOGIN_DBUPDATE_AVAILABLE();
 
 182   $main::lxdebug->enter_sub();
 
 184   my ($form, $db) = @_;
 
 187     'yy-mm-dd'   => 'set DateStyle to \'ISO\'',
 
 188     'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
 
 189     'mm/dd/yy'   => 'set DateStyle to \'SQL, US\'',
 
 190     'dd/mm/yy'   => 'set DateStyle to \'SQL, EUROPEAN\'',
 
 191     'dd.mm.yy'   => 'set DateStyle to \'GERMAN\''
 
 194   $form->{dboptions} = $dboptions{ $form->{dateformat} };
 
 195   $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
 
 197   $main::lxdebug->leave_sub();
 
 201   $main::lxdebug->enter_sub();
 
 203   my ($self, $form) = @_;
 
 208   $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
 
 209   &dbconnect_vars($form, $form->{dbdefault});
 
 211   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 215     qq|SELECT datname FROM pg_database | .
 
 216     qq|WHERE NOT datname IN ('template0', 'template1')|;
 
 217   $sth = $dbh->prepare($query);
 
 218   $sth->execute() || $form->dberror($query);
 
 220   while (my ($db) = $sth->fetchrow_array) {
 
 222     if ($form->{only_acc_db}) {
 
 224       next if ($db =~ /^template/);
 
 226       &dbconnect_vars($form, $db);
 
 227       my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 231         qq|SELECT tablename FROM pg_tables | .
 
 232         qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
 
 233       my $sth = $dbh->prepare($query);
 
 234       $sth->execute($form->{dbuser}) ||
 
 235         $form->dberror($query . " ($form->{dbuser})");
 
 237       if ($sth->fetchrow_array) {
 
 238         push(@dbsources, $db);
 
 244     push(@dbsources, $db);
 
 250   $main::lxdebug->leave_sub();
 
 256   $main::lxdebug->enter_sub();
 
 258   my ($self, $form) = @_;
 
 260   &dbconnect_vars($form, $form->{dbdefault});
 
 262     SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 264   $form->{db} =~ s/\"//g;
 
 268   push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
 
 269   if ($form->{"dbdefault"}) {
 
 270     my $dbdefault = $form->{"dbdefault"};
 
 271     $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
 
 272     push @dboptions, "TEMPLATE = $dbdefault";
 
 275   my $query = qq|CREATE DATABASE "$form->{db}"|;
 
 276   $query   .= " WITH " . join(" ", @dboptions) if @dboptions;
 
 278   # Ignore errors if the database exists.
 
 283   &dbconnect_vars($form, $form->{db});
 
 285   # make a shim myconfig so that rose db connections work
 
 286   $::myconfig{$_}     = $form->{$_} for qw(dbhost dbport dbuser dbpasswd);
 
 287   $::myconfig{dbname} = $form->{db};
 
 289   $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 292   my $dbupdater = SL::DBUpgrade2->new(form => $form, return_on_error => 1, silent => 1)->parse_dbupdate_controls;
 
 294   $dbupdater->process_query($dbh, "sql/lx-office.sql");
 
 295   $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
 
 297   $query = qq|UPDATE defaults SET coa = ?|;
 
 298   do_query($form, $dbh, $query, map { $form->{$_} } qw(chart));
 
 302   # update new database
 
 303   $self->dbupdate2(form => $form, updater => $dbupdater, database => $form->{db}, silent => 1);
 
 305   $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 308   $query = "SELECT * FROM currencies WHERE name = ?";
 
 309   my $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency});
 
 311     do_query($form, $dbh, "INSERT INTO currencies (name) VALUES (?)", $form->{defaultcurrency});
 
 312     $curr = selectfirst_hashref_query($form, $dbh, $query, $form->{defaultcurrency});
 
 315   $query = qq|UPDATE defaults SET
 
 316     accounting_method = ?,
 
 317     profit_determination = ?,
 
 318     inventory_system = ?,
 
 323     feature_erfolgsrechnung = ?,
 
 324     feature_eurechnung = ?,
 
 327   do_query($form, $dbh, $query,
 
 328     $form->{accounting_method},
 
 329     $form->{profit_determination},
 
 330     $form->{inventory_system},
 
 331     $form->parse_amount(\%::myconfig, $form->{precision_as_number}),
 
 333     $form->{feature_balance},
 
 334     $form->{feature_datev},
 
 335     $form->{feature_erfolgsrechnung},
 
 336     $form->{feature_eurechnung},
 
 337     $form->{feature_ustva}
 
 342   $main::lxdebug->leave_sub();
 
 346   $main::lxdebug->enter_sub();
 
 348   my ($self, $form) = @_;
 
 349   $form->{db} =~ s/\"//g;
 
 351   &dbconnect_vars($form, $form->{dbdefault});
 
 352   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 354   my $query = qq|DROP DATABASE "$form->{db}"|;
 
 355   do_query($form, $dbh, $query);
 
 359   $main::lxdebug->leave_sub();
 
 363   $main::lxdebug->enter_sub(2);
 
 365   my (@v, $version, $i);
 
 367   @v = split(/\./, $_[0]);
 
 368   while (scalar(@v) < 4) {
 
 372   for ($i = 0; $i < 4; $i++) {
 
 377   $main::lxdebug->leave_sub(2);
 
 381 sub cmp_script_version {
 
 382   my ($a_from, $a_to, $b_from, $b_to);
 
 383   my ($i, $res_a, $res_b);
 
 384   my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
 
 386   $my_a =~ s/.*-upgrade-//;
 
 388   $my_b =~ s/.*-upgrade-//;
 
 390   my ($my_a_from, $my_a_to) = split(/-/, $my_a);
 
 391   my ($my_b_from, $my_b_to) = split(/-/, $my_b);
 
 393   $res_a = calc_version($my_a_from);
 
 394   $res_b = calc_version($my_b_from);
 
 396   if ($res_a == $res_b) {
 
 397     $res_a = calc_version($my_a_to);
 
 398     $res_b = calc_version($my_b_to);
 
 401   return $res_a <=> $res_b;
 
 404 sub create_schema_info_table {
 
 405   $main::lxdebug->enter_sub();
 
 407   my ($self, $form, $dbh) = @_;
 
 409   my $query = "SELECT tag FROM schema_info LIMIT 1";
 
 410   if (!$dbh->do($query)) {
 
 413       qq|CREATE TABLE schema_info (| .
 
 416       qq|  itime timestamp DEFAULT now(), | .
 
 417       qq|  PRIMARY KEY (tag))|;
 
 418     $dbh->do($query) || $form->dberror($query);
 
 421   $main::lxdebug->leave_sub();
 
 425   my ($self, %params) = @_;
 
 427   my $form            = $params{form};
 
 428   my $dbupdater       = $params{updater};
 
 429   my $db              = $params{database};
 
 430   my $silent          = $params{silent};
 
 432   map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
 
 434   &dbconnect_vars($form, $db);
 
 436   # Flush potentially held database locks.
 
 437 #   $form->get_standard_dbh->commit;
 
 439   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
 
 441   $dbh->do($form->{dboptions}) if ($form->{dboptions});
 
 443   $self->create_schema_info_table($form, $dbh);
 
 445   my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
 
 447   foreach my $control (@upgradescripts) {
 
 448     # Apply upgrade. Control will only return to us if the upgrade has
 
 449     # been applied correctly and if the update has not requested user
 
 451     $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
 
 452     print $form->parse_html_template("dbupgrade/upgrade_message2", $control) unless $silent;
 
 454     $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
 
 464 sub get_default_myconfig {
 
 465   my ($self_or_class, %user_config) = @_;
 
 466   my $defaults = SL::DefaultManager->new($::lx_office_conf{system}->{default_manager});
 
 469     countrycode  => $defaults->language('de'),
 
 470     css_path     => 'css',      # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user
 
 471     dateformat   => $defaults->dateformat('dd.mm.yy'),
 
 472     numberformat => $defaults->numberformat('1.000,00'),
 
 473     stylesheet   => $defaults->stylesheet('kivitendo.css'),
 
 474     timeformat   => $defaults->timeformat('hh:mm'),