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 #=====================================================================
38 $main::lxdebug->enter_sub();
40 my ($type, $memfile, $login) = @_;
44 &error("", "$memfile locked!") if (-f "${memfile}.LCK");
46 open(MEMBER, "$memfile") or &error("", "$memfile : $!");
57 # remove any trailing whitespace
60 ($key, $value) = split(/=/, $_, 2);
62 if (($key eq "stylesheet") && ($value eq "sql-ledger.css")) {
63 $value = "lx-office-erp.css";
66 $self->{$key} = $value;
69 $self->{login} = $login;
77 $main::lxdebug->leave_sub();
82 $main::lxdebug->enter_sub();
87 # scan the locale directory and read in the LANGUAGE files
88 opendir DIR, "locale";
90 my @dir = grep !/(^\.\.?$|\..*)/, readdir DIR;
92 foreach my $dir (@dir) {
93 next unless open(FH, "locale/$dir/LANGUAGE");
97 $cc{$dir} = "@language";
102 $main::lxdebug->leave_sub();
108 $main::lxdebug->enter_sub();
110 my ($self, $form, $userspath) = @_;
114 if ($self->{login}) {
116 if ($self->{password}) {
117 $form->{password} = crypt $form->{password},
118 substr($self->{login}, 0, 2);
119 if ($self->{password} ne $form->{password}) {
120 $main::lxdebug->leave_sub();
125 unless (-e "$userspath/$self->{login}.conf") {
126 $self->create_config("$userspath/$self->{login}.conf");
129 do "$userspath/$self->{login}.conf";
130 $myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd};
132 # check if database is down
134 DBI->connect($myconfig{dbconnect}, $myconfig{dbuser},
136 or $self->error(DBI::errstr);
138 # we got a connection, check the version
139 my $query = qq|SELECT version FROM defaults|;
140 my $sth = $dbh->prepare($query);
141 $sth->execute || $form->dberror($query);
143 my ($dbversion) = $sth->fetchrow_array;
146 # add login to employee table if it does not exist
147 # no error check for employee table, ignore if it does not exist
148 $query = qq|SELECT e.id FROM employee e WHERE e.login = '$self->{login}'|;
149 $sth = $dbh->prepare($query);
152 my ($login) = $sth->fetchrow_array;
156 $query = qq|INSERT INTO employee (login, name, workphone, role)
157 VALUES ('$self->{login}', '$myconfig{name}',
158 '$myconfig{tel}', 'user')|;
165 if (&update_available($myconfig{"dbdriver"}, $dbversion)) {
168 open FH, ">$userspath/nologin" or die "
171 map { $form->{$_} = $myconfig{$_} }
172 qw(dbname dbhost dbport dbdriver dbuser dbpasswd);
174 $form->{dbupdate} = "db$myconfig{dbname}";
175 $form->{ $form->{dbupdate} } = 1;
177 $form->{"stylesheet"} = "lx-office-erp.css";
178 $form->{"title"} = $main::locale->text("Dataset upgrade");
180 print($form->parse_html_template("dbupgrade/header",
181 { "dbname" => $myconfig{dbname} }));
183 # required for Oracle
184 $form->{dbdefault} = $sid;
186 # ignore HUP, QUIT in case the webserver times out
187 $SIG{HUP} = 'IGNORE';
188 $SIG{QUIT} = 'IGNORE';
190 $self->dbupdate($form);
193 unlink "$userspath/nologin";
195 print($form->parse_html_template("dbupgrade/footer"));
202 $main::lxdebug->leave_sub();
208 $main::lxdebug->enter_sub();
210 my ($form, $db) = @_;
213 'Pg' => { 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
214 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
215 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
216 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
217 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
218 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
219 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
222 'yy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
223 'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
224 'mm/dd/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
225 'mm-dd-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
226 'dd/mm/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
227 'dd-mm-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
228 'dd.mm.yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
231 $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
233 if ($form->{dbdriver} eq 'Pg') {
234 $form->{dbconnect} = "dbi:Pg:dbname=$db";
237 if ($form->{dbdriver} eq 'Oracle') {
238 $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
241 if ($form->{dbhost}) {
242 $form->{dbconnect} .= ";host=$form->{dbhost}";
244 if ($form->{dbport}) {
245 $form->{dbconnect} .= ";port=$form->{dbport}";
248 $main::lxdebug->leave_sub();
252 $main::lxdebug->enter_sub();
254 my @drivers = DBI->available_drivers();
256 $main::lxdebug->leave_sub();
258 return (grep { /(Pg|Oracle)/ } @drivers);
262 $main::lxdebug->enter_sub();
264 my ($self, $form) = @_;
269 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
270 $form->{sid} = $form->{dbdefault};
271 &dbconnect_vars($form, $form->{dbdefault});
274 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
277 if ($form->{dbdriver} eq 'Pg') {
279 $query = qq|SELECT datname FROM pg_database WHERE NOT ((datname = 'template0') OR (datname = 'template1'))|;
280 $sth = $dbh->prepare($query);
281 $sth->execute || $form->dberror($query);
283 while (my ($db) = $sth->fetchrow_array) {
285 if ($form->{only_acc_db}) {
287 next if ($db =~ /^template/);
289 &dbconnect_vars($form, $db);
291 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
294 $query = qq|SELECT p.tablename FROM pg_tables p
295 WHERE p.tablename = 'defaults'
296 AND p.tableowner = '$form->{dbuser}'|;
297 my $sth = $dbh->prepare($query);
298 $sth->execute || $form->dberror($query);
300 if ($sth->fetchrow_array) {
301 push @dbsources, $db;
307 push @dbsources, $db;
311 if ($form->{dbdriver} eq 'Oracle') {
312 if ($form->{only_acc_db}) {
313 $query = qq|SELECT o.owner FROM dba_objects o
314 WHERE o.object_name = 'DEFAULTS'
315 AND o.object_type = 'TABLE'|;
317 $query = qq|SELECT username FROM dba_users|;
320 $sth = $dbh->prepare($query);
321 $sth->execute || $form->dberror($query);
323 while (my ($db) = $sth->fetchrow_array) {
324 push @dbsources, $db;
331 $main::lxdebug->leave_sub();
337 $main::lxdebug->enter_sub();
339 my ($self, $form) = @_;
342 'Pg' => qq|CREATE DATABASE "$form->{db}"|,
344 qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
347 $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding};
349 $form->{sid} = $form->{dbdefault};
350 &dbconnect_vars($form, $form->{dbdefault});
352 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
354 my $query = qq|$dbcreate{$form->{dbdriver}}|;
355 $dbh->do($query) || $form->dberror($query);
357 if ($form->{dbdriver} eq 'Oracle') {
358 $query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|;
359 $dbh->do($query) || $form->dberror($query);
363 # setup variables for the new database
364 if ($form->{dbdriver} eq 'Oracle') {
365 $form->{dbuser} = $form->{db};
366 $form->{dbpasswd} = $form->{db};
369 &dbconnect_vars($form, $form->{db});
371 $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
375 my $filename = qq|sql/lx-office.sql|;
376 $self->process_query($form, $dbh, $filename);
379 ($filename) = split /_/, $form->{chart};
381 $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
383 # load chart of accounts
384 $filename = qq|sql/$form->{chart}-chart.sql|;
385 $self->process_query($form, $dbh, $filename);
389 $main::lxdebug->leave_sub();
392 # Process a Perl script which updates the database.
393 # If the script returns 1 then the update was successful.
394 # Return code "2" means "needs more interaction; remove
395 # users/nologin and exit".
396 # All other return codes are fatal errors.
397 sub process_perl_script {
398 $main::lxdebug->enter_sub();
400 my ($self, $form, $dbh, $filename, $version) = @_;
402 open(FH, "$filename") or $form->error("$filename : $!\n");
403 my $contents = join("", <FH>);
408 my $result = eval($contents);
415 if (!defined($result)) {
416 print($form->parse_html_template("dbupgrade/error",
417 { "file" => $filename,
420 } elsif (1 != $result) {
421 unlink("users/nologin") if (2 == $result);
426 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
430 $main::lxdebug->leave_sub();
434 $main::lxdebug->enter_sub();
436 my ($self, $form, $dbh, $filename, $version) = @_;
438 # return unless (-f $filename);
440 open(FH, "$filename") or $form->error("$filename : $!\n");
449 # Remove DOS and Unix style line endings.
455 for (my $i = 0; $i < length($_); $i++) {
456 my $char = substr($_, $i, 1);
458 # Are we inside a string?
460 if ($char eq $quote_chars[-1]) {
466 if (($char eq "'") || ($char eq "\"")) {
467 push(@quote_chars, $char);
469 } elsif ($char eq ";") {
471 # Query is complete. Send it.
473 $sth = $dbh->prepare($query);
474 if (!$sth->execute()) {
475 my $errstr = $dbh->errstr;
478 $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" .
479 "The error message was: ${errstr}<br>" .
480 "All changes in that file have been reverted.");
494 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
500 $main::lxdebug->leave_sub();
504 $main::lxdebug->enter_sub();
506 my ($self, $form) = @_;
508 my %dbdelete = ('Pg' => qq|DROP DATABASE "$form->{db}"|,
509 'Oracle' => qq|DROP USER $form->{db} CASCADE|);
511 $form->{sid} = $form->{dbdefault};
512 &dbconnect_vars($form, $form->{dbdefault});
514 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
516 my $query = qq|$dbdelete{$form->{dbdriver}}|;
517 $dbh->do($query) || $form->dberror($query);
521 $main::lxdebug->leave_sub();
524 sub dbsources_unused {
525 $main::lxdebug->enter_sub();
527 my ($self, $form, $memfile) = @_;
532 $form->error('File locked!') if (-f "${memfile}.LCK");
535 open(FH, "$memfile") or $form->error("$memfile : $!");
539 my ($null, $item) = split(/=/);
546 $form->{only_acc_db} = 1;
547 my @db = &dbsources("", $form);
549 push @dbexcl, $form->{dbdefault};
551 foreach $item (@db) {
552 unless (grep /$item$/, @dbexcl) {
553 push @dbsources, $item;
557 $main::lxdebug->leave_sub();
563 $main::lxdebug->enter_sub();
565 my ($self, $form) = @_;
570 $form->{sid} = $form->{dbdefault};
571 &dbconnect_vars($form, $form->{dbdefault});
574 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
577 if ($form->{dbdriver} eq 'Pg') {
579 $query = qq|SELECT d.datname FROM pg_database d, pg_user u
580 WHERE d.datdba = u.usesysid
581 AND u.usename = '$form->{dbuser}'|;
582 my $sth = $dbh->prepare($query);
583 $sth->execute || $form->dberror($query);
585 while (my ($db) = $sth->fetchrow_array) {
587 next if ($db =~ /^template/);
589 &dbconnect_vars($form, $db);
592 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
595 $query = qq|SELECT t.tablename FROM pg_tables t
596 WHERE t.tablename = 'defaults'|;
597 my $sth = $dbh->prepare($query);
598 $sth->execute || $form->dberror($query);
600 if ($sth->fetchrow_array) {
601 $query = qq|SELECT version FROM defaults|;
602 my $sth = $dbh->prepare($query);
605 if (my ($version) = $sth->fetchrow_array) {
606 $dbsources{$db} = $version;
616 if ($form->{dbdriver} eq 'Oracle') {
617 $query = qq|SELECT o.owner FROM dba_objects o
618 WHERE o.object_name = 'DEFAULTS'
619 AND o.object_type = 'TABLE'|;
621 $sth = $dbh->prepare($query);
622 $sth->execute || $form->dberror($query);
624 while (my ($db) = $sth->fetchrow_array) {
626 $form->{dbuser} = $db;
627 &dbconnect_vars($form, $db);
630 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
633 $query = qq|SELECT version FROM defaults|;
634 my $sth = $dbh->prepare($query);
637 if (my ($version) = $sth->fetchrow_array) {
638 $dbsources{$db} = $version;
648 $main::lxdebug->leave_sub();
655 $main::lxdebug->enter_sub(2);
657 my (@v, $version, $i);
659 @v = split(/\./, $_[0]);
660 while (scalar(@v) < 4) {
664 for ($i = 0; $i < 4; $i++) {
669 $main::lxdebug->leave_sub(2);
673 sub cmp_script_version {
674 my ($a_from, $a_to, $b_from, $b_to);
675 my ($i, $res_a, $res_b);
676 my ($my_a, $my_b) = ($a, $b);
678 $my_a =~ s/.*-upgrade-//;
680 $my_b =~ s/.*-upgrade-//;
682 ($my_a_from, $my_a_to) = split(/-/, $my_a);
683 ($my_b_from, $my_b_to) = split(/-/, $my_b);
685 $res_a = calc_version($my_a_from);
686 $res_b = calc_version($my_b_from);
688 if ($res_a == $res_b) {
689 $res_a = calc_version($my_a_to);
690 $res_b = calc_version($my_b_to);
693 return $res_a <=> $res_b;
697 sub update_available {
698 my ($dbdriver, $cur_version) = @_;
700 opendir SQLDIR, "sql/${dbdriver}-upgrade" or &error("", "sql/${dbdriver}-upgrade: $!");
702 grep(/$form->{dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)/, readdir(SQLDIR));
705 return ($#upgradescripts > -1);
709 $main::lxdebug->enter_sub();
711 my ($self, $form) = @_;
713 $form->{sid} = $form->{dbdefault};
715 my @upgradescripts = ();
719 if ($form->{dbupdate}) {
721 # read update scripts into memory
722 opendir SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade" or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
725 sort(cmp_script_version
726 grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/, readdir(SQLDIR)));
731 foreach my $db (split / /, $form->{dbupdate}) {
733 next unless $form->{$db};
735 # strip db from dataset
737 &dbconnect_vars($form, $db);
740 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
744 $query = qq|SELECT version FROM defaults|;
745 my $sth = $dbh->prepare($query);
747 # no error check, let it fall through
750 my $version = $sth->fetchrow_array;
753 next unless $version;
756 $version = calc_version($version);
759 foreach my $upgradescript (@upgradescripts) {
760 my $a = $upgradescript;
761 $a =~ s/^$form->{dbdriver}-upgrade-|\.(sql|pl)$//g;
764 my ($mindb, $maxdb) = split /-/, $a;
765 my $str_maxdb = $maxdb;
767 $mindb = calc_version($mindb);
768 $maxdb = calc_version($maxdb);
771 next if ($version >= $maxdb);
773 # if there is no upgrade script exit
774 last if ($version < $mindb);
777 $main::lxdebug->message(DEBUG2, "Appliying Update $upgradescript");
778 if ($file_type eq "sql") {
779 $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
781 $self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
793 $main::lxdebug->leave_sub();
799 $main::lxdebug->enter_sub();
801 my ($self, $filename) = @_;
803 @config = &config_vars;
805 open(CONF, ">$filename") or $self->error("$filename : $!");
807 # create the config file
808 print CONF qq|# configuration file for $self->{login}
813 foreach $key (sort @config) {
814 $self->{$key} =~ s/\'/\\\'/g;
815 print CONF qq| $key => '$self->{$key}',\n|;
818 print CONF qq|);\n\n|;
822 $main::lxdebug->leave_sub();
826 $main::lxdebug->enter_sub();
828 my ($self, $memberfile, $userspath) = @_;
832 # format dbconnect and dboptions string
833 &dbconnect_vars($self, $self->{dbname});
835 $self->error('File locked!') if (-f "${memberfile}.LCK");
836 open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
839 open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
846 while ($line = shift @config) {
847 if ($line =~ /^\[$self->{login}\]/) {
854 # remove everything up to next login or EOF
855 while ($line = shift @config) {
856 last if ($line =~ /^\[/);
859 # this one is either the next login or EOF
862 while ($line = shift @config) {
866 print CONF qq|[$self->{login}]\n|;
868 if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
870 $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
871 chop $self->{dbpasswd};
873 if (defined($self->{new_password})) {
874 if ($self->{new_password} ne $self->{old_password}) {
875 $self->{password} = crypt $self->{new_password},
876 substr($self->{login}, 0, 2)
877 if $self->{new_password};
880 if ($self->{password} ne $self->{old_password}) {
881 $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
882 if $self->{password};
886 if ($self->{'root login'}) {
887 @config = ("password");
889 @config = &config_vars;
892 # replace \r\n with \n
893 map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
894 foreach $key (sort @config) {
895 print CONF qq|$key=$self->{$key}\n|;
900 unlink "${memberfile}.LCK";
903 $self->create_config("$userspath/$self->{login}.conf")
904 unless $self->{'root login'};
906 $main::lxdebug->leave_sub();
910 $main::lxdebug->enter_sub();
912 my @conf = qw(acs address admin businessnumber charset company countrycode
913 currency dateformat dbconnect dbdriver dbhost dbport dboptions
914 dbname dbuser dbpasswd email fax name numberformat in_numberformat password
915 printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen
916 anfragen lieferantenbestellungen einkaufsrechnungen steuernummer co_ustid duns menustyle);
918 $main::lxdebug->leave_sub();
924 $main::lxdebug->enter_sub();
926 my ($self, $msg) = @_;
928 if ($ENV{HTTP_USER_AGENT}) {
929 print qq|Content-Type: text/html
931 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
933 <body bgcolor=ffffff>
935 <h2><font color=red>Error!</font></h2>
942 $main::lxdebug->leave_sub();