CHtmlPurifier.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * CHtmlPurifier class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. if(!class_exists('HTMLPurifier_Bootstrap',false))
  11. {
  12. require_once(Yii::getPathOfAlias('system.vendors.htmlpurifier').DIRECTORY_SEPARATOR.'HTMLPurifier.standalone.php');
  13. HTMLPurifier_Bootstrap::registerAutoload();
  14. }
  15. /**
  16. * CHtmlPurifier is wrapper of {@link http://htmlpurifier.org HTML Purifier}.
  17. *
  18. * CHtmlPurifier removes all malicious code (better known as XSS) with a thoroughly audited,
  19. * secure yet permissive whitelist. It will also make sure the resulting code
  20. * is standard-compliant.
  21. *
  22. * CHtmlPurifier can be used as either a widget or a controller filter.
  23. *
  24. * Note: since HTML Purifier is a big package, its performance is not very good.
  25. * You should consider either caching the purification result or purifying the user input
  26. * before saving to database.
  27. *
  28. * Usage as a class:
  29. * <pre>
  30. * $p = new CHtmlPurifier();
  31. * $p->options = array('URI.AllowedSchemes'=>array(
  32. * 'http' => true,
  33. * 'https' => true,
  34. * ));
  35. * $text = $p->purify($text);
  36. * </pre>
  37. *
  38. * Usage as validation rule:
  39. * <pre>
  40. * array('text','filter','filter'=>array($obj=new CHtmlPurifier(),'purify')),
  41. * </pre>
  42. *
  43. * @author Qiang Xue <qiang.xue@gmail.com>
  44. * @package system.web.widgets
  45. * @since 1.0
  46. */
  47. class CHtmlPurifier extends COutputProcessor
  48. {
  49. /**
  50. * @var object the HTML Purifier instance.
  51. */
  52. private $_purifier;
  53. /**
  54. * @var mixed the options to be passed to HTML Purifier instance.
  55. * This can be a HTMLPurifier_Config object, an array of directives (Namespace.Directive => Value)
  56. * or the filename of an ini file.
  57. * @see http://htmlpurifier.org/live/configdoc/plain.html
  58. */
  59. private $_options=null;
  60. /**
  61. * Processes the captured output.
  62. * This method purifies the output using {@link http://htmlpurifier.org HTML Purifier}.
  63. * @param string $output the captured output to be processed
  64. */
  65. public function processOutput($output)
  66. {
  67. $output=$this->purify($output);
  68. parent::processOutput($output);
  69. }
  70. /**
  71. * Purifies the HTML content by removing malicious code.
  72. * @param mixed $content the content to be purified.
  73. * @return mixed the purified content
  74. */
  75. public function purify($content)
  76. {
  77. if(is_array($content))
  78. $content=array_map(array($this,'purify'),$content);
  79. else
  80. $content=$this->getPurifier()->purify($content);
  81. return $content;
  82. }
  83. /**
  84. * Set the options for HTML Purifier and create a new HTML Purifier instance based on these options.
  85. * @param mixed $options the options for HTML Purifier
  86. * @return static the object instance itself
  87. */
  88. public function setOptions($options)
  89. {
  90. $this->_options=$options;
  91. $this->createNewHtmlPurifierInstance();
  92. return $this;
  93. }
  94. /**
  95. * Get the options for the HTML Purifier instance.
  96. * @return mixed the HTML Purifier instance options
  97. */
  98. public function getOptions()
  99. {
  100. return $this->_options;
  101. }
  102. /**
  103. * Get the HTML Purifier instance or create a new one if it doesn't exist.
  104. * @return HTMLPurifier
  105. */
  106. protected function getPurifier()
  107. {
  108. if($this->_purifier!==null)
  109. return $this->_purifier;
  110. return $this->createNewHtmlPurifierInstance();
  111. }
  112. /**
  113. * Create a new HTML Purifier instance.
  114. * @return HTMLPurifier
  115. */
  116. protected function createNewHtmlPurifierInstance()
  117. {
  118. $this->_purifier=new HTMLPurifier($this->getOptions());
  119. $this->_purifier->config->set('Cache.SerializerPath',Yii::app()->getRuntimePath());
  120. return $this->_purifier;
  121. }
  122. }