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 English qw(-no_match_vars);
43 use POSIX qw(strftime);
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/admin_groups.pl";
58 our $cgi = new CGI('');
62 $locale = new Locale $language, "admin";
64 our $auth = SL::Auth->new();
65 if ($auth->session_tables_present()) {
66 $auth->expire_sessions();
67 $auth->restore_session();
68 $auth->set_session_value('rpw', $form->{rpw});
72 if (-f "bin/mozilla/custom_$form->{script}") {
73 eval { require "bin/mozilla/custom_$form->{script}"; };
74 $form->error($@) if ($@);
77 $form->{stylesheet} = "lx-office-erp.css";
78 $form->{favicon} = "favicon.ico";
80 if ($form->{action}) {
82 $subroutine = $locale->findsub($form->{action});
84 if ($auth->authenticate_root($form->{rpw}, 0)) {
85 $form->{error_message} = $locale->text('Incorrect Password!');
90 $auth->create_or_refresh_session() if ($auth->session_tables_present());
92 call_sub($subroutine);
96 # if there are no drivers bail out
97 $form->error($locale->text('No Database Drivers available!'))
98 unless (User->dbdrivers);
110 $form->{title} = qq|Lx-Office ERP $form->{version} | . $locale->text('Administration');
113 print $form->parse_html_template('admin/adminlogin');
117 check_auth_db_and_tables();
122 $auth->destroy_session();
126 sub check_auth_db_and_tables {
129 map { $params{"db_${_}"} = $auth->{DB_config}->{$_} } keys %{ $auth->{DB_config} };
131 if (!$auth->check_database()) {
132 $form->{title} = $locale->text('Authentification database creation');
134 print $form->parse_html_template('admin/check_auth_database', \%params);
139 if (!$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 $memberfile) {
150 if ($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' => $memberfile,
159 'backupdir' => $backupdir });
166 $auth->create_database('superuser' => $form->{db_superuser},
167 'superuser_password' => $form->{db_superuser_password},
168 'template' => $form->{db_template});
172 sub create_auth_tables {
173 $auth->create_tables();
174 $auth->set_session_value('rpw', $form->{rpw});
175 $auth->create_or_refresh_session();
181 $lxdebug->enter_sub();
185 if ($memberfile =~ m|^.*/|) {
189 my $backupdir = "${memberdir}member-file-migration";
191 if (! -d $backupdir && !mkdir $backupdir, 0700) {
192 $form->error(sprintf($locale->text('The directory "%s" could not be created:\n%s'), $backupdir, $!));
195 copy $memberfile, "users/member-file-migration/members";
197 my $in = IO::File->new($memberfile, "r");
199 $form->error($locale->text('Could not open the old memberfile.')) if (!$in);
201 my (%members, $login);
210 $login =~ s/(\[|\])//g;
214 $members{$login} = { "login" => $login };
218 if ($login && m/=/) {
219 my ($key, $value) = split m/\s*=\s*/, $_, 2;
224 $value =~ s|\\n|\n|g;
226 $members{$login}->{$key} = $value;
232 delete $members{"root login"};
234 map { $_->{dbpasswd} = unpack 'u', $_->{dbpasswd} } values %members;
236 while (my ($login, $params) = each %members) {
237 $auth->save_user($login, %{ $params });
238 $auth->change_password($login, $params->{password}, 1);
240 my $conf_file = "${memberdir}${login}.conf";
243 copy $conf_file, "${backupdir}/${login}.conf";
250 my @member_list = sort { lc $a->{login} cmp lc $b->{login} } values %members;
252 $form->{title} = $locale->text('User data migration');
254 print $form->parse_html_template('admin/user_migration_done', { 'MEMBERS' => \@member_list });
256 $lxdebug->leave_sub();
259 sub create_standard_group_ask {
260 $form->{title} = $locale->text('Create a standard group');
263 print $form->parse_html_template("admin/create_standard_group_ask");
266 sub create_standard_group {
267 my %members = $auth->read_all_users();
269 my $groups = $auth->read_groups();
271 foreach my $group (values %{$groups}) {
272 if (($form->{group_id} != $group->{id})
273 && ($form->{name} eq $group->{name})) {
274 $form->show_generic_error($locale->text("A group with that name does already exist."));
279 'name' => $locale->text('Full Access'),
280 'description' => $locale->text('Full access to all functions'),
281 'rights' => { map { $_ => 1 } SL::Auth::all_rights() },
282 'members' => [ map { $_->{id} } values %members ],
285 $auth->save_group($group);
287 user_migration_complete(1);
290 sub dont_create_standard_group {
291 user_migration_complete(0);
294 sub user_migration_complete {
295 my $standard_group_created = shift;
297 $form->{title} = $locale->text('User migration complete');
300 print $form->parse_html_template('admin/user_migration_complete', { 'standard_group_created' => $standard_group_created });
304 my %members = $auth->read_all_users();
306 delete $members{"root login"};
308 map { $_->{templates} =~ s|.*/||; } values %members;
310 $form->{title} = "Lx-Office ERP " . $locale->text('Administration');
311 $form->{LOCKED} = -e "$userspath/nologin";
312 $form->{MEMBERS} = [ @members{sort { lc $a cmp lc $b } keys %members} ];
315 print $form->parse_html_template("admin/list_users");
322 . $locale->text('Administration') . " / "
323 . $locale->text('Add User');
327 "countrycode" => "de",
328 "numberformat" => "1.000,00",
329 "dateformat" => "dd.mm.yy",
330 "stylesheet" => "lx-office-erp.css",
334 edit_user_form($myconfig);
341 . $locale->text('Administration') . " / "
342 . $locale->text('Edit User');
345 $form->isblank("login", $locale->text("The login is missing."));
348 my $myconfig = new User($form->{login});
350 # strip basedir from templates directory
351 $myconfig->{templates} =~ s|.*/||;
353 edit_user_form($myconfig);
359 my @valid_dateformats = qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd);
360 $form->{ALL_DATEFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{dateformat} } } @valid_dateformats ];
362 my @valid_numberformats = qw(1,000.00 1000.00 1.000,00 1000,00);
363 $form->{ALL_NUMBERFORMATS} = [ map { { "format" => $_, "selected" => $_ eq $myconfig->{numberformat} } } @valid_numberformats ];
365 %countrycodes = User->country_codes;
366 $form->{ALL_COUNTRYCODES} = [];
367 foreach $countrycode (sort { $countrycodes{$a} cmp $countrycodes{$b} } keys %countrycodes) {
368 push @{ $form->{ALL_COUNTRYCODES} }, { "value" => $countrycode,
369 "name" => $countrycodes{$countrycode},
370 "selected" => $countrycode eq $myconfig->{countrycode} };
373 # is there a templates basedir
374 if (!-d "$templates") {
375 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
378 opendir TEMPLATEDIR, "$templates/." or $form->error("$templates : $ERRNO");
379 my @all = readdir(TEMPLATEDIR);
380 my @alldir = sort grep { -d "$templates/$_" && !/^\.\.?$/ } @all;
381 my @allhtml = sort grep { -f "$templates/$_" && /\.html$/ } @all;
382 closedir TEMPLATEDIR;
384 @alldir = grep !/\.(html|tex|sty|odt|xml|txb)$/, @alldir;
385 @alldir = grep !/^(webpages|\.svn)$/, @alldir;
387 @allhtml = reverse grep !/Default/, @allhtml;
388 push @allhtml, 'Default';
389 @allhtml = reverse @allhtml;
391 $form->{ALL_TEMPLATES} = [ map { { "name", => $_, "selected" => $_ eq $myconfig->{templates} } } @alldir ];
393 $lastitem = $allhtml[0];
394 $lastitem =~ s/-.*//g;
395 $form->{ALL_MASTER_TEMPLATES} = [ { "name" => $lastitem, "selected" => $lastitem eq "German" } ];
396 foreach $item (@allhtml) {
398 next if ($item eq $lastitem);
400 push @{ $form->{ALL_MASTER_TEMPLATES} }, { "name" => $item, "selected" => $item eq "German" };
404 # css dir has styles that are not intended as general layouts.
405 # reverting to hardcoded list
406 $form->{ALL_STYLESHEETS} = [ map { { "name" => $_, "selected" => $_ eq $myconfig->{stylesheet} } } qw(lx-office-erp.css Win2000.css) ];
408 $form->{"menustyle_" . $myconfig->{menustyle} } = 1;
410 map { $form->{"myc_${_}"} = $myconfig->{$_} } keys %{ $myconfig };
415 my $user_id = $auth->get_user_id($form->{login});
416 my $all_groups = $auth->read_groups();
418 foreach my $group (values %{ $all_groups }) {
419 push @{ $groups }, $group if (grep { $user_id == $_ } @{ $group->{members} });
422 $groups = [ sort { lc $a->{name} cmp lc $b->{name} } @{ $groups } ];
425 $form->{CAN_CHANGE_PASSWORD} = $auth->can_change_password();
428 print $form->parse_html_template("admin/edit_user", { 'GROUPS' => $groups });
432 $form->{dbdriver} = 'Pg';
434 # no spaces allowed in login name
435 $form->{login} =~ s|\s||g;
436 $form->isblank("login", $locale->text('Login name missing!'));
438 # check for duplicates
439 if (!$form->{edit}) {
440 my %members = $auth->read_all_users();
442 if ($members{$form->{login}}) {
443 $form->error("$form->{login} " . $locale->text('is already a member!'));
447 # no spaces allowed in directories
448 ($form->{newtemplates}) = split / /, $form->{newtemplates};
450 if ($form->{newtemplates}) {
451 $form->{templates} = $form->{newtemplates};
454 ($form->{usetemplates}) ? $form->{usetemplates} : $form->{login};
458 if (!-d "$templates") {
459 $form->error(sprintf($locale->text("The directory %s does not exist."), $templates));
462 # add base directory to $form->{templates}
463 $form->{templates} =~ s|.*/||;
464 $form->{templates} = "$templates/$form->{templates}";
466 $myconfig = new User($form->{login});
468 $form->isblank("dbname", $locale->text('Dataset missing!'));
469 $form->isblank("dbuser", $locale->text('Database User missing!'));
471 foreach $item (keys %{$form}) {
472 $myconfig->{$item} = $form->{$item};
475 delete $myconfig->{stylesheet};
476 if ($form->{userstylesheet}) {
477 $myconfig->{stylesheet} = $form->{userstylesheet};
480 $myconfig->save_member();
482 if ($auth->can_change_password()
483 && defined $form->{new_password}
484 && ($form->{new_password} ne '********')) {
485 $auth->change_password($form->{login}, $form->{new_password});
490 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
491 foreach $directory (@webdavdirs) {
492 $file = "webdav/" . $directory . "/webdav-user";
493 if ($form->{$directory}) {
494 if (open(HTACCESS, "$file")) {
496 ($login, $password) = split(/:/, $_);
497 if ($login ne $form->{login}) {
503 open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
504 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
505 print(HTACCESS $newfile);
508 $form->{$directory} = 0;
509 if (open(HTACCESS, "$file")) {
511 ($login, $password) = split(/:/, $_);
512 if ($login ne $form->{login}) {
518 open(HTACCESS, "> $file") or die "cannot open $file $ERRNO\n";
519 print(HTACCESS $newfile);
525 $form->{templates} =~ s|.*/||;
526 $form->{templates} = "${templates}/$form->{templates}";
527 $form->{mastertemplates} =~ s|.*/||;
529 # create user template directory and copy master files
530 if (!-d "$form->{templates}") {
533 if (mkdir "$form->{templates}", oct("771")) {
537 # copy templates to the directory
538 opendir TEMPLATEDIR, "$templates/." or $form - error("$templates : $ERRNO");
539 @templates = grep /$form->{mastertemplates}.*?\.(html|tex|sty|xml|txb)$/,
541 closedir TEMPLATEDIR;
543 foreach $file (@templates) {
544 open(TEMP, "$templates/$file")
545 or $form->error("$templates/$file : $ERRNO");
547 $file =~ s/\Q$form->{mastertemplates}\E-//;
548 open(NEW, ">$form->{templates}/$file")
549 or $form->error("$form->{templates}/$file : $ERRNO");
551 while ($line = <TEMP>) {
558 $form->error("$ERRNO: $form->{templates}");
562 $form->redirect($locale->text('User saved!'));
567 my %members = $auth->read_all_users();
568 my $templates = $members{$form->{login}}->{templates};
570 $auth->delete_user($form->{login});
573 my $templates_in_use = 0;
575 foreach $login (keys %members) {
576 next if $form->{login} eq $login;
577 next if $members{$login}->{templates} ne $templates;
578 $templates_in_use = 1;
582 if (!$templates_in_use && -d $templates) {
583 unlink <$templates/*>;
588 $form->redirect($locale->text('User deleted!'));
596 return ($login) ? $login : undef;
603 my ($null, $value) = split(/=/, $line, 2);
606 $value =~ s/\s#.*//g;
608 # remove any trailing whitespace
609 $value =~ s/^\s*(.*?)\s*$/$1/;
614 sub pg_database_administration {
616 $form->{dbdriver} = 'Pg';
621 sub dbselect_source {
622 $form->{dbport} = '5432';
623 $form->{dbuser} = 'postgres';
624 $form->{dbdefault} = 'template1';
625 $form->{dbhost} = 'localhost';
627 $form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration');
629 $form->{ALLOW_DBBACKUP} = "$pg_dump_exe" ne "DISABLED";
632 print $form->parse_html_template("admin/dbadmin");
636 call_sub($form->{"nextsub"});
640 call_sub($form->{"back_nextsub"});
646 . $locale->text('Database Administration') . " / "
647 . $locale->text('Update Dataset');
649 my @need_updates = User->dbneedsupdate($form);
650 $form->{NEED_UPDATES} = \@need_updates;
651 $form->{ALL_UPDATED} = !scalar @need_updates;
654 print $form->parse_html_template("admin/update_dataset");
658 $form->{stylesheet} = "lx-office-erp.css";
659 $form->{title} = $locale->text("Dataset upgrade");
662 my $rowcount = $form->{rowcount} * 1;
663 my @update_rows = grep { $form->{"update_$_"} } (1 .. $rowcount);
664 $form->{NOTHING_TO_DO} = !scalar @update_rows;
665 my $saved_form = save_form();
669 print $form->parse_html_template("admin/dbupgrade_all_header");
671 foreach my $i (@update_rows) {
672 restore_form($saved_form);
674 map { $form->{$_} = $form->{"${_}_${i}"} } qw(dbname dbdriver dbhost dbport dbuser dbpasswd);
676 my $controls = parse_dbupdate_controls($form, $form->{dbdriver});
678 print $form->parse_html_template("admin/dbupgrade_header");
680 $form->{dbupdate} = $form->{dbname};
681 $form->{$form->{dbname}} = 1;
683 User->dbupdate($form);
684 User->dbupdate2($form, $controls);
686 print $form->parse_html_template("admin/dbupgrade_footer");
689 print $form->parse_html_template("admin/dbupgrade_all_done");
693 $form->{dbsources} = join " ", map { "[${_}]" } sort User->dbsources(\%$form);
695 $form->{CHARTS} = [];
697 opendir SQLDIR, "sql/." or $form - error($ERRNO);
698 foreach $item (sort grep /-chart\.sql\z/, readdir SQLDIR) {
699 next if ($item eq 'Default-chart.sql');
700 $item =~ s/-chart\.sql//;
701 push @{ $form->{CHARTS} }, { "name" => $item,
702 "selected" => $item eq "Germany-DATEV-SKR03EU" };
706 my $default_charset = $dbcharset;
707 $default_charset ||= Common::DEFAULT_CHARSET;
709 $form->{DBENCODINGS} = [];
711 foreach my $encoding (@Common::db_encodings) {
712 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
713 "label" => $encoding->{label},
714 "selected" => $encoding->{charset} eq $default_charset };
719 . $locale->text('Database Administration') . " / "
720 . $locale->text('Create Dataset');
723 print $form->parse_html_template("admin/create_dataset");
727 $form->isblank("db", $locale->text('Dataset missing!'));
729 User->dbcreate(\%$form);
733 . $locale->text('Database Administration') . " / "
734 . $locale->text('Create Dataset');
737 print $form->parse_html_template("admin/dbcreate");
741 @dbsources = User->dbsources_unused($form);
742 $form->error($locale->text('Nothing to delete!')) unless @dbsources;
746 . $locale->text('Database Administration') . " / "
747 . $locale->text('Delete Dataset');
748 $form->{DBSOURCES} = [ map { { "name", $_ } } sort @dbsources ];
751 print $form->parse_html_template("admin/delete_dataset");
757 $form->error($locale->text('No Dataset selected!'));
760 User->dbdelete(\%$form);
764 . $locale->text('Database Administration') . " / "
765 . $locale->text('Delete Dataset');
767 print $form->parse_html_template("admin/dbdelete");
773 . $locale->text('Database Administration') . " / "
774 . $locale->text('Backup Dataset');
776 if ("$pg_dump_exe" eq "DISABLED") {
777 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
780 my @dbsources = sort User->dbsources($form);
781 $form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ];
782 $form->{NO_DATABASES} = !scalar @dbsources;
784 my $username = getpwuid $UID || "unknown-user";
785 my $hostname = hostname() || "unknown-host";
786 $form->{from} = "Lx-Office Admin <${username}\@${hostname}>";
789 print $form->parse_html_template("admin/backup_dataset");
792 sub backup_dataset_start {
795 . $locale->text('Database Administration') . " / "
796 . $locale->text('Backup Dataset');
798 $pg_dump_exe ||= "pg_dump";
800 if ("$pg_dump_exe" eq "DISABLED") {
801 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
804 $form->isblank("dbname", $locale->text('The dataset name is missing.'));
805 $form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email";
807 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
808 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
810 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
814 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
817 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
820 $ENV{HOME} = $tmpdir;
822 my @args = ("-Ft", "-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser});
823 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
824 push @args, $form->{dbname};
826 my $cmd = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
827 my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".tar";
829 if ($form->{destination} ne "email") {
830 my $in = IO::File->new("$cmd |");
833 unlink "${tmpdir}/.pgpass";
836 $form->error($locale->text('The pg_dump process could not be started.'));
839 print "content-type: application/x-tar\n";
840 print "content-disposition: attachment; filename=\"${name}\"\n\n";
842 while (my $line = <$in>) {
848 unlink "${tmpdir}/.pgpass";
852 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
854 if (system("$cmd > $tmp") != 0) {
855 unlink "${tmpdir}/.pgpass", $tmp;
858 $form->error($locale->text('The pg_dump process could not be started.'));
861 my $mail = new Mailer;
863 map { $mail->{$_} = $form->{$_} } qw(from to cc subject message);
865 $mail->{charset} = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET;
866 $mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ];
869 unlink "${tmpdir}/.pgpass", $tmp;
874 . $locale->text('Database Administration') . " / "
875 . $locale->text('Backup Dataset');
878 print $form->parse_html_template("admin/backup_dataset_email_done");
882 sub restore_dataset {
885 . $locale->text('Database Administration') . " / "
886 . $locale->text('Restore Dataset');
888 if ("$pg_restore_exe" eq "DISABLED") {
889 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
892 my $default_charset = $dbcharset;
893 $default_charset ||= Common::DEFAULT_CHARSET;
895 $form->{DBENCODINGS} = [];
897 foreach my $encoding (@Common::db_encodings) {
898 push @{ $form->{DBENCODINGS} }, { "dbencoding" => $encoding->{dbencoding},
899 "label" => $encoding->{label},
900 "selected" => $encoding->{charset} eq $default_charset };
904 print $form->parse_html_template("admin/restore_dataset");
907 sub restore_dataset_start {
910 . $locale->text('Database Administration') . " / "
911 . $locale->text('Restore Dataset');
913 $pg_restore_exe ||= "pg_restore";
915 if ("$pg_restore_exe" eq "DISABLED") {
916 $form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.'));
919 $form->isblank("new_dbname", $locale->text('The dataset name is missing.'));
920 $form->isblank("content", $locale->text('No backup file has been uploaded.'));
922 # Create temporary directories. Write the backup file contents to a temporary
923 # file. Create a .pgpass file with the username and password for the pg_restore
926 my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id();
927 mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $ERRNO");
929 my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600);
933 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
936 print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{new_dbname}:$form->{dbuser}:$form->{dbpasswd}\n";
939 $ENV{HOME} = $tmpdir;
941 my $tmp = $tmpdir . "/dump_" . Common::unique_id();
944 if (substr($form->{content}, 0, 2) eq "\037\213") {
945 $tmpfile = IO::File->new("| gzip -d > $tmp");
949 $tmpfile = IO::File->new($tmp, O_WRONLY | O_CREAT | O_BINARY, 0600);
953 unlink "${tmpdir}/.pgpass";
956 $form->error($locale->text('A temporary file could not be created:') . " $ERRNO");
959 print $tmpfile $form->{content};
962 delete $form->{content};
964 # Try to connect to the database. Find out if a database with the same name exists.
965 # If yes, then drop the existing database. Create a new one with the name and encoding
968 User::dbconnect_vars($form, "template1");
970 my %myconfig = map { $_ => $form->{$_} } grep /^db/, keys %{ $form };
971 my $dbh = $form->dbconnect(\%myconfig) || $form->dberror();
975 $form->{new_dbname} =~ s|[^a-zA-Z0-9_\-]||g;
977 $query = qq|SELECT COUNT(*) FROM pg_database WHERE datname = ?|;
978 my ($count) = selectrow_query($form, $dbh, $query, $form->{new_dbname});
980 do_query($form, $dbh, qq|DROP DATABASE $form->{new_dbname}|);
984 foreach my $item (@Common::db_encodings) {
985 if ($item->{dbencoding} eq $form->{dbencoding}) {
990 $form->{dbencoding} = "LATIN9" unless $form->{dbencoding};
992 do_query($form, $dbh, qq|CREATE DATABASE $form->{new_dbname} ENCODING ? TEMPLATE template0|, $form->{dbencoding});
996 # Spawn pg_restore on the temporary file.
998 my @args = ("-h", $form->{dbhost}, "-U", $form->{dbuser}, "-d", $form->{new_dbname});
999 push @args, ("-p", $form->{dbport}) if ($form->{dbport});
1002 my $cmd = "${pg_restore_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args);
1004 my $in = IO::File->new("$cmd 2>&1 |");
1007 unlink "${tmpdir}/.pgpass", $tmp;
1010 $form->error($locale->text('The pg_restore process could not be started.'));
1016 print $form->parse_html_template("admin/restore_dataset_start_header");
1018 while (my $line = <$in>) {
1023 $form->{retval} = $CHILD_ERROR >> 8;
1024 print $form->parse_html_template("admin/restore_dataset_start_footer");
1026 unlink "${tmpdir}/.pgpass", $tmp;
1032 unlink "$userspath/nologin";
1034 $form->{callback} = "admin.pl?action=list_users";
1036 $form->redirect($locale->text('Lockfile removed!'));
1042 open(FH, ">$userspath/nologin")
1043 or $form->error($locale->text('Cannot create Lock!'));
1046 $form->{callback} = "admin.pl?action=list_users";
1048 $form->redirect($locale->text('Lockfile created!'));
1053 call_sub($form->{yes_nextsub});
1057 call_sub($form->{no_nextsub});
1061 call_sub($form->{add_nextsub});
1065 $form->{edit_nextsub} ||= 'edit_user';
1067 call_sub($form->{edit_nextsub});
1071 $form->{delete_nextsub} ||= 'delete_user';
1073 call_sub($form->{delete_nextsub});
1077 $form->{save_nextsub} ||= 'save_user';
1079 call_sub($form->{save_nextsub});
1083 call_sub($form->{back_nextsub});
1087 foreach my $action (qw(create_standard_group dont_create_standard_group)) {
1088 if ($form->{"action_${action}"}) {
1094 call_sub($form->{default_action}) if ($form->{default_action});
1096 $form->error($locale->text('No action defined.'));