function purify_html($text, $options = array()) {
global $CFG;
- $type = !empty($options['allowid']) ? 'allowid' : 'normal';
static $purifiers = array();
+ static $caches = array();
+
+ $type = !empty($options['allowid']) ? 'allowid' : 'normal';
+
+ if (!array_key_exists($type, $caches)) {
+ $caches[$type] = cache::make('core', 'htmlpurifier', array('type' => $type));
+ }
+ $cache = $caches[$type];
+
+ $filteredtext = $cache->get($text);
+ if ($filteredtext !== false) {
+ return $filteredtext;
+ }
+
if (empty($purifiers[$type])) {
// make sure the serializer dir exists, it should be fine if it disappears later during cache reset
$multilang = (strpos($text, 'class="multilang"') !== false);
+ $filteredtext = $text;
if ($multilang) {
- $text = preg_replace('/<span(\s+lang="([a-zA-Z0-9_-]+)"|\s+class="multilang"){2}\s*>/', '<span xxxlang="${2}">', $text);
+ $filteredtext = preg_replace('/<span(\s+lang="([a-zA-Z0-9_-]+)"|\s+class="multilang"){2}\s*>/', '<span xxxlang="${2}">', $filteredtext);
}
- $text = $purifier->purify($text);
+ $filteredtext = $purifier->purify($filteredtext);
if ($multilang) {
- $text = preg_replace('/<span xxxlang="([a-zA-Z0-9_-]+)">/', '<span lang="${1}" class="multilang">', $text);
+ $filteredtext = preg_replace('/<span xxxlang="([a-zA-Z0-9_-]+)">/', '<span lang="${1}" class="multilang">', $filteredtext);
}
+ $cache->set($text, $filteredtext);
- return $text;
+ return $filteredtext;
}
/**