5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls
15 * TSafeHtml is a control that strips down all potentially dangerous
16 * HTML content. It is mainly a wrapper of {@link http://pear.php.net/package/SafeHTML SafeHTML}
17 * project. According to the SafeHTML project, it tries to safeguard
18 * the following situations when the string is to be displayed to end-users,
19 * - Opening tag without its closing tag
20 * - closing tag without its opening tag
21 * - any of these tags: base, basefont, head, html, body, applet, object,
22 * iframe, frame, frameset, script, layer, ilayer, embed, bgsound, link,
23 * meta, style, title, blink, xml, etc.
24 * - any of these attributes: on*, data*, dynsrc
25 * - javascript:/vbscript:/about: etc. protocols
26 * - expression/behavior etc. in styles
27 * - any other active content.
29 * To use TSafeHtml, simply enclose the content to be secured within
30 * the body of TSafeHtml in a template.
32 * If the content is encoded in UTF-7, you'll need to enable the {@link setRepackUTF7 RepackUTF7} property
33 * to ensure the contents gets parsed correctly.
35 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
36 * @package System.Web.UI.WebControls
39 class TSafeHtml extends TControl
42 * Sets whether to parse the contents as UTF-7. This property enables a routine
43 * that repacks the content as UTF-7 before parsing it. Defaults to false.
44 * @param boolean whether to parse the contents as UTF-7
46 public function setRepackUTF7($value)
48 $this->setViewState('RepackUTF7',TPropertyValue::ensureBoolean($value),false);
52 * @return boolean whether to parse the contents as UTF-7. Defaults to false.
54 public function getRepackUTF7()
56 return $this->getViewState('RepackUTF7',false);
60 * Renders body content.
61 * This method overrides parent implementation by removing
62 * malicious javascript code from the body content
63 * @param THtmlWriter writer
65 public function render($writer)
67 $htmlWriter = Prado::createComponent($this->GetResponse()->getHtmlWriterType(), new TTextWriter());
68 parent::render($htmlWriter);
69 $writer->write($this->parseSafeHtml($htmlWriter->flush()));
73 * Use SafeHTML to remove malicous javascript from the HTML content.
74 * @param string HTML content
75 * @return string safer HTML content
77 protected function parseSafeHtml($text)
79 $renderer = Prado::createComponent('System.3rdParty.SafeHtml.TSafeHtmlParser');
80 return $renderer->parse($text, $this->getRepackUTF7());