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;
 
  47 use SL::System::InstallationLock;
 
  52   $main::lxdebug->enter_sub();
 
  54   my ($type, %params) = @_;
 
  58   if ($params{id} || $params{login}) {
 
  59     my %user_data = $main::auth->read_user(%params);
 
  60     map { $self->{$_} = $user_data{$_} } keys %user_data;
 
  63   $main::lxdebug->leave_sub();
 
  69   $main::lxdebug->enter_sub();
 
  76   # scan the locale directory and read in the LANGUAGE files
 
  77   opendir(DIR, "locale");
 
  79   my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
 
  81   foreach my $dir (@dir) {
 
  82     next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
 
  86     $cc{$dir} = "@language";
 
  91   $main::lxdebug->leave_sub();
 
  97   my ($self, $form) = @_;
 
  99   return -3 if !$self->{login} || !$::auth->client;
 
 101   my %myconfig = $main::auth->read_user(login => $self->{login});
 
 103   # check if database is down
 
 104   my $dbh = $form->dbconnect_noauto;
 
 106   # we got a connection, check the version
 
 107   my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
 
 109   $self->create_schema_info_table($form, $dbh);
 
 111   # Auth DB upgrades available?
 
 112   my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
 
 113   return -3 if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
 
 115   my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
 
 117   my $update_available = $dbupdater->update2_available($dbh);
 
 120   return 0 if !$update_available;
 
 122   $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
 
 123   $form->{$_} = $myconfig{$_}         for qw(datestyle);
 
 125   $form->{"title"} = $main::locale->text("Dataset upgrade");
 
 126   $form->header(no_layout => $form->{no_layout});
 
 127   print $form->parse_html_template("dbupgrade/header");
 
 129   $form->{dbupdate} = "db" . $::auth->client->{dbname};
 
 131   if ($form->{"show_dbupdate_warning"}) {
 
 132     print $form->parse_html_template("dbupgrade/warning");
 
 137   SL::System::InstallationLock->lock;
 
 139   # ignore HUP, QUIT in case the webserver times out
 
 140   $SIG{HUP}  = 'IGNORE';
 
 141   $SIG{QUIT} = 'IGNORE';
 
 143   $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
 
 144   SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(0);
 
 146   SL::System::InstallationLock->unlock;
 
 148   print $form->parse_html_template("dbupgrade/footer");
 
 154   $main::lxdebug->enter_sub();
 
 156   my ($form, $db) = @_;
 
 159     'yy-mm-dd'   => 'set DateStyle to \'ISO\'',
 
 160     'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
 
 161     'mm/dd/yy'   => 'set DateStyle to \'SQL, US\'',
 
 162     'dd/mm/yy'   => 'set DateStyle to \'SQL, EUROPEAN\'',
 
 163     'dd.mm.yy'   => 'set DateStyle to \'GERMAN\''
 
 166   $form->{dboptions} = $dboptions{ $form->{dateformat} };
 
 167   $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
 
 169   $main::lxdebug->leave_sub();
 
 173   $main::lxdebug->enter_sub();
 
 175   my ($self, $form) = @_;
 
 180   $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
 
 181   &dbconnect_vars($form, $form->{dbdefault});
 
 183   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 187     qq|SELECT datname FROM pg_database | .
 
 188     qq|WHERE NOT datname IN ('template0', 'template1')|;
 
 189   $sth = $dbh->prepare($query);
 
 190   $sth->execute() || $form->dberror($query);
 
 192   while (my ($db) = $sth->fetchrow_array) {
 
 194     if ($form->{only_acc_db}) {
 
 196       next if ($db =~ /^template/);
 
 198       &dbconnect_vars($form, $db);
 
 199       my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 203         qq|SELECT tablename FROM pg_tables | .
 
 204         qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
 
 205       my $sth = $dbh->prepare($query);
 
 206       $sth->execute($form->{dbuser}) ||
 
 207         $form->dberror($query . " ($form->{dbuser})");
 
 209       if ($sth->fetchrow_array) {
 
 210         push(@dbsources, $db);
 
 216     push(@dbsources, $db);
 
 222   $main::lxdebug->leave_sub();
 
 228   $main::lxdebug->enter_sub();
 
 230   my ($self, $form) = @_;
 
 232   &dbconnect_vars($form, $form->{dbdefault});
 
 234     SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 236   $form->{db} =~ s/\"//g;
 
 240   push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
 
 241   if ($form->{"dbdefault"}) {
 
 242     my $dbdefault = $form->{"dbdefault"};
 
 243     $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
 
 244     push @dboptions, "TEMPLATE = $dbdefault";
 
 247   my $query = qq|CREATE DATABASE "$form->{db}"|;
 
 248   $query   .= " WITH " . join(" ", @dboptions) if @dboptions;
 
 250   # Ignore errors if the database exists.
 
 255   &dbconnect_vars($form, $form->{db});
 
 257   $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 260   my $dbupdater = SL::DBUpgrade2->new(form => $form);
 
 262   $dbupdater->process_query($dbh, "sql/lx-office.sql");
 
 264   # load chart of accounts
 
 265   $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
 
 267   my $query = qq|UPDATE defaults SET coa = ?, accounting_method = ?, profit_determination = ?, inventory_system = ?, curr = ?|;
 
 268   do_query($form, $dbh, $query, map { $form->{$_} } qw(chart accounting_method profit_determination inventory_system defaultcurrency));
 
 272   $main::lxdebug->leave_sub();
 
 276   $main::lxdebug->enter_sub();
 
 278   my ($self, $form) = @_;
 
 279   $form->{db} =~ s/\"//g;
 
 281   &dbconnect_vars($form, $form->{dbdefault});
 
 282   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 284   my $query = qq|DROP DATABASE "$form->{db}"|;
 
 285   do_query($form, $dbh, $query);
 
 289   $main::lxdebug->leave_sub();
 
 292 sub dbsources_unused {
 
 293   $main::lxdebug->enter_sub();
 
 295   my ($self, $form) = @_;
 
 297   my %dbexcl = map  { $_->dbname => 1 }
 
 298                grep { ($_->dbhost eq $form->{dbhost}) && ($_->dbport eq $form->{dbport}) }
 
 299                     @{ SL::DB::Manager::AuthClient->get_all };
 
 301   $form->{only_acc_db} = 1;
 
 303   $dbexcl{$form->{dbdefault}}             = 1;
 
 304   $dbexcl{$main::auth->{DB_config}->{db}} = 1;
 
 306   my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
 
 308   $main::lxdebug->leave_sub();
 
 314   $main::lxdebug->enter_sub(2);
 
 316   my (@v, $version, $i);
 
 318   @v = split(/\./, $_[0]);
 
 319   while (scalar(@v) < 4) {
 
 323   for ($i = 0; $i < 4; $i++) {
 
 328   $main::lxdebug->leave_sub(2);
 
 332 sub cmp_script_version {
 
 333   my ($a_from, $a_to, $b_from, $b_to);
 
 334   my ($i, $res_a, $res_b);
 
 335   my ($my_a, $my_b) = ($a, $b);
 
 337   $my_a =~ s/.*-upgrade-//;
 
 339   $my_b =~ s/.*-upgrade-//;
 
 341   my ($my_a_from, $my_a_to) = split(/-/, $my_a);
 
 342   my ($my_b_from, $my_b_to) = split(/-/, $my_b);
 
 344   $res_a = calc_version($my_a_from);
 
 345   $res_b = calc_version($my_b_from);
 
 347   if ($res_a == $res_b) {
 
 348     $res_a = calc_version($my_a_to);
 
 349     $res_b = calc_version($my_b_to);
 
 352   return $res_a <=> $res_b;
 
 355 sub create_schema_info_table {
 
 356   $main::lxdebug->enter_sub();
 
 358   my ($self, $form, $dbh) = @_;
 
 360   my $query = "SELECT tag FROM schema_info LIMIT 1";
 
 361   if (!$dbh->do($query)) {
 
 364       qq|CREATE TABLE schema_info (| .
 
 367       qq|  itime timestamp DEFAULT now(), | .
 
 368       qq|  PRIMARY KEY (tag))|;
 
 369     $dbh->do($query) || $form->dberror($query);
 
 372   $main::lxdebug->leave_sub();
 
 376   $main::lxdebug->enter_sub();
 
 378   my ($self, %params) = @_;
 
 380   my $form            = $params{form};
 
 381   my $dbupdater       = $params{updater};
 
 382   my $db              = $params{database};
 
 385   map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
 
 387   &dbconnect_vars($form, $db);
 
 389   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
 
 391   $dbh->do($form->{dboptions}) if ($form->{dboptions});
 
 393   $self->create_schema_info_table($form, $dbh);
 
 395   my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
 
 397   $dbh->disconnect and next if !@upgradescripts;
 
 399   foreach my $control (@upgradescripts) {
 
 401     $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
 
 402     print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
 
 404     $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
 
 410   $main::lxdebug->leave_sub();