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 if ($form->{hashed_password}) {
118 $form->{password} = $form->{hashed_password};
120 $form->{password} = crypt($form->{password},
121 substr($self->{login}, 0, 2));
123 if ($self->{password} ne $form->{password}) {
124 $main::lxdebug->leave_sub();
129 unless (-e "$userspath/$self->{login}.conf") {
130 $self->create_config("$userspath/$self->{login}.conf");
133 do "$userspath/$self->{login}.conf";
134 $myconfig{dbpasswd} = unpack 'u', $myconfig{dbpasswd};
136 # check if database is down
138 DBI->connect($myconfig{dbconnect}, $myconfig{dbuser},
140 or $self->error(DBI::errstr);
142 # we got a connection, check the version
143 my $query = qq|SELECT version FROM defaults|;
144 my $sth = $dbh->prepare($query);
145 $sth->execute || $form->dberror($query);
147 my ($dbversion) = $sth->fetchrow_array;
150 # add login to employee table if it does not exist
151 # no error check for employee table, ignore if it does not exist
152 $query = qq|SELECT e.id FROM employee e WHERE e.login = '$self->{login}'|;
153 $sth = $dbh->prepare($query);
156 my ($login) = $sth->fetchrow_array;
160 $query = qq|INSERT INTO employee (login, name, workphone, role)
161 VALUES ('$self->{login}', '$myconfig{name}',
162 '$myconfig{tel}', 'user')|;
169 if (&update_available($myconfig{"dbdriver"}, $dbversion)) {
171 map { $form->{$_} = $myconfig{$_} }
172 qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect);
174 $form->{"stylesheet"} = "lx-office-erp.css";
175 $form->{"title"} = $main::locale->text("Dataset upgrade");
177 print($form->parse_html_template("dbupgrade/header"));
179 $form->{dbupdate} = "db$myconfig{dbname}";
180 $form->{ $form->{dbupdate} } = 1;
182 if ($form->{"show_dbupdate_warning"}) {
183 print($form->parse_html_template("dbupgrade/warning"));
188 open FH, ">$userspath/nologin" or die "
191 # required for Oracle
192 $form->{dbdefault} = $sid;
194 # ignore HUP, QUIT in case the webserver times out
195 $SIG{HUP} = 'IGNORE';
196 $SIG{QUIT} = 'IGNORE';
198 $self->dbupdate($form);
201 unlink "$userspath/nologin";
203 print($form->parse_html_template("dbupgrade/footer"));
210 $main::lxdebug->leave_sub();
216 $main::lxdebug->enter_sub();
218 my ($form, $db) = @_;
221 'Pg' => { 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
222 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
223 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
224 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
225 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
226 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
227 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
230 'yy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
231 'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
232 'mm/dd/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
233 'mm-dd-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
234 'dd/mm/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
235 'dd-mm-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
236 'dd.mm.yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
239 $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
241 if ($form->{dbdriver} eq 'Pg') {
242 $form->{dbconnect} = "dbi:Pg:dbname=$db";
245 if ($form->{dbdriver} eq 'Oracle') {
246 $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
249 if ($form->{dbhost}) {
250 $form->{dbconnect} .= ";host=$form->{dbhost}";
252 if ($form->{dbport}) {
253 $form->{dbconnect} .= ";port=$form->{dbport}";
256 $main::lxdebug->leave_sub();
260 $main::lxdebug->enter_sub();
262 my @drivers = DBI->available_drivers();
264 $main::lxdebug->leave_sub();
266 return (grep { /(Pg|Oracle)/ } @drivers);
270 $main::lxdebug->enter_sub();
272 my ($self, $form) = @_;
277 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
278 $form->{sid} = $form->{dbdefault};
279 &dbconnect_vars($form, $form->{dbdefault});
282 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
285 if ($form->{dbdriver} eq 'Pg') {
287 $query = qq|SELECT datname FROM pg_database WHERE NOT ((datname = 'template0') OR (datname = 'template1'))|;
288 $sth = $dbh->prepare($query);
289 $sth->execute || $form->dberror($query);
291 while (my ($db) = $sth->fetchrow_array) {
293 if ($form->{only_acc_db}) {
295 next if ($db =~ /^template/);
297 &dbconnect_vars($form, $db);
299 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
302 $query = qq|SELECT p.tablename FROM pg_tables p
303 WHERE p.tablename = 'defaults'
304 AND p.tableowner = '$form->{dbuser}'|;
305 my $sth = $dbh->prepare($query);
306 $sth->execute || $form->dberror($query);
308 if ($sth->fetchrow_array) {
309 push @dbsources, $db;
315 push @dbsources, $db;
319 if ($form->{dbdriver} eq 'Oracle') {
320 if ($form->{only_acc_db}) {
321 $query = qq|SELECT o.owner FROM dba_objects o
322 WHERE o.object_name = 'DEFAULTS'
323 AND o.object_type = 'TABLE'|;
325 $query = qq|SELECT username FROM dba_users|;
328 $sth = $dbh->prepare($query);
329 $sth->execute || $form->dberror($query);
331 while (my ($db) = $sth->fetchrow_array) {
332 push @dbsources, $db;
339 $main::lxdebug->leave_sub();
345 $main::lxdebug->enter_sub();
347 my ($self, $form) = @_;
350 'Pg' => qq|CREATE DATABASE "$form->{db}"|,
352 qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
355 $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding};
357 $form->{sid} = $form->{dbdefault};
358 &dbconnect_vars($form, $form->{dbdefault});
360 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
362 my $query = qq|$dbcreate{$form->{dbdriver}}|;
363 $dbh->do($query) || $form->dberror($query);
365 if ($form->{dbdriver} eq 'Oracle') {
366 $query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|;
367 $dbh->do($query) || $form->dberror($query);
371 # setup variables for the new database
372 if ($form->{dbdriver} eq 'Oracle') {
373 $form->{dbuser} = $form->{db};
374 $form->{dbpasswd} = $form->{db};
377 &dbconnect_vars($form, $form->{db});
379 $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
383 my $filename = qq|sql/lx-office.sql|;
384 $self->process_query($form, $dbh, $filename);
387 ($filename) = split /_/, $form->{chart};
389 $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
391 # load chart of accounts
392 $filename = qq|sql/$form->{chart}-chart.sql|;
393 $self->process_query($form, $dbh, $filename);
395 $query = "UPDATE defaults SET coa = " . $dbh->quote($form->{"chart"});
396 $dbh->do($query) || $form->dberror($query);
400 $main::lxdebug->leave_sub();
403 # Process a Perl script which updates the database.
404 # If the script returns 1 then the update was successful.
405 # Return code "2" means "needs more interaction; remove
406 # users/nologin and exit".
407 # All other return codes are fatal errors.
408 sub process_perl_script {
409 $main::lxdebug->enter_sub();
411 my ($self, $form, $dbh, $filename, $version) = @_;
413 open(FH, "$filename") or $form->error("$filename : $!\n");
414 my $contents = join("", <FH>);
419 my %dbup_myconfig = ();
420 map({ $dbup_myconfig{$_} = $form->{$_}; }
421 qw(dbname dbuser dbpasswd dbhost dbport dbconnect));
423 my $nls_file = $filename;
424 $nls_file =~ s|.*/||;
425 $nls_file =~ s|.pl$||;
426 my $dbup_locale = Locale->new($main::language, $nls_file);
428 my $result = eval($contents);
435 if (!defined($result)) {
436 print($form->parse_html_template("dbupgrade/error",
437 { "file" => $filename,
440 } elsif (1 != $result) {
441 unlink("users/nologin") if (2 == $result);
446 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
450 $main::lxdebug->leave_sub();
454 $main::lxdebug->enter_sub();
456 my ($self, $form, $dbh, $filename, $version) = @_;
458 # return unless (-f $filename);
460 open(FH, "$filename") or $form->error("$filename : $!\n");
469 # Remove DOS and Unix style line endings.
475 for (my $i = 0; $i < length($_); $i++) {
476 my $char = substr($_, $i, 1);
478 # Are we inside a string?
480 if ($char eq $quote_chars[-1]) {
486 if (($char eq "'") || ($char eq "\"")) {
487 push(@quote_chars, $char);
489 } elsif ($char eq ";") {
491 # Query is complete. Send it.
493 $sth = $dbh->prepare($query);
494 if (!$sth->execute()) {
495 my $errstr = $dbh->errstr;
498 $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" .
499 "The error message was: ${errstr}<br>" .
500 "All changes in that file have been reverted.");
514 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
520 $main::lxdebug->leave_sub();
524 $main::lxdebug->enter_sub();
526 my ($self, $form) = @_;
528 my %dbdelete = ('Pg' => qq|DROP DATABASE "$form->{db}"|,
529 'Oracle' => qq|DROP USER $form->{db} CASCADE|);
531 $form->{sid} = $form->{dbdefault};
532 &dbconnect_vars($form, $form->{dbdefault});
534 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
536 my $query = qq|$dbdelete{$form->{dbdriver}}|;
537 $dbh->do($query) || $form->dberror($query);
541 $main::lxdebug->leave_sub();
544 sub dbsources_unused {
545 $main::lxdebug->enter_sub();
547 my ($self, $form, $memfile) = @_;
552 $form->error('File locked!') if (-f "${memfile}.LCK");
555 open(FH, "$memfile") or $form->error("$memfile : $!");
559 my ($null, $item) = split(/=/);
566 $form->{only_acc_db} = 1;
567 my @db = &dbsources("", $form);
569 push @dbexcl, $form->{dbdefault};
571 foreach $item (@db) {
572 unless (grep /$item$/, @dbexcl) {
573 push @dbsources, $item;
577 $main::lxdebug->leave_sub();
583 $main::lxdebug->enter_sub();
585 my ($self, $form) = @_;
590 $form->{sid} = $form->{dbdefault};
591 &dbconnect_vars($form, $form->{dbdefault});
594 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
597 if ($form->{dbdriver} eq 'Pg') {
599 $query = qq|SELECT d.datname FROM pg_database d, pg_user u
600 WHERE d.datdba = u.usesysid
601 AND u.usename = '$form->{dbuser}'|;
602 my $sth = $dbh->prepare($query);
603 $sth->execute || $form->dberror($query);
605 while (my ($db) = $sth->fetchrow_array) {
607 next if ($db =~ /^template/);
609 &dbconnect_vars($form, $db);
612 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
615 $query = qq|SELECT t.tablename FROM pg_tables t
616 WHERE t.tablename = 'defaults'|;
617 my $sth = $dbh->prepare($query);
618 $sth->execute || $form->dberror($query);
620 if ($sth->fetchrow_array) {
621 $query = qq|SELECT version FROM defaults|;
622 my $sth = $dbh->prepare($query);
625 if (my ($version) = $sth->fetchrow_array) {
626 $dbsources{$db} = $version;
636 if ($form->{dbdriver} eq 'Oracle') {
637 $query = qq|SELECT o.owner FROM dba_objects o
638 WHERE o.object_name = 'DEFAULTS'
639 AND o.object_type = 'TABLE'|;
641 $sth = $dbh->prepare($query);
642 $sth->execute || $form->dberror($query);
644 while (my ($db) = $sth->fetchrow_array) {
646 $form->{dbuser} = $db;
647 &dbconnect_vars($form, $db);
650 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
653 $query = qq|SELECT version FROM defaults|;
654 my $sth = $dbh->prepare($query);
657 if (my ($version) = $sth->fetchrow_array) {
658 $dbsources{$db} = $version;
668 $main::lxdebug->leave_sub();
675 $main::lxdebug->enter_sub(2);
677 my (@v, $version, $i);
679 @v = split(/\./, $_[0]);
680 while (scalar(@v) < 4) {
684 for ($i = 0; $i < 4; $i++) {
689 $main::lxdebug->leave_sub(2);
693 sub cmp_script_version {
694 my ($a_from, $a_to, $b_from, $b_to);
695 my ($i, $res_a, $res_b);
696 my ($my_a, $my_b) = ($a, $b);
698 $my_a =~ s/.*-upgrade-//;
700 $my_b =~ s/.*-upgrade-//;
702 ($my_a_from, $my_a_to) = split(/-/, $my_a);
703 ($my_b_from, $my_b_to) = split(/-/, $my_b);
705 $res_a = calc_version($my_a_from);
706 $res_b = calc_version($my_b_from);
708 if ($res_a == $res_b) {
709 $res_a = calc_version($my_a_to);
710 $res_b = calc_version($my_b_to);
713 return $res_a <=> $res_b;
717 sub update_available {
718 my ($dbdriver, $cur_version) = @_;
720 opendir SQLDIR, "sql/${dbdriver}-upgrade" or &error("", "sql/${dbdriver}-upgrade: $!");
722 grep(/$form->{dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)/, readdir(SQLDIR));
725 return ($#upgradescripts > -1);
729 $main::lxdebug->enter_sub();
731 my ($self, $form) = @_;
733 $form->{sid} = $form->{dbdefault};
735 my @upgradescripts = ();
739 if ($form->{dbupdate}) {
741 # read update scripts into memory
742 opendir SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade" or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
745 sort(cmp_script_version
746 grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/, readdir(SQLDIR)));
751 foreach my $db (split / /, $form->{dbupdate}) {
753 next unless $form->{$db};
755 # strip db from dataset
757 &dbconnect_vars($form, $db);
760 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
764 $query = qq|SELECT version FROM defaults|;
765 my $sth = $dbh->prepare($query);
767 # no error check, let it fall through
770 my $version = $sth->fetchrow_array;
773 next unless $version;
776 $version = calc_version($version);
779 foreach my $upgradescript (@upgradescripts) {
780 my $a = $upgradescript;
781 $a =~ s/^$form->{dbdriver}-upgrade-|\.(sql|pl)$//g;
784 my ($mindb, $maxdb) = split /-/, $a;
785 my $str_maxdb = $maxdb;
787 $mindb = calc_version($mindb);
788 $maxdb = calc_version($maxdb);
791 next if ($version >= $maxdb);
793 # if there is no upgrade script exit
794 last if ($version < $mindb);
797 $main::lxdebug->message(DEBUG2, "Appliying Update $upgradescript");
798 if ($file_type eq "sql") {
799 $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
801 $self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
813 $main::lxdebug->leave_sub();
819 $main::lxdebug->enter_sub();
821 my ($self, $filename) = @_;
823 @config = &config_vars;
825 open(CONF, ">$filename") or $self->error("$filename : $!");
827 # create the config file
828 print CONF qq|# configuration file for $self->{login}
833 foreach $key (sort @config) {
834 $self->{$key} =~ s/\'/\\\'/g;
835 print CONF qq| $key => '$self->{$key}',\n|;
838 print CONF qq|);\n\n|;
842 $main::lxdebug->leave_sub();
846 $main::lxdebug->enter_sub();
848 my ($self, $memberfile, $userspath) = @_;
852 # format dbconnect and dboptions string
853 &dbconnect_vars($self, $self->{dbname});
855 $self->error('File locked!') if (-f "${memberfile}.LCK");
856 open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
859 open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
866 while ($line = shift @config) {
867 if ($line =~ /^\[$self->{login}\]/) {
874 # remove everything up to next login or EOF
875 while ($line = shift @config) {
876 last if ($line =~ /^\[/);
879 # this one is either the next login or EOF
882 while ($line = shift @config) {
886 print CONF qq|[$self->{login}]\n|;
888 if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
890 $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
891 chop $self->{dbpasswd};
893 if (defined($self->{new_password})) {
894 if ($self->{new_password} ne $self->{old_password}) {
895 $self->{password} = crypt $self->{new_password},
896 substr($self->{login}, 0, 2)
897 if $self->{new_password};
900 if ($self->{password} ne $self->{old_password}) {
901 $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
902 if $self->{password};
906 if ($self->{'root login'}) {
907 @config = ("password");
909 @config = &config_vars;
912 # replace \r\n with \n
913 map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
914 foreach $key (sort @config) {
915 print CONF qq|$key=$self->{$key}\n|;
920 unlink "${memberfile}.LCK";
923 $self->create_config("$userspath/$self->{login}.conf")
924 unless $self->{'root login'};
926 $main::lxdebug->leave_sub();
930 $main::lxdebug->enter_sub();
932 my @conf = qw(acs address admin businessnumber charset company countrycode
933 currency dateformat dbconnect dbdriver dbhost dbport dboptions
934 dbname dbuser dbpasswd email fax name numberformat in_numberformat password
935 printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen
936 anfragen lieferantenbestellungen einkaufsrechnungen steuernummer co_ustid duns menustyle);
938 $main::lxdebug->leave_sub();
944 $main::lxdebug->enter_sub();
946 my ($self, $msg) = @_;
948 if ($ENV{HTTP_USER_AGENT}) {
949 print qq|Content-Type: text/html
951 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
953 <body bgcolor=ffffff>
955 <h2><font color=red>Error!</font></h2>
962 $main::lxdebug->leave_sub();