mlmmj

changeset 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 diff
     1.1 --- a/ChangeLog	Mon Nov 15 11:12:50 2010 +1100
     1.2 +++ b/ChangeLog	Mon Nov 15 21:07:50 2010 +1100
     1.3 @@ -1,3 +1,5 @@
     1.4 + o Better end-of-line handling and error reporting in php-admin (Franky Van
     1.5 +   Liedekerke)
     1.6   o Avoid losing mail when connecting to relayhost fails
     1.7   o Improved and more consistent closing of SMTP sessions in error cases
     1.8   o Check the relayhost gives a reply before reading it to avoid a crash
     2.1 --- a/contrib/web/php-admin/htdocs/edit.php	Mon Nov 15 11:12:50 2010 +1100
     2.2 +++ b/contrib/web/php-admin/htdocs/edit.php	Mon Nov 15 21:07:50 2010 +1100
     2.3 @@ -58,6 +58,9 @@
     2.4         $value = $lines[0];
     2.5      }
     2.6      
     2.7 +    // remove trailing \n if any, just to be sure
     2.8 +    $value = preg_replace('/\n$/',"",$value);
     2.9 +    
    2.10      $tpl->assign(array("NAME" => htmlentities($name),
    2.11  		       "NICENAME" => htmlentities($nicename),
    2.12  		       "TEXT" => htmlentities($text),
    2.13 @@ -76,6 +79,10 @@
    2.14      if(is_file($file))
    2.15         $value = file_get_contents($file);
    2.16  
    2.17 +    // the last \n would result in an extra empty line in the list box,
    2.18 +    // so we remove it
    2.19 +    $value = preg_replace('/\n$/',"",$value);
    2.20 +
    2.21      $tpl->assign(array("NAME" => htmlentities($name),
    2.22  		       "NICENAME" => htmlentities($nicename),
    2.23  		       "TEXT" => htmlentities($text),
     3.1 --- a/contrib/web/php-admin/htdocs/save.php	Mon Nov 15 11:12:50 2010 +1100
     3.2 +++ b/contrib/web/php-admin/htdocs/save.php	Mon Nov 15 21:07:50 2010 +1100
     3.3 @@ -43,7 +43,10 @@
     3.4            die("Couldn't chmod ".$file);
     3.5      }
     3.6      else {
     3.7 -       @unlink($file);
     3.8 +       if (file_exists($file)) {
     3.9 +          if (!unlink($file))
    3.10 +             die("Couldn't unlink ".$file);
    3.11 +       }
    3.12      }
    3.13  }
    3.14  
    3.15 @@ -58,21 +61,34 @@
    3.16  
    3.17      $file = $topdir."/".$list."/control/".$name;
    3.18      
    3.19 -    if(isset($_POST[$name]) && !empty($_POST[$name]))
    3.20 +    if(isset($_POST[$name]) && !empty($_POST[$name]) && !preg_match('/^\s*$/',$_POST[$name]))
    3.21      {
    3.22 +       // remove all \r
    3.23 +       $_POST[$name]=preg_replace('/\r/',"",$_POST[$name]);
    3.24 +
    3.25 +       // no trailing \n?, then we add one
    3.26 +       if (!preg_match('/\n$/',$_POST[$name]))
    3.27 +          $_POST[$name].="\n";
    3.28 +
    3.29 +       // we don't like whitespace before a \n
    3.30 +       $_POST[$name]=preg_replace('/\s*\n/',"\n",$_POST[$name]);
    3.31 +
    3.32         if (!$fp = fopen($file, "w"))
    3.33            die("Couldn't open ".$file." for writing");
    3.34  
    3.35 -       fwrite($fp, preg_replace('/\\r/',"",$_POST[$name]));
    3.36 +       // write the result in a file
    3.37 +       fwrite($fp, $_POST[$name]);
    3.38         fclose($fp);
    3.39  
    3.40         if (!chmod($file, 0644))
    3.41            die("Couldn't chmod ".$file);
    3.42      }
    3.43      else {
    3.44 -       @unlink($file);
    3.45 +       if (file_exists($file)) {
    3.46 +          if (!unlink($file))
    3.47 +             die("Couldn't unlink ".$file);
    3.48      }
    3.49 -    
    3.50 +    }
    3.51  }
    3.52  
    3.53  // Perl's encode_entities (to be able to use tunables.pl)