/**
 * Output all needed JavaScript
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function js_out()
{
    global $conf;
    global $lang;
    $edit = (bool) $_REQUEST['edit'];
    // edit or preview mode?
    $write = (bool) $_REQUEST['write'];
    // writable?
    // The generated script depends on some dynamic options
    $cache = getCacheName('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . $edit . 'x' . $write, '.js');
    // Array of needed files
    $files = array(DOKU_INC . 'lib/scripts/helpers.js', DOKU_INC . 'lib/scripts/events.js', DOKU_INC . 'lib/scripts/cookie.js', DOKU_INC . 'lib/scripts/script.js', DOKU_INC . 'lib/scripts/tw-sack.js', DOKU_INC . 'lib/scripts/ajax.js', DOKU_INC . 'lib/scripts/index.js');
    if ($edit) {
        if ($write) {
            $files[] = DOKU_INC . 'lib/scripts/edit.js';
        }
        $files[] = DOKU_INC . 'lib/scripts/media.js';
    }
    $files[] = DOKU_TPLINC . 'script.js';
    // get possible plugin scripts
    $plugins = js_pluginscripts();
    // check cache age & handle conditional request
    header('Cache-Control: public, max-age=3600');
    header('Pragma: public');
    if (js_cacheok($cache, array_merge($files, $plugins))) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            if (!http_sendfile($cache)) {
                readfile($cache);
            }
        }
        return;
    } else {
        http_conditionalRequest(time());
    }
    // start output buffering and build the script
    ob_start();
    // add some global variables
    print "var DOKU_BASE   = '" . DOKU_BASE . "';";
    print "var DOKU_TPL    = '" . DOKU_TPL . "';";
    //FIXME: move thes into LANG
    print "var alertText   = '" . js_escape($lang['qb_alert']) . "';";
    print "var notSavedYet = '" . js_escape($lang['notsavedyet']) . "';";
    print "var reallyDel   = '" . js_escape($lang['del_confirm']) . "';";
    // load JS strings form plugins
    $lang['js']['plugins'] = js_pluginstrings();
    // load JS specific translations
    $json = new JSON();
    echo 'LANG = ' . $json->encode($lang['js']) . ";\n";
    // load files
    foreach ($files as $file) {
        echo "\n\n/* XXXXXXXXXX begin of {$file} XXXXXXXXXX */\n\n";
        js_load($file);
        echo "\n\n/* XXXXXXXXXX end of {$file} XXXXXXXXXX */\n\n";
    }
    // init stuff
    js_runonstart("ajax_qsearch.init('qsearch__in','qsearch__out')");
    js_runonstart("addEvent(document,'click',closePopups)");
    js_runonstart('addTocToggle()');
    if ($edit) {
        // size controls
        js_runonstart("initSizeCtl('size__ctl','wiki__text')");
        if ($write) {
            require_once DOKU_INC . 'inc/toolbar.php';
            toolbar_JSdefines('toolbar');
            js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)");
            // add pageleave check
            js_runonstart("initChangeCheck('" . js_escape($lang['notsavedyet']) . "')");
            // add lock timer
            js_runonstart("locktimer.init(" . ($conf['locktime'] - 60) . ",'" . js_escape($lang['willexpire']) . "'," . $conf['usedraft'] . ")");
        }
    }
    // load plugin scripts (suppress warnings for missing ones)
    foreach ($plugins as $plugin) {
        if (@file_exists($plugin)) {
            echo "\n\n/* XXXXXXXXXX begin of {$plugin} XXXXXXXXXX */\n\n";
            js_load($plugin);
            echo "\n\n/* XXXXXXXXXX end of {$plugin} XXXXXXXXXX */\n\n";
        }
    }
    // load user script
    @readfile(DOKU_CONF . 'userscript.js');
    // add scroll event and tooltip rewriting
    js_runonstart('scrollToMarker()');
    js_runonstart('focusMarker()');
    // end output buffering and get contents
    $js = ob_get_contents();
    ob_end_clean();
    // compress whitespace and comments
    if ($conf['compress']) {
        $js = js_compress($js);
    }
    $js .= "\n";
    // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
    // save cache file
    io_saveFile($cache, $js);
    copy($cache, "compress.zlib://{$cache}.gz");
    // finally send output
    if ($conf['gzip_output']) {
        header('Vary: Accept-Encoding');
        header('Content-Encoding: gzip');
        print gzencode($js, 9, FORCE_GZIP);
    } else {
        print $js;
    }
}
Exemple #2
0
/**
 * Output all needed JavaScript
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function js_out()
{
    global $conf;
    global $lang;
    global $config_cascade;
    // The generated script depends on some dynamic options
    $cache = getCacheName('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'], '.js');
    // array of core files
    $files = array(DOKU_INC . 'lib/scripts/helpers.js', DOKU_INC . 'lib/scripts/events.js', DOKU_INC . 'lib/scripts/delay.js', DOKU_INC . 'lib/scripts/cookie.js', DOKU_INC . 'lib/scripts/script.js', DOKU_INC . 'lib/scripts/tw-sack.js', DOKU_INC . 'lib/scripts/ajax.js', DOKU_INC . 'lib/scripts/index.js', DOKU_INC . 'lib/scripts/drag.js', DOKU_INC . 'lib/scripts/textselection.js', DOKU_INC . 'lib/scripts/toolbar.js', DOKU_INC . 'lib/scripts/edit.js', DOKU_INC . 'lib/scripts/linkwiz.js', DOKU_INC . 'lib/scripts/media.js', DOKU_INC . 'lib/scripts/subscriptions.js', DOKU_TPLINC . 'script.js');
    // add possible plugin scripts and userscript
    $files = array_merge($files, js_pluginscripts());
    if (isset($config_cascade['userscript']['default'])) {
        $files[] = $config_cascade['userscript']['default'];
    }
    // check cache age & handle conditional request
    header('Cache-Control: public, max-age=3600');
    header('Pragma: public');
    if (js_cacheok($cache, $files)) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            if (!http_sendfile($cache)) {
                readfile($cache);
            }
        }
        return;
    } else {
        http_conditionalRequest(time());
    }
    // start output buffering and build the script
    ob_start();
    // add some global variables
    print "var DOKU_BASE   = '" . DOKU_BASE . "';";
    print "var DOKU_TPL    = '" . DOKU_TPL . "';";
    print "var DOKU_UHN    = " . (int) useHeading('navigation') . ";";
    print "var DOKU_UHC    = " . (int) useHeading('content') . ";";
    // load JS specific translations
    $json = new JSON();
    $lang['js']['plugins'] = js_pluginstrings();
    echo 'LANG = ' . $json->encode($lang['js']) . ";\n";
    // load toolbar
    toolbar_JSdefines('toolbar');
    // load files
    foreach ($files as $file) {
        echo "\n\n/* XXXXXXXXXX begin of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
        js_load($file);
        echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
    }
    // init stuff
    js_runonstart("addEvent(document,'click',closePopups)");
    js_runonstart('addTocToggle()');
    js_runonstart("initSizeCtl('size__ctl','wiki__text')");
    js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)");
    if ($conf['locktime'] != 0) {
        js_runonstart("locktimer.init(" . ($conf['locktime'] - 60) . ",'" . js_escape($lang['willexpire']) . "'," . $conf['usedraft'] . ")");
    }
    js_runonstart('scrollToMarker()');
    js_runonstart('focusMarker()');
    // init hotkeys - must have been done after init of toolbar
    # disabled for FS#1958    js_runonstart('initializeHotkeys()');
    // end output buffering and get contents
    $js = ob_get_contents();
    ob_end_clean();
    // compress whitespace and comments
    if ($conf['compress']) {
        $js = js_compress($js);
    }
    $js .= "\n";
    // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
    // save cache file
    io_saveFile($cache, $js);
    if (function_exists('gzopen')) {
        io_saveFile("{$cache}.gz", $js);
    }
    // finally send output
    if ($conf['gzip_output']) {
        header('Vary: Accept-Encoding');
        header('Content-Encoding: gzip');
        print gzencode($js, 9, FORCE_GZIP);
    } else {
        print $js;
    }
}
Exemple #3
0
function _js_out()
{
    global $_CONF, $_SYSTEM, $_USER, $_PLUGINS, $themeAPI;
    $js = '';
    $file_content = '';
    $files = array();
    global $_CONF, $_SYSTEM, $_USER, $_PLUGINS;
    $outputHandle = outputHandler::getInstance();
    if (!isset($_CONF['js_cache_filename'])) {
        $_CONF['js_cache_filename'] = 'js.cache';
    }
    list($cacheFile, $cacheURL) = COM_getJSCacheLocation();
    // standard JS used by glFusion
    if (!isset($_SYSTEM['disable_jquery']) || $_SYSTEM['disable_jquery'] == false) {
        $files[] = $_CONF['path_html'] . 'javascript/jquery/jquery.min.js';
        $files[] = $_CONF['path_html'] . 'javascript/jquery/jquery-ui.min.js';
        $files[] = $_CONF['path_html'] . 'javascript/addons/jqrating.min.js';
        if (!isset($_SYSTEM['disable_jquery_tooltip']) || $_SYSTEM['disable_jquery_tooltip'] == false) {
            $files[] = $_CONF['path_html'] . 'javascript/addons/tooltipster/jquery.tooltipster.min.js';
            $files[] = $_CONF['path_html'] . 'javascript/addons/tooltipster/tooltip.min.js';
        }
        if (!isset($_SYSTEM['disable_jquery_menu']) || $_SYSTEM['disable_jquery_menu'] == false) {
            $files[] = $_CONF['path_html'] . 'javascript/addons/superfish/superfish.min.js';
            $files[] = $_CONF['path_html'] . 'javascript/addons/superfish/hoverIntent.min.js';
        }
        if (!isset($_SYSTEM['disable_jquery_slimbox']) || $_SYSTEM['disable_jquery_slimbox'] == false) {
            $files[] = $_CONF['path_html'] . 'javascript/addons/slimbox/slimbox2.min.js';
        }
        if (!isset($_SYSTEM['disable_jquery_validate']) || $_SYSTEM['disable_jquery_validate'] == false) {
            $files[] = $_CONF['path_html'] . 'javascript/addons/jquery-validate/jquery.validate.min.js';
            $files[] = $_CONF['path_html'] . 'javascript/addons/jquery-validate/additional-methods.min.js';
        }
        if (!isset($_SYSTEM['disable_jquery_slideshow']) || $_SYSTEM['disable_jquery_slideshow'] == false) {
            $files[] = $_CONF['path_html'] . 'javascript/addons/tcycle/jquery.tcycle.min.js';
        }
    }
    $files[] = $_CONF['path_html'] . 'javascript/common.min.js';
    // need to parse the outputhandler to see if there are any js scripts to load
    $headerscripts = $outputHandle->getScriptFiles();
    foreach ($headerscripts as $s) {
        $files[] = $s;
    }
    /*
     * Check to see if the theme has any JavaScript to include...
     */
    $function = 'theme_themeJS';
    if (function_exists($function)) {
        $jTheme = $function();
        if (is_array($jTheme)) {
            foreach ($jTheme as $item => $file) {
                $files[] = $file;
            }
        }
    }
    /*
     * Check to see if there are any custom javascript files to include
     */
    if (function_exists('CUSTOM_js')) {
        $jTheme = CUSTOM_js();
        if (is_array($jTheme)) {
            foreach ($jTheme as $item => $file) {
                $files[] = $file;
            }
        }
    }
    /*
     * Let the plugins add their JavaScript needs here...
     */
    if (is_array($_PLUGINS)) {
        foreach ($_PLUGINS as $pi_name) {
            if (function_exists('plugin_getheaderjs_' . $pi_name)) {
                $function = 'plugin_getheaderjs_' . $pi_name;
                $pHeader = array();
                $pHeader = $function();
                if (is_array($pHeader)) {
                    foreach ($pHeader as $item => $file) {
                        $files[] = $file;
                    }
                }
            }
        }
    }
    /*
     * Let the plugins add any global JS variables
     */
    if (is_array($_PLUGINS)) {
        foreach ($_PLUGINS as $pi_name) {
            if (function_exists('plugin_getglobaljs_' . $pi_name)) {
                $function = 'plugin_getglobaljs_' . $pi_name;
                $globalJS = array();
                $globalJS = $function();
                if (is_array($globalJS)) {
                    foreach ($globalJS as $name => $value) {
                        $pluginJSvars[$name] = $value;
                    }
                }
            }
        }
    }
    if (js_cacheok($cacheFile, $files)) {
        return $cacheURL;
    }
    // add some global variables
    $urlparts = parse_url($_CONF['site_url']);
    if (isset($urlparts['path'])) {
        $fileroot = $urlparts['path'];
    } else {
        $fileroot = '';
    }
    $js .= "var glfusionSiteUrl = '" . $_CONF['site_url'] . "';" . LB;
    $js .= "var glfusionFileRoot = '" . $fileroot . "';" . LB;
    $js .= "var glfusionLayoutUrl = '" . $_CONF['layout_url'] . "';" . LB;
    $js .= "var site_admin_url = '" . $_CONF['site_admin_url'] . "';" . LB;
    if (isset($_SYSTEM['use_direct_style_js']) && $_SYSTEM['use_direct_style_js']) {
        $js .= "var glfusionStyleCSS      = '" . $_CONF['site_url'] . '/' . $_CONF['css_cache_filename'] . $_USER['theme'] . '.css?t=' . $_USER['theme'] . "';" . LB;
    } else {
        $js .= "var glfusionStyleCSS      = '" . $_CONF['site_url'] . "/css.php?t=" . $_USER['theme'] . "';" . LB;
    }
    // send any global plugin JS vars
    if (isset($pluginJSvars) && is_array($pluginJSvars)) {
        foreach ($pluginJSvars as $name => $value) {
            $js .= "var " . $name . " = '" . $value . "';";
        }
    }
    if (is_array($files)) {
        foreach ($files as $file) {
            $file_content = @file_get_contents($file);
            if ($file_content === false) {
                COM_errorLog("ERROR: Unable to retrieve JS file: " . $file);
            } else {
                $js .= $file_content;
            }
            $js .= LB;
        }
    }
    $js .= LB;
    // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
    $rc = writeFile_lck($cacheFile, '', $js, 'glfusion_js.lck');
    if ($rc === false) {
        writeFile_lck($cacheFile, '', $js, 'glfusion_js.lck');
    }
    return $cacheURL;
}