From eea3cd54daeb8cb323b8a520856c9ddf1c83556d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Wed, 28 Sep 2016 16:49:49 +0200
Subject: [PATCH] =?UTF-8?q?Fixes=20#3587=20Replacing=20CRLF=20by=20LF?=
 =?UTF-8?q?=C2=A0in=20TextAreaAttribute=20and=20FileTextAreaAttribute?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 include/simpleplugin/class_attribute.inc | 26 +++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/include/simpleplugin/class_attribute.inc b/include/simpleplugin/class_attribute.inc
index a1e38c244..ab7a6a302 100644
--- a/include/simpleplugin/class_attribute.inc
+++ b/include/simpleplugin/class_attribute.inc
@@ -149,14 +149,21 @@ class Attribute
     $this->manageAttributes($this->value);
   }
 
+  /*! \brief Set the postValue */
   function setPostValue ($value)
   {
     if ($this->isVisible()) {
-      $this->postValue = $value;
+      $this->postValue = $this->fixPostValue($value);
       $this->manageAttributes($this->postValue);
     }
   }
 
+  /*! \brief In case a treatment is needed on POST content */
+  function fixPostValue ($value)
+  {
+    return $value;
+  }
+
   /*! \brief Reset this attribute to its default value
    */
   function resetToDefault ()
@@ -864,6 +871,12 @@ class StringAttribute extends Attribute
     return $this->renderAcl($display);
   }
 
+  function fixPostValue ($value)
+  {
+    /* Replace CRLF by LF, to avoid non-ASCII chars in multiline values (mainly useful for textarea) */
+    return str_replace(array("\r\n", "\r"), "\n", $value);
+  }
+
   function check ()
   {
     $error = parent::check();
@@ -1614,10 +1627,11 @@ class FileAttribute extends Attribute
    */
   function readFile($handle)
   {
-    $this->postValue = fread($handle, 1024);
+    $postValue = fread($handle, 1024);
     while (!feof($handle)) {
-      $this->postValue .= fread($handle, 1024);
+      $postValue .= fread($handle, 1024);
     }
+    $this->setPostValue($postValue);
     @fclose($handle);
   }
 
@@ -1758,6 +1772,12 @@ class FileTextAreaAttribute extends FileDownloadAttribute
     return $this->renderAcl($display).parent::renderFormInput();
   }
 
+  function fixPostValue ($value)
+  {
+    /* Replace CRLF by LF, to avoid non-ASCII chars */
+    return str_replace(array("\r\n", "\r"), "\n", $value);
+  }
+
   public function htmlIds()
   {
     $ids    = parent::htmlIds();
-- 
GitLab