/** * Do some output manipulation. * * Auto-inject <b>jsn-master tmpl-nameOfDefaultTemplate</b> into the class * attribute of <b><body></b> tag if not already exists. This * automation only affects backend page. * * @return void */ public static function onAfterRender() { // Make sure the remaining process is executed in last order if (!defined('JSN_EXTFW_LAST_EXECUTION')) { return; } // Get active component $option = self::$_app->input->getCmd('option'); // Get the rendered HTML code $html = JResponse::getBody(); if (self::$_app->input->getVar('poweradmin')) { preg_match_all('#<a[^\\>]*href\\s*=\\s*[\'"]([^"]*[^"]+)[\'"]#i', $html, $ms, PREG_SET_ORDER); if (count($ms)) { foreach ($ms as $m) { $html = str_replace($m[0], str_replace($m[1], 'javascript:void(0)', $m[0]), $html); } } } // Do some fixes if this is admin page if (self::$_app->isAdmin()) { // Fix asset links for Joomla 2.5 if (JSNVersion::isJoomlaCompatible('2.5') and !JSNVersion::isJoomlaCompatible('3.0') and strpos($html, JSN_URL_ASSETS) !== false) { // Get asset link if (preg_match_all('#<(link|script)([^\\>]*)(href|src)="([^"]*' . JSN_URL_ASSETS . '[^"]+)"#i', $html, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { // Do replace $html = str_replace($match[0], '<' . $match[1] . $match[2] . $match[3] . '="' . dirname(dirname($match[4])) . '/' . str_replace('.', '-', basename(dirname($match[4]))) . '/' . basename($match[4]) . '"', $html); } } } // Remove our extensions from the Joomla 3.0's global config page if ($option == 'com_config' and JSNVersion::isJoomlaCompatible('3.0')) { $html = preg_replace('#[\\s\\t]*<li[^\\r\\n]*>[\\r\\n\\s\\t]*<a[^\\r\\n]+index.php\\?option=com_config&view=component&component=(' . implode('|', JSNVersion::$products) . ')[^\\r\\n]+</a>[\\r\\n\\s\\t]*</li>[\\r\\n]#', '', $html); } // Alter body tag if (preg_match('/<body[^>]*>/i', $html, $match) and strpos($match[0], 'jsn-master tmpl-' . self::$_app->getTemplate()) === false) { if (strpos($match[0], 'class=') === false) { $match[1] = substr($match[0], 0, -1) . ' class=" jsn-master tmpl-' . self::$_app->getTemplate() . ' ">'; } else { $match[1] = str_replace('class="', 'class=" jsn-master tmpl-' . self::$_app->getTemplate() . ' ', $match[0]); } $html = str_replace($match[0], $match[1], $html); } if (JSNVersion::isJoomlaCompatible('3.2')) { // Clean-up HTML5 fall-back script if running on Joomla 3.2 if (in_array($option, JSNVersion::$products) and preg_match('#[\\r\\n][\\s\\t]+<script src="[^"]*/media/system/js/html5fallback(-uncompressed)?\\.js"[^>]+></script>#', $html, $match)) { $html = str_replace($match[0], '', $html); } // Temporary fix jQuery version conflict on Joomla 3.2 $pos = strpos($html, JSN_URL_ASSETS . '/3rd-party/jquery/jquery.min.js'); if ($pos !== false and preg_match('#<script[^>]+src="' . JSN_URL_ASSETS . '/3rd-party/jquery/jquery.min.js"[^>]*></script>#', $html, $match)) { $html = explode($match[0], $html); // Do some tricks on multiple jQuery instances $script = '<script type="text/javascript">' . "\n\t\t" . '(JoomlaShine = window.JoomlaShine || {});' . "\n\t\t" . '(!window.jQuery || (JoomlaShine.jQueryBackup = jQuery));' . "\n\t" . '</script>' . "\n\t" . $match[0]; // Update document header $html[0] .= $script; // Truncate content $html[2] = substr($html[1], strpos($html[1], '</head>')); $html[1] = substr($html[1], 0, strpos($html[1], '</head>')); if (preg_match('#<script[^>]+src="[^"]*/media/jui/js/jquery(\\.min)?\\.js"[^>]*></script>#', $html[1], $match)) { $script = '<script type="text/javascript">' . "\n\t\t" . '(JoomlaShine = window.JoomlaShine || {});' . "\n\t\t" . '(!window.jQuery || (JoomlaShine.jQuery = jQuery));' . "\n\t" . '</script>' . "\n\t" . $match[0]; // Update document header $html[1] = str_replace($match[0], $script, $html[1]); } elseif (preg_match('#<script[^>]+src="[^"]*/js/template\\.js[^"]*"[^>]*></script>#', $html[1], $match)) { $script = '<script type="text/javascript">' . "\n\t\t" . '(JoomlaShine = window.JoomlaShine || {});' . "\n\t\t" . '(!window.jQuery || (JoomlaShine.jQuery = jQuery));' . "\n\t\t" . '(!JoomlaShine.jQueryBackup || (jQuery = JoomlaShine.jQueryBackup));' . "\n\t" . '</script>' . "\n\t" . $match[0]; // Update document header $html[1] = str_replace($match[0], $script, $html[1]); } elseif (preg_match('#<script type="text/javascript">#', $html[1], $match)) { $script = '<script type="text/javascript">' . "\n\t\t" . '(JoomlaShine = window.JoomlaShine || {});' . "\n\t\t" . '(!window.jQuery || (JoomlaShine.jQuery = jQuery));' . "\n\t\t" . '(!JoomlaShine.jQueryBackup || (jQuery = JoomlaShine.jQueryBackup));'; // Update document header $html[1] = str_replace($match[0], $script, $html[1]); } $html = implode($html); // Fix for (function($) { ... })(jQuery); $tmp = preg_split('/\\}[\\s\\t\\r\\n]*\\)*\\([^\\r\\n]*jQuery[^\\r\\n]*\\)\\s*;?/', $html); $html = ''; $i = 0; foreach ($tmp as $part) { $i++; if ($i == count($tmp)) { $html .= $part; } else { $parts = preg_split('/\\(\\s*function\\s*\\(\\s*\\$\\s*\\)\\s*\\{/', $part, 2); if (count($parts) < 2) { $html .= $part; } elseif (stripos($parts[1], 'jsn') !== false) { $html .= "{$parts[0]}(function(\$) {{$parts[1]}})((window.JoomlaShine && JoomlaShine.jQuery) ? JoomlaShine.jQuery : jQuery);"; } else { $html .= "{$parts[0]}(function(\$) {{$parts[1]}})(jQuery);"; } } } // Remove JSN ImageShow's buggy fix for jQuery conflict if (strpos($html, 'administrator/components/com_imageshow/assets/js/joomlashine/jquery.safe.element.js') !== false) { $html = preg_replace('#[\\r\\n][\\s\\t]*<script[^>]+src="[^"]*/joomlashine/jquery.safe.element.js[^"]*"[^>]*></script>#', '', $html); } } } } // Attach JS declaration $html = str_replace('</head>', JSNHtmlAsset::buildHeader() . '</head>', $html); // Optimize script tags position self::moveScriptTags($html); // Fix compatibility problem between require.js and RokPad editor if (strpos($html, '/plugins/editors/rokpad/')) { if (preg_match_all('#[\\r\\n][\\s\\t]*<script[^>]+src="[^"]*/plugins/editors/rokpad/[^"]+"[^>]*></script>#', $html, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { // Clean the script tag from its original position $html = str_replace($match[0], '', $html); // Inject the removed script tag into the end of head section $html = str_replace('</head>', $match[0] . '</head>', $html); } } } // Set new response body JResponse::setBody($html); // Execute update checker if (!JSNVersion::isJoomlaCompatible('3.1')) { !isset($this) or $this->checkUpdate(); } }