]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TSafeHtml.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TSafeHtml.php
1 <?php
2 /**
3  * TSafeHtml class file
4  *
5  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * TSafeHtml class
14  *
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.
28  *
29  * To use TSafeHtml, simply enclose the content to be secured within
30  * the body of TSafeHtml in a template.
31  *
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.
34  *
35  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
36  * @package System.Web.UI.WebControls
37  * @since 3.0
38  */
39 class TSafeHtml extends TControl
40 {
41         /**
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
45          */
46         public function setRepackUTF7($value)
47         {
48                 $this->setViewState('RepackUTF7',TPropertyValue::ensureBoolean($value),false);
49         }
50
51         /**
52          * @return boolean whether to parse the contents as UTF-7. Defaults to false.
53          */
54         public function getRepackUTF7()
55         {
56                 return $this->getViewState('RepackUTF7',false);
57         }
58
59         /**
60          * Renders body content.
61          * This method overrides parent implementation by removing
62          * malicious javascript code from the body content
63          * @param THtmlWriter writer
64          */
65         public function render($writer)
66         {
67                 $htmlWriter = Prado::createComponent($this->GetResponse()->getHtmlWriterType(), new TTextWriter());
68                 parent::render($htmlWriter);
69                 $writer->write($this->parseSafeHtml($htmlWriter->flush()));
70         }
71
72         /**
73          * Use SafeHTML to remove malicous javascript from the HTML content.
74          * @param string HTML content
75          * @return string safer HTML content
76          */
77         protected function parseSafeHtml($text)
78         {
79                 $renderer = Prado::createComponent('System.3rdParty.SafeHtml.TSafeHtmlParser');
80                 return $renderer->parse($text, $this->getRepackUTF7());
81         }
82 }
83