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($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->info("Upgrading Dataset $myconfig{dbname} ...");
 
 179       # required for Oracle
 
 180       $form->{dbdefault} = $sid;
 
 182       # ignore HUP, QUIT in case the webserver times out
 
 183       $SIG{HUP}  = 'IGNORE';
 
 184       $SIG{QUIT} = 'IGNORE';
 
 186       $self->dbupdate($form);
 
 189       unlink "$userspath/nologin";
 
 191       $form->info("... done");
 
 198   $main::lxdebug->leave_sub();
 
 204   $main::lxdebug->enter_sub();
 
 206   my ($form, $db) = @_;
 
 209         'Pg' => { 'yy-mm-dd'   => 'set DateStyle to \'ISO\'',
 
 210                   'yyyy-mm-dd' => 'set DateStyle to \'ISO\'',
 
 211                   'mm/dd/yy'   => 'set DateStyle to \'SQL, US\'',
 
 212                   'mm-dd-yy'   => 'set DateStyle to \'POSTGRES, US\'',
 
 213                   'dd/mm/yy'   => 'set DateStyle to \'SQL, EUROPEAN\'',
 
 214                   'dd-mm-yy'   => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
 
 215                   'dd.mm.yy'   => 'set DateStyle to \'GERMAN\''
 
 218           'yy-mm-dd'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YY-MM-DD\'',
 
 219           'yyyy-mm-dd' => 'ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'',
 
 220           'mm/dd/yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM/DD/YY\'',
 
 221           'mm-dd-yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'MM-DD-YY\'',
 
 222           'dd/mm/yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD/MM/YY\'',
 
 223           'dd-mm-yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD-MM-YY\'',
 
 224           'dd.mm.yy'   => 'ALTER SESSION SET NLS_DATE_FORMAT = \'DD.MM.YY\'',
 
 227   $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
 
 229   if ($form->{dbdriver} eq 'Pg') {
 
 230     $form->{dbconnect} = "dbi:Pg:dbname=$db";
 
 233   if ($form->{dbdriver} eq 'Oracle') {
 
 234     $form->{dbconnect} = "dbi:Oracle:sid=$form->{sid}";
 
 237   if ($form->{dbhost}) {
 
 238     $form->{dbconnect} .= ";host=$form->{dbhost}";
 
 240   if ($form->{dbport}) {
 
 241     $form->{dbconnect} .= ";port=$form->{dbport}";
 
 244   $main::lxdebug->leave_sub();
 
 248   $main::lxdebug->enter_sub();
 
 250   my @drivers = DBI->available_drivers();
 
 252   $main::lxdebug->leave_sub();
 
 254   return (grep { /(Pg|Oracle)/ } @drivers);
 
 258   $main::lxdebug->enter_sub();
 
 260   my ($self, $form) = @_;
 
 265   $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
 
 266   $form->{sid} = $form->{dbdefault};
 
 267   &dbconnect_vars($form, $form->{dbdefault});
 
 270     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 273   if ($form->{dbdriver} eq 'Pg') {
 
 275     $query = qq|SELECT datname FROM pg_database|;
 
 276     $sth   = $dbh->prepare($query);
 
 277     $sth->execute || $form->dberror($query);
 
 279     while (my ($db) = $sth->fetchrow_array) {
 
 281       if ($form->{only_acc_db}) {
 
 283         next if ($db =~ /^template/);
 
 285         &dbconnect_vars($form, $db);
 
 287           DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 290         $query = qq|SELECT p.tablename FROM pg_tables p
 
 291                     WHERE p.tablename = 'defaults'
 
 292                     AND p.tableowner = '$form->{dbuser}'|;
 
 293         my $sth = $dbh->prepare($query);
 
 294         $sth->execute || $form->dberror($query);
 
 296         if ($sth->fetchrow_array) {
 
 297           push @dbsources, $db;
 
 303       push @dbsources, $db;
 
 307   if ($form->{dbdriver} eq 'Oracle') {
 
 308     if ($form->{only_acc_db}) {
 
 309       $query = qq|SELECT o.owner FROM dba_objects o
 
 310                   WHERE o.object_name = 'DEFAULTS'
 
 311                   AND o.object_type = 'TABLE'|;
 
 313       $query = qq|SELECT username FROM dba_users|;
 
 316     $sth = $dbh->prepare($query);
 
 317     $sth->execute || $form->dberror($query);
 
 319     while (my ($db) = $sth->fetchrow_array) {
 
 320       push @dbsources, $db;
 
 327   $main::lxdebug->leave_sub();
 
 333   $main::lxdebug->enter_sub();
 
 335   my ($self, $form) = @_;
 
 338     'Pg'     => qq|CREATE DATABASE "$form->{db}"|,
 
 340       qq|CREATE USER "$form->{db}" DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP IDENTIFIED BY "$form->{db}"|
 
 343   $dbcreate{Pg} .= " WITH ENCODING = '$form->{encoding}'" if $form->{encoding};
 
 345   $form->{sid} = $form->{dbdefault};
 
 346   &dbconnect_vars($form, $form->{dbdefault});
 
 348     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 350   my $query = qq|$dbcreate{$form->{dbdriver}}|;
 
 351   $dbh->do($query) || $form->dberror($query);
 
 353   if ($form->{dbdriver} eq 'Oracle') {
 
 354     $query = qq|GRANT CONNECT,RESOURCE TO "$form->{db}"|;
 
 355     $dbh->do($query) || $form->dberror($query);
 
 359   # setup variables for the new database
 
 360   if ($form->{dbdriver} eq 'Oracle') {
 
 361     $form->{dbuser}   = $form->{db};
 
 362     $form->{dbpasswd} = $form->{db};
 
 365   &dbconnect_vars($form, $form->{db});
 
 367   $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 371   my $filename = qq|sql/lx-office.sql|;
 
 372   $self->process_query($form, $dbh, $filename);
 
 375   ($filename) = split /_/, $form->{chart};
 
 377   $self->process_query($form, $dbh, "sql/${filename}-gifi.sql");
 
 379   # load chart of accounts
 
 380   $filename = qq|sql/$form->{chart}-chart.sql|;
 
 381   $self->process_query($form, $dbh, $filename);
 
 384   # Indices sind auch in lx-office.sql
 
 385   # $filename = qq|sql/$form->{dbdriver}-indices.sql|;
 
 386   # $self->process_query($form, $dbh, $filename);
 
 390   $main::lxdebug->leave_sub();
 
 394   $main::lxdebug->enter_sub();
 
 396   my ($self, $form, $dbh, $filename, $version) = @_;
 
 398   #  return unless (-f $filename);
 
 400   open(FH, "$filename") or $form->error("$filename : $!\n");
 
 409     # Remove DOS and Unix style line endings.
 
 412     # don't add comments or empty lines
 
 413     next if /^(--.*|\s+)$/;
 
 415     for (my $i = 0; $i < length($_); $i++) {
 
 416       my $char = substr($_, $i, 1);
 
 418       # Are we inside a string?
 
 420         if ($char eq $quote_chars[-1]) {
 
 426         if (($char eq "'") || ($char eq "\"")) {
 
 427           push(@quote_chars, $char);
 
 429         } elsif ($char eq ";") {
 
 431           # Query is complete. Send it.
 
 433           $sth = $dbh->prepare($query);
 
 434           if (!$sth->execute()) {
 
 437             $form->dberror("The database update/creation did not succeed. The file ${filename} containing the following query failed:<br>${query}<br>" .
 
 438                            "All changes in that file have been reverted.");
 
 452     $dbh->do("UPDATE defaults SET version = " . $dbh->quote($version));
 
 458   $main::lxdebug->leave_sub();
 
 462   $main::lxdebug->enter_sub();
 
 464   my ($self, $form) = @_;
 
 466   my %dbdelete = ('Pg'     => qq|DROP DATABASE "$form->{db}"|,
 
 467                   'Oracle' => qq|DROP USER $form->{db} CASCADE|);
 
 469   $form->{sid} = $form->{dbdefault};
 
 470   &dbconnect_vars($form, $form->{dbdefault});
 
 472     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 474   my $query = qq|$dbdelete{$form->{dbdriver}}|;
 
 475   $dbh->do($query) || $form->dberror($query);
 
 479   $main::lxdebug->leave_sub();
 
 482 sub dbsources_unused {
 
 483   $main::lxdebug->enter_sub();
 
 485   my ($self, $form, $memfile) = @_;
 
 490   $form->error('File locked!') if (-f "${memfile}.LCK");
 
 493   open(FH, "$memfile") or $form->error("$memfile : $!");
 
 497       my ($null, $item) = split /=/;
 
 504   $form->{only_acc_db} = 1;
 
 505   my @db = &dbsources("", $form);
 
 507   push @dbexcl, $form->{dbdefault};
 
 509   foreach $item (@db) {
 
 510     unless (grep /$item$/, @dbexcl) {
 
 511       push @dbsources, $item;
 
 515   $main::lxdebug->leave_sub();
 
 521   $main::lxdebug->enter_sub();
 
 523   my ($self, $form) = @_;
 
 528   $form->{sid} = $form->{dbdefault};
 
 529   &dbconnect_vars($form, $form->{dbdefault});
 
 532     DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 535   if ($form->{dbdriver} eq 'Pg') {
 
 537     $query = qq|SELECT d.datname FROM pg_database d, pg_user u
 
 538                 WHERE d.datdba = u.usesysid
 
 539                 AND u.usename = '$form->{dbuser}'|;
 
 540     my $sth = $dbh->prepare($query);
 
 541     $sth->execute || $form->dberror($query);
 
 543     while (my ($db) = $sth->fetchrow_array) {
 
 545       next if ($db =~ /^template/);
 
 547       &dbconnect_vars($form, $db);
 
 550         DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 553       $query = qq|SELECT t.tablename FROM pg_tables t
 
 554                   WHERE t.tablename = 'defaults'|;
 
 555       my $sth = $dbh->prepare($query);
 
 556       $sth->execute || $form->dberror($query);
 
 558       if ($sth->fetchrow_array) {
 
 559         $query = qq|SELECT version FROM defaults|;
 
 560         my $sth = $dbh->prepare($query);
 
 563         if (my ($version) = $sth->fetchrow_array) {
 
 564           $dbsources{$db} = $version;
 
 574   if ($form->{dbdriver} eq 'Oracle') {
 
 575     $query = qq|SELECT o.owner FROM dba_objects o
 
 576                 WHERE o.object_name = 'DEFAULTS'
 
 577                 AND o.object_type = 'TABLE'|;
 
 579     $sth = $dbh->prepare($query);
 
 580     $sth->execute || $form->dberror($query);
 
 582     while (my ($db) = $sth->fetchrow_array) {
 
 584       $form->{dbuser} = $db;
 
 585       &dbconnect_vars($form, $db);
 
 588         DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 591       $query = qq|SELECT version FROM defaults|;
 
 592       my $sth = $dbh->prepare($query);
 
 595       if (my ($version) = $sth->fetchrow_array) {
 
 596         $dbsources{$db} = $version;
 
 606   $main::lxdebug->leave_sub();
 
 613   $main::lxdebug->enter_sub();
 
 615   my (@v, $version, $i);
 
 617   @v = split(/\./, $_[0]);
 
 618   while (scalar(@v) < 4) {
 
 622   for ($i = 0; $i < 4; $i++) {
 
 627   $main::lxdebug->leave_sub();
 
 631 sub cmp_script_version {
 
 632   my ($a_from, $a_to, $b_from, $b_to);
 
 633   my ($i, $res_a, $res_b);
 
 634   my ($my_a, $my_b) = ($a, $b);
 
 636   $my_a =~ s/.*-upgrade-//;
 
 638   $my_b =~ s/.*-upgrade-//;
 
 640   ($my_a_from, $my_a_to) = split(/-/, $my_a);
 
 641   ($my_b_from, $my_b_to) = split(/-/, $my_b);
 
 643   $res_a = calc_version($my_a_from);
 
 644   $res_b = calc_version($my_b_from);
 
 646   if ($res_a == $res_b) {
 
 647     $res_a = calc_version($my_a_to);
 
 648     $res_b = calc_version($my_b_to);
 
 651   return $res_a <=> $res_b;
 
 655 sub update_available {
 
 657     opendir SQLDIR, "sql/." or &error("", "$!");
 
 659       grep(/$form->{dbdriver}-upgrade-$cur_version.*\.sql/, readdir(SQLDIR));
 
 662     return ($#upgradescripts > -1);
 
 666   $main::lxdebug->enter_sub();
 
 668   my ($self, $form) = @_;
 
 670   $form->{sid} = $form->{dbdefault};
 
 672   my @upgradescripts = ();
 
 676   if ($form->{dbupdate}) {
 
 678     # read update scripts into memory
 
 679     opendir SQLDIR, "sql/." or &error("", "$!");
 
 682       sort(cmp_script_version
 
 683            grep(/$form->{dbdriver}-upgrade-.*?\.sql$/, readdir(SQLDIR)));
 
 688   foreach my $db (split / /, $form->{dbupdate}) {
 
 690     next unless $form->{$db};
 
 692     # strip db from dataset
 
 694     &dbconnect_vars($form, $db);
 
 697       DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
 
 701     $query = qq|SELECT version FROM defaults|;
 
 702     my $sth = $dbh->prepare($query);
 
 704     # no error check, let it fall through
 
 707     my $version = $sth->fetchrow_array;
 
 710     next unless $version;
 
 713     $version = calc_version($version);
 
 716     foreach my $upgradescript (@upgradescripts) {
 
 717       my $a = $upgradescript;
 
 718       $a =~ s/^$form->{dbdriver}-upgrade-|\.sql$//g;
 
 720       my ($mindb, $maxdb) = split /-/, $a;
 
 721       my $str_maxdb = $maxdb;
 
 723       $mindb = calc_version($mindb);
 
 724       $maxdb = calc_version($maxdb);
 
 727       next if ($version >= $maxdb);
 
 729       # if there is no upgrade script exit
 
 730       last if ($version < $mindb);
 
 733       $self->process_query($form, $dbh, "sql/$upgradescript", $str_maxdb);
 
 744   $main::lxdebug->leave_sub();
 
 750   $main::lxdebug->enter_sub();
 
 752   my ($self, $filename) = @_;
 
 754   @config = &config_vars;
 
 756   open(CONF, ">$filename") or $self->error("$filename : $!");
 
 758   # create the config file
 
 759   print CONF qq|# configuration file for $self->{login}
 
 764   foreach $key (sort @config) {
 
 765     $self->{$key} =~ s/\'/\\\'/g;
 
 766     print CONF qq|  $key => '$self->{$key}',\n|;
 
 769   print CONF qq|);\n\n|;
 
 773   $main::lxdebug->leave_sub();
 
 777   $main::lxdebug->enter_sub();
 
 779   my ($self, $memberfile, $userspath) = @_;
 
 783   # format dbconnect and dboptions string
 
 784   &dbconnect_vars($self, $self->{dbname});
 
 786   $self->error('File locked!') if (-f "${memberfile}.LCK");
 
 787   open(FH, ">${memberfile}.LCK") or $self->error("${memberfile}.LCK : $!");
 
 790   open(CONF, "+<$memberfile") or $self->error("$memberfile : $!");
 
 797   while ($line = shift @config) {
 
 798     if ($line =~ /^\[$self->{login}\]/) {
 
 805   # remove everything up to next login or EOF
 
 806   while ($line = shift @config) {
 
 807     last if ($line =~ /^\[/);
 
 810   # this one is either the next login or EOF
 
 813   while ($line = shift @config) {
 
 817   print CONF qq|[$self->{login}]\n|;
 
 819   if ((($self->{dbpasswd} ne $self->{old_dbpasswd}) || $newmember)
 
 821     $self->{dbpasswd} = pack 'u', $self->{dbpasswd};
 
 822     chop $self->{dbpasswd};
 
 824   if (defined($self->{new_password})) {
 
 825     if ($self->{new_password} ne $self->{old_password}) {
 
 826       $self->{password} = crypt $self->{new_password},
 
 827         substr($self->{login}, 0, 2)
 
 828         if $self->{new_password};
 
 831     if ($self->{password} ne $self->{old_password}) {
 
 832       $self->{password} = crypt $self->{password}, substr($self->{login}, 0, 2)
 
 833         if $self->{password};
 
 837   if ($self->{'root login'}) {
 
 838     @config = ("password");
 
 840     @config = &config_vars;
 
 843   # replace \r\n with \n
 
 844   map { $self->{$_} =~ s/\r\n/\\n/g } qw(address signature);
 
 845   foreach $key (sort @config) {
 
 846     print CONF qq|$key=$self->{$key}\n|;
 
 851   unlink "${memberfile}.LCK";
 
 854   $self->create_config("$userspath/$self->{login}.conf")
 
 855     unless $self->{'root login'};
 
 857   $main::lxdebug->leave_sub();
 
 861   $main::lxdebug->enter_sub();
 
 863   my @conf = qw(acs address admin businessnumber charset company countrycode
 
 864     currency dateformat dbconnect dbdriver dbhost dbport dboptions
 
 865     dbname dbuser dbpasswd email fax name numberformat in_numberformat password
 
 866     printer role sid signature stylesheet tel templates vclimit angebote bestellungen rechnungen
 
 867     anfragen lieferantenbestellungen einkaufsrechnungen steuernummer co_ustid duns menustyle);
 
 869   $main::lxdebug->leave_sub();
 
 875   $main::lxdebug->enter_sub();
 
 877   my ($self, $msg) = @_;
 
 879   if ($ENV{HTTP_USER_AGENT}) {
 
 880     print qq|Content-Type: text/html
 
 882 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
 
 884 <body bgcolor=ffffff>
 
 886 <h2><font color=red>Error!</font></h2>
 
 893   $main::lxdebug->leave_sub();