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);
45 use SL::Auth::PasswordPolicy;
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/admin_groups.pl";
57 require "bin/mozilla/admin_printer.pl";
67 $::lxdebug->enter_sub;
68 my $session_result = shift;
75 $::auth->set_session_value('rpw', $::form->{rpw}) if $session_result == SL::Auth->SESSION_OK;
77 $form->{stylesheet} = "lx-office-erp.css";
78 $form->{favicon} = "favicon.ico";
80 if ($form->{action}) {
81 if ($auth->authenticate_root($form->{rpw}, 0) != $auth->OK()) {
82 $form->{error_message} = $locale->text('Incorrect Password!');
85 if ($auth->session_tables_present()) {
86 $::auth->set_session_value('rpw', $::form->{rpw});
87 $::auth->create_or_refresh_session();
88 _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 my $memberfile = $::lx_office_conf{paths}->{memberfile};
148 if (-f $memberfile) {
151 if ($memberfile =~ m|^.*/|) {
155 my $backupdir = "${memberdir}member-file-migration";
157 $form->{title} = $locale->text('User data migration');
159 print $form->parse_html_template('admin/user_migration', { 'memberfile' => $memberfile,
160 'backupdir' => $backupdir });
167 my $form = $main::form;
169 $main::auth->create_database('superuser' => $form->{db_superuser},
170 'superuser_password' => $form->{db_superuser_password},
171 'template' => $form->{db_template});
175 sub create_auth_tables {
176 my $form = $main::form;
177 my $locale = $main::locale;
179 $main::auth->create_tables();
180 $main::auth->set_session_value('rpw', $form->{rpw});
181 $main::auth->create_or_refresh_session();
183 my $memberfile = $::lx_office_conf{paths}->{memberfile};
184 if (!-f $memberfile) {
185 # New installation -- create a standard group with full access
188 'name' => $locale->text('Full Access'),
189 'description' => $locale->text('Full access to all functions'),
190 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
191 'members' => [ map { $_->{id} } values %members ],
194 $main::auth->save_group($group);
201 $main::lxdebug->enter_sub();
203 my $form = $main::form;
204 my $locale = $main::locale;
208 my $memberfile = $::lx_office_conf{paths}->{memberfile};
209 if ($memberfile =~ m|^.*/|) {
213 my $backupdir = "${memberdir}member-file-migration";
215 if (! -d $backupdir && !mkdir $backupdir, 0700) {
216 $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
219 copy $memberfile, "users/member-file-migration/members";
221 my $in = IO::File->new($memberfile, "r");
223 $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
225 my (%members, $login);
234 $login =~ s/(\[|\])//g;
238 $members{$login} = { "login" => $login };
242 if ($login && m/=/) {
243 my ($key, $value) = split m/\s*=\s*/, $_, 2;
248 $value =~ s|\\n|\n|g;
250 $members{$login}->{$key} = $value;
256 delete $members{"root login"};
258 map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
260 while (my ($login, $params) = each %members) {
261 $main::auth->save_user($login, %{ $params });
262 $main::auth->change_password($login, $params->{password}, 1);
264 my $conf_file = "${memberdir}${login}.conf";
267 copy $conf_file, "${backupdir}/${login}.conf";
274 my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
276 $form->{title} = $locale->text('User data migration');
278 print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
280 $main::lxdebug->leave_sub();
283 sub create_standard_group_ask {
284 my $form = $main::form;
285 my $locale = $main::locale;
287 $form->{title} = $locale->text('Create a standard group');
290 print $form->parse_html_template("admin/create_standard_group_ask");
293 sub create_standard_group {
294 my $form = $main::form;
295 my $locale = $main::locale;
297 my %members = $main::auth->read_all_users();
299 my $groups = $main::auth->read_groups();
301 foreach my $group (values %{$groups}) {
302 if (($form->{group_id} != $group->{id})
303 && ($form->{name} eq $group->{name})) {
304 $form->show_generic_error($locale->text("A group with that name does already exist."));
309 'name' => $locale->text('Full Access'),
310 'description' => $locale->text('Full access to all functions'),
311 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
312 'members' => [ map { $_->{id} } values %members ],
315 $main::auth->save_group($group);
317 user_migration_complete(1);
320 sub dont_create_standard_group {
321 user_migration_complete(0);
324 sub user_migration_complete {
325 my $standard_group_created = shift;
327 my $form = $main::form;
328 my $locale = $main::locale;
330 $form->{title} = $locale->text('User migration complete');
333 print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
337 my $form = $main::form;
338 my $locale = $main::locale;
340 my %members = $main::auth->read_all_users();
342 delete $members{"root login"};
344 map { $_->{templates} =~ s|.*/||; } values %members;
346 $form->{title} = "Lx-Office ERP " . $locale->text('Administration');
347 $form->{LOCKED} = -e _nologin_file_name();
348 $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
351 print $form->parse_html_template("admin/list_users");
355 my $form = $main::form;
356 my $locale = $main::locale;
358 $form->{title} = "Lx-Office ERP " . $locale->text('Administration') . " / " . $locale->text('Add User');
360 # Note: Menu Style 'v3' is not compatible to all browsers!
361 # "menustyle" => "old" sets the HTML Menu to default.
364 "countrycode" => "de",
365 "numberformat" => "1.000,00",
366 "dateformat" => "dd.mm.yy",
367 "stylesheet" => "lx-office-erp.css",
368 "menustyle" => "old",
369 dbport => $::auth->{DB_config}->{port} || 5432,
370 dbuser => $::auth->{DB_config}->{user} || 'lxoffice',
371 dbhost => $::auth->{DB_config}->{host} || 'localhost',
375 edit_user_form($myconfig);
379 my $form = $main::form;
380 my $locale = $main::locale;
382 $form->{title} = "Lx-Office ERP " . $locale->text('Administration') . " / " . $locale->text('Edit User');
385 $form->isblank("login", $locale->text("The login is missing."));
388 my $myconfig = new User($form->{login});
390 # strip basedir from templates directory
391 $myconfig->{templates} =~ s|.*/||;
393 edit_user_form($myconfig);
399 my $form = $main::form;
400 my $locale = $main::locale;
402 my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
403 $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
405 my @valid_numberformats = ('1,000.00', '1000.00', '1.000,00', '1000,00');
406 $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
408 my %countrycodes = User->country_codes;
409 $form->{ALL_COUNTRYCODES} = [];
410 foreach my $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
411 push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode,
412 "name" => $countrycodes{$countrycode},
413 "selected" => $countrycode eq $myconfig->{countrycode} };
416 # is there a templates basedir
417 if (!-d $::lx_office_conf{paths}->{templates}) {
418 $form->error(sprintf($locale->text("The directory %s does not exist."), $::lx_office_conf{paths}->{templates}));
421 opendir TEMPLATEDIR, $::lx_office_conf{paths}->{templates} or $form->error($::lx_office_conf{paths}->{templates} . " : $ERRNO");
422 my @all = readdir(TEMPLATEDIR);
423 my @alldir = sort grep { -d ($::lx_office_conf{paths}->{templates} . "/$_") && !/^\.\.?$/ } @all;
424 my @allhtml = sort grep { -f ($::lx_office_conf{paths}->{templates} . "/$_") && /\.html$/ } @all;
425 closedir TEMPLATEDIR;
427 @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
428 @alldir = grep !/^(webpages|\.svn)$/, @alldir;
430 @allhtml = reverse grep !/Default/, @allhtml;
431 push @allhtml, 'Default';
432 @allhtml = reverse @allhtml;
434 $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
436 my $lastitem = $allhtml[0];
437 $lastitem =~ s/-.*//g;
438 $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
439 foreach my $item (@allhtml) {
441 next if ($item eq $lastitem);
443 push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
447 # css dir has styles that are not intended as general layouts.
448 # reverting to hardcoded list
449 $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
451 $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
453 map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
458 my $user_id = $main::auth->get_user_id($form->{login});
459 my $all_groups = $main::auth->read_groups();
461 foreach my $group (values %{ $all_groups }) {
462 push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
465 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
468 $form->{CAN_CHANGE_PASSWORD} = $main::auth->can_change_password();
471 print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
475 my $form = $main::form;
476 my $locale = $main::locale;
478 $form->{dbdriver} = 'Pg';
480 # no spaces allowed in login name
481 $form->{login} =~ s|\s||g;
482 $form->isblank("login", $locale->text('Login name missing!'));
484 # check for duplicates
485 if (!$form->{edit}) {
486 my %members = $main::auth->read_all_users();
487 if ($members{$form->{login}}) {
488 $form->show_generic_error($locale->text('Another user with the login #1 does already exist.', $form->{login}), 'back_button' => 1);
492 # no spaces allowed in directories
493 ($form->{newtemplates}) = split / /, $form->{newtemplates};
495 if ($form->{newtemplates}) {
496 $form->{templates} = $form->{newtemplates};
499 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
503 if (!-d $::lx_office_conf{paths}->{templates}) {
504 $form->error(sprintf($locale->text("The directory %s does not exist."), $::lx_office_conf{paths}->{templates}));
507 # add base directory to $form->{templates}
508 $form->{templates} =~ s|.*/||;
509 $form->{templates} = $::lx_office_conf{paths}->{templates} . "/$form->{templates}";
511 my $myconfig = new User($form->{login});
513 $form->isblank("dbname", $locale->text('Dataset missing!'));
514 $form->isblank("dbuser", $locale->text('Database User missing!'));
516 foreach my $item (keys %{$form}) {
517 $myconfig->{$item} = $form->{$item};
520 delete $myconfig->{stylesheet};
521 if ($form->{userstylesheet}) {
522 $myconfig->{stylesheet} = $form->{userstylesheet};
525 $myconfig->save_member();
527 $form->{templates} =~ s|.*/||;
528 $form->{templates} = $::lx_office_conf{paths}->{templates} . "/$form->{templates}";
529 $form->{mastertemplates} =~ s|.*/||;
531 # create user template directory and copy master files
532 if (!-d "$form->{templates}") {
535 if (mkdir "$form->{templates}", oct("771")) {
539 # copy templates to the directory
540 opendir TEMPLATEDIR, $::lx_office_conf{paths}->{templates} or $form->error($::lx_office_conf{paths}->{templates} . " : $ERRNO");
541 my @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|odt|xml|txb)$/,
543 closedir TEMPLATEDIR;
545 foreach my $file (@templates) {
546 open(TEMP, $::lx_office_conf{paths}->{templates} . "/$file")
547 or $form->error($::lx_office_conf{paths}->{templates} . "/$file : $ERRNO");
549 $file =~ s/\Q$form->{mastertemplates}\E-//;
550 open(NEW, ">$form->{templates}/$file")
551 or $form->error("$form->{templates}/$file : $ERRNO");
553 while (my $line = <TEMP>) {
560 $form->error("$ERRNO: $form->{templates}");
564 # Add new user to his groups.
565 if (ref $form->{new_user_group_ids} eq 'ARRAY') {
566 my $all_groups = $main::auth->read_groups();
567 my %user = $main::auth->read_user($form->{login});
569 foreach my $group_id (@{ $form->{new_user_group_ids} }) {
570 my $group = $all_groups->{$group_id};
574 push @{ $group->{members} }, $user{id};
575 $main::auth->save_group($group);
579 if ($main::auth->can_change_password()
580 && defined $form->{new_password}
581 && ($form->{new_password} ne '********')) {
582 my $verifier = SL::Auth::PasswordPolicy->new;
583 my $result = $verifier->verify($form->{new_password}, 1);
585 if ($result != SL::Auth::PasswordPolicy->OK()) {
586 $form->error($::locale->text('The settings were saved, but the password was not changed.') . ' ' . join(' ', $verifier->errors($result)));
589 $main::auth->change_password($form->{login}, $form->{new_password});
592 $form->redirect($locale->text('User saved!'));
595 sub save_user_as_new {
596 my $form = $main::form;
598 $form->{login} = $form->{new_user_login};
599 delete @{$form}{qw(edit new_user_login)};
605 my $form = $main::form;
606 my $locale = $main::locale;
608 my %members = $main::auth->read_all_users();
609 my $templates = $members{$form->{login}}->{templates};
611 $main::auth->delete_user($form->{login});
614 my $templates_in_use = 0;
616 foreach my $login (keys %members) {
617 next if $form->{login} eq $login;
618 next if $members{$login}->{templates} ne $templates;
619 $templates_in_use = 1;
623 if (!$templates_in_use && -d $templates) {
624 unlink <$templates/*>;
629 $form->redirect($locale->text('User deleted!'));
637 return ($login) ? $login : undef;
643 my ($null, $value) = split(/=/, $line, 2);
646 $value =~ s/\s#.*//g;
648 # remove any trailing whitespace
649 $value =~ s/^\s*(.*?)\s*$/$1/;
654 sub pg_database_administration {
655 my $form = $main::form;
657 $form->{dbdriver} = 'Pg';
662 sub dbselect_source {
663 my $form = $main::form;
664 my $locale = $main::locale;
666 $form->{dbport} = $::auth->{DB_config}->{port} || 5432;
667 $form->{dbuser} = $::auth->{DB_config}->{user} || 'lxoffice';
668 $form->{dbdefault} = 'template1';
669 $form->{dbhost} = $::auth->{DB_config}->{host} || 'localhost';
671 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
673 # Intentionnaly disabled unless fixed to work with the authentication DB.
674 $form->{ALLOW_DBBACKUP} = 0; # "$pg_dump_exe" ne "DISABLED";
677 print $form->parse_html_template("admin/dbadmin");
680 sub test_db_connection {
681 my $form = $main::form;
682 my $locale = $main::locale;
684 $form->{dbdriver} = 'Pg';
685 User::dbconnect_vars($form, $form->{dbname});
687 my $dbh = DBI->connect($form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd});
689 $form->{connection_ok} = $dbh ? 1 : 0;
690 $form->{errstr} = $DBI::errstr;
692 $dbh->disconnect() if ($dbh);
694 $form->{title} = $locale->text('Database Connection Test');
696 print $form->parse_html_template("admin/test_db_connection");
700 call_sub($main::form->{"nextsub"});
704 my $form = $main::form;
705 my $locale = $main::locale;
707 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Update Dataset');
709 my @need_updates = User->dbneedsupdate($form);
710 $form->{NEED_UPDATES} = \@need_updates;
711 $form->{ALL_UPDATED} = !scalar @need_updates;
714 print $form->parse_html_template("admin/update_dataset");
718 my $form = $main::form;
719 my $locale = $main::locale;
721 $form->{stylesheet} = "lx-office-erp.css";
722 $form->{title} = $locale->text("Dataset upgrade");
725 my $rowcount = $form->{rowcount} * 1;
726 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
727 $form->{NOTHING_TO_DO} = !scalar @update_rows;
728 my $saved_form = save_form();
732 print $form->parse_html_template("admin/dbupgrade_all_header");
734 foreach my $i (@update_rows) {
735 restore_form($saved_form);
737 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
739 print $form->parse_html_template("admin/dbupgrade_header");
741 $form->{dbupdate} = $form->{dbname};
742 $form->{$form->{dbname}} = 1;
744 User->dbupdate($form);
745 User->dbupdate2($form, SL::DBUpgrade2->new(form => $form, dbdriver => $form->{dbdriver})->parse_dbupdate_controls);
747 print $form->parse_html_template("admin/dbupgrade_footer");
750 print $form->parse_html_template("admin/dbupgrade_all_done");
754 my $form = $main::form;
755 my $locale = $main::locale;
757 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources($form);
759 $form->{CHARTS} = [];
761 opendir SQLDIR, "sql/." or $form->error($ERRNO);
762 foreach my $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
763 next if ($item eq 'Default-chart.sql');
764 $item =~ s/-chart\.sql//;
765 push @{ $form->{CHARTS} }, { "name" => $item,
766 "selected" => $item eq "Germany-DATEV-SKR03EU" };
770 my $default_charset = $::lx_office_conf{system}->{dbcharset};
771 $default_charset ||= Common::DEFAULT_CHARSET;
773 my $cluster_encoding = User->dbclusterencoding($form);
774 if ($cluster_encoding && ($cluster_encoding =~ m/^(?:UTF-?8|UNICODE)$/i)) {
775 if ($::lx_office_conf{system}->{dbcharset} !~ m/^UTF-?8$/i) {
776 $form->show_generic_error($locale->text('The selected PostgreSQL installation uses UTF-8 as its encoding. ' .
777 'Therefore you have to configure Lx-Office to use UTF-8 as well.'),
781 $form->{FORCE_DBENCODING} = 'UNICODE';
784 $form->{DBENCODINGS} = [];
786 foreach my $encoding (@Common::db_encodings) {
787 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
788 "label" => $encoding->{label},
789 "selected" => $encoding->{charset} eq $default_charset };
793 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
796 print $form->parse_html_template("admin/create_dataset");
800 my $form = $main::form;
801 my $locale = $main::locale;
803 $form->isblank("db", $locale->text('Dataset missing!'));
805 User->dbcreate(\%$form);
807 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Create Dataset');
810 print $form->parse_html_template("admin/dbcreate");
814 my $form = $main::form;
815 my $locale = $main::locale;
817 my @dbsources = User->dbsources_unused($form);
818 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
820 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
821 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
824 print $form->parse_html_template("admin/delete_dataset");
828 my $form = $main::form;
829 my $locale = $main::locale;
832 $form->error($locale->text('No Dataset selected!'));
835 User->dbdelete(\%$form);
837 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Delete Dataset');
839 print $form->parse_html_template("admin/dbdelete");
843 my $form = $main::form;
844 my $locale = $main::locale;
846 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
848 if ($::lx_office_conf{applications}->{pg_dump} eq "DISABLED") {
849 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
852 my @dbsources = sort User->dbsources($form);
853 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
854 $form->{NO_DATABASES} = !scalar @dbsources;
856 my $username = getpwuid $UID || "unknown-user";
857 my $hostname = hostname() || "unknown-host";
858 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
861 print $form->parse_html_template("admin/backup_dataset");
864 sub backup_dataset_start {
865 my $form = $main::form;
866 my $locale = $main::locale;
868 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
870 my $pg_dump_exe = $::lx_office_conf{applications}->{pg_dump} || "pg_dump";
872 if ("$pg_dump_exe" eq "DISABLED") {
873 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
876 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
877 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
879 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
880 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
882 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
886 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
889 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
892 $ENV{HOME} = $tmpdir;
894 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
895 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
896 push @args, $form->{dbname};
898 my $cmd = "$pg_dump_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
899 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
901 if ($form->{destination} ne "email") {
902 my $in = IO::File->new("$cmd |");
905 unlink "${tmpdir}/.pgpass";
908 $form->error($locale->text('The pg_dump process could not be started.'));
911 print "content-type: application/x-tar\n";
912 print "content-disposition: attachment; filename=\"${name}\"\n\n";
914 while (my $line = <$in>) {
920 unlink "${tmpdir}/.pgpass";
924 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
926 if (system("$cmd > $tmp") != 0) {
927 unlink "${tmpdir}/.pgpass", $tmp;
930 $form->error($locale->text('The pg_dump process could not be started.'));
933 my $mail = new Mailer;
935 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
937 $mail->{charset} = $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET;
938 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
941 unlink "${tmpdir}/.pgpass", $tmp;
944 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Backup Dataset');
947 print $form->parse_html_template("admin/backup_dataset_email_done");
951 sub restore_dataset {
952 my $form = $main::form;
953 my $locale = $main::locale;
955 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
957 if ($::lx_office_conf{applications}->{pg_restore} eq "DISABLED") {
958 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
961 my $default_charset = $::lx_office_conf{system}->{dbcharset};
962 $default_charset ||= Common::DEFAULT_CHARSET;
964 $form->{DBENCODINGS} = [];
966 foreach my $encoding (@Common::db_encodings) {
967 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
968 "label" => $encoding->{label},
969 "selected" => $encoding->{charset} eq $default_charset };
973 print $form->parse_html_template("admin/restore_dataset");
976 sub restore_dataset_start {
977 my $form = $main::form;
978 my $locale = $main::locale;
980 $form->{title} = "Lx-Office ERP " . $locale->text('Database Administration') . " / " . $locale->text('Restore Dataset');
982 my $pg_restore_exe = $::lx_office_conf{applications}->{pg_restore} || "pg_restore";
984 if ("$pg_restore_exe" eq "DISABLED") {
985 $form->error($locale->text('Database backups and restorations are disabled in the configuration.'));
988 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
989 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
991 # Create temporary directories. Write the backup file contents to a temporary
992 # file. Create a .pgpass file with the username and password for the pg_restore
995 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
996 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
998 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
1002 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
1005 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
1008 $ENV{HOME} = $tmpdir;
1010 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
1013 if (substr($form->{content}, 0, 2) eq "\037\213") {
1014 $tmpfile = IO::File->new("| gzip -d > $tmp");
1018 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
1022 unlink "${tmpdir}/.pgpass";
1025 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
1028 print $tmpfile $form->{content};
1031 delete $form->{content};
1033 # Try to connect to the database. Find out if a database with the same name exists.
1034 # If yes, then drop the existing database. Create a new one with the name and encoding
1035 # given by the user.
1037 User::dbconnect_vars($form, "template1");
1039 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
1040 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
1044 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
1046 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
1047 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
1049 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
1053 foreach my $item (@Common::db_encodings) {
1054 if ($item->{dbencoding} eq $form->{dbencoding}) {
1059 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
1061 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
1065 # Spawn pg_restore on the temporary file.
1067 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
1068 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
1071 my $cmd = "$pg_restore_exe " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
1073 my $in = IO::File->new("$cmd 2>&1 |");
1076 unlink "${tmpdir}/.pgpass", $tmp;
1079 $form->error($locale->text('The pg_restore process could not be started.'));
1082 $English::AUTOFLUSH = 1;
1085 print $form->parse_html_template("admin/restore_dataset_start_header");
1087 while (my $line = <$in>) {
1092 $form->{retval} = $CHILD_ERROR >> 8;
1093 print $form->parse_html_template("admin/restore_dataset_start_footer");
1095 unlink "${tmpdir}/.pgpass", $tmp;
1100 my $form = $main::form;
1101 my $locale = $main::locale;
1103 unlink _nologin_file_name();;
1105 $form->{callback} = "admin.pl?action=list_users";
1107 $form->redirect($locale->text('Lockfile removed!'));
1112 my $form = $main::form;
1113 my $locale = $main::locale;
1115 open(FH, ">" . _nologin_file_name())
1116 or $form->error($locale->text('Cannot create Lock!'));
1119 $form->{callback} = "admin.pl?action=list_users";
1121 $form->redirect($locale->text('Lockfile created!'));
1126 call_sub($main::form->{yes_nextsub});
1130 call_sub($main::form->{no_nextsub});
1134 call_sub($main::form->{add_nextsub});
1138 my $form = $main::form;
1140 $form->{edit_nextsub} ||= 'edit_user';
1142 call_sub($form->{edit_nextsub});
1146 my $form = $main::form;
1148 $form->{delete_nextsub} ||= 'delete_user';
1150 call_sub($form->{delete_nextsub});
1154 my $form = $main::form;
1156 $form->{save_nextsub} ||= 'save_user';
1158 call_sub($form->{save_nextsub});
1162 call_sub($main::form->{back_nextsub});
1166 my $form = $main::form;
1167 my $locale = $main::locale;
1169 foreach my $action (qw(create_standard_group dont_create_standard_group
1170 save_user delete_user save_user_as_new)) {
1171 if ($form->{"action_${action}"}) {
1177 call_sub($form->{default_action}) if ($form->{default_action});
1179 $form->error($locale->text('No action defined.'));
1182 sub _apply_dbupgrade_scripts {
1183 ::end_of_request() if SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(1);
1186 sub _nologin_file_name {
1187 return $::lx_office_conf{paths}->{userspath} . '/nologin';