changeset 782:1311f31713ba

Better EOL handling and error reporting in php-admin (Franky Van Liedekerke)
author Ben Schmidt
date Mon, 15 Nov 2010 21:07:50 +1100
parents 563b513fae21
children 44778d21edad
files ChangeLog contrib/web/php-admin/htdocs/edit.php contrib/web/php-admin/htdocs/save.php
diffstat 3 files changed, 31 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Nov 15 11:12:50 2010 +1100
+++ b/ChangeLog	Mon Nov 15 21:07:50 2010 +1100
@@ -1,3 +1,5 @@
+ o Better end-of-line handling and error reporting in php-admin (Franky Van
+   Liedekerke)
  o Avoid losing mail when connecting to relayhost fails
  o Improved and more consistent closing of SMTP sessions in error cases
  o Check the relayhost gives a reply before reading it to avoid a crash
--- a/contrib/web/php-admin/htdocs/edit.php	Mon Nov 15 11:12:50 2010 +1100
+++ b/contrib/web/php-admin/htdocs/edit.php	Mon Nov 15 21:07:50 2010 +1100
@@ -58,6 +58,9 @@
        $value = $lines[0];
     }
     
+    // remove trailing \n if any, just to be sure
+    $value = preg_replace('/\n$/',"",$value);
+    
     $tpl->assign(array("NAME" => htmlentities($name),
 		       "NICENAME" => htmlentities($nicename),
 		       "TEXT" => htmlentities($text),
@@ -76,6 +79,10 @@
     if(is_file($file))
        $value = file_get_contents($file);
 
+    // the last \n would result in an extra empty line in the list box,
+    // so we remove it
+    $value = preg_replace('/\n$/',"",$value);
+
     $tpl->assign(array("NAME" => htmlentities($name),
 		       "NICENAME" => htmlentities($nicename),
 		       "TEXT" => htmlentities($text),
--- a/contrib/web/php-admin/htdocs/save.php	Mon Nov 15 11:12:50 2010 +1100
+++ b/contrib/web/php-admin/htdocs/save.php	Mon Nov 15 21:07:50 2010 +1100
@@ -43,7 +43,10 @@
           die("Couldn't chmod ".$file);
     }
     else {
-       @unlink($file);
+       if (file_exists($file)) {
+          if (!unlink($file))
+             die("Couldn't unlink ".$file);
+       }
     }
 }
 
@@ -58,21 +61,34 @@
 
     $file = $topdir."/".$list."/control/".$name;
     
-    if(isset($_POST[$name]) && !empty($_POST[$name]))
+    if(isset($_POST[$name]) && !empty($_POST[$name]) && !preg_match('/^\s*$/',$_POST[$name]))
     {
+       // remove all \r
+       $_POST[$name]=preg_replace('/\r/',"",$_POST[$name]);
+
+       // no trailing \n?, then we add one
+       if (!preg_match('/\n$/',$_POST[$name]))
+          $_POST[$name].="\n";
+
+       // we don't like whitespace before a \n
+       $_POST[$name]=preg_replace('/\s*\n/',"\n",$_POST[$name]);
+
        if (!$fp = fopen($file, "w"))
           die("Couldn't open ".$file." for writing");
 
-       fwrite($fp, preg_replace('/\\r/',"",$_POST[$name]));
+       // write the result in a file
+       fwrite($fp, $_POST[$name]);
        fclose($fp);
 
        if (!chmod($file, 0644))
           die("Couldn't chmod ".$file);
     }
     else {
-       @unlink($file);
+       if (file_exists($file)) {
+          if (!unlink($file))
+             die("Couldn't unlink ".$file);
     }
-    
+    }
 }
 
 // Perl's encode_entities (to be able to use tunables.pl)