You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\helpers;
  8. /**
  9. * BaseHtmlPurifier provides concrete implementation for [[HtmlPurifier]].
  10. *
  11. * Do not use BaseHtmlPurifier. Use [[HtmlPurifier]] instead.
  12. *
  13. * @author Alexander Makarov <sam@rmcreative.ru>
  14. * @since 2.0
  15. */
  16. class BaseHtmlPurifier
  17. {
  18. /**
  19. * Passes markup through HTMLPurifier making it safe to output to end user
  20. *
  21. * @param string $content The HTML content to purify
  22. * @param array|\Closure|null $config The config to use for HtmlPurifier.
  23. * If not specified or `null` the default config will be used.
  24. * You can use an array or an anonymous function to provide configuration options:
  25. *
  26. * - An array will be passed to the `HTMLPurifier_Config::create()` method.
  27. * - An anonymous function will be called after the config was created.
  28. * The signature should be: `function($config)` where `$config` will be an
  29. * instance of `HTMLPurifier_Config`.
  30. *
  31. * Here is a usage example of such a function:
  32. *
  33. * ~~~
  34. * // Allow the HTML5 data attribute `data-type` on `img` elements.
  35. * $content = HtmlPurifier::process($content, function($config) {
  36. * $config->getHTMLDefinition(true)
  37. * ->addAttribute('img', 'data-type', 'Text');
  38. * });
  39. * ~~~
  40. *
  41. * @return string the purified HTML content.
  42. */
  43. public static function process($content, $config = null)
  44. {
  45. $configInstance = \HTMLPurifier_Config::create($config instanceof \Closure ? null : $config);
  46. $configInstance->autoFinalize = false;
  47. $purifier=\HTMLPurifier::instance($configInstance);
  48. $purifier->config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
  49. if ($config instanceof \Closure) {
  50. call_user_func($config, $configInstance);
  51. }
  52. static::configure($configInstance);
  53. return $purifier->purify($content);
  54. }
  55. /**
  56. * Allow the extended HtmlPurifier class to set some default config options.
  57. * @param \HTMLPurifier_Config $config
  58. * @since 2.0.3
  59. */
  60. protected static function configure($config)
  61. {
  62. }
  63. }