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_employee_entry($form, $dbh, \%myconfig);
110 $self->create_schema_info_table($form, $dbh);
112 # Auth DB upgrades available?
113 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, auth => 1)->parse_dbupdate_controls;
114 return -3 if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
116 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
118 my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
121 return 0 if !$update_available;
123 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
124 $form->{$_} = $myconfig{$_} for qw(datestyle);
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" . $::auth->client->{dbname};
132 if ($form->{"show_dbupdate_warning"}) {
133 print $form->parse_html_template("dbupgrade/warning");
138 SL::System::InstallationLock->lock;
140 # ignore HUP, QUIT in case the webserver times out
141 $SIG{HUP} = 'IGNORE';
142 $SIG{QUIT} = 'IGNORE';
144 $self->dbupdate($form);
145 $self->dbupdate2(form => $form, updater => $dbupdater, database => $::auth->client->{dbname});
146 SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(0);
148 SL::System::InstallationLock->unlock;
150 print $form->parse_html_template("dbupgrade/footer");
156 $main::lxdebug->enter_sub();
158 my ($form, $db) = @_;
161 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
162 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
163 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
164 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
165 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
168 $form->{dboptions} = $dboptions{ $form->{dateformat} };
169 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
171 $main::lxdebug->leave_sub();
175 $main::lxdebug->enter_sub();
177 my ($self, $form) = @_;
182 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
183 &dbconnect_vars($form, $form->{dbdefault});
185 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
189 qq|SELECT datname FROM pg_database | .
190 qq|WHERE NOT datname IN ('template0', 'template1')|;
191 $sth = $dbh->prepare($query);
192 $sth->execute() || $form->dberror($query);
194 while (my ($db) = $sth->fetchrow_array) {
196 if ($form->{only_acc_db}) {
198 next if ($db =~ /^template/);
200 &dbconnect_vars($form, $db);
201 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
205 qq|SELECT tablename FROM pg_tables | .
206 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
207 my $sth = $dbh->prepare($query);
208 $sth->execute($form->{dbuser}) ||
209 $form->dberror($query . " ($form->{dbuser})");
211 if ($sth->fetchrow_array) {
212 push(@dbsources, $db);
218 push(@dbsources, $db);
224 $main::lxdebug->leave_sub();
229 sub dbclusterencoding {
230 $main::lxdebug->enter_sub();
232 my ($self, $form) = @_;
234 $form->{dbdefault} ||= $form->{dbuser};
236 dbconnect_vars($form, $form->{dbdefault});
238 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) || $form->dberror();
239 my $query = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
240 my ($cluster_encoding) = $dbh->selectrow_array($query);
243 $main::lxdebug->leave_sub();
245 return $cluster_encoding;
249 $main::lxdebug->enter_sub();
251 my ($self, $form) = @_;
253 &dbconnect_vars($form, $form->{dbdefault});
255 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
257 $form->{db} =~ s/\"//g;
261 push @dboptions, "ENCODING = " . $dbh->quote($form->{"encoding"}) if $form->{"encoding"};
262 if ($form->{"dbdefault"}) {
263 my $dbdefault = $form->{"dbdefault"};
264 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
265 push @dboptions, "TEMPLATE = $dbdefault";
268 my $query = qq|CREATE DATABASE "$form->{db}"|;
269 $query .= " WITH " . join(" ", @dboptions) if @dboptions;
271 # Ignore errors if the database exists.
276 &dbconnect_vars($form, $form->{db});
278 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
281 my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
282 $db_charset ||= Common::DEFAULT_CHARSET;
284 my $dbupdater = SL::DBUpgrade2->new(form => $form);
286 $dbupdater->process_query($dbh, "sql/lx-office.sql", undef, $db_charset);
288 # load chart of accounts
289 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
291 $query = "UPDATE defaults SET coa = ?";
292 do_query($form, $dbh, $query, $form->{chart});
293 $query = "UPDATE defaults SET accounting_method = ?";
294 do_query($form, $dbh, $query, $form->{accounting_method});
295 $query = "UPDATE defaults SET profit_determination = ?";
296 do_query($form, $dbh, $query, $form->{profit_determination});
297 $query = "UPDATE defaults SET inventory_system = ?";
298 do_query($form, $dbh, $query, $form->{inventory_system});
299 $query = "UPDATE defaults SET curr = ?";
300 do_query($form, $dbh, $query, $form->{defaultcurrency});
304 $main::lxdebug->leave_sub();
308 $main::lxdebug->enter_sub();
310 my ($self, $form) = @_;
311 $form->{db} =~ s/\"//g;
313 &dbconnect_vars($form, $form->{dbdefault});
314 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
316 my $query = qq|DROP DATABASE "$form->{db}"|;
317 do_query($form, $dbh, $query);
321 $main::lxdebug->leave_sub();
324 sub dbsources_unused {
325 $main::lxdebug->enter_sub();
327 my ($self, $form) = @_;
329 $form->{only_acc_db} = 1;
331 my %members = $main::auth->read_all_users();
332 my %dbexcl = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
334 $dbexcl{$form->{dbdefault}} = 1;
335 $dbexcl{$main::auth->{DB_config}->{db}} = 1;
337 my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
339 $main::lxdebug->leave_sub();
345 $main::lxdebug->enter_sub();
347 my ($self, $form) = @_;
349 my %members = $main::auth->read_all_users();
350 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
352 my ($query, $sth, %dbs_needing_updates);
354 foreach my $login (grep /[a-z]/, keys %members) {
355 my $member = $members{$login};
357 map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
358 dbconnect_vars($form, $form->{dbname});
360 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options);
366 $query = qq|SELECT version FROM defaults|;
367 $sth = prepare_query($form, $dbh, $query);
368 if ($sth->execute()) {
369 ($version) = $sth->fetchrow_array();
373 $dbh->disconnect and next unless $version;
375 my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh);
378 if ($update_available) {
380 map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
381 $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
385 $main::lxdebug->leave_sub();
387 return values %dbs_needing_updates;
391 $main::lxdebug->enter_sub(2);
393 my (@v, $version, $i);
395 @v = split(/\./, $_[0]);
396 while (scalar(@v) < 4) {
400 for ($i = 0; $i < 4; $i++) {
405 $main::lxdebug->leave_sub(2);
409 sub cmp_script_version {
410 my ($a_from, $a_to, $b_from, $b_to);
411 my ($i, $res_a, $res_b);
412 my ($my_a, $my_b) = ($a, $b);
414 $my_a =~ s/.*-upgrade-//;
416 $my_b =~ s/.*-upgrade-//;
418 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
419 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
421 $res_a = calc_version($my_a_from);
422 $res_b = calc_version($my_b_from);
424 if ($res_a == $res_b) {
425 $res_a = calc_version($my_a_to);
426 $res_b = calc_version($my_b_to);
429 return $res_a <=> $res_b;
432 sub create_schema_info_table {
433 $main::lxdebug->enter_sub();
435 my ($self, $form, $dbh) = @_;
437 my $query = "SELECT tag FROM schema_info LIMIT 1";
438 if (!$dbh->do($query)) {
441 qq|CREATE TABLE schema_info (| .
444 qq| itime timestamp DEFAULT now(), | .
445 qq| PRIMARY KEY (tag))|;
446 $dbh->do($query) || $form->dberror($query);
449 $main::lxdebug->leave_sub();
453 $main::lxdebug->enter_sub();
455 my ($self, $form) = @_;
459 my @upgradescripts = ();
463 if ($form->{dbupdate}) {
465 # read update scripts into memory
466 opendir(SQLDIR, "sql/Pg-upgrade")
467 or &error("", "sql/Pg-upgrade : $!");
469 sort(cmp_script_version
470 grep(/Pg-upgrade-.*?\.(sql|pl)$/,
475 my $db_charset = $::lx_office_conf{system}->{dbcharset};
476 $db_charset ||= Common::DEFAULT_CHARSET;
478 my $dbupdater = SL::DBUpgrade2->new(form => $form);
480 foreach my $db (split(/ /, $form->{dbupdate})) {
482 next unless $form->{$db};
484 # strip db from dataset
486 &dbconnect_vars($form, $db);
488 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
491 $dbh->do($form->{dboptions}) if ($form->{dboptions});
494 $query = qq|SELECT version FROM defaults|;
495 my ($version) = selectrow_query($form, $dbh, $query);
497 next unless $version;
499 $version = calc_version($version);
501 foreach my $upgradescript (@upgradescripts) {
502 my $a = $upgradescript;
503 $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
505 my ($mindb, $maxdb) = split /-/, $a;
506 my $str_maxdb = $maxdb;
507 $mindb = calc_version($mindb);
508 $maxdb = calc_version($maxdb);
510 next if ($version >= $maxdb);
512 # if there is no upgrade script exit
513 last if ($version < $mindb);
516 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
517 $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
528 $main::lxdebug->leave_sub();
534 $main::lxdebug->enter_sub();
536 my ($self, %params) = @_;
538 my $form = $params{form};
539 my $dbupdater = $params{updater};
540 my $db = $params{database};
542 my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
544 map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
546 &dbconnect_vars($form, $db);
548 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
550 $dbh->do($form->{dboptions}) if ($form->{dboptions});
552 $self->create_schema_info_table($form, $dbh);
554 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
556 $dbh->disconnect and next if !@upgradescripts;
558 foreach my $control (@upgradescripts) {
560 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
561 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
563 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control, $db_charset);
569 $main::lxdebug->leave_sub();
575 $main::lxdebug->enter_sub();
579 # format dbconnect and dboptions string
580 dbconnect_vars($self, $self->{dbname});
582 map { $self->{$_} =~ s/\r//g; } qw(address signature);
584 $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
586 my $dbh = SL::DBConnect->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}, SL::DBConnect->get_options);
588 $self->create_employee_entry($::form, $dbh, $self, 1);
592 $main::lxdebug->leave_sub();
595 sub create_employee_entry {
596 $main::lxdebug->enter_sub();
601 my $myconfig = shift;
602 my $update_existing = shift;
604 if (!does_table_exist($dbh, 'employee')) {
605 $main::lxdebug->leave_sub();
609 # add login to employee table if it does not exist
610 # no error check for employee table, ignore if it does not exist
611 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
612 my ($good_db) = selectrow_query($form, $dbh, qq|select * from pg_tables where tablename = ? and schemaname = ?|, 'schema_info', 'public');
614 ($can_delete) = selectrow_query($form, $dbh, qq|SELECT tag FROM schema_info WHERE tag = ?|, 'employee_deleted') if $good_db;
617 my $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
618 do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
620 } elsif ($update_existing && $can_delete) {
621 my $query = qq|UPDATE employee SET name = ?, workphone = ?, role = 'user', deleted = 'f' WHERE id = ?|;
622 do_query($form, $dbh, $query, $myconfig->{name}, $myconfig->{tel}, $id);
625 $main::lxdebug->leave_sub();
629 $main::lxdebug->enter_sub();
631 my @conf = qw(copies countrycode dateformat default_media default_printer_id email favorites fax hide_cvar_search_options mandatory_departments menustyle name
632 numberformat show_form_details signature stylesheet taxincluded_checked tel template_format vclimit);
634 $main::lxdebug->leave_sub();