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;
51 use constant LOGIN_OK => 0;
52 use constant LOGIN_BASIC_TABLES_MISSING => -1;
53 use constant LOGIN_DBUPDATE_AVAILABLE => -2;
54 use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3;
55 use constant LOGIN_GENERAL_ERROR => -4;
58 $main::lxdebug->enter_sub();
60 my ($type, %params) = @_;
64 if ($params{id} || $params{login}) {
65 my %user_data = $main::auth->read_user(%params);
66 map { $self->{$_} = $user_data{$_} } keys %user_data;
69 $main::lxdebug->leave_sub();
75 $main::lxdebug->enter_sub();
82 # scan the locale directory and read in the LANGUAGE files
83 opendir(DIR, "locale");
85 my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
87 foreach my $dir (@dir) {
88 next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
92 $cc{$dir} = "@language";
97 $main::lxdebug->leave_sub();
103 my ($self, $form) = @_;
105 return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
107 my %myconfig = $main::auth->read_user(login => $self->{login});
109 # Auth DB upgrades available?
110 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
111 return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
113 # check if database is down
114 my $dbh = $form->dbconnect_noauto;
116 # we got a connection, check the version
117 my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
120 return LOGIN_BASIC_TABLES_MISSING();
123 $self->create_schema_info_table($form, $dbh);
125 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
126 my $update_available = $dbupdater->update2_available($dbh);
129 return LOGIN_OK() if !$update_available;
131 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
132 $form->{$_} = $myconfig{$_} for qw(datestyle);
134 $form->{"title"} = $main::locale->text("Dataset upgrade");
135 $form->header(no_layout => $form->{no_layout});
136 print $form->parse_html_template("dbupgrade/header");
138 $form->{dbupdate} = "db" . $::auth->client->{dbname};
140 if ($form->{"show_dbupdate_warning"}) {
141 print $form->parse_html_template("dbupgrade/warning");
146 SL::System::InstallationLock->lock;
148 # ignore HUP, QUIT in case the webserver times out
149 $SIG{HUP} = 'IGNORE';
150 $SIG{QUIT} = 'IGNORE';
152 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
154 SL::System::InstallationLock->unlock;
156 print $form->parse_html_template("dbupgrade/footer");
158 return LOGIN_DBUPDATE_AVAILABLE();
162 $main::lxdebug->enter_sub();
164 my ($form, $db) = @_;
167 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
168 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
169 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
170 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
171 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
174 $form->{dboptions} = $dboptions{ $form->{dateformat} };
175 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
177 $main::lxdebug->leave_sub();
181 $main::lxdebug->enter_sub();
183 my ($self, $form) = @_;
188 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
189 &dbconnect_vars($form, $form->{dbdefault});
191 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
195 qq|SELECT datname FROM pg_database | .
196 qq|WHERE NOT datname IN ('template0', 'template1')|;
197 $sth = $dbh->prepare($query);
198 $sth->execute() || $form->dberror($query);
200 while (my ($db) = $sth->fetchrow_array) {
202 if ($form->{only_acc_db}) {
204 next if ($db =~ /^template/);
206 &dbconnect_vars($form, $db);
207 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
211 qq|SELECT tablename FROM pg_tables | .
212 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
213 my $sth = $dbh->prepare($query);
214 $sth->execute($form->{dbuser}) ||
215 $form->dberror($query . " ($form->{dbuser})");
217 if ($sth->fetchrow_array) {
218 push(@dbsources, $db);
224 push(@dbsources, $db);
230 $main::lxdebug->leave_sub();
236 $main::lxdebug->enter_sub();
238 my ($self, $form) = @_;
240 &dbconnect_vars($form, $form->{dbdefault});
242 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
244 $form->{db} =~ s/\"//g;
248 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
249 if ($form->{"dbdefault"}) {
250 my $dbdefault = $form->{"dbdefault"};
251 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
252 push @dboptions, "TEMPLATE = $dbdefault";
255 my $query = qq|CREATE DATABASE "$form->{db}"|;
256 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
258 # Ignore errors if the database exists.
263 &dbconnect_vars($form, $form->{db});
265 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
268 my $dbupdater = SL::DBUpgrade2->new(form => $form);
270 $dbupdater->process_query($dbh, "sql/lx-office.sql");
272 # load chart of accounts
273 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
275 $query = qq|UPDATE defaults SET coa = ?, accounting_method = ?, profit_determination = ?, inventory_system = ?, curr = ?|;
276 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart accounting_method profit_determination inventory_system defaultcurrency));
280 $main::lxdebug->leave_sub();
284 $main::lxdebug->enter_sub();
286 my ($self, $form) = @_;
287 $form->{db} =~ s/\"//g;
289 &dbconnect_vars($form, $form->{dbdefault});
290 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
292 my $query = qq|DROP DATABASE "$form->{db}"|;
293 do_query($form, $dbh, $query);
297 $main::lxdebug->leave_sub();
301 $main::lxdebug->enter_sub(2);
303 my (@v, $version, $i);
305 @v = split(/\./, $_[0]);
306 while (scalar(@v) < 4) {
310 for ($i = 0; $i < 4; $i++) {
315 $main::lxdebug->leave_sub(2);
319 sub cmp_script_version {
320 my ($a_from, $a_to, $b_from, $b_to);
321 my ($i, $res_a, $res_b);
322 my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
324 $my_a =~ s/.*-upgrade-//;
326 $my_b =~ s/.*-upgrade-//;
328 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
329 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
331 $res_a = calc_version($my_a_from);
332 $res_b = calc_version($my_b_from);
334 if ($res_a == $res_b) {
335 $res_a = calc_version($my_a_to);
336 $res_b = calc_version($my_b_to);
339 return $res_a <=> $res_b;
342 sub create_schema_info_table {
343 $main::lxdebug->enter_sub();
345 my ($self, $form, $dbh) = @_;
347 my $query = "SELECT tag FROM schema_info LIMIT 1";
348 if (!$dbh->do($query)) {
351 qq|CREATE TABLE schema_info (| .
354 qq| itime timestamp DEFAULT now(), | .
355 qq| PRIMARY KEY (tag))|;
356 $dbh->do($query) || $form->dberror($query);
359 $main::lxdebug->leave_sub();
363 $main::lxdebug->enter_sub();
365 my ($self, %params) = @_;
367 my $form = $params{form};
368 my $dbupdater = $params{updater};
369 my $db = $params{database};
372 map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
374 &dbconnect_vars($form, $db);
376 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
378 $dbh->do($form->{dboptions}) if ($form->{dboptions});
380 $self->create_schema_info_table($form, $dbh);
382 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
384 $dbh->disconnect and next if !@upgradescripts;
386 foreach my $control (@upgradescripts) {
388 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
389 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
391 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
397 $main::lxdebug->leave_sub();