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->update_available($dbversion) || $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->dbupdate($form);
144 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
145 SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(0);
147 SL::System::InstallationLock->unlock;
149 print $form->parse_html_template("dbupgrade/footer");
155 $main::lxdebug->enter_sub();
157 my ($form, $db) = @_;
160 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
161 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
162 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
163 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
164 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
167 $form->{dboptions} = $dboptions{ $form->{dateformat} };
168 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
170 $main::lxdebug->leave_sub();
174 $main::lxdebug->enter_sub();
176 my ($self, $form) = @_;
181 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
182 &dbconnect_vars($form, $form->{dbdefault});
184 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
188 qq|SELECT datname FROM pg_database | .
189 qq|WHERE NOT datname IN ('template0', 'template1')|;
190 $sth = $dbh->prepare($query);
191 $sth->execute() || $form->dberror($query);
193 while (my ($db) = $sth->fetchrow_array) {
195 if ($form->{only_acc_db}) {
197 next if ($db =~ /^template/);
199 &dbconnect_vars($form, $db);
200 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
204 qq|SELECT tablename FROM pg_tables | .
205 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
206 my $sth = $dbh->prepare($query);
207 $sth->execute($form->{dbuser}) ||
208 $form->dberror($query . " ($form->{dbuser})");
210 if ($sth->fetchrow_array) {
211 push(@dbsources, $db);
217 push(@dbsources, $db);
223 $main::lxdebug->leave_sub();
229 $main::lxdebug->enter_sub();
231 my ($self, $form) = @_;
233 &dbconnect_vars($form, $form->{dbdefault});
235 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
237 $form->{db} =~ s/\"//g;
241 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
242 if ($form->{"dbdefault"}) {
243 my $dbdefault = $form->{"dbdefault"};
244 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
245 push @dboptions, "TEMPLATE = $dbdefault";
248 my $query = qq|CREATE DATABASE "$form->{db}"|;
249 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
251 # Ignore errors if the database exists.
256 &dbconnect_vars($form, $form->{db});
258 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
261 my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
262 $db_charset ||= Common::DEFAULT_CHARSET;
264 my $dbupdater = SL::DBUpgrade2->new(form => $form);
266 $dbupdater->process_query($dbh, "sql/lx-office.sql", undef, $db_charset);
268 # load chart of accounts
269 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
271 my $query = qq|UPDATE defaults SET coa = ?, accounting_method = ?, profit_determination = ?, inventory_system = ?, curr = ?|;
272 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart accounting_method profit_determination inventory_system defaultcurrency));
276 $main::lxdebug->leave_sub();
280 $main::lxdebug->enter_sub();
282 my ($self, $form) = @_;
283 $form->{db} =~ s/\"//g;
285 &dbconnect_vars($form, $form->{dbdefault});
286 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
288 my $query = qq|DROP DATABASE "$form->{db}"|;
289 do_query($form, $dbh, $query);
293 $main::lxdebug->leave_sub();
296 sub dbsources_unused {
297 $main::lxdebug->enter_sub();
299 my ($self, $form) = @_;
301 my %dbexcl = map { $_->dbname => 1 }
302 grep { ($_->dbhost eq $form->{dbhost}) && ($_->dbport eq $form->{dbport}) }
303 @{ SL::DB::Manager::AuthClient->get_all };
305 $form->{only_acc_db} = 1;
307 $dbexcl{$form->{dbdefault}} = 1;
308 $dbexcl{$main::auth->{DB_config}->{db}} = 1;
310 my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
312 $main::lxdebug->leave_sub();
318 $main::lxdebug->enter_sub(2);
320 my (@v, $version, $i);
322 @v = split(/\./, $_[0]);
323 while (scalar(@v) < 4) {
327 for ($i = 0; $i < 4; $i++) {
332 $main::lxdebug->leave_sub(2);
336 sub cmp_script_version {
337 my ($a_from, $a_to, $b_from, $b_to);
338 my ($i, $res_a, $res_b);
339 my ($my_a, $my_b) = ($a, $b);
341 $my_a =~ s/.*-upgrade-//;
343 $my_b =~ s/.*-upgrade-//;
345 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
346 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
348 $res_a = calc_version($my_a_from);
349 $res_b = calc_version($my_b_from);
351 if ($res_a == $res_b) {
352 $res_a = calc_version($my_a_to);
353 $res_b = calc_version($my_b_to);
356 return $res_a <=> $res_b;
359 sub create_schema_info_table {
360 $main::lxdebug->enter_sub();
362 my ($self, $form, $dbh) = @_;
364 my $query = "SELECT tag FROM schema_info LIMIT 1";
365 if (!$dbh->do($query)) {
368 qq|CREATE TABLE schema_info (| .
371 qq| itime timestamp DEFAULT now(), | .
372 qq| PRIMARY KEY (tag))|;
373 $dbh->do($query) || $form->dberror($query);
376 $main::lxdebug->leave_sub();
380 $main::lxdebug->enter_sub();
382 my ($self, $form) = @_;
386 my @upgradescripts = ();
390 if ($form->{dbupdate}) {
392 # read update scripts into memory
393 opendir(SQLDIR, "sql/Pg-upgrade")
394 or &error("", "sql/Pg-upgrade : $!");
396 sort(cmp_script_version
397 grep(/Pg-upgrade-.*?\.(sql|pl)$/,
402 my $db_charset = $::lx_office_conf{system}->{dbcharset};
403 $db_charset ||= Common::DEFAULT_CHARSET;
405 my $dbupdater = SL::DBUpgrade2->new(form => $form);
407 foreach my $db (split(/ /, $form->{dbupdate})) {
409 next unless $form->{$db};
411 # strip db from dataset
413 &dbconnect_vars($form, $db);
415 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
418 $dbh->do($form->{dboptions}) if ($form->{dboptions});
421 $query = qq|SELECT version FROM defaults|;
422 my ($version) = selectrow_query($form, $dbh, $query);
424 next unless $version;
426 $version = calc_version($version);
428 foreach my $upgradescript (@upgradescripts) {
429 my $a = $upgradescript;
430 $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
432 my ($mindb, $maxdb) = split /-/, $a;
433 my $str_maxdb = $maxdb;
434 $mindb = calc_version($mindb);
435 $maxdb = calc_version($maxdb);
437 next if ($version >= $maxdb);
439 # if there is no upgrade script exit
440 last if ($version < $mindb);
443 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
444 $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
455 $main::lxdebug->leave_sub();
461 $main::lxdebug->enter_sub();
463 my ($self, %params) = @_;
465 my $form = $params{form};
466 my $dbupdater = $params{updater};
467 my $db = $params{database};
469 my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
471 map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
473 &dbconnect_vars($form, $db);
475 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
477 $dbh->do($form->{dboptions}) if ($form->{dboptions});
479 $self->create_schema_info_table($form, $dbh);
481 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
483 $dbh->disconnect and next if !@upgradescripts;
485 foreach my $control (@upgradescripts) {
487 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
488 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
490 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control, $db_charset);
496 $main::lxdebug->leave_sub();