diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 1b42e436a64..7a3483261d5 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -1224,13 +1224,25 @@ function portfolio_format_text_options() { * @return object|array|string */ function portfolio_rewrite_pluginfile_url_callback($contextid, $component, $filearea, $itemid, $format, $options, $matches) { - $matches = $matches[0]; // no internal matching + $matches = $matches[0]; // No internal matching. + + // Loads the HTML. $dom = new DomDocument(); - if (!$dom->loadXML($matches)) { + if (!$dom->loadHTML($matches)) { return $matches; } + + // Navigates to the node. + $xpath = new DOMXPath($dom); + $nodes = $xpath->query('/html/body/child::*'); + if (empty($nodes) || count($nodes) > 1) { + // Unexpected sequence, none or too many nodes. + return $matches; + } + $dom = $nodes->item(0); + $attributes = array(); - foreach ($dom->documentElement->attributes as $attr => $node) { + foreach ($dom->attributes as $attr => $node) { $attributes[$attr] = $node->value; } // now figure out the file @@ -1360,7 +1372,11 @@ function portfolio_include_callback_file($component, $class = null) { * @return mixed */ function portfolio_rewrite_pluginfile_urls($text, $contextid, $component, $filearea, $itemid, $format, $options=null) { - $pattern = '/(<[^<]*?="@@PLUGINFILE@@\/[^>]*?(?:\/>|>.*?<\/[^>]*?>))/'; + $patterns = array( + '(<(a|A)[^<]*?href="@@PLUGINFILE@@/[^>]*?>.*?)', + '(<(img|IMG)\s[^<]*?src="@@PLUGINFILE@@/[^>]*?/?>)', + ); + $pattern = '~' . implode('|', $patterns) . '~'; $callback = partial('portfolio_rewrite_pluginfile_url_callback', $contextid, $component, $filearea, $itemid, $format, $options); return preg_replace_callback($pattern, $callback, $text); }