From 2a3e3b2fedf6eff17e083d27e4c5e371c18b628d Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Sun, 12 Mar 2006 09:28:12 +0000
Subject: [PATCH] - Patch #53480 by chx et al: clean and harden form_builder
 for various types of posted elements.

---
 includes/form.inc | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/includes/form.inc b/includes/form.inc
index 9ad1e79dc199..e028edbd78c4 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -323,12 +323,24 @@ function form_builder($form_id, $form) {
     $form['#ref'] = &$ref;
     if (!isset($form['#value'])) {
       if ($posted) {
-        if (isset($edit)) {
-          $form['#value'] = $edit; // normal element
-          $form['#needs_validation'] = TRUE;
+        switch ($form['#type']) {
+          case 'checkbox':
+            $form['#value'] = isset($edit) ? $form['#return_value'] : 0;
+            break;
+          case 'select':
+            $form['#value'] = isset($edit) ? $edit : array();
+            break;
+          case 'textfield':
+            if (isset($edit)) {
+              $form['#value'] = str_replace(array("\r", "\n"), '', $edit);
+            }
+            break;
+          default:
+            if (isset($edit)) {
+              $form['#value'] = $edit;
+            }
         }
-        elseif (isset($form['#return_value'])) {
-          $form['#value'] = '0'; // checkbox unchecked
+        if (isset($form['#value'])) {
           $form['#needs_validation'] = TRUE;
         }
       }
-- 
GitLab