/** * fix_mediafilter * * @return xxx */ function fix_mediafilter() { global $CFG; if (!$this->hotpot->usemediafilter) { return false; } if (!hotpot::load_mediafilter_filter($this->hotpot->usemediafilter)) { return false; } $mediafilterclass = 'hotpot_mediafilter_' . $this->hotpot->usemediafilter; $mediafilter = new $mediafilterclass($this); $mediafilter->fix('headcontent', $this); $mediafilter->fix('bodycontent', $this); if ($mediafilter->js_inline) { // remove the internal </script><script ... > joins from the inline javascripts (js_inline) $search = '/(?:\\s*\\/\\/\\]\\]>)?' . '\\s*<\\/script>\\s*<script type="text\\/javascript">' . '(?:\\s*\\/\\/<!\\[CDATA\\[[ \\t]*)?/is'; $mediafilter->js_inline = preg_replace($search, '', $mediafilter->js_inline); // extract urls of deferred scripts from $mediafilter->js_external if (preg_match_all($this->tagpattern('script'), $mediafilter->js_external, $scripts, PREG_OFFSET_CAPTURE)) { $deferred_js = array(); $strlen_wwwroot = strlen($CFG->wwwroot); foreach (array_reverse($scripts[0]) as $script) { // $script [0] => matched string, [1] => offset to start of matched string $remove = false; if (strpos($script[0], 'type="text/javascript"')) { if (preg_match('/src="(.*?)"/i', $script[0], $matches)) { if (strpos($script[0], 'defer="defer"') === false) { $inhead = true; } else { $inhead = false; } // we do not add scripts with $this->page->requires->js() because // they will not then be added when content is retrieved from cache //if (substr($matches[1], 0, $strlen_wwwroot)==$CFG->wwwroot) { // $this->page->requires->js(substr($matches[1], $strlen_wwwroot), $inhead); // $remove = true; //} else if ($inhead) { // leave this script where it is (i.e. in the head) } else { array_unshift($deferred_js, '"' . addslashes_js($matches[1]) . '"'); $remove = true; } } } if ($remove) { $mediafilter->js_external = substr_replace($mediafilter->js_external, '', $script[1], strlen($script[0])); } } $deferred_js = implode(',', array_unique($deferred_js)); } else { $deferred_js = ''; } if ($deferred_js) { $deferred_js = '' . ' // load deferred scripts' . "\n" . ' var head = document.getElementsByTagName("head")[0];' . "\n" . ' var urls = new Array(' . $deferred_js . ');' . "\n" . ' for (var i=0; i<urls.length; i++) {' . "\n" . ' var script = document.createElement("script");' . "\n" . ' script.type = "text/javascript";' . "\n" . ' script.src = urls[i];' . "\n" . ' head.appendChild(script);' . "\n" . ' }' . "\n"; } $functions = ''; if (preg_match_all('/(?<=function )\\w+/', $mediafilter->js_inline, $names)) { foreach ($names[0] as $name) { list($start, $finish) = $this->locate_js_block('function', $name, $mediafilter->js_inline, true); if ($finish) { $functions .= trim(substr($mediafilter->js_inline, $start, $finish - $start)) . "\n"; $mediafilter->js_inline = substr_replace($mediafilter->js_inline, '', $start, $finish - $start); } } } // put all the inline javascript into one single function called "hotpot_mediafilter_loader()", // which also loads up any deferred js, and force this function to be run when the page has loaded $onload = 'hotpot_mediafilter_loader()'; $search = '/(\\/\\/<!\\[CDATA\\[)(.*)(\\/\\/\\]\\]>)/s'; $replace = '$1' . "\n" . $functions . 'function ' . $onload . '{' . '$2' . $deferred_js . $this->fix_mediafilter_onload_extra() . '} // end function ' . $onload . "\n" . "\n" . $this->fix_onload($onload) . '$3'; $mediafilter->js_inline = preg_replace($search, $replace, $mediafilter->js_inline, 1); // append the inline javascripts to the end of the bodycontent $this->bodycontent .= $mediafilter->js_inline; } if ($mediafilter->js_external) { // append the external javascripts to the head content $this->headcontent .= $mediafilter->js_external; } }