178a6aeb1SAndreas Gohr<?php 278a6aeb1SAndreas Gohr/** 378a6aeb1SAndreas Gohr * DokuWiki JavaScript creator 478a6aeb1SAndreas Gohr * 578a6aeb1SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 778a6aeb1SAndreas Gohr */ 85a5ec053SGerrit Uitslag 9e3c3abf1SAndreas Gohruse dokuwiki\Utf8\PhpString; 100db5771eSMichael Großeuse dokuwiki\Cache\Cache; 11cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 12af28745aSAndreas Gohruse splitbrain\JSStrip\Exception as JSStripException; 13af28745aSAndreas Gohruse splitbrain\JSStrip\JSStrip; 140db5771eSMichael Große 15cbb44eabSAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../'); 161c2d1019SAndreas Gohrif (!defined('NOSESSION')) define('NOSESSION', true); // we do not use a session or authentication here (better caching) 17b1a4fe22SAndreas Gohrif (!defined('NL')) define('NL', "\n"); 1898bda4fdSAndreas Gohrif (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); // we gzip ourself here 1978a6aeb1SAndreas Gohrrequire_once(DOKU_INC . 'inc/init.php'); 2078a6aeb1SAndreas Gohr 2178a6aeb1SAndreas Gohr// Main (don't run when UNIT test) 2278a6aeb1SAndreas Gohrif (!defined('SIMPLE_TEST')) { 23138a9500SAndreas Gohr header('Content-Type: application/javascript; charset=utf-8'); 2478a6aeb1SAndreas Gohr js_out(); 2578a6aeb1SAndreas Gohr} 2678a6aeb1SAndreas Gohr 2778a6aeb1SAndreas Gohr 2878a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------ 2978a6aeb1SAndreas Gohr 3078a6aeb1SAndreas Gohr/** 3178a6aeb1SAndreas Gohr * Output all needed JavaScript 3278a6aeb1SAndreas Gohr * 3378a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3478a6aeb1SAndreas Gohr */ 35d868eb89SAndreas Gohrfunction js_out() 36d868eb89SAndreas Gohr{ 3778a6aeb1SAndreas Gohr global $conf; 3878a6aeb1SAndreas Gohr global $lang; 3909edb711SAndreas Gohr global $config_cascade; 405fc6f52eSAnika Henke global $INPUT; 415fc6f52eSAnika Henke 425fc6f52eSAnika Henke // decide from where to get the template 435fc6f52eSAnika Henke $tpl = trim(preg_replace('/[^\w-]+/', '', $INPUT->str('t'))); 445fc6f52eSAnika Henke if (!$tpl) $tpl = $conf['template']; 4578a6aeb1SAndreas Gohr 466392c3b6SAndreas Gohr // array of core files 47e3c3abf1SAndreas Gohr $files = [ 48fdfb9c6aSMichal Rezler DOKU_INC . 'lib/scripts/jquery/jquery.cookie.js', 49138bfd16SAndreas Gohr DOKU_INC . 'inc/lang/' . $conf['lang'] . '/jquery.ui.datepicker.js', 5009063cc6SKate Arzamastseva DOKU_INC . "lib/scripts/fileuploader.js", 518d744859SKate Arzamastseva DOKU_INC . "lib/scripts/fileuploaderextended.js", 5286045fe9Swingedfox DOKU_INC . 'lib/scripts/helpers.js', 5321ed6025SAdrian Lang DOKU_INC . 'lib/scripts/delay.js', 543df72098SAndreas Gohr DOKU_INC . 'lib/scripts/cookie.js', 5578a6aeb1SAndreas Gohr DOKU_INC . 'lib/scripts/script.js', 56c949174aSAdrian Lang DOKU_INC . 'lib/scripts/qsearch.js', 57bb8ef867SMichael Große DOKU_INC . 'lib/scripts/search.js', 58d10c9a74SAdrian Lang DOKU_INC . 'lib/scripts/tree.js', 59a06884abSAndreas Gohr DOKU_INC . 'lib/scripts/index.js', 606392c3b6SAndreas Gohr DOKU_INC . 'lib/scripts/textselection.js', 616392c3b6SAndreas Gohr DOKU_INC . 'lib/scripts/toolbar.js', 626392c3b6SAndreas Gohr DOKU_INC . 'lib/scripts/edit.js', 63ddf8a04fSAndreas Gohr DOKU_INC . 'lib/scripts/editor.js', 6455f92d7eSAndreas Gohr DOKU_INC . 'lib/scripts/locktimer.js', 656392c3b6SAndreas Gohr DOKU_INC . 'lib/scripts/linkwiz.js', 666392c3b6SAndreas Gohr DOKU_INC . 'lib/scripts/media.js', 67767d1669SAndreas Gohr DOKU_INC . 'lib/scripts/compatibility.js', 6887e7175eSAndreas Gohr # disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 6917e2e254SAndreas Gohr DOKU_INC . 'lib/scripts/behaviour.js', 70fbedf126SAndreas Gohr DOKU_INC . 'lib/scripts/page.js', 715fc6f52eSAnika Henke tpl_incdir($tpl) . 'script.js', 72e3c3abf1SAndreas Gohr ]; 7378a6aeb1SAndreas Gohr 746392c3b6SAndreas Gohr // add possible plugin scripts and userscript 756392c3b6SAndreas Gohr $files = array_merge($files, js_pluginscripts()); 7660764596SAndreas Gohr if (is_array($config_cascade['userscript']['default'])) { 777b909d5eSGerrit Uitslag foreach ($config_cascade['userscript']['default'] as $userscript) { 787b909d5eSGerrit Uitslag $files[] = $userscript; 797b909d5eSGerrit Uitslag } 8009edb711SAndreas Gohr } 8178a6aeb1SAndreas Gohr 826d3bebffSGerry Weißbach // Let plugins decide to either put more scripts here or to remove some 83cbb44eabSAndreas Gohr Event::createAndTrigger('JS_SCRIPT_LIST', $files); 846d3bebffSGerry Weißbach 856d3bebffSGerry Weißbach // The generated script depends on some dynamic options 860db5771eSMichael Große $cache = new Cache('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . md5(serialize($files)), '.js'); 87dc7da772SMichael Große $cache->setEvent('JS_CACHE_USE'); 886d3bebffSGerry Weißbach 896619f42eSAdrian Lang $cache_files = array_merge($files, getConfigFiles('main')); 906619f42eSAdrian Lang $cache_files[] = __FILE__; 91ca2b464bSChris Smith 926619f42eSAdrian Lang // check cache age & handle conditional request 936619f42eSAdrian Lang // This may exit if a cache can be used 94e3c3abf1SAndreas Gohr $cache_ok = $cache->useCache(['files' => $cache_files]); 95e67004f5SAndreas Gohr http_cached($cache->cache, $cache_ok); 9678a6aeb1SAndreas Gohr 9778a6aeb1SAndreas Gohr // start output buffering and build the script 9878a6aeb1SAndreas Gohr ob_start(); 9978a6aeb1SAndreas Gohr 1003df72098SAndreas Gohr // add some global variables 101*26dfc232SAndreas Gohr echo "var DOKU_BASE = '" . DOKU_BASE . "';"; 102*26dfc232SAndreas Gohr echo "var DOKU_TPL = '" . tpl_basedir($tpl) . "';"; 103*26dfc232SAndreas Gohr echo "var DOKU_COOKIE_PARAM = " . json_encode([ 104df5d307eSGerrit Uitslag 'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'], 105df5d307eSGerrit Uitslag 'secure' => $conf['securecookie'] && is_ssl() 106e3c3abf1SAndreas Gohr ], JSON_THROW_ON_ERROR) . ";"; 1075e7a2926SAdrian Lang // FIXME: Move those to JSINFO 108*26dfc232SAndreas Gohr echo "Object.defineProperty(window, 'DOKU_UHN', { get: function() {" . 10964159a61SAndreas Gohr "console.warn('Using DOKU_UHN is deprecated. Please use JSINFO.useHeadingNavigation instead');" . 11064159a61SAndreas Gohr "return JSINFO.useHeadingNavigation; } });"; 111*26dfc232SAndreas Gohr echo "Object.defineProperty(window, 'DOKU_UHC', { get: function() {" . 11264159a61SAndreas Gohr "console.warn('Using DOKU_UHC is deprecated. Please use JSINFO.useHeadingContent instead');" . 11364159a61SAndreas Gohr "return JSINFO.useHeadingContent; } });"; 1143df72098SAndreas Gohr 1153df72098SAndreas Gohr // load JS specific translations 1166392c3b6SAndreas Gohr $lang['js']['plugins'] = js_pluginstrings(); 1175fc6f52eSAnika Henke $templatestrings = js_templatestrings($tpl); 118acd3eb67SKlap-in if (!empty($templatestrings)) { 119acd3eb67SKlap-in $lang['js']['template'] = $templatestrings; 120acd3eb67SKlap-in } 121e3c3abf1SAndreas Gohr echo 'LANG = ' . json_encode($lang['js'], JSON_THROW_ON_ERROR) . ";\n"; 12278a6aeb1SAndreas Gohr 1236392c3b6SAndreas Gohr // load toolbar 1246392c3b6SAndreas Gohr toolbar_JSdefines('toolbar'); 1256392c3b6SAndreas Gohr 12678a6aeb1SAndreas Gohr // load files 12778a6aeb1SAndreas Gohr foreach ($files as $file) { 128138bfd16SAndreas Gohr if (!file_exists($file)) continue; 12980a47290SAndreas Gohr $ismin = (substr($file, -7) == '.min.js'); 1306b0ec830SMichael Hamann $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC . 'lib/scripts/') !== 0); 13180a47290SAndreas Gohr 13257d846dbSAndreas Gohr echo "\n\n/* XXXXXXXXXX begin of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 13380a47290SAndreas Gohr if ($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 1346b0ec830SMichael Hamann if ($debugjs) echo "\ntry {\n"; 135176da19bSAndreas Gohr js_load($file); 1366b0ec830SMichael Hamann if ($debugjs) echo "\n} catch (e) {\n logError(e, '" . str_replace(DOKU_INC, '', $file) . "');\n}\n"; 13780a47290SAndreas Gohr if ($ismin) echo "\n/* END NOCOMPRESS */\n"; 13857d846dbSAndreas Gohr echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 13978a6aeb1SAndreas Gohr } 14078a6aeb1SAndreas Gohr 14178a6aeb1SAndreas Gohr // init stuff 142544ed901SDaniel Calviño Sánchez if ($conf['locktime'] != 0) { 14399e7bfd4SAndreas Gohr js_runonstart("dw_locktimer.init(" . ($conf['locktime'] - 60) . "," . $conf['usedraft'] . ")"); 144544ed901SDaniel Calviño Sánchez } 1454062d3d5SMarek Sacha // init hotkeys - must have been done after init of toolbar 146de32b03eSMichael Hamann # disabled for FS#1958 js_runonstart('initializeHotkeys()'); 14700540a38SAndreas Gohr 14878a6aeb1SAndreas Gohr // end output buffering and get contents 14978a6aeb1SAndreas Gohr $js = ob_get_contents(); 15078a6aeb1SAndreas Gohr ob_end_clean(); 15178a6aeb1SAndreas Gohr 152f8fb2d18SAndreas Gohr // strip any source maps 153f8fb2d18SAndreas Gohr stripsourcemaps($js); 154f8fb2d18SAndreas Gohr 15578a6aeb1SAndreas Gohr // compress whitespace and comments 15678a6aeb1SAndreas Gohr if ($conf['compress']) { 157af28745aSAndreas Gohr try { 158af28745aSAndreas Gohr $js = (new JSStrip())->compress($js); 159af28745aSAndreas Gohr } catch (JSStripException $e) { 160e3c3abf1SAndreas Gohr $js .= "\nconsole.error(" . json_encode($e->getMessage(), JSON_THROW_ON_ERROR) . ");\n"; 161af28745aSAndreas Gohr } 16278a6aeb1SAndreas Gohr } 16378a6aeb1SAndreas Gohr 1644d2d451eSAndreas Gohr $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 1654d2d451eSAndreas Gohr 1666619f42eSAdrian Lang http_cached_finish($cache->cache, $js); 167ca2b464bSChris Smith} 16878a6aeb1SAndreas Gohr 16978a6aeb1SAndreas Gohr/** 170176da19bSAndreas Gohr * Load the given file, handle include calls and print it 1713825ddd3SAndreas Gohr * 172253d4b48SGerrit Uitslag * @param string $file filename path to file 1735a5ec053SGerrit Uitslag * 1745a5ec053SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 175176da19bSAndreas Gohr */ 176d868eb89SAndreas Gohrfunction js_load($file) 177d868eb89SAndreas Gohr{ 17879e79377SAndreas Gohr if (!file_exists($file)) return; 179e3c3abf1SAndreas Gohr static $loaded = []; 180176da19bSAndreas Gohr 181176da19bSAndreas Gohr $data = io_readFile($file); 18298dba5adSAdrian Lang while (preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#', $data, $match)) { 1833825ddd3SAndreas Gohr $ifile = $match[2]; 1843825ddd3SAndreas Gohr 1853825ddd3SAndreas Gohr // is it a include_once? 1863825ddd3SAndreas Gohr if ($match[1]) { 187e3c3abf1SAndreas Gohr $base = PhpString::basename($ifile); 1885c97ad3aSMarius van Witzenburg if (array_key_exists($base, $loaded) && $loaded[$base] === true) { 189969df2f1SAndreas Gohr $data = str_replace($match[0], '', $data); 190969df2f1SAndreas Gohr continue; 191969df2f1SAndreas Gohr } 1923825ddd3SAndreas Gohr $loaded[$base] = true; 1933825ddd3SAndreas Gohr } 1943825ddd3SAndreas Gohr 1955312cb0bSSyntaxseed if ($ifile[0] != '/') $ifile = dirname($file) . '/' . $ifile; 196176da19bSAndreas Gohr 197176da19bSAndreas Gohr $idata = ''; 198b4af56dcSMoritz Raguschat if (file_exists($ifile)) { 199b4af56dcSMoritz Raguschat $ismin = (substr($ifile, -7) == '.min.js');; 200b4af56dcSMoritz Raguschat if ($ismin) $idata .= "\n/* BEGIN NOCOMPRESS */\n"; 201b4af56dcSMoritz Raguschat $idata .= io_readFile($ifile); 202b4af56dcSMoritz Raguschat if ($ismin) $idata .= "\n/* END NOCOMPRESS */\n"; 203176da19bSAndreas Gohr } 204176da19bSAndreas Gohr $data = str_replace($match[0], $idata, $data); 205176da19bSAndreas Gohr } 20617582ec6SAndreas Gohr echo "$data\n"; 207176da19bSAndreas Gohr} 208176da19bSAndreas Gohr 209176da19bSAndreas Gohr/** 21078a6aeb1SAndreas Gohr * Returns a list of possible Plugin Scripts (no existance check here) 21178a6aeb1SAndreas Gohr * 212253d4b48SGerrit Uitslag * @return array 2135a5ec053SGerrit Uitslag * 2145a5ec053SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 21578a6aeb1SAndreas Gohr */ 216d868eb89SAndreas Gohrfunction js_pluginscripts() 217d868eb89SAndreas Gohr{ 218e3c3abf1SAndreas Gohr $list = []; 21978a6aeb1SAndreas Gohr $plugins = plugin_list(); 22078a6aeb1SAndreas Gohr foreach ($plugins as $p) { 22178a6aeb1SAndreas Gohr $list[] = DOKU_PLUGIN . "$p/script.js"; 22278a6aeb1SAndreas Gohr } 22378a6aeb1SAndreas Gohr return $list; 22478a6aeb1SAndreas Gohr} 22578a6aeb1SAndreas Gohr 22678a6aeb1SAndreas Gohr/** 2279f01cf6aSGabriel Birke * Return an two-dimensional array with strings from the language file of each plugin. 2289f01cf6aSGabriel Birke * 2299f01cf6aSGabriel Birke * - $lang['js'] must be an array. 2309f01cf6aSGabriel Birke * - Nothing is returned for plugins without an entry for $lang['js'] 2319f01cf6aSGabriel Birke * 2325a5ec053SGerrit Uitslag * @return array 2339f01cf6aSGabriel Birke * @author Gabriel Birke <birke@d-scribe.de> 234253d4b48SGerrit Uitslag * 2359f01cf6aSGabriel Birke */ 236d868eb89SAndreas Gohrfunction js_pluginstrings() 237d868eb89SAndreas Gohr{ 23819e06537SGerrit Uitslag global $conf, $config_cascade; 239e3c3abf1SAndreas Gohr $pluginstrings = []; 2409f01cf6aSGabriel Birke $plugins = plugin_list(); 2419f01cf6aSGabriel Birke foreach ($plugins as $p) { 24219e06537SGerrit Uitslag $path = DOKU_PLUGIN . $p . '/lang/'; 24319e06537SGerrit Uitslag 2449f01cf6aSGabriel Birke if (isset($lang)) unset($lang); 24519e06537SGerrit Uitslag if (file_exists($path . "en/lang.php")) { 24619e06537SGerrit Uitslag include $path . "en/lang.php"; 2479f01cf6aSGabriel Birke } 24819e06537SGerrit Uitslag foreach ($config_cascade['lang']['plugin'] as $config_file) { 24919e06537SGerrit Uitslag if (file_exists($config_file . $p . '/en/lang.php')) { 25019e06537SGerrit Uitslag include($config_file . $p . '/en/lang.php'); 2519f01cf6aSGabriel Birke } 25219e06537SGerrit Uitslag } 25319e06537SGerrit Uitslag if (isset($conf['lang']) && $conf['lang'] != 'en') { 25419e06537SGerrit Uitslag if (file_exists($path . $conf['lang'] . "/lang.php")) { 25519e06537SGerrit Uitslag include($path . $conf['lang'] . '/lang.php'); 25619e06537SGerrit Uitslag } 25719e06537SGerrit Uitslag foreach ($config_cascade['lang']['plugin'] as $config_file) { 25819e06537SGerrit Uitslag if (file_exists($config_file . $p . '/' . $conf['lang'] . '/lang.php')) { 25919e06537SGerrit Uitslag include($config_file . $p . '/' . $conf['lang'] . '/lang.php'); 26019e06537SGerrit Uitslag } 26119e06537SGerrit Uitslag } 26219e06537SGerrit Uitslag } 26319e06537SGerrit Uitslag 2649f01cf6aSGabriel Birke if (isset($lang['js'])) { 2659f01cf6aSGabriel Birke $pluginstrings[$p] = $lang['js']; 2669f01cf6aSGabriel Birke } 2679f01cf6aSGabriel Birke } 2689f01cf6aSGabriel Birke return $pluginstrings; 2699f01cf6aSGabriel Birke} 2709f01cf6aSGabriel Birke 27156b0b744SGerrit Uitslag/** 27256b0b744SGerrit Uitslag * Return an two-dimensional array with strings from the language file of current active template. 27356b0b744SGerrit Uitslag * 27456b0b744SGerrit Uitslag * - $lang['js'] must be an array. 27556b0b744SGerrit Uitslag * - Nothing is returned for template without an entry for $lang['js'] 276253d4b48SGerrit Uitslag * 2775fc6f52eSAnika Henke * @param string $tpl 278253d4b48SGerrit Uitslag * @return array 27956b0b744SGerrit Uitslag */ 280d868eb89SAndreas Gohrfunction js_templatestrings($tpl) 281d868eb89SAndreas Gohr{ 28219e06537SGerrit Uitslag global $conf, $config_cascade; 28319e06537SGerrit Uitslag 28419e06537SGerrit Uitslag $path = tpl_incdir() . 'lang/'; 28519e06537SGerrit Uitslag 286e3c3abf1SAndreas Gohr $templatestrings = []; 28719e06537SGerrit Uitslag if (file_exists($path . "en/lang.php")) { 28819e06537SGerrit Uitslag include $path . "en/lang.php"; 289acd3eb67SKlap-in } 29019e06537SGerrit Uitslag foreach ($config_cascade['lang']['template'] as $config_file) { 29119e06537SGerrit Uitslag if (file_exists($config_file . $conf['template'] . '/en/lang.php')) { 29219e06537SGerrit Uitslag include($config_file . $conf['template'] . '/en/lang.php'); 293acd3eb67SKlap-in } 29419e06537SGerrit Uitslag } 29519e06537SGerrit Uitslag if (isset($conf['lang']) && $conf['lang'] != 'en' && file_exists($path . $conf['lang'] . "/lang.php")) { 29619e06537SGerrit Uitslag include $path . $conf['lang'] . "/lang.php"; 29719e06537SGerrit Uitslag } 29819e06537SGerrit Uitslag if (isset($conf['lang']) && $conf['lang'] != 'en') { 29919e06537SGerrit Uitslag if (file_exists($path . $conf['lang'] . "/lang.php")) { 30019e06537SGerrit Uitslag include $path . $conf['lang'] . "/lang.php"; 30119e06537SGerrit Uitslag } 30219e06537SGerrit Uitslag foreach ($config_cascade['lang']['template'] as $config_file) { 30319e06537SGerrit Uitslag if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) { 30419e06537SGerrit Uitslag include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php'); 30519e06537SGerrit Uitslag } 30619e06537SGerrit Uitslag } 30719e06537SGerrit Uitslag } 30819e06537SGerrit Uitslag 309acd3eb67SKlap-in if (isset($lang['js'])) { 3105fc6f52eSAnika Henke $templatestrings[$tpl] = $lang['js']; 311acd3eb67SKlap-in } 312acd3eb67SKlap-in return $templatestrings; 313acd3eb67SKlap-in} 314acd3eb67SKlap-in 3159f01cf6aSGabriel Birke/** 31678a6aeb1SAndreas Gohr * Escapes a String to be embedded in a JavaScript call, keeps \n 31778a6aeb1SAndreas Gohr * as newline 31878a6aeb1SAndreas Gohr * 319253d4b48SGerrit Uitslag * @param string $string 320253d4b48SGerrit Uitslag * @return string 3215a5ec053SGerrit Uitslag * 3225a5ec053SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 32378a6aeb1SAndreas Gohr */ 324d868eb89SAndreas Gohrfunction js_escape($string) 325d868eb89SAndreas Gohr{ 32678a6aeb1SAndreas Gohr return str_replace('\\\\n', '\\n', addslashes($string)); 32778a6aeb1SAndreas Gohr} 32878a6aeb1SAndreas Gohr 32978a6aeb1SAndreas Gohr/** 33078a6aeb1SAndreas Gohr * Adds the given JavaScript code to the window.onload() event 33178a6aeb1SAndreas Gohr * 332253d4b48SGerrit Uitslag * @param string $func 3335a5ec053SGerrit Uitslag * 3345a5ec053SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 33578a6aeb1SAndreas Gohr */ 336d868eb89SAndreas Gohrfunction js_runonstart($func) 337d868eb89SAndreas Gohr{ 3385e7a2926SAdrian Lang echo "jQuery(function(){ $func; });" . NL; 33978a6aeb1SAndreas Gohr} 340