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;
52 use constant LOGIN_OK => 0;
53 use constant LOGIN_BASIC_TABLES_MISSING => -1;
54 use constant LOGIN_DBUPDATE_AVAILABLE => -2;
55 use constant LOGIN_AUTH_DBUPDATE_AVAILABLE => -3;
56 use constant LOGIN_GENERAL_ERROR => -4;
59 $main::lxdebug->enter_sub();
61 my ($type, %params) = @_;
65 if ($params{id} || $params{login}) {
66 my %user_data = $main::auth->read_user(%params);
67 map { $self->{$_} = $user_data{$_} } keys %user_data;
70 $main::lxdebug->leave_sub();
76 $main::lxdebug->enter_sub();
83 # scan the locale directory and read in the LANGUAGE files
84 opendir(DIR, "locale");
86 my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
88 foreach my $dir (@dir) {
89 next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
93 $cc{$dir} = "@language";
98 $main::lxdebug->leave_sub();
104 my ($self, $form) = @_;
106 return LOGIN_GENERAL_ERROR() if !$self->{login} || !$::auth->client;
108 my %myconfig = $main::auth->read_user(login => $self->{login});
110 # Auth DB upgrades available?
111 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
112 return LOGIN_AUTH_DBUPDATE_AVAILABLE() if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
114 # check if database is down
115 my $dbh = $form->dbconnect_noauto;
117 # we got a connection, check the version
118 my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
121 return LOGIN_BASIC_TABLES_MISSING();
124 $self->create_schema_info_table($form, $dbh);
126 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
127 my @unapplied_scripts = $dbupdater->unapplied_upgrade_scripts($dbh);
130 if (!@unapplied_scripts) {
131 SL::DB::Manager::Employee->update_entries_for_authorized_users;
135 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
136 $form->{$_} = $myconfig{$_} for qw(datestyle);
138 $form->{"title"} = $main::locale->text("Dataset upgrade");
139 $form->header(no_layout => $form->{no_layout});
140 print $form->parse_html_template("dbupgrade/header");
142 $form->{dbupdate} = "db" . $::auth->client->{dbname};
144 if ($form->{"show_dbupdate_warning"}) {
145 print $form->parse_html_template("dbupgrade/warning", { unapplied_scripts => \@unapplied_scripts });
150 SL::System::InstallationLock->lock;
152 # ignore HUP, QUIT in case the webserver times out
153 $SIG{HUP} = 'IGNORE';
154 $SIG{QUIT} = 'IGNORE';
156 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
158 # If $self->dbupdate2 returns than this means all upgrade scripts
159 # have been applied successfully, none required user
160 # interaction. Otherwise the deeper layers would have called
161 # ::end_of_request() already, and return would not have returned to
162 # us. Therefore we can now use RDBO instances because their supposed
163 # table structures do match the actual structures. So let's ensure
164 # that the "employee" table contains the appropriate entries for all
165 # users authorized for the current client.
166 SL::DB::Manager::Employee->update_entries_for_authorized_users;
168 SL::System::InstallationLock->unlock;
170 print $form->parse_html_template("dbupgrade/footer");
172 return LOGIN_DBUPDATE_AVAILABLE();
176 $main::lxdebug->enter_sub();
178 my ($form, $db) = @_;
181 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
182 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
183 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
184 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
185 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
188 $form->{dboptions} = $dboptions{ $form->{dateformat} };
189 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
191 $main::lxdebug->leave_sub();
195 $main::lxdebug->enter_sub();
197 my ($self, $form) = @_;
202 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
203 &dbconnect_vars($form, $form->{dbdefault});
205 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
209 qq|SELECT datname FROM pg_database | .
210 qq|WHERE NOT datname IN ('template0', 'template1')|;
211 $sth = $dbh->prepare($query);
212 $sth->execute() || $form->dberror($query);
214 while (my ($db) = $sth->fetchrow_array) {
216 if ($form->{only_acc_db}) {
218 next if ($db =~ /^template/);
220 &dbconnect_vars($form, $db);
221 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
225 qq|SELECT tablename FROM pg_tables | .
226 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
227 my $sth = $dbh->prepare($query);
228 $sth->execute($form->{dbuser}) ||
229 $form->dberror($query . " ($form->{dbuser})");
231 if ($sth->fetchrow_array) {
232 push(@dbsources, $db);
238 push(@dbsources, $db);
244 $main::lxdebug->leave_sub();
250 $main::lxdebug->enter_sub();
252 my ($self, $form) = @_;
254 &dbconnect_vars($form, $form->{dbdefault});
256 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
258 $form->{db} =~ s/\"//g;
262 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
263 if ($form->{"dbdefault"}) {
264 my $dbdefault = $form->{"dbdefault"};
265 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
266 push @dboptions, "TEMPLATE = $dbdefault";
269 my $query = qq|CREATE DATABASE "$form->{db}"|;
270 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
272 # Ignore errors if the database exists.
277 &dbconnect_vars($form, $form->{db});
279 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
282 my $dbupdater = SL::DBUpgrade2->new(form => $form);
284 $dbupdater->process_query($dbh, "sql/lx-office.sql");
286 # load chart of accounts
287 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql");
289 $query = qq|UPDATE defaults SET coa = ?, accounting_method = ?, profit_determination = ?, inventory_system = ?, curr = ?|;
290 do_query($form, $dbh, $query, map { $form->{$_} } qw(chart accounting_method profit_determination inventory_system defaultcurrency));
294 $main::lxdebug->leave_sub();
298 $main::lxdebug->enter_sub();
300 my ($self, $form) = @_;
301 $form->{db} =~ s/\"//g;
303 &dbconnect_vars($form, $form->{dbdefault});
304 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
306 my $query = qq|DROP DATABASE "$form->{db}"|;
307 do_query($form, $dbh, $query);
311 $main::lxdebug->leave_sub();
315 $main::lxdebug->enter_sub(2);
317 my (@v, $version, $i);
319 @v = split(/\./, $_[0]);
320 while (scalar(@v) < 4) {
324 for ($i = 0; $i < 4; $i++) {
329 $main::lxdebug->leave_sub(2);
333 sub cmp_script_version {
334 my ($a_from, $a_to, $b_from, $b_to);
335 my ($i, $res_a, $res_b);
336 my ($my_a, $my_b) = do { no warnings 'once'; ($a, $b) };
338 $my_a =~ s/.*-upgrade-//;
340 $my_b =~ s/.*-upgrade-//;
342 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
343 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
345 $res_a = calc_version($my_a_from);
346 $res_b = calc_version($my_b_from);
348 if ($res_a == $res_b) {
349 $res_a = calc_version($my_a_to);
350 $res_b = calc_version($my_b_to);
353 return $res_a <=> $res_b;
356 sub create_schema_info_table {
357 $main::lxdebug->enter_sub();
359 my ($self, $form, $dbh) = @_;
361 my $query = "SELECT tag FROM schema_info LIMIT 1";
362 if (!$dbh->do($query)) {
365 qq|CREATE TABLE schema_info (| .
368 qq| itime timestamp DEFAULT now(), | .
369 qq| PRIMARY KEY (tag))|;
370 $dbh->do($query) || $form->dberror($query);
373 $main::lxdebug->leave_sub();
377 my ($self, %params) = @_;
379 my $form = $params{form};
380 my $dbupdater = $params{updater};
381 my $db = $params{database};
383 map { $_->{description} = SL::Iconv::convert($_->{charset}, 'UTF-8', $_->{description}) } values %{ $dbupdater->{all_controls} };
385 &dbconnect_vars($form, $db);
387 # Flush potentially held database locks.
388 $form->get_standard_dbh->commit;
390 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
392 $dbh->do($form->{dboptions}) if ($form->{dboptions});
394 $self->create_schema_info_table($form, $dbh);
396 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
398 foreach my $control (@upgradescripts) {
399 # Apply upgrade. Control will only return to us if the upgrade has
400 # been applied correctly and if the update has not requested user
402 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
403 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
405 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control);
415 sub get_default_myconfig {
416 my ($self_or_class, %user_config) = @_;
420 css_path => 'css', # Needed for menunew, see SL::Layout::Base::get_stylesheet_for_user
421 dateformat => 'dd.mm.yy',
422 numberformat => '1.000,00',
423 stylesheet => 'kivitendo.css',
424 timeformat => 'hh:mm',