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 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
119 $form->{$_} = $myconfig{$_} for qw(dateformat);
121 dbconnect_vars($form, $form->{dbname});
123 my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
126 return 0 if !$update_available;
127 $form->{"title"} = $main::locale->text("Dataset upgrade");
128 $form->header(no_layout => $form->{no_layout});
129 print $form->parse_html_template("dbupgrade/header");
131 $form->{dbupdate} = "db" . $form->{dbname};
133 if ($form->{"show_dbupdate_warning"}) {
134 print $form->parse_html_template("dbupgrade/warning");
139 SL::System::InstallationLock->lock;
141 # ignore HUP, QUIT in case the webserver times out
142 $SIG{HUP} = 'IGNORE';
143 $SIG{QUIT} = 'IGNORE';
145 $self->dbupdate($form);
146 $self->dbupdate2($form, $dbupdater);
147 SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(0);
149 SL::System::InstallationLock->unlock;
151 print $form->parse_html_template("dbupgrade/footer");
157 $main::lxdebug->enter_sub();
159 my ($form, $db) = @_;
162 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
163 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
164 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
165 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
166 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
169 $form->{dboptions} = $dboptions{ $form->{dateformat} };
170 $form->{dbconnect} = "dbi:Pg:dbname=${db};host=" . ($form->{dbhost} || 'localhost') . ";port=" . ($form->{dbport} || 5432);
172 $main::lxdebug->leave_sub();
176 $main::lxdebug->enter_sub();
178 my ($self, $form) = @_;
183 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
184 &dbconnect_vars($form, $form->{dbdefault});
186 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
190 qq|SELECT datname FROM pg_database | .
191 qq|WHERE NOT datname IN ('template0', 'template1')|;
192 $sth = $dbh->prepare($query);
193 $sth->execute() || $form->dberror($query);
195 while (my ($db) = $sth->fetchrow_array) {
197 if ($form->{only_acc_db}) {
199 next if ($db =~ /^template/);
201 &dbconnect_vars($form, $db);
202 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
206 qq|SELECT tablename FROM pg_tables | .
207 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
208 my $sth = $dbh->prepare($query);
209 $sth->execute($form->{dbuser}) ||
210 $form->dberror($query . " ($form->{dbuser})");
212 if ($sth->fetchrow_array) {
213 push(@dbsources, $db);
219 push(@dbsources, $db);
225 $main::lxdebug->leave_sub();
230 sub dbclusterencoding {
231 $main::lxdebug->enter_sub();
233 my ($self, $form) = @_;
235 $form->{dbdefault} ||= $form->{dbuser};
237 dbconnect_vars($form, $form->{dbdefault});
239 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) || $form->dberror();
240 my $query = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
241 my ($cluster_encoding) = $dbh->selectrow_array($query);
244 $main::lxdebug->leave_sub();
246 return $cluster_encoding;
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 $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
283 $db_charset ||= Common::DEFAULT_CHARSET;
285 my $dbupdater = SL::DBUpgrade2->new(form => $form);
287 $dbupdater->process_query($dbh, "sql/lx-office.sql", undef, $db_charset);
289 # load chart of accounts
290 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
292 $query = "UPDATE defaults SET coa = ?";
293 do_query($form, $dbh, $query, $form->{chart});
294 $query = "UPDATE defaults SET accounting_method = ?";
295 do_query($form, $dbh, $query, $form->{accounting_method});
296 $query = "UPDATE defaults SET profit_determination = ?";
297 do_query($form, $dbh, $query, $form->{profit_determination});
298 $query = "UPDATE defaults SET inventory_system = ?";
299 do_query($form, $dbh, $query, $form->{inventory_system});
300 $query = "UPDATE defaults SET curr = ?";
301 do_query($form, $dbh, $query, $form->{defaultcurrency});
305 $main::lxdebug->leave_sub();
309 $main::lxdebug->enter_sub();
311 my ($self, $form) = @_;
312 $form->{db} =~ s/\"//g;
314 &dbconnect_vars($form, $form->{dbdefault});
315 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
317 my $query = qq|DROP DATABASE "$form->{db}"|;
318 do_query($form, $dbh, $query);
322 $main::lxdebug->leave_sub();
325 sub dbsources_unused {
326 $main::lxdebug->enter_sub();
328 my ($self, $form) = @_;
330 $form->{only_acc_db} = 1;
332 my %members = $main::auth->read_all_users();
333 my %dbexcl = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
335 $dbexcl{$form->{dbdefault}} = 1;
336 $dbexcl{$main::auth->{DB_config}->{db}} = 1;
338 my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
340 $main::lxdebug->leave_sub();
346 $main::lxdebug->enter_sub();
348 my ($self, $form) = @_;
350 my %members = $main::auth->read_all_users();
351 my $dbupdater = SL::DBUpgrade2->new(form => $form)->parse_dbupdate_controls;
353 my ($query, $sth, %dbs_needing_updates);
355 foreach my $login (grep /[a-z]/, keys %members) {
356 my $member = $members{$login};
358 map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
359 dbconnect_vars($form, $form->{dbname});
361 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options);
367 $query = qq|SELECT version FROM defaults|;
368 $sth = prepare_query($form, $dbh, $query);
369 if ($sth->execute()) {
370 ($version) = $sth->fetchrow_array();
374 $dbh->disconnect and next unless $version;
376 my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh);
379 if ($update_available) {
381 map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
382 $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
386 $main::lxdebug->leave_sub();
388 return values %dbs_needing_updates;
392 $main::lxdebug->enter_sub(2);
394 my (@v, $version, $i);
396 @v = split(/\./, $_[0]);
397 while (scalar(@v) < 4) {
401 for ($i = 0; $i < 4; $i++) {
406 $main::lxdebug->leave_sub(2);
410 sub cmp_script_version {
411 my ($a_from, $a_to, $b_from, $b_to);
412 my ($i, $res_a, $res_b);
413 my ($my_a, $my_b) = ($a, $b);
415 $my_a =~ s/.*-upgrade-//;
417 $my_b =~ s/.*-upgrade-//;
419 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
420 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
422 $res_a = calc_version($my_a_from);
423 $res_b = calc_version($my_b_from);
425 if ($res_a == $res_b) {
426 $res_a = calc_version($my_a_to);
427 $res_b = calc_version($my_b_to);
430 return $res_a <=> $res_b;
433 sub create_schema_info_table {
434 $main::lxdebug->enter_sub();
436 my ($self, $form, $dbh) = @_;
438 my $query = "SELECT tag FROM schema_info LIMIT 1";
439 if (!$dbh->do($query)) {
442 qq|CREATE TABLE schema_info (| .
445 qq| itime timestamp DEFAULT now(), | .
446 qq| PRIMARY KEY (tag))|;
447 $dbh->do($query) || $form->dberror($query);
450 $main::lxdebug->leave_sub();
454 $main::lxdebug->enter_sub();
456 my ($self, $form) = @_;
460 my @upgradescripts = ();
464 if ($form->{dbupdate}) {
466 # read update scripts into memory
467 opendir(SQLDIR, "sql/Pg-upgrade")
468 or &error("", "sql/Pg-upgrade : $!");
470 sort(cmp_script_version
471 grep(/Pg-upgrade-.*?\.(sql|pl)$/,
476 my $db_charset = $::lx_office_conf{system}->{dbcharset};
477 $db_charset ||= Common::DEFAULT_CHARSET;
479 my $dbupdater = SL::DBUpgrade2->new(form => $form);
481 foreach my $db (split(/ /, $form->{dbupdate})) {
483 next unless $form->{$db};
485 # strip db from dataset
487 &dbconnect_vars($form, $db);
489 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
492 $dbh->do($form->{dboptions}) if ($form->{dboptions});
495 $query = qq|SELECT version FROM defaults|;
496 my ($version) = selectrow_query($form, $dbh, $query);
498 next unless $version;
500 $version = calc_version($version);
502 foreach my $upgradescript (@upgradescripts) {
503 my $a = $upgradescript;
504 $a =~ s/^Pg-upgrade-|\.(sql|pl)$//g;
506 my ($mindb, $maxdb) = split /-/, $a;
507 my $str_maxdb = $maxdb;
508 $mindb = calc_version($mindb);
509 $maxdb = calc_version($maxdb);
511 next if ($version >= $maxdb);
513 # if there is no upgrade script exit
514 last if ($version < $mindb);
517 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
518 $dbupdater->process_file($dbh, "sql/Pg-upgrade/$upgradescript", $str_maxdb, $db_charset);
529 $main::lxdebug->leave_sub();
535 $main::lxdebug->enter_sub();
537 my ($self, $form, $dbupdater) = @_;
540 my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
542 map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
544 foreach my $db (split / /, $form->{dbupdate}) {
545 next unless $form->{$db};
547 # strip db from dataset
549 &dbconnect_vars($form, $db);
551 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
553 $dbh->do($form->{dboptions}) if ($form->{dboptions});
555 $self->create_schema_info_table($form, $dbh);
557 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
559 $dbh->disconnect and next if !@upgradescripts;
561 foreach my $control (@upgradescripts) {
563 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
564 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
566 $dbupdater->process_file($dbh, "sql/Pg-upgrade2/$control->{file}", $control, $db_charset);
574 $main::lxdebug->leave_sub();
580 $main::lxdebug->enter_sub();
584 # format dbconnect and dboptions string
585 dbconnect_vars($self, $self->{dbname});
587 map { $self->{$_} =~ s/\r//g; } qw(address signature);
589 $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
591 my $dbh = SL::DBConnect->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}, SL::DBConnect->get_options);
593 $self->create_employee_entry($::form, $dbh, $self, 1);
597 $main::lxdebug->leave_sub();
600 sub create_employee_entry {
601 $main::lxdebug->enter_sub();
606 my $myconfig = shift;
607 my $update_existing = shift;
609 if (!does_table_exist($dbh, 'employee')) {
610 $main::lxdebug->leave_sub();
614 # add login to employee table if it does not exist
615 # no error check for employee table, ignore if it does not exist
616 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
617 my ($good_db) = selectrow_query($form, $dbh, qq|select * from pg_tables where tablename = ? and schemaname = ?|, 'schema_info', 'public');
619 ($can_delete) = selectrow_query($form, $dbh, qq|SELECT tag FROM schema_info WHERE tag = ?|, 'employee_deleted') if $good_db;
622 my $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
623 do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
625 } elsif ($update_existing && $can_delete) {
626 my $query = qq|UPDATE employee SET name = ?, workphone = ?, role = 'user', deleted = 'f' WHERE id = ?|;
627 do_query($form, $dbh, $query, $myconfig->{name}, $myconfig->{tel}, $id);
630 $main::lxdebug->leave_sub();
634 $main::lxdebug->enter_sub();
636 my @conf = qw(copies countrycode dateformat default_media default_printer_id email favorites fax hide_cvar_search_options mandatory_departments menustyle name
637 numberformat show_form_details signature stylesheet taxincluded_checked tel template_format vclimit);
639 $main::lxdebug->leave_sub();