From: Ben Schmidt Date: Mon, 15 Nov 2010 10:07:50 +0000 (+1100) Subject: Better EOL handling and error reporting in php-admin (Franky Van Liedekerke) X-Git-Tag: RELEASE_1_2_18a1~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d8f8b36d52ec5c670e4b1703b98e976bc2a11fb;p=thirdparty%2Fmlmmj.git Better EOL handling and error reporting in php-admin (Franky Van Liedekerke) --- diff --git a/ChangeLog b/ChangeLog index 8f76c404..313deeb9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/contrib/web/php-admin/htdocs/edit.php b/contrib/web/php-admin/htdocs/edit.php index 77871468..21791828 100644 --- a/contrib/web/php-admin/htdocs/edit.php +++ b/contrib/web/php-admin/htdocs/edit.php @@ -57,6 +57,9 @@ function mlmmj_string($name, $nicename, $text) $lines = file($file); $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), @@ -76,6 +79,10 @@ function mlmmj_list($name, $nicename, $text) 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), diff --git a/contrib/web/php-admin/htdocs/save.php b/contrib/web/php-admin/htdocs/save.php index 2867eea1..73f249f6 100644 --- a/contrib/web/php-admin/htdocs/save.php +++ b/contrib/web/php-admin/htdocs/save.php @@ -43,14 +43,17 @@ function mlmmj_boolean($name, $nicename, $text) die("Couldn't chmod ".$file); } else { - @unlink($file); + if (file_exists($file)) { + if (!unlink($file)) + die("Couldn't unlink ".$file); + } } } function mlmmj_string ($name, $nicename, $text) { mlmmj_list($name, $nicename, $text); -} +} function mlmmj_list($name, $nicename, $text) { @@ -58,21 +61,34 @@ function mlmmj_list($name, $nicename, $text) $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)