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 #======================================================================
37 use English qw(-no_match_vars);
41 use POSIX qw(strftime);
54 require "bin/mozilla/common.pl";
55 require "bin/mozilla/admin_groups.pl";
56 require "bin/mozilla/admin_printer.pl";
66 $::lxdebug->enter_sub;
67 my $session_result = shift;
74 $::auth->set_session_value('rpw', $::form->{rpw}) if $session_result == SL::Auth->SESSION_OK;
76 $form->{stylesheet} = "lx-office-erp.css";
77 $form->{favicon} = "favicon.ico";
79 if ($form->{action}) {
80 if ($auth->authenticate_root($form->{rpw}, 0) != $auth->OK()) {
81 $form->{error_message} = $locale->text('Incorrect Password!');
84 if ($auth->session_tables_present()) {
85 $::auth->set_session_value('rpw', $::form->{rpw});
86 $::auth->create_or_refresh_session();
89 _apply_dbupgrade_scripts();
91 call_sub($locale->findsub($form->{action}));
94 # if there are no drivers bail out
95 $form->error($locale->text('No Database Drivers available!'))
96 unless (User->dbdrivers);
100 $::lxdebug->leave_sub;
104 my $form = $main::form;
105 my $locale = $main::locale;
107 $form->{title} = qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
110 print $form->parse_html_template('admin/adminlogin');
114 check_auth_db_and_tables();
119 $main::auth->destroy_session();
123 sub check_auth_db_and_tables {
124 my $form = $main::form;
125 my $locale = $main::locale;
129 map { $params{"db_${_}"} = $main::auth->{DB_config}->{$_} } keys %{ $auth->{DB_config} };
131 if (!$main::auth->check_database()) {
132 $form->{title} = $locale->text('Authentification database creation');
134 print $form->parse_html_template('admin/check_auth_database', \%params);
139 if (!$main::auth->check_tables()) {
140 $form->{title} = $locale->text('Authentification tables creation');
142 print $form->parse_html_template('admin/check_auth_tables', \%params);
147 if (-f $main::memberfile) {
150 if ($main::memberfile =~ m|^.*/|) {
154 my $backupdir = "${memberdir}member-file-migration";
156 $form->{title} = $locale->text('User data migration');
158 print $form->parse_html_template('admin/user_migration', { 'memberfile' => $main::memberfile,
159 'backupdir' => $backupdir });
166 my $form = $main::form;
168 $main::auth->create_database('superuser' => $form->{db_superuser},
169 'superuser_password' => $form->{db_superuser_password},
170 'template' => $form->{db_template});
174 sub create_auth_tables {
175 my $form = $main::form;
176 my $locale = $main::locale;
178 $main::auth->create_tables();
179 $main::auth->set_session_value('rpw', $form->{rpw});
180 $main::auth->create_or_refresh_session();
182 if (!-f $main::memberfile) {
183 # New installation -- create a standard group with full access
186 'name' => $locale->text('Full Access'),
187 'description' => $locale->text('Full access to all functions'),
188 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
189 'members' => [ map { $_->{id} } values %members ],
192 $main::auth->save_group($group);
199 $main::lxdebug->enter_sub();
201 my $form = $main::form;
202 my $locale = $main::locale;
206 if ($main::memberfile =~ m|^.*/|) {
210 my $backupdir = "${memberdir}member-file-migration";
212 if (! -d $backupdir && !mkdir $backupdir, 0700) {
213 $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
216 copy $main::memberfile, "users/member-file-migration/members";
218 my $in = IO::File->new($main::memberfile, "r");
220 $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
222 my (%members, $login);
231 $login =~ s/(\[|\])//g;
235 $members{$login} = { "login" => $login };
239 if ($login && m/=/) {
240 my ($key, $value) = split m/\s*=\s*/, $_, 2;
245 $value =~ s|\\n|\n|g;
247 $members{$login}->{$key} = $value;
253 delete $members{"root login"};
255 map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
257 while (my ($login, $params) = each %members) {
258 $main::auth->save_user($login, %{ $params });
259 $main::auth->change_password($login, $params->{password}, 1);
261 my $conf_file = "${memberdir}${login}.conf";
264 copy $conf_file, "${backupdir}/${login}.conf";
269 unlink $main::memberfile;
271 my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
273 $form->{title} = $locale->text('User data migration');
275 print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
277 $main::lxdebug->leave_sub();
280 sub create_standard_group_ask {
281 my $form = $main::form;
282 my $locale = $main::locale;
284 $form->{title} = $locale->text('Create a standard group');
287 print $form->parse_html_template("admin/create_standard_group_ask");
290 sub create_standard_group {
291 my $form = $main::form;
292 my $locale = $main::locale;
294 my %members = $main::auth->read_all_users();
296 my $groups = $main::auth->read_groups();
298 foreach my $group (values %{$groups}) {
299 if (($form->{group_id} != $group->{id})
300 && ($form->{name} eq $group->{name})) {
301 $form->show_generic_error($locale->text("A group with that name does already exist."));
306 'name' => $locale->text('Full Access'),
307 'description' => $locale->text('Full access to all functions'),
308 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
309 'members' => [ map { $_->{id} } values %members ],
312 $main::auth->save_group($group);
314 user_migration_complete(1);
317 sub dont_create_standard_group {
318 user_migration_complete(0);
321 sub user_migration_complete {
322 my $standard_group_created = shift;
324 my $form = $main::form;
325 my $locale = $main::locale;
327 $form->{title} = $locale->text('User migration complete');
330 print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
334 my $form = $main::form;
335 my $locale = $main::locale;
337 my %members = $main::auth->read_all_users();
339 delete $members{"root login"};
341 map { $_->{templates} =~ s|.*/||; } values %members;
343 $form->{title} = "Lx-Office ERP " . $locale->text('Administration');
344 $form->{LOCKED} = -e "$main::userspath/nologin";
345 $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
348 print $form->parse_html_template("admin/list_users");
352 my $form = $main::form;
353 my $locale = $main::locale;
355 $form->{title} = "Lx-Office ERP " . $locale->text('Administration') . " / " . $locale->text('Add User');
357 # Note: Menu Style 'v3' is not compatible to all browsers!
358 # "menustyle" => "old" sets the HTML Menu to default.
361 "countrycode" => "de",
362 "numberformat" => "1.000,00",
363 "dateformat" => "dd.mm.yy",
364 "stylesheet" => "lx-office-erp.css",
365 "menustyle" => "old",
368 edit_user_form($myconfig);
372 my $form = $main::form;
373 my $locale = $main::locale;
375 $form->{title} = "Lx-Office ERP " . $locale->text('Administration') . " / " . $locale->text('Edit User');
378 $form->isblank("login", $locale->text("The login is missing."));
381 my $myconfig = new User($form->{login});
383 # strip basedir from templates directory
384 $myconfig->{templates} =~ s|.*/||;
386 edit_user_form($myconfig);
392 my $form = $main::form;
393 my $locale = $main::locale;
395 my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
396 $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
398 my @valid_numberformats = ('1,000.00', '1000.00', '1.000,00', '1000,00');
399 $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
401 my %countrycodes = User->country_codes;
402 $form->{ALL_COUNTRYCODES} = [];
403 foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
404 push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode,
405 "name" => $countrycodes{$countrycode},
406 "selected" => $countrycode eq $myconfig->{countrycode} };
409 # is there a templates basedir
410 if (!-d "$main::templates") {
411 $form->error(sprintf($locale->text("The directory %s does not exist."), $main::templates));
414 opendir TEMPLATEDIR, "$main::templates/." or $form->error("$main::templates : $ERRNO");
415 my @all = readdir(TEMPLATEDIR);
416 my @alldir = sort grep { -d "$main::templates/$_" && !/^\.\.?$/ } @all;
417 my @allhtml = sort grep { -f "$main::templates/$_" && /\.html$/ } @all;
418 closedir TEMPLATEDIR;
420 @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
421 @alldir = grep !/^(webpages|\.svn)$/, @alldir;
423 @allhtml = reverse grep !/Default/, @allhtml;
424 push @allhtml, 'Default';
425 @allhtml = reverse @allhtml;
427 $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
429 my $lastitem = $allhtml[0];
430 $lastitem =~ s/-.*//g;
431 $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
432 foreach my $item (@allhtml) {
434 next if ($item eq $lastitem);
436 push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
440 # css dir has styles that are not intended as general layouts.
441 # reverting to hardcoded list
442 $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
444 $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
446 map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
451 my $user_id = $main::auth->get_user_id($form->{login});
452 my $all_groups = $main::auth->read_groups();
454 foreach my $group (values %{ $all_groups }) {
455 push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
458 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
461 $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
464 print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
468 my $form = $main::form;
469 my $locale = $main::locale;
471 $form->{dbdriver} = 'Pg';
473 # no spaces allowed in login name
474 $form->{login} =~ s|\s||g;
475 $form->isblank("login", $locale->text('Login name missing!'));
477 # check for duplicates
478 if (!$form->{edit}) {
479 my %members = $main::auth->read_all_users();
480 if ($members{$form->{login}}) {
481 $form->show_generic_error($locale->text('Another user with the login #1 does already exist.', $form->{login}), 'back_button' => 1);
485 # no spaces allowed in directories
486 ($form->{newtemplates}) = split / /, $form->{newtemplates};
488 if ($form->{newtemplates}) {
489 $form->{templates} = $form->{newtemplates};
492 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
496 if (!-d "$main::templates") {
497 $form->error(sprintf($locale->text("The directory %s does not exist."), $main::templates));
500 # add base directory to $form->{templates}
501 $form->{templates} =~ s|.*/||;
502 $form->{templates} = "$main::templates/$form->{templates}";
504 my $myconfig = new User($form->{login});
506 $form->isblank("dbname", $locale->text('Dataset missing!'));
507 $form->isblank("dbuser", $locale->text('Database User missing!'));
509 foreach my $item (keys %{$form}) {
510 $myconfig->{$item} = $form->{$item};
513 delete $myconfig->{stylesheet};
514 if ($form->{userstylesheet}) {
515 $myconfig->{stylesheet} = $form->{userstylesheet};
518 $myconfig->save_member();
520 if ($main::auth->can_change_password()
521 && defined $form->{new_password}
522 && ($form->{new_password} ne '********')) {
523 $main::auth->change_password($form->{login}, $form->{new_password});
526 $form->{templates} =~ s|.*/||;
527 $form->{templates} = "$main::templates/$form->{templates}";
528 $form->{mastertemplates} =~ s|.*/||;
530 # create user template directory and copy master files
531 if (!-d "$form->{templates}") {
534 if (mkdir "$form->{templates}", oct("771")) {
538 # copy templates to the directory
539 opendir TEMPLATEDIR, "$main::templates/." or $form->error("$main::templates : $ERRNO");
540 my @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|odt|xml|txb)$/,
542 closedir TEMPLATEDIR;
544 foreach my $file (@templates) {
545 open(TEMP, "$main::templates/$file")
546 or $form->error("$main::templates/$file : $ERRNO");
548 $file =~ s/\Q$form->{mastertemplates}\E-//;
549 open(NEW, ">$form->{templates}/$file")
550 or $form->error("$form->{templates}/$file : $ERRNO");
552 while (my $line = <TEMP>) {
559 $form->error("$ERRNO: $form->{templates}");
563 # Add new user to his groups.
564 if (ref $form->{new_user_group_ids} eq 'ARRAY') {
565 my $all_groups = $main::auth->read_groups();
566 my %user = $main::auth->read_user($form->{login});
568 foreach my $group_id (@{ $form->{new_user_group_ids} }) {
569 my $group = $all_groups->{$group_id};
573 push @{ $group->{members} }, $user{id};
574 $main::auth->save_group($group);
578 $form->redirect($locale->text('User saved!'));
582 sub save_user_as_new {
583 my $form = $main::form;
585 $form->{login} = $form->{new_user_login};
586 delete @{$form}{qw(edit new_user_login)};
592 my $form = $main::form;
593 my $locale = $main::locale;
595 my %members = $main::auth->read_all_users();
596 my $templates = $members{$form->{login}}->{templates};
598 $main::auth->delete_user($form->{login});
601 my $templates_in_use = 0;
603 foreach my $login (keys %members) {
604 next if $form->{login} eq $login;
605 next if $members{$login}->{templates} ne $templates;
606 $templates_in_use = 1;
610 if (!$templates_in_use && -d $templates) {
611 unlink <$templates/*>;
616 $form->redirect($locale->text('User deleted!'));
624 return ($login) ? $login : undef;
630 my ($null, $value) = split(/=/, $line, 2);
633 $value =~ s/\s#.*//g;
635 # remove any trailing whitespace
636 $value =~ s/^\s*(.*?)\s*$/$1/;
641 sub pg_database_administration {
642 my $form = $main::form;
644 $form->{dbdriver} = 'Pg';
649 sub dbselect_source {
650 my $form = $main::form;
651 my $locale = $main::locale;
653 $form->{dbport} = '5432';
654 $form->{dbuser} = 'lxoffice';
655 $form->{dbdefault} = 'template1';
656 $form->{dbhost} = 'localhost';
658 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
660 # Intentionnaly disabled unless fixed to work with the authentication DB.
661 $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
664 print $form->parse_html_template("admin/dbadmin");
667 sub test_db_connection {
668 my $form = $main::form;
669 my $locale = $main::locale;
671 $form->{dbdriver} = 'Pg';
672 User::dbconnect_vars($form, $form->{dbname});
674 my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
676 $form->{connection_ok} = $dbh ? 1 : 0;
677 $form->{errstr} = $DBI::errstr;
679 $dbh->disconnect() if ($dbh);
681 $form->{title} = $locale->text('Database Connection Test');
683 print $form->parse_html_template("admin/test_db_connection");
687 call_sub($main::form->{"nextsub"});
691 my $form = $main::form;
692 my $locale = $main::locale;
694 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Update Dataset');
696 my @need_updates = User->dbneedsupdate($form);
697 $form->{NEED_UPDATES} = \@need_updates;
698 $form->{ALL_UPDATED} = !scalar @need_updates;
701 print $form->parse_html_template("admin/update_dataset");
705 my $form = $main::form;
706 my $locale = $main::locale;
708 $form->{stylesheet} = "lx-office-erp.css";
709 $form->{title} = $locale->text("Dataset upgrade");
712 my $rowcount = $form->{rowcount} * 1;
713 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
714 $form->{NOTHING_TO_DO} = !scalar @update_rows;
715 my $saved_form = save_form();
719 print $form->parse_html_template("admin/dbupgrade_all_header");
721 foreach my $i (@update_rows) {
722 restore_form($saved_form);
724 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
726 print $form->parse_html_template("admin/dbupgrade_header");
728 $form->{dbupdate} = $form->{dbname};
729 $form->{$form->{dbname}} = 1;
731 User->dbupdate($form);
732 User->dbupdate2($form, SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls);
734 print $form->parse_html_template("admin/dbupgrade_footer");
737 print $form->parse_html_template("admin/dbupgrade_all_done");
741 my $form = $main::form;
742 my $locale = $main::locale;
744 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
746 $form->{CHARTS} = [];
748 opendir SQLDIR, "sql/." or $form->error($ERRNO);
749 foreach my $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
750 next if ($item eq 'Default-chart.sql');
751 $item =~ s/-chart\.sql//;
752 push @{ $form->{CHARTS} }, { "name" => $item,
753 "selected" => $item eq "Germany-DATEV-SKR03EU" };
757 my $default_charset = $main::dbcharset;
758 $default_charset ||= Common::DEFAULT_CHARSET;
760 my $cluster_encoding = User->dbclusterencoding($form);
761 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
762 if ($main::dbcharset !~ m/^UTF-?8$/i) {
763 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
764 'Therefore you have to configure Lx-Office to use UTF-8 as well.'),
768 $form->{FORCE_DBENCODING} = 'UNICODE';
771 $form->{DBENCODINGS} = [];
773 foreach my $encoding (@Common::db_encodings) {
774 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
775 "label" => $encoding->{label},
776 "selected" => $encoding->{charset} eq $default_charset };
780 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
783 print $form->parse_html_template("admin/create_dataset");
787 my $form = $main::form;
788 my $locale = $main::locale;
790 $form->isblank("db", $locale->text('Dataset missing!'));
792 User->dbcreate(\%$form);
794 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
797 print $form->parse_html_template("admin/dbcreate");
801 my $form = $main::form;
802 my $locale = $main::locale;
804 my @dbsources = User->dbsources_unused($form);
805 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
807 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
808 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
811 print $form->parse_html_template("admin/delete_dataset");
815 my $form = $main::form;
816 my $locale = $main::locale;
819 $form->error($locale->text('No Dataset selected!'));
822 User->dbdelete(\%$form);
824 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
826 print $form->parse_html_template("admin/dbdelete");
830 my $form = $main::form;
831 my $locale = $main::locale;
833 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
835 if ("$main::pg_dump_exe" eq "DISABLED") {
836 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
839 my @dbsources = sort User->dbsources($form);
840 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
841 $form->{NO_DATABASES} = !scalar @dbsources;
843 my $username = getpwuid $UID || "unknown-user";
844 my $hostname = hostname() || "unknown-host";
845 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
848 print $form->parse_html_template("admin/backup_dataset");
851 sub backup_dataset_start {
852 my $form = $main::form;
853 my $locale = $main::locale;
855 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
857 $main::pg_dump_exe ||= "pg_dump";
859 if ("$main::pg_dump_exe" eq "DISABLED") {
860 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
863 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
864 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
866 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
867 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
869 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
873 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
876 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
879 $ENV{HOME} = $tmpdir;
881 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
882 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
883 push @args, $form->{dbname};
885 my $cmd = "$main::pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
886 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
888 if ($form->{destination} ne "email") {
889 my $in = IO::File->new("$cmd |");
892 unlink "${tmpdir}/.pgpass";
895 $form->error($locale->text('The pg_dump process could not be started.'));
898 print "content-type: application/x-tar\n";
899 print "content-disposition: attachment; filename=\"${name}\"\n\n";
901 while (my $line = <$in>) {
907 unlink "${tmpdir}/.pgpass";
911 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
913 if (system("$cmd > $tmp") != 0) {
914 unlink "${tmpdir}/.pgpass", $tmp;
917 $form->error($locale->text('The pg_dump process could not be started.'));
920 my $mail = new Mailer;
922 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
924 $mail->{charset} = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET;
925 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
928 unlink "${tmpdir}/.pgpass", $tmp;
931 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
934 print $form->parse_html_template("admin/backup_dataset_email_done");
938 sub restore_dataset {
939 my $form = $main::form;
940 my $locale = $main::locale;
942 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
944 if ("$main::pg_restore_exe" eq "DISABLED") {
945 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
948 my $default_charset = $main::dbcharset;
949 $default_charset ||= Common::DEFAULT_CHARSET;
951 $form->{DBENCODINGS} = [];
953 foreach my $encoding (@Common::db_encodings) {
954 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
955 "label" => $encoding->{label},
956 "selected" => $encoding->{charset} eq $default_charset };
960 print $form->parse_html_template("admin/restore_dataset");
963 sub restore_dataset_start {
964 my $form = $main::form;
965 my $locale = $main::locale;
967 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
969 $main::pg_restore_exe ||= "pg_restore";
971 if ("$main::pg_restore_exe" eq "DISABLED") {
972 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
975 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
976 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
978 # Create temporary directories. Write the backup file contents to a temporary
979 # file. Create a .pgpass file with the username and password for the pg_restore
982 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
983 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
985 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
989 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
992 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
995 $ENV{HOME} = $tmpdir;
997 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
1000 if (substr($form->{content}, 0, 2) eq "\037\213") {
1001 $tmpfile = IO::File->new("| gzip -d > $tmp");
1005 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
1009 unlink "${tmpdir}/.pgpass";
1012 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
1015 print $tmpfile $form->{content};
1018 delete $form->{content};
1020 # Try to connect to the database. Find out if a database with the same name exists.
1021 # If yes, then drop the existing database. Create a new one with the name and encoding
1022 # given by the user.
1024 User::dbconnect_vars($form, "template1");
1026 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
1027 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
1031 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
1033 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
1034 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
1036 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
1040 foreach my $item (@Common::db_encodings) {
1041 if ($item->{dbencoding} eq $form->{dbencoding}) {
1046 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
1048 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
1052 # Spawn pg_restore on the temporary file.
1054 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
1055 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
1058 my $cmd = "$main::pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
1060 my $in = IO::File->new("$cmd 2>&1 |");
1063 unlink "${tmpdir}/.pgpass", $tmp;
1066 $form->error($locale->text('The pg_restore process could not be started.'));
1069 $English::AUTOFLUSH = 1;
1072 print $form->parse_html_template("admin/restore_dataset_start_header");
1074 while (my $line = <$in>) {
1079 $form->{retval} = $CHILD_ERROR >> 8;
1080 print $form->parse_html_template("admin/restore_dataset_start_footer");
1082 unlink "${tmpdir}/.pgpass", $tmp;
1087 my $form = $main::form;
1088 my $locale = $main::locale;
1090 unlink "$main::userspath/nologin";
1092 $form->{callback} = "admin.pl?action=list_users";
1094 $form->redirect($locale->text('Lockfile removed!'));
1099 my $form = $main::form;
1100 my $locale = $main::locale;
1102 open(FH, ">$main::userspath/nologin")
1103 or $form->error($locale->text('Cannot create Lock!'));
1106 $form->{callback} = "admin.pl?action=list_users";
1108 $form->redirect($locale->text('Lockfile created!'));
1113 call_sub($main::form->{yes_nextsub});
1117 call_sub($main::form->{no_nextsub});
1121 call_sub($main::form->{add_nextsub});
1125 my $form = $main::form;
1127 $form->{edit_nextsub} ||= 'edit_user';
1129 call_sub($form->{edit_nextsub});
1133 my $form = $main::form;
1135 $form->{delete_nextsub} ||= 'delete_user';
1137 call_sub($form->{delete_nextsub});
1141 my $form = $main::form;
1143 $form->{save_nextsub} ||= 'save_user';
1145 call_sub($form->{save_nextsub});
1149 call_sub($main::form->{back_nextsub});
1153 my $form = $main::form;
1154 my $locale = $main::locale;
1156 foreach my $action (qw(create_standard_group dont_create_standard_group
1157 save_user delete_user save_user_as_new)) {
1158 if ($form->{"action_${action}"}) {
1164 call_sub($form->{default_action}) if ($form->{default_action});
1166 $form->error($locale->text('No action defined.'));
1169 sub _apply_dbupgrade_scripts {
1170 ::end_of_request() if SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(1);