2 // $Id: htmlpurifier.module,v 1.2 2007/12/09 21:33:38 ezyang Exp $
5 * Implementation of hook_help().
9 function htmlpurifier_help($section) {
11 case 'admin/modules#description':
12 $output = t('Filter that removes malicious HTML and ensures standards compliant output.');
19 * Implementation of hook_filter().
21 function htmlpurifier_filter($op, $delta = 0, $format = -1, $text = '') {
24 return array(0 => t('HTML Purifier'));
27 // caching HTML Purifier's output is STRONGLY recommended due to
28 // HTML Purifier's data-intensive nature
32 // TODO: Include description of what tags and properties are active
33 return t('Removes malicious HTML code and ensures that the output is standards compliant.');
39 $config = _htmlpurifier_get_config($format);
40 $purifier = htmlpurifier_create_purifier($config);
41 _htmlpurifier_add_filters($format, $purifier);
42 return $purifier->purify($text);
45 return _htmlpurifier_settings($format);
52 function htmlpurifier_purify($text, $config) {
53 $purifier = htmlpurifier_create_purifier($config);
54 return $purifier->purify($text);
57 function htmlpurifier_create_purifier($config) {
58 // Load HTML Purifier library
59 $module_path = drupal_get_path('module', 'htmlpurifier');
60 require_once "$module_path/library/HTMLPurifier.auto.php";
62 // Overload HTML Purifiers default cache handler with our own so that
63 // Drupals built-in cache is used
64 require_once "$module_path/HTMLPurifier_DefinitionCache_Drupal.php";
65 $factory =& HTMLPurifier_DefinitionCacheFactory::instance();
66 if (method_exists($factory, 'register')) {
67 $factory->register('Drupal', 'HTMLPurifier_DefinitionCache_Drupal');
69 // Brittle, used for previous HTML Purifier version compatibility
70 $factory->caches['Drupal']['HTML'] = new HTMLPurifier_DefinitionCache_Drupal('HTML');
71 $factory->caches['Drupal']['CSS'] = new HTMLPurifier_DefinitionCache_Drupal('CSS');
72 $factory->caches['Drupal']['URI'] = new HTMLPurifier_DefinitionCache_Drupal('URI');
74 $config['Cache.DefinitionImpl'] = 'Drupal';
76 return new HTMLPurifier($config);
79 function _htmlpurifier_settings($format) {
82 $form['htmlpurifier'] = array(
83 '#type' => 'fieldset',
84 '#title' => t('HTML Purifier'),
85 '#collapsible' => TRUE,
87 $form['htmlpurifier']["htmlpurifier_enableattrid_$format"] = array(
88 '#type' => 'checkbox',
89 '#title' => t('Allow the ID attribute in HTML'),
90 '#default_value' => variable_get("htmlpurifier_enableattrid_$format", FALSE),
91 '#description' => t('If enabled, allows the use of ID attributes in HTML tags. This is disabled by default due to the fact that without proper configuration user input can easily break the validation of a webpage by specifying an ID that is already on the surrounding HTML.'),
93 $form['htmlpurifier']["htmlpurifier_preserveyoutube_$format"] = array(
94 '#type' => 'checkbox',
95 '#title' => t('Preserve embedded YouTube videos'),
96 '#default_value' => variable_get("htmlpurifier_preserveyoutube_$format", FALSE),
97 '#description' => t('If enabled, allows the use of embedded YouTube videos. See !url for more information.', array('!url' => l('Embedding YouTube Videos', 'http://hp.jpsband.org/live/docs/enduser-youtube.html'))),
99 $form['htmlpurifier']["htmlpurifier_help_$format"] = array(
100 '#type' => 'checkbox',
101 '#title' => t('Display help text'),
102 '#default_value' => variable_get("htmlpurifier_help_$format", TRUE),
103 '#description' => t('If enabled, a short note will be added to the filter tips explaining that HTML will be transformed to conform with HTML standards. You may want to disable this option when the HTML Purifier is used to check the output of another filter like BBCode.'),
109 function _htmlpurifier_get_config($format) {
111 'HTML.EnableAttrID' => variable_get("htmlpurifier_enableattrid_$format", FALSE),
116 function _htmlpurifier_add_filters($format, &$purifier) {
117 if (variable_get("htmlpurifier_preserveyoutube_$format", FALSE) && version_compare($purifier->version, '1.4.1', '>=')) {
118 require_once 'HTMLPurifier/Filter/YouTube.php';
119 $purifier->addFilter(new HTMLPurifier_Filter_YouTube());
124 * Implementation of hook_filter_tips().
126 function htmlpurifier_filter_tips($delta, $format, $long = FALSE) {
127 if (variable_get("htmlpurifier_help_$format", TRUE)) {
128 return t('HTML tags will be transformed to conform to HTML standards.');