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)) {
172 open FH, ">$userspath/nologin" or die "
175 map { $form->{$_} = $myconfig{$_} }
176 qw(dbname dbhost dbport dbdriver dbuser dbpasswd dbconnect);
178 $form->{dbupdate} = "db$myconfig{dbname}";
179 $form->{ $form->{dbupdate} } = 1;
181 $form->{"stylesheet"} = "lx-office-erp.css";
182 $form->{"title"} = $main::locale->text("Dataset upgrade");
184 print($form->parse_html_template("dbupgrade/header",
185 { "dbname" => $myconfig{dbname} }));
187 # required for Oracle
188 $form->{dbdefault} = $sid;
190 # ignore HUP, QUIT in case the webserver times out
191 $SIG{HUP} = 'IGNORE';
192 $SIG{QUIT} = 'IGNORE';
194 $self->dbupdate($form);
197 unlink "$userspath/nologin";
199 print($form->parse_html_template("dbupgrade/footer"));
206 $main::lxdebug->leave_sub();
212 $main::lxdebug->enter_sub();
214 my ($form, $db) = @_;
217 'Pg' => { 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
218 'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
219 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
220 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
221 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
222 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
223 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
226 'yy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
227 'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
228 'mm/dd/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
229 'mm-dd-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
230 'dd/mm/yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
231 'dd-mm-yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
232 'dd.mm.yy' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
235 $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
237 if ($form->{dbdriver} eq 'Pg') {
238 $form->{dbconnect} = "dbi:Pg:dbname=$db";
241 if ($form->{dbdriver} eq 'Oracle') {
242 $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
245 if ($form->{dbhost}) {
246 $form->{dbconnect} .= ";host=$form->{dbhost}";
248 if ($form->{dbport}) {
249 $form->{dbconnect} .= ";port=$form->{dbport}";
252 $main::lxdebug->leave_sub();
256 $main::lxdebug->enter_sub();
258 my @drivers = DBI->available_drivers();
260 $main::lxdebug->leave_sub();
262 return (grep { /(Pg|Oracle)/ } @drivers);
266 $main::lxdebug->enter_sub();
268 my ($self, $form) = @_;
273 $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
274 $form->{sid} = $form->{dbdefault};
275 &dbconnect_vars($form, $form->{dbdefault});
278 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
281 if ($form->{dbdriver} eq 'Pg') {
283 $query = qq|SELECT datname FROM pg_database WHERE NOT ((datname = 'template0') OR (datname = 'template1'))|;
284 $sth = $dbh->prepare($query);
285 $sth->execute || $form->dberror($query);
287 while (my ($db) = $sth->fetchrow_array) {
289 if ($form->{only_acc_db}) {
291 next if ($db =~ /^template/);
293 &dbconnect_vars($form, $db);
295 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
298 $query = qq|SELECT p.tablename FROM pg_tables p
299 WHERE p.tablename = 'defaults'
300 AND p.tableowner = '$form->{dbuser}'|;
301 my $sth = $dbh->prepare($query);
302 $sth->execute || $form->dberror($query);
304 if ($sth->fetchrow_array) {
305 push @dbsources, $db;
311 push @dbsources, $db;
315 if ($form->{dbdriver} eq 'Oracle') {
316 if ($form->{only_acc_db}) {
317 $query = qq|SELECT o.owner FROM dba_objects o
318 WHERE o.object_name = 'DEFAULTS'
319 AND o.object_type = 'TABLE'|;
321 $query = qq|SELECT username FROM dba_users|;
324 $sth = $dbh->prepare($query);
325 $sth->execute || $form->dberror($query);
327 while (my ($db) = $sth->fetchrow_array) {
328 push @dbsources, $db;
335 $main::lxdebug->leave_sub();
341 $main::lxdebug->enter_sub();
343 my ($self, $form) = @_;
346 'Pg' => qq|CREATE DATABASE "$form->{db}"|,
348 qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
351 $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding};
353 $form->{sid} = $form->{dbdefault};
354 &dbconnect_vars($form, $form->{dbdefault});
356 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
358 my $query = qq|$dbcreate{$form->{dbdriver}}|;
359 $dbh->do($query) || $form->dberror($query);
361 if ($form->{dbdriver} eq 'Oracle') {
362 $query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|;
363 $dbh->do($query) || $form->dberror($query);
367 # setup variables for the new database
368 if ($form->{dbdriver} eq 'Oracle') {
369 $form->{dbuser} = $form->{db};
370 $form->{dbpasswd} = $form->{db};
373 &dbconnect_vars($form, $form->{db});
375 $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
379 my $filename = qq|sql/lx-office.sql|;
380 $self->process_query($form, $dbh, $filename);
383 ($filename) = split /_/, $form->{chart};
385 $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
387 # load chart of accounts
388 $filename = qq|sql/$form->{chart}-chart.sql|;
389 $self->process_query($form, $dbh, $filename);
393 $main::lxdebug->leave_sub();
396 # Process a Perl script which updates the database.
397 # If the script returns 1 then the update was successful.
398 # Return code "2" means "needs more interaction; remove
399 # users/nologin and exit".
400 # All other return codes are fatal errors.
401 sub process_perl_script {
402 $main::lxdebug->enter_sub();
404 my ($self, $form, $dbh, $filename, $version) = @_;
406 open(FH, "$filename") or $form->error("$filename : $!\n");
407 my $contents = join("", <FH>);
412 my $result = eval($contents);
419 if (!defined($result)) {
420 print($form->parse_html_template("dbupgrade/error",
421 { "file" => $filename,
424 } elsif (1 != $result) {
425 unlink("users/nologin") if (2 == $result);
430 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
434 $main::lxdebug->leave_sub();
438 $main::lxdebug->enter_sub();
440 my ($self, $form, $dbh, $filename, $version) = @_;
442 # return unless (-f $filename);
444 open(FH, "$filename") or $form->error("$filename : $!\n");
453 # Remove DOS and Unix style line endings.
459 for (my $i = 0; $i < length($_); $i++) {
460 my $char = substr($_, $i, 1);
462 # Are we inside a string?
464 if ($char eq $quote_chars[-1]) {
470 if (($char eq "'") || ($char eq "\"")) {
471 push(@quote_chars, $char);
473 } elsif ($char eq ";") {
475 # Query is complete. Send it.
477 $sth = $dbh->prepare($query);
478 if (!$sth->execute()) {
479 my $errstr = $dbh->errstr;
482 $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" .
483 "The error message was: ${errstr}<br>" .
484 "All changes in that file have been reverted.");
498 $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
504 $main::lxdebug->leave_sub();
508 $main::lxdebug->enter_sub();
510 my ($self, $form) = @_;
512 my %dbdelete = ('Pg' => qq|DROP DATABASE "$form->{db}"|,
513 'Oracle' => qq|DROP USER $form->{db} CASCADE|);
515 $form->{sid} = $form->{dbdefault};
516 &dbconnect_vars($form, $form->{dbdefault});
518 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
520 my $query = qq|$dbdelete{$form->{dbdriver}}|;
521 $dbh->do($query) || $form->dberror($query);
525 $main::lxdebug->leave_sub();
528 sub dbsources_unused {
529 $main::lxdebug->enter_sub();
531 my ($self, $form, $memfile) = @_;
536 $form->error('File locked!') if (-f "${memfile}.LCK");
539 open(FH, "$memfile") or $form->error("$memfile : $!");
543 my ($null, $item) = split(/=/);
550 $form->{only_acc_db} = 1;
551 my @db = &dbsources("", $form);
553 push @dbexcl, $form->{dbdefault};
555 foreach $item (@db) {
556 unless (grep /$item$/, @dbexcl) {
557 push @dbsources, $item;
561 $main::lxdebug->leave_sub();
567 $main::lxdebug->enter_sub();
569 my ($self, $form) = @_;
574 $form->{sid} = $form->{dbdefault};
575 &dbconnect_vars($form, $form->{dbdefault});
578 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
581 if ($form->{dbdriver} eq 'Pg') {
583 $query = qq|SELECT d.datname FROM pg_database d, pg_user u
584 WHERE d.datdba = u.usesysid
585 AND u.usename = '$form->{dbuser}'|;
586 my $sth = $dbh->prepare($query);
587 $sth->execute || $form->dberror($query);
589 while (my ($db) = $sth->fetchrow_array) {
591 next if ($db =~ /^template/);
593 &dbconnect_vars($form, $db);
596 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
599 $query = qq|SELECT t.tablename FROM pg_tables t
600 WHERE t.tablename = 'defaults'|;
601 my $sth = $dbh->prepare($query);
602 $sth->execute || $form->dberror($query);
604 if ($sth->fetchrow_array) {
605 $query = qq|SELECT version FROM defaults|;
606 my $sth = $dbh->prepare($query);
609 if (my ($version) = $sth->fetchrow_array) {
610 $dbsources{$db} = $version;
620 if ($form->{dbdriver} eq 'Oracle') {
621 $query = qq|SELECT o.owner FROM dba_objects o
622 WHERE o.object_name = 'DEFAULTS'
623 AND o.object_type = 'TABLE'|;
625 $sth = $dbh->prepare($query);
626 $sth->execute || $form->dberror($query);
628 while (my ($db) = $sth->fetchrow_array) {
630 $form->{dbuser} = $db;
631 &dbconnect_vars($form, $db);
634 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
637 $query = qq|SELECT version FROM defaults|;
638 my $sth = $dbh->prepare($query);
641 if (my ($version) = $sth->fetchrow_array) {
642 $dbsources{$db} = $version;
652 $main::lxdebug->leave_sub();
659 $main::lxdebug->enter_sub(2);
661 my (@v, $version, $i);
663 @v = split(/\./, $_[0]);
664 while (scalar(@v) < 4) {
668 for ($i = 0; $i < 4; $i++) {
673 $main::lxdebug->leave_sub(2);
677 sub cmp_script_version {
678 my ($a_from, $a_to, $b_from, $b_to);
679 my ($i, $res_a, $res_b);
680 my ($my_a, $my_b) = ($a, $b);
682 $my_a =~ s/.*-upgrade-//;
684 $my_b =~ s/.*-upgrade-//;
686 ($my_a_from, $my_a_to) = split(/-/, $my_a);
687 ($my_b_from, $my_b_to) = split(/-/, $my_b);
689 $res_a = calc_version($my_a_from);
690 $res_b = calc_version($my_b_from);
692 if ($res_a == $res_b) {
693 $res_a = calc_version($my_a_to);
694 $res_b = calc_version($my_b_to);
697 return $res_a <=> $res_b;
701 sub update_available {
702 my ($dbdriver, $cur_version) = @_;
704 opendir SQLDIR, "sql/${dbdriver}-upgrade" or &error("", "sql/${dbdriver}-upgrade: $!");
706 grep(/$form->{dbdriver}-upgrade-\Q$cur_version\E.*\.(sql|pl)/, readdir(SQLDIR));
709 return ($#upgradescripts > -1);
713 $main::lxdebug->enter_sub();
715 my ($self, $form) = @_;
717 $form->{sid} = $form->{dbdefault};
719 my @upgradescripts = ();
723 if ($form->{dbupdate}) {
725 # read update scripts into memory
726 opendir SQLDIR, "sql/" . $form->{dbdriver} . "-upgrade" or &error("", "sql/" . $form->{dbdriver} . "-upgrade : $!");
729 sort(cmp_script_version
730 grep(/$form->{dbdriver}-upgrade-.*?\.(sql|pl)$/, readdir(SQLDIR)));
735 foreach my $db (split / /, $form->{dbupdate}) {
737 next unless $form->{$db};
739 # strip db from dataset
741 &dbconnect_vars($form, $db);
744 DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
748 $query = qq|SELECT version FROM defaults|;
749 my $sth = $dbh->prepare($query);
751 # no error check, let it fall through
754 my $version = $sth->fetchrow_array;
757 next unless $version;
760 $version = calc_version($version);
763 foreach my $upgradescript (@upgradescripts) {
764 my $a = $upgradescript;
765 $a =~ s/^$form->{dbdriver}-upgrade-|\.(sql|pl)$//g;
768 my ($mindb, $maxdb) = split /-/, $a;
769 my $str_maxdb = $maxdb;
771 $mindb = calc_version($mindb);
772 $maxdb = calc_version($maxdb);
775 next if ($version >= $maxdb);
777 # if there is no upgrade script exit
778 last if ($version < $mindb);
781 $main::lxdebug->message(DEBUG2, "Appliying Update $upgradescript");
782 if ($file_type eq "sql") {
783 $self->process_query($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
785 $self->process_perl_script($form, $dbh, "sql/" . $form->{"dbdriver"} . "-upgrade/$upgradescript", $str_maxdb);
797 $main::lxdebug->leave_sub();
803 $main::lxdebug->enter_sub();
805 my ($self, $filename) = @_;
807 @config = &config_vars;
809 open(CONF, ">$filename") or $self->error("$filename : $!");
811 # create the config file
812 print CONF qq|# configuration file for $self->{login}
817 foreach $key (sort @config) {
818 $self->{$key} =~ s/\'/\\\'/g;
819 print CONF qq| $key => '$self->{$key}',\n|;
822 print CONF qq|);\n\n|;
826 $main::lxdebug->leave_sub();
830 $main::lxdebug->enter_sub();
832 my ($self, $memberfile, $userspath) = @_;
836 # format dbconnect and dboptions string
837 &dbconnect_vars($self, $self->{dbname});
839 $self->error('File locked!') if (-f "${memberfile}.LCK");
840 open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
843 open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
850 while ($line = shift @config) {
851 if ($line =~ /^\[$self->{login}\]/) {
858 # remove everything up to next login or EOF
859 while ($line = shift @config) {
860 last if ($line =~ /^\[/);
863 # this one is either the next login or EOF
866 while ($line = shift @config) {
870 print CONF qq|[$self->{login}]\n|;
872 if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
874 $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
875 chop $self->{dbpasswd};
877 if (defined($self->{new_password})) {
878 if ($self->{new_password} ne $self->{old_password}) {
879 $self->{password} = crypt $self->{new_password},
880 substr($self->{login}, 0, 2)
881 if $self->{new_password};
884 if ($self->{password} ne $self->{old_password}) {
885 $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
886 if $self->{password};
890 if ($self->{'root login'}) {
891 @config = ("password");
893 @config = &config_vars;
896 # replace \r\n with \n
897 map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
898 foreach $key (sort @config) {
899 print CONF qq|$key=$self->{$key}\n|;
904 unlink "${memberfile}.LCK";
907 $self->create_config("$userspath/$self->{login}.conf")
908 unless $self->{'root login'};
910 $main::lxdebug->leave_sub();
914 $main::lxdebug->enter_sub();
916 my @conf = qw(acs address admin businessnumber charset company countrycode
917 currency dateformat dbconnect dbdriver dbhost dbport dboptions
918 dbname dbuser dbpasswd email fax name numberformat in_numberformat password
919 printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen
920 anfragen lieferantenbestellungen einkaufsrechnungen steuernummer co_ustid duns menustyle);
922 $main::lxdebug->leave_sub();
928 $main::lxdebug->enter_sub();
930 my ($self, $msg) = @_;
932 if ($ENV{HTTP_USER_AGENT}) {
933 print qq|Content-Type: text/html
935 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
937 <body bgcolor=ffffff>
939 <h2><font color=red>Error!</font></h2>
946 $main::lxdebug->leave_sub();