Re: QuickForm : Usage of QF textarea without htmlspecialchars()

From: Date: Sun, 15 Apr 2007 18:35:20 +0000
Subject: Re: QuickForm : Usage of QF textarea without htmlspecialchars()
References: 1 2 3  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27049@lists.php.net to get a copy of this message
Please send responses to the list. On 4/15/07, James PIC <jamespic@gmail.com> wrote:
What do you mean 'the other hand', for the moment i made a patch to give me the possibility to allow html in certain elements, it's clean but it will break if the sys admin updates the package ...
Why do you want to allow HTML in a textarea?
--- /usr/share/php/HTML/QuickForm/textarea.php 2004-02-28 23:10: 16.000000000 +0100 +++ /usr/share/php/HTML/QuickForm/textarea.php 2007-04-15 20:13:17.000000000 +0200 @@ -42,6 +42,13 @@
      */
     var $_value = null;
+    /**
+     * Wheter HTML is allowed or not : subject to XSS vulnerabilities, safe
usage is private usage.
+     * @var       boolean
+     * @access    public
+     */
+    var $allow_HTML=false;
+
     // }}}
     // {{{ constructor
@@ -55,9 +62,12 @@
      * @access    public
      * @return    void
      */
-    function HTML_QuickForm_textarea($elementName=null,
$elementLabel=null, $attributes=null)
+    function HTML_QuickForm_textarea($elementName=null,
$elementLabel=null, $attributes=null, $allowHTML=null)
     {
- HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes); + HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes, $allowHTML);
+        if ($allowHTML) {
+            $this->allow_HTML = true;
+        }
         $this->_persistantFreeze = true;
         $this->_type = 'textarea';
     } //end constructor
@@ -187,10 +197,13 @@
         if ($this->_flagFrozen) {
             return $this->getFrozenHtml();
         } else {
+            if (!$this->allow_HTML) {
+                $this->_value =
htmlspecialchars($this->_value);
+            }
             return $this->_getTabs() .
                    '<textarea' .
$this->_getAttrString($this->_attributes) . '>' .
                    // because we wrap the form later we don't want the text
indented
-                   preg_replace("/(\r\n|\n|\r)/", '&#010;',
htmlspecialchars($this->_value)) .
+                   preg_replace("/(\r\n|\n|\r)/", '&#010;', $this->_value)
.
                    '</textarea>';
         }
     } //end func toHtml
@@ -207,11 +220,13 @@
      */
     function getFrozenHtml()
     {
-        $value = htmlspecialchars($this->getValue());
+        if (!$this->allow_HTML) {
+            $this->_value =
htmlspecialchars($this->_value);
+        }
         if ($this->getAttribute('wrap') == 'off') {
-            $html = $this->_getTabs() . '<pre>' . $value."</pre>\n";
+            $html = $this->_getTabs() . '<pre>' . $this->_value."</pre>\n";
         } else {
-            $html = nl2br($value)."\n";
+            $html = nl2br($this->_value)."\n";
         }
         return $html . $this->_getPersistantData();
     } //end func getFrozenHtml
--- /usr/share/php/HTML/QuickForm/element.php    2006-10-07
22:12:17.000000000 +0200
+++ /usr/share/php/HTML/QuickForm/element.php    2007-04-15
20:09: 32.000000000 +0200 @@ -66,6 +66,13 @@
      * @access    private
      */
     var $_persistantFreeze = false;
+
+     /**
+     * Wheter HTML is allowed or not : subject to XSS vulnerabilities, safe
usage is private usage.
+     * @var       boolean
+     * @access    public
+     */
+    var $allow_HTML=false;
     // }}}
     // {{{ constructor
@@ -225,8 +232,8 @@
      */
     function getFrozenHtml()
     {
-        $value = $this->getValue();
-        return ('' != $value? htmlspecialchars($value): '&nbsp;') .
+        $value = ($this->allow_HTML) ? $this->getValue() :
htmlspecialchars($this->getValue());
+        return ('' != $value? $value: '&nbsp;') .
                $this->_getPersistantData();
     } //end func getFrozenHtml
On 4/15/07, Justin Patrin <papercrane@gmail.com> wrote: On 4/15/07, James PIC <jamespic@gmail.com> wrote:
Hello everybody, I am a newbie, is-it possible to use QF textareas values without having
it
being passed through htmlspecialchars(), for example in an HTML form ? Thank you very much for your support.
You could possibly extend the class and make your own that doesn't escape its output but I'd suggest you instead fix the other end. You shouldn't be escaping the data before it makes it to the form. -- Justin Patrin --
-- Justin Patrin

« previous php.pear.general (#27049) next »