additional changes to it.pm (post 3.0.0beta2)
[BackupPC.git] / lib / BackupPC / CGI / EditConfig.pm
index 11ac989..df38ebd 100644 (file)
@@ -10,7 +10,7 @@
 #   Craig Barratt  <cbarratt@users.sourceforge.net>
 #
 # COPYRIGHT
-#   Copyright (C) 2004  Craig Barratt
+#   Copyright (C) 2005  Craig Barratt
 #
 #   This program is free software; you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@
 #
 #========================================================================
 #
-# Version 3.0.0alpha, released 23 Jan 2006.
+# Version 3.0.0beta2, released 11 Nov 2006.
 #
 # See http://backuppc.sourceforge.net.
 #
@@ -41,6 +41,7 @@ use BackupPC::CGI::Lib qw(:all);
 use BackupPC::Config::Meta qw(:all);
 use BackupPC::Storage;
 use Data::Dumper;
+use Encode;
 
 our %ConfigMenu = (
     server => {
@@ -181,6 +182,8 @@ our %ConfigMenu = (
                 visible => sub { return $_[0]->{XferMethod} eq "rsyncd"; } },
             {name => "RsyncShareName",
                 visible => sub { return $_[0]->{XferMethod} =~ /rsync/; } },
+            {name => "RsyncdUserName",
+                visible => sub { return $_[0]->{XferMethod} eq "rsyncd"; } },
             {name => "RsyncdPasswd",
                 visible => sub { return $_[0]->{XferMethod} eq "rsyncd"; } },
             {name => "RsyncdAuthRequired",
@@ -188,6 +191,17 @@ our %ConfigMenu = (
             {name => "RsyncCsumCacheVerifyProb",
                 visible => sub { return $_[0]->{XferMethod} =~ /rsync/; } },
 
+            {text => "CfgEdit_Title_BackupPCd_Settings",
+                visible => sub { return $_[0]->{XferMethod} eq "backuppcd"; } },
+            {name => "BackupPCdShareName",
+                visible => sub { return $_[0]->{XferMethod} eq "backuppcd"; } },
+            {name => "BackupPCdPath",
+                visible => sub { return $_[0]->{XferMethod} eq "backuppcd"; } },
+            {name => "BackupPCdCmd",
+                visible => sub { return $_[0]->{XferMethod} eq "backuppcd"; } },
+            {name => "BackupPCdRestoreCmd",
+                visible => sub { return $_[0]->{XferMethod} eq "backuppcd"; } },
+
             {text => "CfgEdit_Title_Archive_Settings",
                 visible => sub { return $_[0]->{XferMethod} eq "archive"; } },
             {name => "ArchiveDest",
@@ -268,9 +282,11 @@ our %ConfigMenu = (
            {name => "IncrKeepCnt"},
            {name => "IncrKeepCntMin"},
            {name => "IncrAgeMax"},
+           {name => "IncrLevels"},
            {name => "IncrFill"},
 
            {text => "CfgEdit_Title_Blackouts"},
+            {name => "BackupsDisable"},
             {name => "BlackoutBadPingLimit"},
             {name => "BlackoutGoodCnt"},
             {name => "BlackoutPeriods"},
@@ -307,6 +323,7 @@ our %ConfigMenu = (
            {name => "RestorePostUserCmd"},
            {name => "ArchivePreUserCmd"},
            {name => "ArchivePostUserCmd"},
+           {name => "UserCmdCheckStatus"},
        ],
     },
     hosts => {
@@ -340,14 +357,14 @@ sub action
         ErrorExit(eval("qq{$Lang->{Only_privileged_users_can_edit_config_files}}"));
     }
 
-    if ( defined($In{menu}) || $In{editAction} eq $Lang->{CfgEdit_Button_Save} ) {
+    if ( defined($In{menu}) || $In{saveAction} eq "Save" ) {
        $errors = errorCheck();
        if ( %$errors ) {
            #
            # If there are errors, then go back to the same menu
            #
-           $In{editAction} = "";
-            $In{newMenu} = "";
+           $In{saveAction} = "";
+            #$In{newMenu} = "";
        }
         if ( (my $var = $In{overrideUncheck}) ne "" ) {
             #
@@ -384,7 +401,7 @@ sub action
        $newConf = { %$mainConf, %$hostConf };
     }
 
-    if ( $In{editAction} ne $Lang->{CfgEdit_Button_Save} && $In{newMenu} ne ""
+    if ( $In{saveAction} ne "Save" && $In{newMenu} ne ""
                    && defined($ConfigMenu{$In{newMenu}}) ) {
         $menu = $In{newMenu};
     }
@@ -458,9 +475,15 @@ EOF
        $content .= eval("qq($Lang->{CfgEdit_Header_Host})");
     }
 
-    my $saveDisplay = "block";
-    $saveDisplay = "none" if ( !$In{modified}
-                          || $In{editAction} eq $Lang->{CfgEdit_Button_Save} );
+    my $saveStyle = "";
+    my $saveColor = "#ff0000";
+    
+    if ( $In{modified} && $In{saveAction} ne "Save" && !%$errors ) {
+        $saveStyle = "style=\"color:$saveColor\"";
+    } else {
+        $In{modified} = 0;
+    }
+
     #
     # Add action and host to the URL so the nav bar link is
     # highlighted
@@ -471,7 +494,7 @@ EOF
 <table border="0" cellpadding="2">
 <tr>$groupText</tr>
 <tr>
-<form method="post" name="form1" action="$url">
+<form method="post" name="editForm" action="$url">
 <input type="hidden" name="host" value="$host">
 <input type="hidden" name="menu" value="$menu">
 <input type="hidden" name="newMenu" value="">
@@ -481,65 +504,78 @@ EOF
 <input type="hidden" name="overrideUncheck" value="">
 <input type="hidden" name="addVar" value="">
 <input type="hidden" name="action" value="editConfig">
-<input type="submit" class="editSaveButton" style="display: $saveDisplay" name="editAction" value="${EscHTML($Lang->{CfgEdit_Button_Save})}">
+<input type="hidden" name="saveAction" value="">
+<input type="button" class="editSaveButton" name="editAction"
+    value="${EscHTML($Lang->{CfgEdit_Button_Save})}" $saveStyle
+    onClick="saveSubmit();">
+<p>
 
 <script language="javascript" type="text/javascript">
 <!--
 
+    function saveSubmit()
+    {
+        if ( document.editForm.modified.value != 0 ) {
+            document.editForm.saveAction.value = 'Save';
+            document.editForm.submit();
+        }
+        return false;
+    }
+
     function deleteSubmit(varName)
     {
-        document.form1.deleteVar.value = varName;
-       document.form1.modified.value = 1;
-        document.form1.submit();
+        document.editForm.deleteVar.value = varName;
+       document.editForm.modified.value = 1;
+        document.editForm.submit();
         return;
     }
 
     function insertSubmit(varName)
     {
-        document.form1.insertVar.value = varName;
-       document.form1.modified.value = 1;
-        document.form1.submit();
+        document.editForm.insertVar.value = varName;
+       document.editForm.modified.value = 1;
+        document.editForm.submit();
         return;
     }
 
     function addSubmit(varName, checkKey)
     {
         if ( checkKey
-            && eval("document.form1.addVarKey_" + varName + ".value") == "" ) {
+            && eval("document.editForm.addVarKey_" + varName + ".value") == "" ) {
             alert("New key must be non-empty");
             return;
         }
-        document.form1.addVar.value = varName;
-       document.form1.modified.value = 1;
-        document.form1.submit();
+        document.editForm.addVar.value = varName;
+       document.editForm.modified.value = 1;
+        document.editForm.submit();
         return;
     }
 
     function menuSubmit(menuName)
     {
-        document.form1.newMenu.value = menuName;
-        document.form1.submit();
+        document.editForm.newMenu.value = menuName;
+        document.editForm.submit();
     }
 
     function varChange(varName)
     {
-       document.form1.editAction.style.display = "block";
-       document.form1.modified.value = 1;
+       document.editForm.modified.value = 1;
+        document.editForm.editAction.style.color = '$saveColor';
     }
 
     function checkboxChange(varName)
     {
-       document.form1.editAction.style.display = "block";
-       document.form1.modified.value = 1;
+       document.editForm.modified.value = 1;
+        document.editForm.editAction.style.color = '$saveColor';
        // Do nothing if the checkbox is now set
-        if ( eval("document.form1.override_" + varName + ".checked") ) {
+        if ( eval("document.editForm.override_" + varName + ".checked") ) {
            return false;
        }
        var allVars = {};
        var varRE  = new RegExp("^v_z_(" + varName + ".*)");
        var origRE = new RegExp("^orig_z_(" + varName + ".*)");
-        for ( var i = 0 ; i < document.form1.elements.length ; i++ ) {
-           var e = document.form1.elements[i];
+        for ( var i = 0 ; i < document.editForm.elements.length ; i++ ) {
+           var e = document.editForm.elements[i];
            var re;
            if ( (re = varRE.exec(e.name)) != null ) {
                if ( allVars[re[1]] == null ) {
@@ -564,7 +600,7 @@ EOF
            } else {
                 // copy the original variable values
                //debugMsg("setting " + v);
-               eval("document.form1.v_z_" + v + ".value = document.form1.orig_z_" + v + ".value");
+               eval("document.editForm.v_z_" + v + ".value = document.editForm.orig_z_" + v + ".value");
             }
        }
        if ( sameShape ) {
@@ -572,17 +608,17 @@ EOF
        } else {
             // need to rebuild the form since the compound variable
             // has changed shape
-            document.form1.overrideUncheck.value = varName;
-           document.form1.submit();
+            document.editForm.overrideUncheck.value = varName;
+           document.editForm.submit();
            return false;
        }
     }
 
     function checkboxSet(varName)
     {
-       document.form1.editAction.style.display = "block";
-       document.form1.modified.value = 1;
-        eval("document.form1.override_" + varName + ".checked = 1;");
+       document.editForm.modified.value = 1;
+        document.editForm.editAction.style.color = '$saveColor';
+        eval("document.editForm.override_" + varName + ".checked = 1;");
         return false;
     }
 
@@ -615,6 +651,7 @@ EOF
 EOF
 
     my $doneParam = {};
+    my $tblContent;
 
     #
     # There is a special case of the user deleting just the field
@@ -634,7 +671,7 @@ EOF
 
     my $isError = %$errors;
 
-    if ( !$isError && $In{editAction} eq $Lang->{CfgEdit_Button_Save} ) {
+    if ( !$isError && $In{saveAction} eq "Save" ) {
         my($mesg, $err);
        if ( $host ne "" ) {
            $hostConf = $bpc->ConfigDataRead($host) if ( !defined($hostConf) );
@@ -690,16 +727,17 @@ EOF
             $newConf->{Hosts} = $hostsSave;
        }
         if ( defined($err) ) {
-            $content .= <<EOF;
+            $tblContent .= <<EOF;
 <tr><td colspan="2" class="border"><span class="editError">$err</span></td></tr>
 EOF
         }
         $bpc->ServerConnect();
         if ( $mesg ne "" ) {
             (my $mesgBR = $mesg) =~ s/\n/<br>\n/g;
-            $content .= <<EOF;
-<tr><td colspan="2" class="border"><span class="editComment">$mesgBR</span></td></tr>
-EOF
+             # uncomment this if you want the changes to be displayed
+#            $tblContent .= <<EOF;
+#<tr><td colspan="2" class="border"><span class="editComment">$mesgBR</span></td></tr>
+#EOF
             foreach my $str ( split(/\n/, $mesg) ) {
                 $bpc->ServerMesg("log $str") if ( $str ne "" );
             }
@@ -726,7 +764,7 @@ EOF
 
        if ( defined($paramInfo->{text}) ) {
             my $text = eval("qq($Lang->{$paramInfo->{text}})");
-           $content .= <<EOF;
+           $tblContent .= <<EOF;
 <tr><td colspan="2" class="editHeader">$text</td></tr>
 EOF
            next;
@@ -742,7 +780,7 @@ EOF
 
         $doneParam->{$param} = 1;
 
-        $content .= fieldEditBuild($ConfigMeta{$param},
+        $tblContent .= fieldEditBuild($ConfigMeta{$param},
                               $param,
                               $newConf->{$param},
                               $errors,
@@ -756,23 +794,32 @@ EOF
         if ( defined($paramInfo->{comment}) ) {
             my $topDir = $bpc->TopDir;
             my $text = eval("qq($Lang->{$paramInfo->{comment}})");
-           $content .= <<EOF;
+           $tblContent .= <<EOF;
 <tr><td colspan="2" class="editComment">$text</td></tr>
 EOF
         }
     }
 
     #
-    # Emit any remaining errors - should not happen
+    # Emit a summary of all the errors
     #
+    my $errorTxt;
+
+    if ( %$errors ) {
+       $errorTxt .= <<EOF;
+<tr><td colspan="2" class="border"><span class="editError">$Lang->{CfgEdit_Error_No_Save}</span></td></tr>
+EOF
+    }
+
     foreach my $param ( sort(keys(%$errors)) ) {
-       $content .= <<EOF;
+       $errorTxt .= <<EOF;
 <tr><td colspan="2" class="border"><span class="editError">$errors->{$param}</span></td></tr>
 EOF
-       delete($errors->{$param});
     }
 
     $content .= <<EOF;
+$errorTxt
+$tblContent
 </table>
 EOF
 
@@ -798,9 +845,8 @@ EOF
         $doneParam->{$param} = 1;
     }
 
-    if ( defined($In{menu}) || $In{editAction} eq $Lang->{CfgEdit_Button_Save} ) {
-        if ( $In{editAction} eq $Lang->{CfgEdit_Button_Save}
-                && !$userHost ) {
+    if ( defined($In{menu}) || $In{saveAction} eq "Save" ) {
+        if ( $In{saveAction} eq "Save" && !$userHost ) {
             #
             # Emit the new settings as orig_z_ parameters
             #
@@ -825,8 +871,9 @@ EOF
             #
             foreach my $var ( keys(%In) ) {
                 next if ( $var !~ /^orig_z_/ );
+                my $val = decode_utf8($In{$var});
                 $contentHidden .= <<EOF;
-<input type="hidden" name="$var" value="${EscHTML($In{$var})}">
+<input type="hidden" name="$var" value="${EscHTML($val)}">
 EOF
             }
        }
@@ -930,7 +977,7 @@ sub fieldEditBuild
     $size = $type->{size} if ( defined($type->{size}) );
 
     #
-    # These fragments allow inline conent to be turned on and off
+    # These fragments allow inline content to be turned on and off
     #
     # <tr><td colspan="2"><span id="id_$varName" style="display: none" class="editComment">$comment</span></td></tr>
     # <tr><td class="border"><a href="javascript: displayHelp('$varName')">$varName</a>
@@ -987,6 +1034,7 @@ EOF
             $In{addVar} = "";
         }
         $content .= "<table border=\"1\" cellspacing=\"0\">\n";
+        my $colspan;
 
         if ( ref($type) eq "HASH" && ref($type->{child}) eq "HASH"
                     && $type->{child}{type} eq "horizHash" ) {
@@ -998,8 +1046,9 @@ EOF
             }
             $content .= "<tr><td class=\"border\"></td>\n";
             for ( my $i = 0 ; $i < @order ; $i++ ) {
-                $content .= "<td>$order[$i]</td>\n";
+                $content .= "<td class=\"tableheader\">$order[$i]</td>\n";
             }
+            $colspan = @order + 1;
             $content .= "</tr>\n";
             for ( my $i = 0 ; $i < @$varValue ; $i++ ) {
                 if ( @$varValue > 1 || $type->{emptyOk} ) {
@@ -1036,9 +1085,10 @@ EOF
                                     $overrideVar, $overrideSet);
                 $content .= "</tr>\n";
             }
+            $colspan = 2;
         }
         $content .= <<EOF;
-<tr><td class="border"><input type="button" name="add_$varName" value="${EscHTML($Lang->{CfgEdit_Button_Add})}"
+<tr><td class="border" colspan="$colspan"><input type="button" name="add_$varName" value="${EscHTML($Lang->{CfgEdit_Button_Add})}"
     onClick="addSubmit('$varName')"></td></tr>
 </table>
 EOF
@@ -1111,7 +1161,7 @@ EOF
         if ( !$type->{noKeyEdit} ) {
             $content .= <<EOF;
 <tr><td class="border" colspan="2">
-New key: <input type="text" name="addVarKey_$varName" size="20" maxlength="256" value="">
+$Lang->{CfgEdit_Button_New_Key}: <input type="text" class="editTextInput" name="addVarKey_$varName" size="20" maxlength="256" value="">
 <input type="button" name="add_$varName" value="${EscHTML($Lang->{CfgEdit_Button_Add})}" onClick="addSubmit('$varName', 1)">
 </td></tr>
 EOF
@@ -1162,7 +1212,6 @@ EOF
             $content .= <<EOF;
 <span class="editError">$errors->{$varName}</span><br>
 EOF
-           delete($errors->{$varName});
         }
         my $onChange;
        if ( defined($overrideVar) ) {
@@ -1171,7 +1220,7 @@ EOF
             $onChange .= "varChange('$overrideVar');";
        }
         if ( $onchangeSubmit ) {
-            $onChange .= "document.form1.submit();";
+            $onChange .= "document.editForm.submit();";
         }
        if ( $onChange ne "" ) {
             $onChange = " onChange=\"$onChange\"";
@@ -1189,7 +1238,7 @@ EOF
            }
             my $textType = ($varName =~ /Passwd/) ? "password" : "text";
             $content .= <<EOF;
-<input type="$textType" name="v_z_$varName" size="$size" maxlength="256" value="${EscHTML($varValue)}"$onChange>
+<input type="$textType" class="editTextInput" name="v_z_$varName" size="$size" maxlength="256" value="${EscHTML($varValue)}"$onChange>
 EOF
         } elsif ( $type->{type} eq "boolean" ) {
             # checkbox
@@ -1211,7 +1260,7 @@ EOF
            my $rowCnt = $varValue =~ tr/\n//;
            $rowCnt = 1 if ( $rowCnt < 1 );
             $content .= <<EOF;
-<textarea name="v_z_$varName" cols="$size" rows="$rowCnt"$onChange>${EscHTML($varValue)}</textarea>
+<textarea name="v_z_$varName" class="editTextArea" cols="$size" rows="$rowCnt"$onChange>${EscHTML($varValue)}</textarea>
 EOF
         }
         $content .= "</td>\n";
@@ -1382,21 +1431,40 @@ sub fieldInputParse
             return 1;
         }
 
+        my $v = $In{"v_z_$varName"};
+
         if ( $type->{type} eq "integer" ) {
-            $$value = 0 + $In{"v_z_$varName"};
+            if ( $v =~ /^-?\d+\s*$/s || $v eq "" ) {
+                $$value = 0 + $v;
+            } else {
+                # error value - keep in string form
+                $$value = $v;
+            }
         } elsif ( $type->{type} eq "float" ) {
-            $$value = 0 + $In{"v_z_$varName"};
+            if ( $v =~ /^-?\d*(\.\d*)?\s*$/s || $v eq "" ) {
+                $$value = 0 + $v;
+            } else {
+                # error value - keep in string form
+                $$value = $v;
+            }
         } elsif ( $type->{type} eq "shortlist" ) {
-            $$value = [split(/[,\s]+/, $In{"v_z_$varName"})];
-            if ( $type->{child} eq "float"
-                    || $type->{child} eq "integer"
-                    || $type->{child} eq "boolean" ) {
+            $$value = [split(/[,\s]+/, $v)];
+            if ( $type->{child} eq "float" ) {
+                foreach ( @$$value ) {
+                    if ( /^-?\d*(\.\d*)?\s*$/s || $v eq "" ) {
+                        $_ += 0;
+                    }
+                }
+            } elsif ( $type->{child} eq "integer"
+                        || $type->{child} eq "boolean" ) {
                 foreach ( @$$value ) {
-                    $_ += 0;
+                    if ( /^-?\d+\s*$/s || $v eq "" ) {
+                        $_ += 0;
+                    }
                 }
             }
         } else {
-            $$value = $In{"v_z_$varName"};
+            $$value = decode_utf8($In{"v_z_$varName"});
             $$value =~ s/\r\n/\n/g;
         }
         $$value = undef if ( $type->{undefIfEmpty} && $$value eq "" );
@@ -1449,12 +1517,15 @@ sub configDiffMesg
 
             (my $valueNew2 = $valueNew) =~ s/['\n\r]//g;
             (my $valueOld2 = $valueOld) =~ s/['\n\r]//g;
+
+            next if ( $valueOld2 eq $valueNew2 );
+
             $valueNew =~ s/\n/\\n/g;
             $valueOld =~ s/\n/\\n/g;
             $valueNew =~ s/\r/\\r/g;
             $valueOld =~ s/\r/\\r/g;
-            $mesg .= eval("qq($Lang->{CfgEdit_Log_Change_param_value})")
-                                    if ( $valueOld2 ne $valueNew2 );
+
+            $mesg .= eval("qq($Lang->{CfgEdit_Log_Change_param_value})");
         }
     }
     return $mesg;