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
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
31 # add/edit/delete users
33 #======================================================================
35 $menufile = "menu.ini";
39 use POSIX qw(strftime);
42 use English qw(-no_match_vars);
52 require "bin/mozilla/common.pl";
54 our $cgi = new CGI('');
57 $form->{"root"} = "root login";
59 $locale = new Locale $language, "admin";
62 if (-f "bin/mozilla/custom_$form->{script}") {
63 eval { require "bin/mozilla/custom_$form->{script}"; };
64 $form->error($@) if ($@);
67 $form->{stylesheet} = "lx-office-erp.css";
68 $form->{favicon} = "favicon.ico";
70 if ($form->{action}) {
73 $subroutine = $locale->findsub($form->{action});
75 if ($subroutine eq 'login') {
77 $form->{rpw} = crypt $form->{rpw}, "ro";
83 call_sub($subroutine);
87 # if there are no drivers bail out
88 $form->error($locale->text('No Database Drivers available!'))
89 unless (User->dbdrivers);
92 if (!-f $memberfile) {
93 open(FH, ">$memberfile") or $form->error("$memberfile : $!");
94 print FH qq|# SQL-Ledger Accounting members
114 qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
117 print $form->parse_html_template('admin/adminlogin');
126 $form->error($locale->text('File locked!')) if (-f "${memberfile}.LCK");
128 open(FH, "$memberfile") or $form->error("$memberfile : $!");
137 $login =~ s/(\[|\])//g;
139 $members{$login} = { "login" => $login };
142 if (/^([a-z]+)=(.*)/) {
143 $members{$login}->{$1} = $2;
149 delete $members{"root login"};
150 map { $_->{templates} =~ s|.*/||; } values %members;
152 $form->{title} = "Lx-Office ERP " . $locale->text('Administration');
153 $form->{LOCKED} = -e "$userspath/nologin";
154 $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
157 print $form->parse_html_template("admin/list_users");
164 . $locale->text('Administration') . " / "
165 . $locale->text('Add User');
169 "countrycode" => "de",
170 "numberformat" => "1000,00",
171 "dateformat" => "dd.mm.yy",
172 "stylesheet" => "lx-office-erp.css",
176 edit_user_form($myconfig);
183 . $locale->text('Administration') . " / "
184 . $locale->text('Edit User');
187 $form->isblank("login", $locale->text("The login is missing."));
190 my $myconfig = new User "$memberfile", "$form->{login}";
192 $myconfig->{signature} =~ s/\\n/\r\n/g;
193 $myconfig->{address} =~ s/\\n/\r\n/g;
195 # strip basedir from templates directory
196 $myconfig->{templates} =~ s|.*/||;
198 edit_user_form($myconfig);
204 my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
205 $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
207 my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
208 $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
210 %countrycodes = User->country_codes;
211 $form->{ALL_COUNTRYCODES} = [];
212 foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
213 push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode,
214 "name" => $countrycodes{$countrycode},
215 "selected" => $countrycode eq $myconfig->{countrycode} };
218 # is there a templates basedir
219 if (!-d "$templates") {
220 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
223 opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $!");
224 my @all = readdir(TEMPLATEDIR);
225 my @alldir = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
226 my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
227 closedir TEMPLATEDIR;
229 @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
230 @alldir = grep !/^(webpages|\.svn)$/, @alldir;
232 @allhtml = reverse grep !/Default/, @allhtml;
233 push @allhtml, 'Default';
234 @allhtml = reverse @allhtml;
236 $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
238 $lastitem = $allhtml[0];
239 $lastitem =~ s/-.*//g;
240 $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
241 foreach $item (@allhtml) {
243 next if ($item eq $lastitem);
245 push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
249 # css dir has styles that are not intended as general layouts.
250 # reverting to hardcoded list
251 $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
253 $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
255 map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
261 open(FH, $menufile) or $form->error("$menufile : $!");
263 while ($item = <FH>) {
264 next unless $item =~ /\[/;
265 next if $item =~ /\#/;
267 $item =~ s/(\[|\])//g;
270 my ($level, $menuitem);
273 ($level, $menuitem) = split /--/, $item, 2;
277 push @acsorder, $item;
281 push @{ $acs{$level} }, $menuitem;
285 foreach $item (split(/;/, $myconfig->{acs})) {
286 ($key, $value) = split /--/, $item, 2;
287 $excl{$key}{$value} = 1;
291 $form->{all_acs} = "";
293 foreach $key (@acsorder) {
294 my $acl = { "checked" => $form->{login} ? !$excl{$key}->{$key} : 1,
295 "name" => "${key}--${key}",
298 $form->{all_acs} .= "${key}--${key};";
300 foreach $item (@{ $acs{$key} }) {
301 next if ($key eq $item);
303 my $subacl = { "checked" => $form->{login} ? !$excl{$key}->{$item} : 1,
304 "name" => "${key}--${item}",
306 push @{ $acl->{SUBACLS} }, $subacl;
307 $form->{all_acs} .= "${key}--${item};";
309 push @{ $form->{ACLS} }, $acl;
312 chop $form->{all_acs};
315 print $form->parse_html_template("admin/edit_user");
320 $form->{dbdriver} = 'Pg';
322 # no spaces allowed in login name
323 ($form->{login}) = split / /, $form->{login};
325 $form->isblank("login", $locale->text('Login name missing!'));
327 # check for duplicates
328 if (!$form->{edit}) {
329 $temp = new User "$memberfile", "$form->{login}";
331 if ($temp->{login}) {
332 $form->error("$form->{login} " . $locale->text('is already a member!'));
336 # no spaces allowed in directories
337 ($form->{newtemplates}) = split / /, $form->{newtemplates};
339 if ($form->{newtemplates}) {
340 $form->{templates} = $form->{newtemplates};
343 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
347 if (!-d "$templates") {
348 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
351 # add base directory to $form->{templates}
352 $form->{templates} =~ s|.*/||;
353 $form->{templates} = "$templates/$form->{templates}";
355 $myconfig = new User "$memberfile", "$form->{login}";
357 # redo acs variable and delete all the acs codes
359 foreach $item (split m|;|, $form->{all_acs}) {
360 my $name = "ACS_${item}";
362 push @acs, $item if !$form->{$name};
363 delete $form->{$name};
365 $form->{acs} = join ";", @acs;
367 $form->isblank("dbname", $locale->text('Dataset missing!'));
368 $form->isblank("dbuser", $locale->text('Database User missing!'));
370 foreach $item (keys %{$form}) {
371 $myconfig->{$item} = $form->{$item};
374 delete $myconfig->{stylesheet};
375 if ($form->{userstylesheet}) {
376 $myconfig->{stylesheet} = $form->{userstylesheet};
379 $myconfig->save_member($memberfile, $userspath);
383 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
384 foreach $directory (@webdavdirs) {
385 $file = "webdav/" . $directory . "/webdav-user";
386 if ($form->{$directory}) {
387 if (open(HTACCESS, "$file")) {
389 ($login, $password) = split(/:/, $_);
390 if ($login ne $form->{login}) {
396 open(HTACCESS, "> $file") or die "cannot open $file $!\n";
397 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
398 print(HTACCESS $newfile);
401 $form->{$directory} = 0;
402 if (open(HTACCESS, "$file")) {
404 ($login, $password) = split(/:/, $_);
405 if ($login ne $form->{login}) {
411 open(HTACCESS, "> $file") or die "cannot open $file $!\n";
412 print(HTACCESS $newfile);
418 $form->{templates} =~ s|.*/||;
419 $form->{templates} = "${templates}/$form->{templates}";
420 $form->{mastertemplates} =~ s|.*/||;
422 # create user template directory and copy master files
423 if (!-d "$form->{templates}") {
426 if (mkdir "$form->{templates}", oct("771")) {
430 # copy templates to the directory
431 opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $!");
432 @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
434 closedir TEMPLATEDIR;
436 foreach $file (@templates) {
437 open(TEMP, "$templates/$file")
438 or $form->error("$templates/$file : $!");
440 $file =~ s/$form->{mastertemplates}-//;
441 open(NEW, ">$form->{templates}/$file")
442 or $form->error("$form->{templates}/$file : $!");
444 while ($line = <TEMP>) {
451 $form->error("$!: $form->{templates}");
455 $form->redirect($locale->text('User saved!'));
460 $form->error($locale->text('File locked!')) if (-f ${memberfile} . LCK);
461 open(FH, ">${memberfile}.LCK") or $form->error("${memberfile}.LCK : $!");
464 my $members = Inifile->new($memberfile);
465 my $templates = $members->{$form->{login}}->{templates};
466 delete $members->{$form->{login}};
468 unlink "${memberfile}.LCK";
471 my $templates_in_use = 0;
472 foreach $login (keys %{ $members }) {
473 next if $login =~ m/^[A-Z]+$/;
474 next if $members->{$login}->{templates} ne $templates;
475 $templates_in_use = 1;
479 if (!$templates_in_use && -d $templates) {
480 unlink <$templates/*>;
485 # delete config file for user
486 unlink "$userspath/$form->{login}.conf";
488 $form->redirect($locale->text('User deleted!'));
496 return ($login) ? $login : undef;
503 my ($null, $value) = split(/=/, $line, 2);
506 $value =~ s/\s#.*//g;
508 # remove any trailing whitespace
509 $value =~ s/^\s*(.*?)\s*$/$1/;
514 sub change_admin_password {
518 . $locale->text('Administration') . " / "
519 . $locale->text('Change Admin Password');
522 print $form->parse_html_template("admin/change_admin_password");
525 sub change_password {
526 if ($form->{"password"} ne $form->{"password_again"}) {
529 . $locale->text('Administration') . " / "
530 . $locale->text('Change Admin Password');
533 $form->error($locale->text("The passwords do not match."));
536 $root->{password} = $form->{password};
538 $root->{'root login'} = 1;
539 $root->save_member($memberfile);
542 "$form->{script}?action=list_users&rpw=$root->{password}";
544 $form->redirect($locale->text('Password changed!'));
548 $root = new User "$memberfile", $form->{root};
550 if (!defined($root->{password}) || ($root->{password} ne $form->{rpw})) {
551 $form->error($locale->text('Incorrect Password!'));
556 sub pg_database_administration {
558 $form->{dbdriver} = 'Pg';
563 sub dbselect_source {
564 $form->{dbport} = '5432';
565 $form->{dbuser} = 'postgres';
566 $form->{dbdefault} = 'template1';
567 $form->{dbhost} = 'localhost';
569 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
571 $form->{ALLOW_DBBACKUP} = "$pg_dump_exe" ne "DISABLED";
574 print $form->parse_html_template("admin/dbadmin");
578 call_sub($form->{"nextsub"});
584 . $locale->text('Database Administration') . " / "
585 . $locale->text('Update Dataset');
587 my @need_updates = User->dbneedsupdate($form);
588 $form->{NEED_UPDATES} = \@need_updates;
589 $form->{ALL_UPDATED} = !scalar @need_updates;
592 print $form->parse_html_template("admin/update_dataset");
596 $form->{stylesheet} = "lx-office-erp.css";
597 $form->{title} = $locale->text("Dataset upgrade");
600 my $rowcount = $form->{rowcount} * 1;
601 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
602 $form->{NOTHING_TO_DO} = !scalar @update_rows;
603 my $saved_form = save_form();
607 print $form->parse_html_template("admin/dbupgrade_all_header");
609 foreach my $i (@update_rows) {
610 restore_form($saved_form);
612 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
614 my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
616 print $form->parse_html_template("admin/dbupgrade_header");
618 $form->{dbupdate} = $form->{dbname};
619 $form->{$form->{dbname}} = 1;
621 User->dbupdate($form);
622 User->dbupdate2($form, $controls);
624 print $form->parse_html_template("admin/dbupgrade_footer");
627 print $form->parse_html_template("admin/dbupgrade_all_done");
631 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
633 $form->{CHARTS} = [];
635 opendir SQLDIR, "sql/." or $form - error($!);
636 foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
637 next if ($item eq 'Default-chart.sql');
638 $item =~ s/-chart\.sql//;
639 push @{ $form->{CHARTS} }, { "name" => $item,
640 "selected" => $item eq "Germany-DATEV-SKR03EU" };
644 my $default_charset = $dbcharset;
645 $default_charset ||= Common::DEFAULT_CHARSET;
647 $form->{DBENCODINGS} = [];
649 foreach my $encoding (@Common::db_encodings) {
650 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
651 "label" => $encoding->{label},
652 "selected" => $encoding->{charset} eq $default_charset };
657 . $locale->text('Database Administration') . " / "
658 . $locale->text('Create Dataset');
661 print $form->parse_html_template("admin/create_dataset");
665 $form->isblank("db", $locale->text('Dataset missing!'));
667 User->dbcreate(\%$form);
671 . $locale->text('Database Administration') . " / "
672 . $locale->text('Create Dataset');
675 print $form->parse_html_template("admin/dbcreate");
679 @dbsources = User->dbsources_unused(\%$form, $memberfile);
680 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
684 . $locale->text('Database Administration') . " / "
685 . $locale->text('Delete Dataset');
686 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
689 print $form->parse_html_template("admin/delete_dataset");
695 $form->error($locale->text('No Dataset selected!'));
698 User->dbdelete(\%$form);
702 . $locale->text('Database Administration') . " / "
703 . $locale->text('Delete Dataset');
705 print $form->parse_html_template("admin/dbdelete");
711 . $locale->text('Database Administration') . " / "
712 . $locale->text('Backup Dataset');
714 if ("$pg_dump_exe" eq "DISABLED") {
715 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
718 my @dbsources = sort User->dbsources($form);
719 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
720 $form->{NO_DATABASES} = !scalar @dbsources;
722 my $username = getpwuid $UID || "unknown-user";
723 my $hostname = hostname() || "unknown-host";
724 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
727 print $form->parse_html_template("admin/backup_dataset");
730 sub backup_dataset_start {
733 . $locale->text('Database Administration') . " / "
734 . $locale->text('Backup Dataset');
736 $pg_dump_exe ||= "pg_dump";
738 if ("$pg_dump_exe" eq "DISABLED") {
739 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
742 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
743 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
745 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
746 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $!");
748 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
752 $form->error($locale->text('A temporary file could not be created:') . " $!");
755 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
758 $ENV{HOME} = $tmpdir;
760 my @args = ("-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
761 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
762 push @args, $form->{dbname};
764 my $cmd = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
765 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".sql.gz";
767 if ($form->{destination} ne "email") {
768 my $in = IO::File->new("$cmd |");
771 unlink "${tmpdir}/.pgpass";
774 $form->error($locale->text('The pg_dump process could not be started.'));
777 print "content-type: application/octet-stream\n";
778 print "content-disposition: attachment; filename=\"${name}\"\n\n";
780 while (my $line = <$in>) {
786 unlink "${tmpdir}/.pgpass";
790 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
792 if (system("$cmd > $tmp") != 0) {
793 unlink "${tmpdir}/.pgpass", $tmp;
796 $form->error($locale->text('The pg_dump process could not be started.'));
799 my $mail = new Mailer;
801 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
803 $mail->{charset} = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
804 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
807 unlink "${tmpdir}/.pgpass", $tmp;
812 . $locale->text('Database Administration') . " / "
813 . $locale->text('Backup Dataset');
816 print $form->parse_html_template("admin/backup_dataset_email_done");
820 sub restore_dataset {
823 . $locale->text('Database Administration') . " / "
824 . $locale->text('Restore Dataset');
826 if ("$pg_dump_exe" eq "DISABLED") {
827 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
833 unlink "$userspath/nologin";
836 "$form->{script}?action=list_users&rpw=$root->{password}";
838 $form->redirect($locale->text('Lockfile removed!'));
844 open(FH, ">$userspath/nologin")
845 or $form->error($locale->text('Cannot create Lock!'));
849 "$form->{script}?action=list_users&rpw=$root->{password}";
851 $form->redirect($locale->text('Lockfile created!'));