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) = @_;
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 $query = qq|SELECT version FROM defaults|;
107 my $sth = $dbh->prepare($query);
108 $sth->execute || $form->dberror($query);
110 my ($dbversion) = $sth->fetchrow_array;
113 $self->create_employee_entry($form, $dbh, \%myconfig);
115 $self->create_schema_info_table($form, $dbh);
117 # Auth DB upgrades available?
118 my $dbupdater_auth = SL::DBUpgrade2->new(form => $form, dbdriver => 'Pg', auth => 1)->parse_dbupdate_controls;
119 return -3 if $dbupdater_auth->unapplied_upgrade_scripts($::auth->dbconnect);
121 my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $myconfig{dbdriver})->parse_dbupdate_controls;
123 $form->{$_} = $::auth->client->{$_} for qw(dbname dbhost dbport dbuser dbpasswd);
124 $form->{$_} = $myconfig{$_} for qw(dateformat);
126 dbconnect_vars($form, $form->{dbname});
128 my $update_available = $dbupdater->update_available($dbversion) || $dbupdater->update2_available($dbh);
131 return 0 if !$update_available;
132 $form->{"title"} = $main::locale->text("Dataset upgrade");
133 $form->header(no_layout => $form->{no_layout});
134 print $form->parse_html_template("dbupgrade/header");
136 $form->{dbupdate} = "db" . $form->{dbname};
138 if ($form->{"show_dbupdate_warning"}) {
139 print $form->parse_html_template("dbupgrade/warning");
145 if (!$::lx_office_conf{debug}->{keep_installation_unlocked} && !open($fh, ">", $::lx_office_conf{paths}->{userspath} . "/nologin")) {
146 $form->show_generic_error($main::locale->text('A temporary file could not be created. ' .
147 'Please verify that the directory "#1" is writeable by the webserver.',
148 $::lx_office_conf{paths}->{userspath}),
152 # ignore HUP, QUIT in case the webserver times out
153 $SIG{HUP} = 'IGNORE';
154 $SIG{QUIT} = 'IGNORE';
156 $self->dbupdate($form);
157 $self->dbupdate2($form, $dbupdater);
158 SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(0);
163 unlink($::lx_office_conf{paths}->{userspath} . "/nologin");
165 print $form->parse_html_template("dbupgrade/footer");
171 $main::lxdebug->enter_sub();
173 my ($form, $db) = @_;
176 'Pg' => { 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
177 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
178 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
179 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
180 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
183 'yy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
184 'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
185 'mm/dd/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
186 'dd/mm/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
187 'dd.mm.yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
190 $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
192 if ($form->{dbdriver} eq 'Pg') {
193 $form->{dbconnect} = "dbi:Pg:dbname=$db";
196 if ($form->{dbdriver} eq 'Oracle') {
197 $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
200 if ($form->{dbhost}) {
201 $form->{dbconnect} .= ";host=$form->{dbhost}";
203 if ($form->{dbport}) {
204 $form->{dbconnect} .= ";port=$form->{dbport}";
207 $main::lxdebug->leave_sub();
211 $main::lxdebug->enter_sub();
213 my @drivers = DBI->available_drivers();
215 $main::lxdebug->leave_sub();
217 return (grep { /(Pg|Oracle)/ } @drivers);
221 $main::lxdebug->enter_sub();
223 my ($self, $form) = @_;
228 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
229 $form->{sid} = $form->{dbdefault};
230 &dbconnect_vars($form, $form->{dbdefault});
232 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
235 if ($form->{dbdriver} eq 'Pg') {
237 qq|SELECT datname FROM pg_database | .
238 qq|WHERE NOT datname IN ('template0', 'template1')|;
239 $sth = $dbh->prepare($query);
240 $sth->execute() || $form->dberror($query);
242 while (my ($db) = $sth->fetchrow_array) {
244 if ($form->{only_acc_db}) {
246 next if ($db =~ /^template/);
248 &dbconnect_vars($form, $db);
249 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
253 qq|SELECT tablename FROM pg_tables | .
254 qq|WHERE (tablename = 'defaults') AND (tableowner = ?)|;
255 my $sth = $dbh->prepare($query);
256 $sth->execute($form->{dbuser}) ||
257 $form->dberror($query . " ($form->{dbuser})");
259 if ($sth->fetchrow_array) {
260 push(@dbsources, $db);
266 push(@dbsources, $db);
270 if ($form->{dbdriver} eq 'Oracle') {
271 if ($form->{only_acc_db}) {
273 qq|SELECT owner FROM dba_objects | .
274 qq|WHERE object_name = 'DEFAULTS' AND object_type = 'TABLE'|;
276 $query = qq|SELECT username FROM dba_users|;
279 $sth = $dbh->prepare($query);
280 $sth->execute || $form->dberror($query);
282 while (my ($db) = $sth->fetchrow_array) {
283 push(@dbsources, $db);
290 $main::lxdebug->leave_sub();
295 sub dbclusterencoding {
296 $main::lxdebug->enter_sub();
298 my ($self, $form) = @_;
300 $form->{dbdefault} ||= $form->{dbuser};
302 dbconnect_vars($form, $form->{dbdefault});
304 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) || $form->dberror();
305 my $query = qq|SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = 'template0'|;
306 my ($cluster_encoding) = $dbh->selectrow_array($query);
309 $main::lxdebug->leave_sub();
311 return $cluster_encoding;
315 $main::lxdebug->enter_sub();
317 my ($self, $form) = @_;
319 $form->{sid} = $form->{dbdefault};
320 &dbconnect_vars($form, $form->{dbdefault});
322 SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
324 $form->{db} =~ s/\"//g;
326 'Pg' => qq|CREATE DATABASE "$form->{db}"|,
328 qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS | .
329 qq|TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
336 push(@{$dboptions{"Pg"}}, "ENCODING = " . $dbh->quote($form->{"encoding"}))
337 if ($form->{"encoding"});
338 if ($form->{"dbdefault"}) {
339 my $dbdefault = $form->{"dbdefault"};
340 $dbdefault =~ s/[^a-zA-Z0-9_\-]//g;
341 push(@{$dboptions{"Pg"}}, "TEMPLATE = $dbdefault");
344 my $query = $dbcreate{$form->{dbdriver}};
345 $query .= " WITH " . join(" ", @{$dboptions{"Pg"}}) if (@{$dboptions{"Pg"}});
347 # Ignore errors if the database exists.
350 if ($form->{dbdriver} eq 'Oracle') {
351 $query = qq|GRANT CONNECT, RESOURCE TO "$form->{db}"|;
352 do_query($form, $dbh, $query);
356 # setup variables for the new database
357 if ($form->{dbdriver} eq 'Oracle') {
358 $form->{dbuser} = $form->{db};
359 $form->{dbpasswd} = $form->{db};
362 &dbconnect_vars($form, $form->{db});
364 $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
367 my $db_charset = $Common::db_encoding_to_charset{$form->{encoding}};
368 $db_charset ||= Common::DEFAULT_CHARSET;
370 my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver});
372 $dbupdater->process_query($dbh, "sql/lx-office.sql", undef, $db_charset);
374 # load chart of accounts
375 $dbupdater->process_query($dbh, "sql/$form->{chart}-chart.sql", undef, $db_charset);
377 $query = "UPDATE defaults SET coa = ?";
378 do_query($form, $dbh, $query, $form->{chart});
379 $query = "UPDATE defaults SET accounting_method = ?";
380 do_query($form, $dbh, $query, $form->{accounting_method});
381 $query = "UPDATE defaults SET profit_determination = ?";
382 do_query($form, $dbh, $query, $form->{profit_determination});
383 $query = "UPDATE defaults SET inventory_system = ?";
384 do_query($form, $dbh, $query, $form->{inventory_system});
385 $query = "UPDATE defaults SET curr = ?";
386 do_query($form, $dbh, $query, $form->{defaultcurrency});
390 $main::lxdebug->leave_sub();
394 $main::lxdebug->enter_sub();
396 my ($self, $form) = @_;
397 $form->{db} =~ s/\"//g;
398 my %dbdelete = ('Pg' => qq|DROP DATABASE "$form->{db}"|,
399 'Oracle' => qq|DROP USER "$form->{db}" CASCADE|);
401 $form->{sid} = $form->{dbdefault};
402 &dbconnect_vars($form, $form->{dbdefault});
403 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
405 my $query = $dbdelete{$form->{dbdriver}};
406 do_query($form, $dbh, $query);
410 $main::lxdebug->leave_sub();
413 sub dbsources_unused {
414 $main::lxdebug->enter_sub();
416 my ($self, $form) = @_;
418 $form->{only_acc_db} = 1;
420 my %members = $main::auth->read_all_users();
421 my %dbexcl = map { $_ => 1 } grep { $_ } map { $_->{dbname} } values %members;
423 $dbexcl{$form->{dbdefault}} = 1;
424 $dbexcl{$main::auth->{DB_config}->{db}} = 1;
426 my @dbunused = grep { !$dbexcl{$_} } dbsources("", $form);
428 $main::lxdebug->leave_sub();
434 $main::lxdebug->enter_sub();
436 my ($self, $form) = @_;
438 my %members = $main::auth->read_all_users();
439 my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls;
441 my ($query, $sth, %dbs_needing_updates);
443 foreach my $login (grep /[a-z]/, keys %members) {
444 my $member = $members{$login};
446 map { $form->{$_} = $member->{$_} } qw(dbname dbuser dbpasswd dbhost dbport);
447 dbconnect_vars($form, $form->{dbname});
449 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options);
455 $query = qq|SELECT version FROM defaults|;
456 $sth = prepare_query($form, $dbh, $query);
457 if ($sth->execute()) {
458 ($version) = $sth->fetchrow_array();
462 $dbh->disconnect and next unless $version;
464 my $update_available = $dbupdater->update_available($version) || $dbupdater->update2_available($dbh);
467 if ($update_available) {
469 map { $dbinfo->{$_} = $member->{$_} } grep /^db/, keys %{ $member };
470 $dbs_needing_updates{$member->{dbhost} . "::" . $member->{dbname}} = $dbinfo;
474 $main::lxdebug->leave_sub();
476 return values %dbs_needing_updates;
480 $main::lxdebug->enter_sub(2);
482 my (@v, $version, $i);
484 @v = split(/\./, $_[0]);
485 while (scalar(@v) < 4) {
489 for ($i = 0; $i < 4; $i++) {
494 $main::lxdebug->leave_sub(2);
498 sub cmp_script_version {
499 my ($a_from, $a_to, $b_from, $b_to);
500 my ($i, $res_a, $res_b);
501 my ($my_a, $my_b) = ($a, $b);
503 $my_a =~ s/.*-upgrade-//;
505 $my_b =~ s/.*-upgrade-//;
507 my ($my_a_from, $my_a_to) = split(/-/, $my_a);
508 my ($my_b_from, $my_b_to) = split(/-/, $my_b);
510 $res_a = calc_version($my_a_from);
511 $res_b = calc_version($my_b_from);
513 if ($res_a == $res_b) {
514 $res_a = calc_version($my_a_to);
515 $res_b = calc_version($my_b_to);
518 return $res_a <=> $res_b;
521 sub create_schema_info_table {
522 $main::lxdebug->enter_sub();
524 my ($self, $form, $dbh) = @_;
526 my $query = "SELECT tag FROM schema_info LIMIT 1";
527 if (!$dbh->do($query)) {
530 qq|CREATE TABLE schema_info (| .
533 qq| itime timestamp DEFAULT now(), | .
534 qq| PRIMARY KEY (tag))|;
535 $dbh->do($query) || $form->dberror($query);
538 $main::lxdebug->leave_sub();
542 $main::lxdebug->enter_sub();
544 my ($self, $form) = @_;
548 $form->{sid} = $form->{dbdefault};
550 my @upgradescripts = ();
554 if ($form->{dbupdate}) {
556 # read update scripts into memory
557 opendir(SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade")
558 or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
560 sort(cmp_script_version
561 grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/,
566 my $db_charset = $::lx_office_conf{system}->{dbcharset};
567 $db_charset ||= Common::DEFAULT_CHARSET;
569 my $dbupdater = SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver});
571 foreach my $db (split(/ /, $form->{dbupdate})) {
573 next unless $form->{$db};
575 # strip db from dataset
577 &dbconnect_vars($form, $db);
579 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options)
582 $dbh->do($form->{dboptions}) if ($form->{dboptions});
585 $query = qq|SELECT version FROM defaults|;
586 my ($version) = selectrow_query($form, $dbh, $query);
588 next unless $version;
590 $version = calc_version($version);
592 foreach my $upgradescript (@upgradescripts) {
593 my $a = $upgradescript;
594 $a =~ s/^\Q$form->{dbdriver}\E-upgrade-|\.(sql|pl)$//g;
596 my ($mindb, $maxdb) = split /-/, $a;
597 my $str_maxdb = $maxdb;
598 $mindb = calc_version($mindb);
599 $maxdb = calc_version($maxdb);
601 next if ($version >= $maxdb);
603 # if there is no upgrade script exit
604 last if ($version < $mindb);
607 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $upgradescript");
608 $dbupdater->process_file($dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb, $db_charset);
619 $main::lxdebug->leave_sub();
625 $main::lxdebug->enter_sub();
627 my ($self, $form, $dbupdater) = @_;
629 $form->{sid} = $form->{dbdefault};
632 my $db_charset = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
634 map { $_->{description} = SL::Iconv::convert($_->{charset}, $db_charset, $_->{description}) } values %{ $dbupdater->{all_controls} };
636 foreach my $db (split / /, $form->{dbupdate}) {
637 next unless $form->{$db};
639 # strip db from dataset
641 &dbconnect_vars($form, $db);
643 my $dbh = SL::DBConnect->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd}, SL::DBConnect->get_options) or $form->dberror;
645 $dbh->do($form->{dboptions}) if ($form->{dboptions});
647 $self->create_schema_info_table($form, $dbh);
649 my @upgradescripts = $dbupdater->unapplied_upgrade_scripts($dbh);
651 $dbh->disconnect and next if !@upgradescripts;
653 foreach my $control (@upgradescripts) {
655 $main::lxdebug->message(LXDebug->DEBUG2(), "Applying Update $control->{file}");
656 print $form->parse_html_template("dbupgrade/upgrade_message2", $control);
658 $dbupdater->process_file($dbh, "sql/" . $form->{"dbdriver"} . "-upgrade2/$control->{file}", $control, $db_charset);
666 $main::lxdebug->leave_sub();
672 $main::lxdebug->enter_sub();
676 # format dbconnect and dboptions string
677 dbconnect_vars($self, $self->{dbname});
679 map { $self->{$_} =~ s/\r//g; } qw(address signature);
681 $main::auth->save_user($self->{login}, map { $_, $self->{$_} } config_vars());
683 my $dbh = SL::DBConnect->connect($self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd}, SL::DBConnect->get_options);
685 $self->create_employee_entry($::form, $dbh, $self, 1);
689 $main::lxdebug->leave_sub();
692 sub create_employee_entry {
693 $main::lxdebug->enter_sub();
698 my $myconfig = shift;
699 my $update_existing = shift;
701 if (!does_table_exist($dbh, 'employee')) {
702 $main::lxdebug->leave_sub();
706 # add login to employee table if it does not exist
707 # no error check for employee table, ignore if it does not exist
708 my ($id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $self->{login});
709 my ($good_db) = selectrow_query($form, $dbh, qq|select * from pg_tables where tablename = ? and schemaname = ?|, 'schema_info', 'public');
711 ($can_delete) = selectrow_query($form, $dbh, qq|SELECT tag FROM schema_info WHERE tag = ?|, 'employee_deleted') if $good_db;
714 my $query = qq|INSERT INTO employee (login, name, workphone, role) VALUES (?, ?, ?, ?)|;
715 do_query($form, $dbh, $query, ($self->{login}, $myconfig->{name}, $myconfig->{tel}, "user"));
717 } elsif ($update_existing && $can_delete) {
718 my $query = qq|UPDATE employee SET name = ?, workphone = ?, role = 'user', deleted = 'f' WHERE id = ?|;
719 do_query($form, $dbh, $query, $myconfig->{name}, $myconfig->{tel}, $id);
722 $main::lxdebug->leave_sub();
726 $main::lxdebug->enter_sub();
728 my @conf = qw(copies countrycode dateformat default_media default_printer_id email favorites fax hide_cvar_search_options mandatory_departments menustyle name
729 numberformat show_form_details signature stylesheet taxincluded_checked tel template_format vclimit);
731 $main::lxdebug->leave_sub();