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 #=====================================================================
 
  46 use SL::System::InstallationLock;
 
  51   $main::lxdebug->enter_sub();
 
  53   my ($type, %params) = @_;
 
  57   if ($params{id} || $params{login}) {
 
  58     my %user_data = $main::auth->read_user(%params);
 
  59     map { $self->{$_} = $user_data{$_} } keys %user_data;
 
  62   $main::lxdebug->leave_sub();
 
  68   $main::lxdebug->enter_sub();
 
  75   # scan the locale directory and read in the LANGUAGE files
 
  76   opendir(DIR, "locale");
 
  78   my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
 
  80   foreach my $dir (@dir) {
 
  81     next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
 
  85     $cc{$dir} = "@language";
 
  90   $main::lxdebug->leave_sub();
 
  96   my ($self, $form) = @_;
 
  98   return -3 if !$self->{login} || !$::auth->client;
 
 100   my %myconfig = $main::auth->read_user(login => $self->{login});
 
 102   # check if database is down
 
 103   my $dbh = $form->dbconnect_noauto;
 
 105   # we got a connection, check the version
 
 106   my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
 
 108   $self->create_schema_info_table($form, $dbh);
 
 110   # Auth DB upgrades available?
 
 111   my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
 
 112   return -3 if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
 
 114   my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
 
 116   my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
 
 119   return 0 if !$update_available;
 
 121   $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
 
 122   $form->{$_} = $myconfig{$_}         for qw(datestyle);
 
 124   $form->{"title"} = $main::locale->text("Dataset upgrade");
 
 125   $form->header(no_layout => $form->{no_layout});
 
 126   print $form->parse_html_template("dbupgrade/header");
 
 128   $form->{dbupdate} = "db" . $::auth->client->{dbname};
 
 130   if ($form->{"show_dbupdate_warning"}) {
 
 131     print $form->parse_html_template("dbupgrade/warning");
 
 136   SL::System::InstallationLock->lock;
 
 138   # ignore HUP, QUIT in case the webserver times out
 
 139   $SIG{HUP}  = 'IGNORE';
 
 140   $SIG{QUIT} = 'IGNORE';
 
 142   $self->dbupdate($form);
 
 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();
 
 227 sub dbclusterencoding {
 
 228   $main::lxdebug->enter_sub();
 
 230   my ($self, $form) = @_;
 
 232   $form->{dbdefault} ||= $form->{dbuser};
 
 234   dbconnect_vars($form, $form->{dbdefault});
 
 236   my $dbh                = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) || $form->dberror();
 
 237   my $query              = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
 
 238   my ($cluster_encoding) = $dbh->selectrow_array($query);
 
 241   $main::lxdebug->leave_sub();
 
 243   return $cluster_encoding;
 
 247   $main::lxdebug->enter_sub();
 
 249   my ($self, $form) = @_;
 
 251   &dbconnect_vars($form, $form->{dbdefault});
 
 253     SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 255   $form->{db} =~ s/\"//g;
 
 259   push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
 
 260   if ($form->{"dbdefault"}) {
 
 261     my $dbdefault = $form->{"dbdefault"};
 
 262     $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
 
 263     push @dboptions, "TEMPLATE = $dbdefault";
 
 266   my $query = qq|CREATE DATABASE "$form->{db}"|;
 
 267   $query   .= " WITH " . join(" ", @dboptions) if @dboptions;
 
 269   # Ignore errors if the database exists.
 
 274   &dbconnect_vars($form, $form->{db});
 
 276   $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 279   my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
 
 280   $db_charset ||= Common::DEFAULT_CHARSET;
 
 282   my $dbupdater = SL::DBUpgrade2->new(form => $form);
 
 284   $dbupdater->process_query($dbh, "sql/lx-office.sql", undef, $db_charset);
 
 286   # load chart of accounts
 
 287   $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
 
 289   $query = "UPDATE defaults SET coa = ?";
 
 290   do_query($form, $dbh, $query, $form->{chart});
 
 291   $query = "UPDATE defaults SET accounting_method = ?";
 
 292   do_query($form, $dbh, $query, $form->{accounting_method});
 
 293   $query = "UPDATE defaults SET profit_determination = ?";
 
 294   do_query($form, $dbh, $query, $form->{profit_determination});
 
 295   $query = "UPDATE defaults SET inventory_system = ?";
 
 296   do_query($form, $dbh, $query, $form->{inventory_system});
 
 297   $query = "UPDATE defaults SET curr = ?";
 
 298   do_query($form, $dbh, $query, $form->{defaultcurrency});
 
 302   $main::lxdebug->leave_sub();
 
 306   $main::lxdebug->enter_sub();
 
 308   my ($self, $form) = @_;
 
 309   $form->{db} =~ s/\"//g;
 
 311   &dbconnect_vars($form, $form->{dbdefault});
 
 312   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 314   my $query = qq|DROP DATABASE "$form->{db}"|;
 
 315   do_query($form, $dbh, $query);
 
 319   $main::lxdebug->leave_sub();
 
 322 sub dbsources_unused {
 
 323   $main::lxdebug->enter_sub();
 
 325   my ($self, $form) = @_;
 
 327   $form->{only_acc_db} = 1;
 
 329   my %members = $main::auth->read_all_users();
 
 330   my %dbexcl  = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
 
 332   $dbexcl{$form->{dbdefault}}             = 1;
 
 333   $dbexcl{$main::auth->{DB_config}->{db}} = 1;
 
 335   my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
 
 337   $main::lxdebug->leave_sub();
 
 343   $main::lxdebug->enter_sub();
 
 345   my ($self, $form) = @_;
 
 347   my %members   = $main::auth->read_all_users();
 
 348   my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
 
 350   my ($query, $sth, %dbs_needing_updates);
 
 352   foreach my $login (grep /[a-z]/, keys %members) {
 
 353     my $member = $members{$login};
 
 355     map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
 
 356     dbconnect_vars($form, $form->{dbname});
 
 358     my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options);
 
 364     $query = qq|SELECT version FROM defaults|;
 
 365     $sth = prepare_query($form, $dbh, $query);
 
 366     if ($sth->execute()) {
 
 367       ($version) = $sth->fetchrow_array();
 
 371     $dbh->disconnect and next unless $version;
 
 373     my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh);
 
 376    if ($update_available) {
 
 378       map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
 
 379       $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
 
 383   $main::lxdebug->leave_sub();
 
 385   return values %dbs_needing_updates;
 
 389   $main::lxdebug->enter_sub(2);
 
 391   my (@v, $version, $i);
 
 393   @v = split(/\./, $_[0]);
 
 394   while (scalar(@v) < 4) {
 
 398   for ($i = 0; $i < 4; $i++) {
 
 403   $main::lxdebug->leave_sub(2);
 
 407 sub cmp_script_version {
 
 408   my ($a_from, $a_to, $b_from, $b_to);
 
 409   my ($i, $res_a, $res_b);
 
 410   my ($my_a, $my_b) = ($a, $b);
 
 412   $my_a =~ s/.*-upgrade-//;
 
 414   $my_b =~ s/.*-upgrade-//;
 
 416   my ($my_a_from, $my_a_to) = split(/-/, $my_a);
 
 417   my ($my_b_from, $my_b_to) = split(/-/, $my_b);
 
 419   $res_a = calc_version($my_a_from);
 
 420   $res_b = calc_version($my_b_from);
 
 422   if ($res_a == $res_b) {
 
 423     $res_a = calc_version($my_a_to);
 
 424     $res_b = calc_version($my_b_to);
 
 427   return $res_a <=> $res_b;
 
 430 sub create_schema_info_table {
 
 431   $main::lxdebug->enter_sub();
 
 433   my ($self, $form, $dbh) = @_;
 
 435   my $query = "SELECT tag FROM schema_info LIMIT 1";
 
 436   if (!$dbh->do($query)) {
 
 439       qq|CREATE TABLE schema_info (| .
 
 442       qq|  itime timestamp DEFAULT now(), | .
 
 443       qq|  PRIMARY KEY (tag))|;
 
 444     $dbh->do($query) || $form->dberror($query);
 
 447   $main::lxdebug->leave_sub();
 
 451   $main::lxdebug->enter_sub();
 
 453   my ($self, $form) = @_;
 
 457   my @upgradescripts = ();
 
 461   if ($form->{dbupdate}) {
 
 463     # read update scripts into memory
 
 464     opendir(SQLDIR, "sql/Pg-upgrade")
 
 465       or &error("", "sql/Pg-upgrade : $!");
 
 467       sort(cmp_script_version
 
 468            grep(/Pg-upgrade-.*?\.(sql|pl)$/,
 
 473   my $db_charset = $::lx_office_conf{system}->{dbcharset};
 
 474   $db_charset ||= Common::DEFAULT_CHARSET;
 
 476   my $dbupdater = SL::DBUpgrade2->new(form => $form);
 
 478   foreach my $db (split(/ /, $form->{dbupdate})) {
 
 480     next unless $form->{$db};
 
 482     # strip db from dataset
 
 484     &dbconnect_vars($form, $db);
 
 486     my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
 
 489     $dbh->do($form->{dboptions}) if ($form->{dboptions});
 
 492     $query = qq|SELECT version FROM defaults|;
 
 493     my ($version) = selectrow_query($form, $dbh, $query);
 
 495     next unless $version;
 
 497     $version = calc_version($version);
 
 499     foreach my $upgradescript (@upgradescripts) {
 
 500       my $a = $upgradescript;
 
 501       $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
 
 503       my ($mindb, $maxdb) = split /-/, $a;
 
 504       my $str_maxdb = $maxdb;
 
 505       $mindb = calc_version($mindb);
 
 506       $maxdb = calc_version($maxdb);
 
 508       next if ($version >= $maxdb);
 
 510       # if there is no upgrade script exit
 
 511       last if ($version < $mindb);
 
 514       $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
 
 515       $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
 
 526   $main::lxdebug->leave_sub();
 
 532   $main::lxdebug->enter_sub();
 
 534   my ($self, %params) = @_;
 
 536   my $form            = $params{form};
 
 537   my $dbupdater       = $params{updater};
 
 538   my $db              = $params{database};
 
 540   my $db_charset      = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
 
 542   map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
 
 544   &dbconnect_vars($form, $db);
 
 546   my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
 
 548   $dbh->do($form->{dboptions}) if ($form->{dboptions});
 
 550   $self->create_schema_info_table($form, $dbh);
 
 552   my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
 
 554   $dbh->disconnect and next if !@upgradescripts;
 
 556   foreach my $control (@upgradescripts) {
 
 558     $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
 
 559     print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
 
 561     $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control, $db_charset);
 
 567   $main::lxdebug->leave_sub();