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 #=====================================================================
50 $main::lxdebug->enter_sub();
52 my ($type, %params) = @_;
56 if ($params{id} || $params{login}) {
57 my %user_data = $main::auth->read_user(%params);
58 map { $self->{$_} = $user_data{$_} } keys %user_data;
61 $main::lxdebug->leave_sub();
67 $main::lxdebug->enter_sub();
74 # scan the locale directory and read in the LANGUAGE files
75 opendir(DIR, "locale");
77 my @dir = grep(!/(^\.\.?$|\..*)/, readdir(DIR));
79 foreach my $dir (@dir) {
80 next unless open(my $fh, '<:encoding(UTF-8)', "locale/$dir/LANGUAGE");
84 $cc{$dir} = "@language";
89 $main::lxdebug->leave_sub();
95 my ($self, $form) = @_;
97 return -3 if !$self->{login} || !$::auth->client;
99 my %myconfig = $main::auth->read_user(login => $self->{login});
101 # check if database is down
102 my $dbh = $form->dbconnect_noauto;
104 # we got a connection, check the version
105 my ($dbversion) = $dbh->selectrow_array(qq|SELECT version FROM defaults|);
107 $self->create_employee_entry($form, $dbh, \%myconfig);
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 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
118 $form->{$_} = $myconfig{$_} for qw(dateformat);
120 dbconnect_vars($form, $form->{dbname});
122 my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
125 return 0 if !$update_available;
126 $form->{"title"} = $main::locale->text("Dataset upgrade");
127 $form->header(no_layout => $form->{no_layout});
128 print $form->parse_html_template("dbupgrade/header");
130 $form->{dbupdate} = "db" . $form->{dbname};
132 if ($form->{"show_dbupdate_warning"}) {
133 print $form->parse_html_template("dbupgrade/warning");
139 if (!$::lx_office_conf{debug}->{keep_installation_unlocked} && !open($fh, ">", $::lx_office_conf{paths}->{userspath} . "/nologin")) {
140 $form->show_generic_error($main::locale->text('A temporary file could not be created. ' .
141 'Please verify that the directory "#1" is writeable by the webserver.',
142 $::lx_office_conf{paths}->{userspath}),
146 # ignore HUP, QUIT in case the webserver times out
147 $SIG{HUP} = 'IGNORE';
148 $SIG{QUIT} = 'IGNORE';
150 $self->dbupdate($form);
151 $self->dbupdate2($form, $dbupdater);
152 SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(0);
157 unlink($::lx_office_conf{paths}->{userspath} . "/nologin");
159 print $form->parse_html_template("dbupgrade/footer");
165 $main::lxdebug->enter_sub();
167 my ($form, $db) = @_;
170 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
171 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
172 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
173 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
174 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
177 $form->{dboptions} = $dboptions{ $form->{dateformat} };
178 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
180 $main::lxdebug->leave_sub();
184 $main::lxdebug->enter_sub();
186 my ($self, $form) = @_;
191 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
192 &dbconnect_vars($form, $form->{dbdefault});
194 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
198 qq|SELECT datname FROM pg_database | .
199 qq|WHERE NOT datname IN ('template0', 'template1')|;
200 $sth = $dbh->prepare($query);
201 $sth->execute() || $form->dberror($query);
203 while (my ($db) = $sth->fetchrow_array) {
205 if ($form->{only_acc_db}) {
207 next if ($db =~ /^template/);
209 &dbconnect_vars($form, $db);
210 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
214 qq|SELECT tablename FROM pg_tables | .
215 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
216 my $sth = $dbh->prepare($query);
217 $sth->execute($form->{dbuser}) ||
218 $form->dberror($query . " ($form->{dbuser})");
220 if ($sth->fetchrow_array) {
221 push(@dbsources, $db);
227 push(@dbsources, $db);
233 $main::lxdebug->leave_sub();
238 sub dbclusterencoding {
239 $main::lxdebug->enter_sub();
241 my ($self, $form) = @_;
243 $form->{dbdefault} ||= $form->{dbuser};
245 dbconnect_vars($form, $form->{dbdefault});
247 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) || $form->dberror();
248 my $query = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
249 my ($cluster_encoding) = $dbh->selectrow_array($query);
252 $main::lxdebug->leave_sub();
254 return $cluster_encoding;
258 $main::lxdebug->enter_sub();
260 my ($self, $form) = @_;
262 &dbconnect_vars($form, $form->{dbdefault});
264 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
266 $form->{db} =~ s/\"//g;
270 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
271 if ($form->{"dbdefault"}) {
272 my $dbdefault = $form->{"dbdefault"};
273 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
274 push @dboptions, "TEMPLATE = $dbdefault";
277 my $query = qq|CREATE DATABASE "$form->{db}"|;
278 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
280 # Ignore errors if the database exists.
285 &dbconnect_vars($form, $form->{db});
287 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
290 my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
291 $db_charset ||= Common::DEFAULT_CHARSET;
293 my $dbupdater = SL::DBUpgrade2->new(form => $form);
295 $dbupdater->process_query($dbh, "sql/lx-office.sql", undef, $db_charset);
297 # load chart of accounts
298 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
300 $query = "UPDATE defaults SET coa = ?";
301 do_query($form, $dbh, $query, $form->{chart});
302 $query = "UPDATE defaults SET accounting_method = ?";
303 do_query($form, $dbh, $query, $form->{accounting_method});
304 $query = "UPDATE defaults SET profit_determination = ?";
305 do_query($form, $dbh, $query, $form->{profit_determination});
306 $query = "UPDATE defaults SET inventory_system = ?";
307 do_query($form, $dbh, $query, $form->{inventory_system});
308 $query = "UPDATE defaults SET curr = ?";
309 do_query($form, $dbh, $query, $form->{defaultcurrency});
313 $main::lxdebug->leave_sub();
317 $main::lxdebug->enter_sub();
319 my ($self, $form) = @_;
320 $form->{db} =~ s/\"//g;
322 &dbconnect_vars($form, $form->{dbdefault});
323 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
325 my $query = qq|DROP DATABASE "$form->{db}"|;
326 do_query($form, $dbh, $query);
330 $main::lxdebug->leave_sub();
333 sub dbsources_unused {
334 $main::lxdebug->enter_sub();
336 my ($self, $form) = @_;
338 $form->{only_acc_db} = 1;
340 my %members = $main::auth->read_all_users();
341 my %dbexcl = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
343 $dbexcl{$form->{dbdefault}} = 1;
344 $dbexcl{$main::auth->{DB_config}->{db}} = 1;
346 my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
348 $main::lxdebug->leave_sub();
354 $main::lxdebug->enter_sub();
356 my ($self, $form) = @_;
358 my %members = $main::auth->read_all_users();
359 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
361 my ($query, $sth, %dbs_needing_updates);
363 foreach my $login (grep /[a-z]/, keys %members) {
364 my $member = $members{$login};
366 map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
367 dbconnect_vars($form, $form->{dbname});
369 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options);
375 $query = qq|SELECT version FROM defaults|;
376 $sth = prepare_query($form, $dbh, $query);
377 if ($sth->execute()) {
378 ($version) = $sth->fetchrow_array();
382 $dbh->disconnect and next unless $version;
384 my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh);
387 if ($update_available) {
389 map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
390 $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
394 $main::lxdebug->leave_sub();
396 return values %dbs_needing_updates;
400 $main::lxdebug->enter_sub(2);
402 my (@v, $version, $i);
404 @v = split(/\./, $_[0]);
405 while (scalar(@v) < 4) {
409 for ($i = 0; $i < 4; $i++) {
414 $main::lxdebug->leave_sub(2);
418 sub cmp_script_version {
419 my ($a_from, $a_to, $b_from, $b_to);
420 my ($i, $res_a, $res_b);
421 my ($my_a, $my_b) = ($a, $b);
423 $my_a =~ s/.*-upgrade-//;
425 $my_b =~ s/.*-upgrade-//;
427 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
428 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
430 $res_a = calc_version($my_a_from);
431 $res_b = calc_version($my_b_from);
433 if ($res_a == $res_b) {
434 $res_a = calc_version($my_a_to);
435 $res_b = calc_version($my_b_to);
438 return $res_a <=> $res_b;
441 sub create_schema_info_table {
442 $main::lxdebug->enter_sub();
444 my ($self, $form, $dbh) = @_;
446 my $query = "SELECT tag FROM schema_info LIMIT 1";
447 if (!$dbh->do($query)) {
450 qq|CREATE TABLE schema_info (| .
453 qq| itime timestamp DEFAULT now(), | .
454 qq| PRIMARY KEY (tag))|;
455 $dbh->do($query) || $form->dberror($query);
458 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 my ($self, $form) = @_;
468 my @upgradescripts = ();
472 if ($form->{dbupdate}) {
474 # read update scripts into memory
475 opendir(SQLDIR, "sql/Pg-upgrade")
476 or &error("", "sql/Pg-upgrade : $!");
478 sort(cmp_script_version
479 grep(/Pg-upgrade-.*?\.(sql|pl)$/,
484 my $db_charset = $::lx_office_conf{system}->{dbcharset};
485 $db_charset ||= Common::DEFAULT_CHARSET;
487 my $dbupdater = SL::DBUpgrade2->new(form => $form);
489 foreach my $db (split(/ /, $form->{dbupdate})) {
491 next unless $form->{$db};
493 # strip db from dataset
495 &dbconnect_vars($form, $db);
497 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
500 $dbh->do($form->{dboptions}) if ($form->{dboptions});
503 $query = qq|SELECT version FROM defaults|;
504 my ($version) = selectrow_query($form, $dbh, $query);
506 next unless $version;
508 $version = calc_version($version);
510 foreach my $upgradescript (@upgradescripts) {
511 my $a = $upgradescript;
512 $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
514 my ($mindb, $maxdb) = split /-/, $a;
515 my $str_maxdb = $maxdb;
516 $mindb = calc_version($mindb);
517 $maxdb = calc_version($maxdb);
519 next if ($version >= $maxdb);
521 # if there is no upgrade script exit
522 last if ($version < $mindb);
525 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
526 $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
537 $main::lxdebug->leave_sub();
543 $main::lxdebug->enter_sub();
545 my ($self, $form, $dbupdater) = @_;
548 my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
550 map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
552 foreach my $db (split / /, $form->{dbupdate}) {
553 next unless $form->{$db};
555 # strip db from dataset
557 &dbconnect_vars($form, $db);
559 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
561 $dbh->do($form->{dboptions}) if ($form->{dboptions});
563 $self->create_schema_info_table($form, $dbh);
565 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
567 $dbh->disconnect and next if !@upgradescripts;
569 foreach my $control (@upgradescripts) {
571 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
572 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
574 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control, $db_charset);
582 $main::lxdebug->leave_sub();
588 $main::lxdebug->enter_sub();
592 # format dbconnect and dboptions string
593 dbconnect_vars($self, $self->{dbname});
595 map { $self->{$_} =~ s/\r//g; } qw(address signature);
597 $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
599 my $dbh = SL::DBConnect->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}, SL::DBConnect->get_options);
601 $self->create_employee_entry($::form, $dbh, $self, 1);
605 $main::lxdebug->leave_sub();
608 sub create_employee_entry {
609 $main::lxdebug->enter_sub();
614 my $myconfig = shift;
615 my $update_existing = shift;
617 if (!does_table_exist($dbh, 'employee')) {
618 $main::lxdebug->leave_sub();
622 # add login to employee table if it does not exist
623 # no error check for employee table, ignore if it does not exist
624 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
625 my ($good_db) = selectrow_query($form, $dbh, qq|select * from pg_tables where tablename = ? and schemaname = ?|, 'schema_info', 'public');
627 ($can_delete) = selectrow_query($form, $dbh, qq|SELECT tag FROM schema_info WHERE tag = ?|, 'employee_deleted') if $good_db;
630 my $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
631 do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
633 } elsif ($update_existing && $can_delete) {
634 my $query = qq|UPDATE employee SET name = ?, workphone = ?, role = 'user', deleted = 'f' WHERE id = ?|;
635 do_query($form, $dbh, $query, $myconfig->{name}, $myconfig->{tel}, $id);
638 $main::lxdebug->leave_sub();
642 $main::lxdebug->enter_sub();
644 my @conf = qw(copies countrycode dateformat default_media default_printer_id email favorites fax hide_cvar_search_options mandatory_departments menustyle name
645 numberformat show_form_details signature stylesheet taxincluded_checked tel template_format vclimit);
647 $main::lxdebug->leave_sub();