/** * 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; } }
/** * Set HTTP headers and echo cachefile, if useable * * This function handles output of cacheable resource files. It ses the needed * HTTP headers. If a useable cache is present, it is passed to the web server * and the script is terminated. * * @param string $cache cache file name * @param bool $cache_ok if cache can be used */ function http_cached($cache, $cache_ok) { global $conf; // check cache age & handle conditional request // since the resource files are timestamped, we can use a long max age: 1 year header('Cache-Control: public, max-age=31536000'); header('Pragma: public'); if ($cache_ok) { 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 { http_sendfile($cache); readfile($cache); } exit; } http_conditionalRequest(time()); }
/** * 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; } }
/** * Output all needed Styles * * @author Andreas Gohr <*****@*****.**> */ function css_out() { global $conf; global $lang; global $config_cascade; $mediatype = 'screen'; if (isset($_REQUEST['s']) && in_array($_REQUEST['s'], array('all', 'print', 'feed'))) { $mediatype = $_REQUEST['s']; } $tpl = trim(preg_replace('/[^\\w-]+/', '', $_REQUEST['t'])); if ($tpl) { $tplinc = DOKU_INC . 'lib/tpl/' . $tpl . '/'; $tpldir = DOKU_BASE . 'lib/tpl/' . $tpl . '/'; } else { $tplinc = DOKU_TPLINC; $tpldir = DOKU_TPL; } // The generated script depends on some dynamic options $cache = getCacheName('styles' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . DOKU_BASE . $tplinc . $mediatype, '.css'); // load template styles $tplstyles = array(); if (@file_exists($tplinc . 'style.ini')) { $ini = parse_ini_file($tplinc . 'style.ini', true); foreach ($ini['stylesheets'] as $file => $mode) { $tplstyles[$mode][$tplinc . $file] = $tpldir; } } // Array of needed files and their web locations, the latter ones // are needed to fix relative paths in the stylesheets $files = array(); // load core styles $files[DOKU_INC . 'lib/styles/' . $mediatype . '.css'] = DOKU_BASE . 'lib/styles/'; // load plugin styles $files = array_merge($files, css_pluginstyles($mediatype)); // load template styles if (isset($tplstyles[$mediatype])) { $files = array_merge($files, $tplstyles[$mediatype]); } // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility if (isset($config_cascade['userstyle']['default'])) { $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default']; } // load user styles if (isset($config_cascade['userstyle'][$mediatype])) { $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE; } // load rtl styles // @todo: this currently adds the rtl styles only to the 'screen' media type // but 'print' and 'all' should also be supported if ($mediatype == 'screen') { if ($lang['direction'] == 'rtl') { if (isset($tplstyles['rtl'])) { $files = array_merge($files, $tplstyles['rtl']); } } } // check cache age & handle conditional request header('Cache-Control: public, max-age=3600'); header('Pragma: public'); if (css_cacheok($cache, array_keys($files), $tplinc)) { 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 stylesheet ob_start(); // print the default classes for interwiki links and file downloads css_interwiki(); css_filetypes(); // load files foreach ($files as $file => $location) { print css_loadfile($file, $location); } // end output buffering and get contents $css = ob_get_contents(); ob_end_clean(); // apply style replacements $css = css_applystyle($css, $tplinc); // place all @import statements at the top of the file $css = css_moveimports($css); // compress whitespace and comments if ($conf['compress']) { $css = css_compress($css); } // save cache file io_saveFile($cache, $css); if (function_exists('gzopen')) { io_saveFile("{$cache}.gz", $css); } // finally send output if ($conf['gzip_output']) { header('Vary: Accept-Encoding'); header('Content-Encoding: gzip'); print gzencode($css, 9, FORCE_GZIP); } else { print $css; } }
/** * Output all needed Styles * * @author Andreas Gohr <*****@*****.**> */ function css_out() { global $conf; global $lang; switch ($_REQUEST['s']) { case 'all': case 'print': case 'feed': $style = $_REQUEST['s']; break; default: $style = ''; break; } $tpl = trim(preg_replace('/[^\\w-]+/', '', $_REQUEST['t'])); if ($tpl) { $tplinc = DOKU_INC . 'lib/tpl/' . $tpl . '/'; $tpldir = DOKU_BASE . 'lib/tpl/' . $tpl . '/'; } else { $tplinc = DOKU_TPLINC; $tpldir = DOKU_TPL; } // The generated script depends on some dynamic options $cache = getCacheName('styles' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . DOKU_BASE . $tplinc . $style, '.css'); // load template styles $tplstyles = array(); if (@file_exists($tplinc . 'style.ini')) { $ini = parse_ini_file($tplinc . 'style.ini', true); foreach ($ini['stylesheets'] as $file => $mode) { $tplstyles[$mode][$tplinc . $file] = $tpldir; } } // Array of needed files and their web locations, the latter ones // are needed to fix relative paths in the stylesheets $files = array(); //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']); if (!empty($style)) { $files[DOKU_INC . 'lib/styles/' . $style . '.css'] = DOKU_BASE . 'lib/styles/'; // load plugin, template, user styles $files = array_merge($files, css_pluginstyles($style)); if (isset($tplstyles[$style])) { $files = array_merge($files, $tplstyles[$style]); } $files[DOKU_CONF . 'user' . $style . '.css'] = DOKU_BASE; } else { $files[DOKU_INC . 'lib/styles/style.css'] = DOKU_BASE . 'lib/styles/'; if ($conf['spellchecker']) { $files[DOKU_INC . 'lib/styles/spellcheck.css'] = DOKU_BASE . 'lib/styles/'; } // load plugin, template, user styles $files = array_merge($files, css_pluginstyles('screen')); if (isset($tplstyles['screen'])) { $files = array_merge($files, $tplstyles['screen']); } if ($lang['direction'] == 'rtl') { if (isset($tplstyles['rtl'])) { $files = array_merge($files, $tplstyles['rtl']); } } $files[DOKU_CONF . 'userstyle.css'] = DOKU_BASE; } // check cache age & handle conditional request header('Cache-Control: public, max-age=3600'); header('Pragma: public'); if (css_cacheok($cache, array_keys($files), $tplinc)) { 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 stylesheet ob_start(); // print the default classes for interwiki links and file downloads css_interwiki(); css_filetypes(); // load files foreach ($files as $file => $location) { print css_loadfile($file, $location); } // end output buffering and get contents $css = ob_get_contents(); ob_end_clean(); // apply style replacements $css = css_applystyle($css, $tplinc); // compress whitespace and comments if ($conf['compress']) { $css = css_compress($css); } // save cache file io_saveFile($cache, $css); copy($cache, "compress.zlib://{$cache}.gz"); // finally send output if ($conf['gzip_output']) { header('Vary: Accept-Encoding'); header('Content-Encoding: gzip'); print gzencode($css, 9, FORCE_GZIP); } else { print $css; } }