Example #1
0
/**
 * This is a workaround which ensures that Akeeba's namespaced JavaScript and CSS will be loaded
 * without being tampered with by any system plugin. Moreover, since we are loading first, we can
 * be pretty sure that namespacing *will* work and we won't cause any incompatibilities with third
 * party extensions loading different versions of these GUI libraries.
 *
 * This code works by registering a system plugin hook :) It will grab the HTML and drop its own
 * JS and CSS definitions in the head of the script, before anything else has the chance to run.
 *
 * Peace.
 */
function AkeebaStrapperLoader()
{
    // If there are no script defs, just go to sleep
    if (empty(AkeebaStrapper::$scriptURLs) && empty(AkeebaStrapper::$scriptDefs) && empty(AkeebaStrapper::$cssDefs) && empty(AkeebaStrapper::$cssURLs) && empty(AkeebaStrapper::$lessURLs)) {
        return;
    }
    $myscripts = '';
    $preload = AkeebaStrapper::needPreload();
    if (version_compare(JVERSION, '3.2', 'ge')) {
        $buffer = JFactory::getApplication()->getBody();
    } else {
        $buffer = JResponse::getBody();
    }
    // Include Javascript files
    if (!empty(AkeebaStrapper::$scriptURLs)) {
        foreach (AkeebaStrapper::$scriptURLs as $entry) {
            list($url, $tag) = $entry;
            if ($preload) {
                $myscripts .= '<script type="text/javascript" src="' . $url . $tag . '"></script>' . "\n";
            } else {
                JFactory::getDocument()->addScript($url . $tag);
            }
        }
    }
    // Include Javscript snippets
    if (!empty(AkeebaStrapper::$scriptDefs)) {
        if ($preload) {
            $myscripts .= '<script type="text/javascript" language="javascript">' . "\n";
        } else {
            $myscripts = '';
        }
        foreach (AkeebaStrapper::$scriptDefs as $def) {
            $myscripts .= $def . "\n";
        }
        if ($preload) {
            $myscripts .= '</script>' . "\n";
        } else {
            JFactory::getDocument()->addScriptDeclaration($myscripts);
        }
    }
    // Include LESS files
    if (!empty(AkeebaStrapper::$lessURLs)) {
        foreach (AkeebaStrapper::$lessURLs as $entry) {
            list($lessFile, $altFiles, $tag) = $entry;
            $url = F0FTemplateUtils::addLESS($lessFile, $altFiles, true);
            if ($preload) {
                if (empty($url)) {
                    if (!is_array($altFiles) && empty($altFiles)) {
                        $altFiles = array($altFiles);
                    }
                    if (!empty($altFiles)) {
                        foreach ($altFiles as $altFile) {
                            $url = F0FTemplateUtils::parsePath($altFile);
                            $myscripts .= '<link type="text/css" rel="stylesheet" href="' . $url . $tag . '" />' . "\n";
                        }
                    }
                } else {
                    $myscripts .= '<link type="text/css" rel="stylesheet" href="' . $url . $tag . '" />' . "\n";
                }
            } else {
                if (empty($url)) {
                    if (!is_array($altFiles) && empty($altFiles)) {
                        $altFiles = array($altFiles);
                    }
                    if (!empty($altFiles)) {
                        foreach ($altFiles as $altFile) {
                            $url = F0FTemplateUtils::parsePath($altFile);
                            JFactory::getDocument()->addStyleSheet($url . $tag);
                        }
                    }
                } else {
                    JFactory::getDocument()->addStyleSheet($url . $tag);
                }
            }
        }
    }
    // Include CSS files
    if (!empty(AkeebaStrapper::$cssURLs)) {
        foreach (AkeebaStrapper::$cssURLs as $entry) {
            list($url, $tag) = $entry;
            if ($preload) {
                $myscripts .= '<link type="text/css" rel="stylesheet" href="' . $url . $tag . '" />' . "\n";
            } else {
                JFactory::getDocument()->addStyleSheet($url . $tag);
            }
        }
    }
    // Include style definitions
    if (!empty(AkeebaStrapper::$cssDefs)) {
        $myscripts .= '<style type="text/css">' . "\n";
        foreach (AkeebaStrapper::$cssDefs as $def) {
            if ($preload) {
                $myscripts .= $def . "\n";
            } else {
                JFactory::getDocument()->addScriptDeclaration($def . "\n");
            }
        }
        $myscripts .= '</style>' . "\n";
    }
    if ($preload) {
        $pos = strpos($buffer, "<head>");
        if ($pos > 0) {
            $buffer = substr($buffer, 0, $pos + 6) . $myscripts . substr($buffer, $pos + 6);
            if (version_compare(JVERSION, '3.2', 'ge')) {
                JFactory::getApplication()->setBody($buffer);
            } else {
                JResponse::setBody($buffer);
            }
        }
    }
}
Example #2
0
/**
 * This is a workaround which ensures that Akeeba's namespaced JavaScript and CSS will be loaded
 * without being tampered with by any system plugin. Moreover, since we are loading first, we can
 * be pretty sure that namespacing *will* work and we won't cause any incompatibilities with third
 * party extensions loading different versions of these GUI libraries.
 *
 * This code works by registering a system plugin hook :) It will grab the HTML and drop its own
 * JS and CSS definitions in the head of the script, before anything else has the chance to run.
 *
 * Peace.
 */
function AkeebaStrapperLoader()
{
    // If there are no script defs, just go to sleep
    if (empty(AkeebaStrapper::$scriptURLs) && empty(AkeebaStrapper::$scriptDefs) && empty(AkeebaStrapper::$cssDefs) && empty(AkeebaStrapper::$cssURLs) && empty(AkeebaStrapper::$lessURLs)) {
        return;
    }
    $myscripts = '';
    $preloadJ2 = (bool) AkeebaStrapper::getPreference('preload_joomla2', 1);
    $preload = AkeebaStrapper::needPreload();
    if ($preload) {
        $buffer = JResponse::getBody();
    } else {
        $preloadJ2 = false;
        $preload = false;
    }
    // Include Javascript files
    if (!empty(AkeebaStrapper::$scriptURLs)) {
        foreach (AkeebaStrapper::$scriptURLs as $entry) {
            list($url, $tag) = $entry;
            if ($preloadJ2 && basename($url) == 'bootstrap.min.js') {
                // Special case: check that nobody else is using bootstrap[.min].js on the page.
                $scriptRegex = "/<script [^>]+(\\/>|><\\/script>)/i";
                $jsRegex = "/([^\"\\'=]+\\.(js)(\\?[^\"\\']*){0,1})[\"\\']/i";
                preg_match_all($scriptRegex, $buffer, $matches);
                $scripts = @implode('', $matches[0]);
                preg_match_all($jsRegex, $scripts, $matches);
                $skip = false;
                foreach ($matches[1] as $scripturl) {
                    $scripturl = basename($scripturl);
                    if (in_array($scripturl, array('bootstrap.min.js', 'bootstrap.js'))) {
                        $skip = true;
                    }
                }
                if ($skip) {
                    continue;
                }
            }
            if ($preload) {
                $myscripts .= '<script type="text/javascript" src="' . $url . $tag . '"></script>' . "\n";
            } else {
                JFactory::getDocument()->addScript($url . $tag);
            }
        }
    }
    // Include Javscript snippets
    if (!empty(AkeebaStrapper::$scriptDefs)) {
        if ($preload) {
            $myscripts .= '<script type="text/javascript" language="javascript">' . "\n";
        } else {
            $myscripts = '';
        }
        foreach (AkeebaStrapper::$scriptDefs as $def) {
            $myscripts .= $def . "\n";
        }
        if ($preload) {
            $myscripts .= '</script>' . "\n";
        } else {
            JFactory::getDocument()->addScriptDeclaration($myscripts);
        }
    }
    // Include LESS files
    if (!empty(AkeebaStrapper::$lessURLs)) {
        foreach (AkeebaStrapper::$lessURLs as $entry) {
            list($lessFile, $altFiles, $tag) = $entry;
            $url = FOFTemplateUtils::addLESS($lessFile, $altFiles, true);
            if ($preload) {
                if (empty($url)) {
                    if (!is_array($altFiles) && empty($altFiles)) {
                        $altFiles = array($altFiles);
                    }
                    if (!empty($altFiles)) {
                        foreach ($altFiles as $altFile) {
                            $url = FOFTemplateUtils::parsePath($altFile);
                            $myscripts .= '<link type="text/css" rel="stylesheet" href="' . $url . $tag . '" />' . "\n";
                        }
                    }
                } else {
                    $myscripts .= '<link type="text/css" rel="stylesheet" href="' . $url . $tag . '" />' . "\n";
                }
            } else {
                if (empty($url)) {
                    if (!is_array($altFiles) && empty($altFiles)) {
                        $altFiles = array($altFiles);
                    }
                    if (!empty($altFiles)) {
                        foreach ($altFiles as $altFile) {
                            $url = FOFTemplateUtils::parsePath($altFile);
                            JFactory::getDocument()->addStyleSheet($url . $tag);
                        }
                    }
                } else {
                    JFactory::getDocument()->addStyleSheet($url . $tag);
                }
            }
        }
    }
    // Include CSS files
    if (!empty(AkeebaStrapper::$cssURLs)) {
        foreach (AkeebaStrapper::$cssURLs as $entry) {
            list($url, $tag) = $entry;
            if ($preload) {
                $myscripts .= '<link type="text/css" rel="stylesheet" href="' . $url . $tag . '" />' . "\n";
            } else {
                JFactory::getDocument()->addStyleSheet($url . $tag);
            }
        }
    }
    // Include style definitions
    if (!empty(AkeebaStrapper::$cssDefs)) {
        $myscripts .= '<style type="text/css">' . "\n";
        foreach (AkeebaStrapper::$cssDefs as $def) {
            if ($preload) {
                $myscripts .= $def . "\n";
            } else {
                JFactory::getDocument()->addScriptDeclaration($def . "\n");
            }
        }
        $myscripts .= '</style>' . "\n";
    }
    if ($preload) {
        $pos = strpos($buffer, "<head>");
        if ($pos > 0) {
            $buffer = substr($buffer, 0, $pos + 6) . $myscripts . substr($buffer, $pos + 6);
            JResponse::setBody($buffer);
        }
    }
}