1<?php 2/** 3 * DokuWiki JavaScript creator 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9use dokuwiki\Utf8\PhpString; 10use dokuwiki\Cache\Cache; 11use dokuwiki\Extension\Event; 12use splitbrain\JSStrip\Exception as JSStripException; 13use splitbrain\JSStrip\JSStrip; 14 15if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../'); 16if (!defined('NOSESSION')) define('NOSESSION', true); // we do not use a session or authentication here (better caching) 17if (!defined('NL')) define('NL', "\n"); 18if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); // we gzip ourself here 19require_once(DOKU_INC . 'inc/init.php'); 20 21// Main (don't run when UNIT test) 22if (!defined('SIMPLE_TEST')) { 23 header('Content-Type: application/javascript; charset=utf-8'); 24 js_out(); 25} 26 27 28// ---------------------- functions ------------------------------ 29 30/** 31 * Output all needed JavaScript 32 * 33 * @author Andreas Gohr <andi@splitbrain.org> 34 */ 35function js_out() 36{ 37 global $conf; 38 global $lang; 39 global $config_cascade; 40 global $INPUT; 41 42 // decide from where to get the template 43 $tpl = trim(preg_replace('/[^\w-]+/', '', $INPUT->str('t'))); 44 if (!$tpl) $tpl = $conf['template']; 45 46 // array of core files 47 $files = [ 48 DOKU_INC . 'lib/scripts/jquery/jquery.cookie.js', 49 DOKU_INC . 'inc/lang/' . $conf['lang'] . '/jquery.ui.datepicker.js', 50 DOKU_INC . "lib/scripts/fileuploader.js", 51 DOKU_INC . "lib/scripts/fileuploaderextended.js", 52 DOKU_INC . 'lib/scripts/helpers.js', 53 DOKU_INC . 'lib/scripts/delay.js', 54 DOKU_INC . 'lib/scripts/cookie.js', 55 DOKU_INC . 'lib/scripts/script.js', 56 DOKU_INC . 'lib/scripts/qsearch.js', 57 DOKU_INC . 'lib/scripts/search.js', 58 DOKU_INC . 'lib/scripts/tree.js', 59 DOKU_INC . 'lib/scripts/index.js', 60 DOKU_INC . 'lib/scripts/textselection.js', 61 DOKU_INC . 'lib/scripts/toolbar.js', 62 DOKU_INC . 'lib/scripts/edit.js', 63 DOKU_INC . 'lib/scripts/editor.js', 64 DOKU_INC . 'lib/scripts/locktimer.js', 65 DOKU_INC . 'lib/scripts/linkwiz.js', 66 DOKU_INC . 'lib/scripts/media.js', 67 DOKU_INC . 'lib/scripts/compatibility.js', 68 # disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 69 DOKU_INC . 'lib/scripts/behaviour.js', 70 DOKU_INC . 'lib/scripts/page.js', 71 tpl_incdir($tpl) . 'script.js', 72 ]; 73 74 // add possible plugin scripts and userscript 75 $files = array_merge($files, js_pluginscripts()); 76 if (is_array($config_cascade['userscript']['default'])) { 77 foreach ($config_cascade['userscript']['default'] as $userscript) { 78 $files[] = $userscript; 79 } 80 } 81 82 // Let plugins decide to either put more scripts here or to remove some 83 Event::createAndTrigger('JS_SCRIPT_LIST', $files); 84 85 // The generated script depends on some dynamic options 86 $cache = new Cache('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . md5(serialize($files)), '.js'); 87 $cache->setEvent('JS_CACHE_USE'); 88 89 $cache_files = array_merge($files, getConfigFiles('main')); 90 $cache_files[] = __FILE__; 91 92 // check cache age & handle conditional request 93 // This may exit if a cache can be used 94 $cache_ok = $cache->useCache(['files' => $cache_files]); 95 http_cached($cache->cache, $cache_ok); 96 97 // start output buffering and build the script 98 ob_start(); 99 100 // add some global variables 101 echo "var DOKU_BASE = '" . DOKU_BASE . "';"; 102 echo "var DOKU_TPL = '" . tpl_basedir($tpl) . "';"; 103 echo "var DOKU_COOKIE_PARAM = " . json_encode([ 104 'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'], 105 'secure' => $conf['securecookie'] && is_ssl() 106 ], JSON_THROW_ON_ERROR) . ";"; 107 // FIXME: Move those to JSINFO 108 echo "Object.defineProperty(window, 'DOKU_UHN', { get: function() {" . 109 "console.warn('Using DOKU_UHN is deprecated. Please use JSINFO.useHeadingNavigation instead');" . 110 "return JSINFO.useHeadingNavigation; } });"; 111 echo "Object.defineProperty(window, 'DOKU_UHC', { get: function() {" . 112 "console.warn('Using DOKU_UHC is deprecated. Please use JSINFO.useHeadingContent instead');" . 113 "return JSINFO.useHeadingContent; } });"; 114 115 // load JS specific translations 116 $lang['js']['plugins'] = js_pluginstrings(); 117 $templatestrings = js_templatestrings($tpl); 118 if (!empty($templatestrings)) { 119 $lang['js']['template'] = $templatestrings; 120 } 121 echo 'LANG = ' . json_encode($lang['js'], JSON_THROW_ON_ERROR) . ";\n"; 122 123 // load toolbar 124 toolbar_JSdefines('toolbar'); 125 126 // load files 127 foreach ($files as $file) { 128 if (!file_exists($file)) continue; 129 $ismin = (substr($file, -7) == '.min.js'); 130 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC . 'lib/scripts/') !== 0); 131 132 echo "\n\n/* XXXXXXXXXX begin of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 133 if ($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 134 if ($debugjs) echo "\ntry {\n"; 135 js_load($file); 136 if ($debugjs) echo "\n} catch (e) {\n logError(e, '" . str_replace(DOKU_INC, '', $file) . "');\n}\n"; 137 if ($ismin) echo "\n/* END NOCOMPRESS */\n"; 138 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 139 } 140 141 // init stuff 142 if ($conf['locktime'] != 0) { 143 js_runonstart("dw_locktimer.init(" . ($conf['locktime'] - 60) . "," . $conf['usedraft'] . ")"); 144 } 145 // init hotkeys - must have been done after init of toolbar 146 # disabled for FS#1958 js_runonstart('initializeHotkeys()'); 147 148 // end output buffering and get contents 149 $js = ob_get_contents(); 150 ob_end_clean(); 151 152 // strip any source maps 153 stripsourcemaps($js); 154 155 // compress whitespace and comments 156 if ($conf['compress']) { 157 try { 158 $js = (new JSStrip())->compress($js); 159 } catch (JSStripException $e) { 160 $js .= "\nconsole.error(" . json_encode($e->getMessage(), JSON_THROW_ON_ERROR) . ");\n"; 161 } 162 } 163 164 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 165 166 http_cached_finish($cache->cache, $js); 167} 168 169/** 170 * Load the given file, handle include calls and print it 171 * 172 * @param string $file filename path to file 173 * 174 * @author Andreas Gohr <andi@splitbrain.org> 175 */ 176function js_load($file) 177{ 178 if (!file_exists($file)) return; 179 static $loaded = []; 180 181 $data = io_readFile($file); 182 while (preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#', $data, $match)) { 183 $ifile = $match[2]; 184 185 // is it a include_once? 186 if ($match[1]) { 187 $base = PhpString::basename($ifile); 188 if (array_key_exists($base, $loaded) && $loaded[$base] === true) { 189 $data = str_replace($match[0], '', $data); 190 continue; 191 } 192 $loaded[$base] = true; 193 } 194 195 if ($ifile[0] != '/') $ifile = dirname($file) . '/' . $ifile; 196 197 $idata = ''; 198 if (file_exists($ifile)) { 199 $ismin = (substr($ifile, -7) == '.min.js');; 200 if ($ismin) $idata .= "\n/* BEGIN NOCOMPRESS */\n"; 201 $idata .= io_readFile($ifile); 202 if ($ismin) $idata .= "\n/* END NOCOMPRESS */\n"; 203 } 204 $data = str_replace($match[0], $idata, $data); 205 } 206 echo "$data\n"; 207} 208 209/** 210 * Returns a list of possible Plugin Scripts (no existance check here) 211 * 212 * @return array 213 * 214 * @author Andreas Gohr <andi@splitbrain.org> 215 */ 216function js_pluginscripts() 217{ 218 $list = []; 219 $plugins = plugin_list(); 220 foreach ($plugins as $p) { 221 $list[] = DOKU_PLUGIN . "$p/script.js"; 222 } 223 return $list; 224} 225 226/** 227 * Return an two-dimensional array with strings from the language file of each plugin. 228 * 229 * - $lang['js'] must be an array. 230 * - Nothing is returned for plugins without an entry for $lang['js'] 231 * 232 * @return array 233 * @author Gabriel Birke <birke@d-scribe.de> 234 * 235 */ 236function js_pluginstrings() 237{ 238 global $conf, $config_cascade; 239 $pluginstrings = []; 240 $plugins = plugin_list(); 241 foreach ($plugins as $p) { 242 $path = DOKU_PLUGIN . $p . '/lang/'; 243 244 if (isset($lang)) unset($lang); 245 if (file_exists($path . "en/lang.php")) { 246 include $path . "en/lang.php"; 247 } 248 foreach ($config_cascade['lang']['plugin'] as $config_file) { 249 if (file_exists($config_file . $p . '/en/lang.php')) { 250 include($config_file . $p . '/en/lang.php'); 251 } 252 } 253 if (isset($conf['lang']) && $conf['lang'] != 'en') { 254 if (file_exists($path . $conf['lang'] . "/lang.php")) { 255 include($path . $conf['lang'] . '/lang.php'); 256 } 257 foreach ($config_cascade['lang']['plugin'] as $config_file) { 258 if (file_exists($config_file . $p . '/' . $conf['lang'] . '/lang.php')) { 259 include($config_file . $p . '/' . $conf['lang'] . '/lang.php'); 260 } 261 } 262 } 263 264 if (isset($lang['js'])) { 265 $pluginstrings[$p] = $lang['js']; 266 } 267 } 268 return $pluginstrings; 269} 270 271/** 272 * Return an two-dimensional array with strings from the language file of current active template. 273 * 274 * - $lang['js'] must be an array. 275 * - Nothing is returned for template without an entry for $lang['js'] 276 * 277 * @param string $tpl 278 * @return array 279 */ 280function js_templatestrings($tpl) 281{ 282 global $conf, $config_cascade; 283 284 $path = tpl_incdir() . 'lang/'; 285 286 $templatestrings = []; 287 if (file_exists($path . "en/lang.php")) { 288 include $path . "en/lang.php"; 289 } 290 foreach ($config_cascade['lang']['template'] as $config_file) { 291 if (file_exists($config_file . $conf['template'] . '/en/lang.php')) { 292 include($config_file . $conf['template'] . '/en/lang.php'); 293 } 294 } 295 if (isset($conf['lang']) && $conf['lang'] != 'en' && file_exists($path . $conf['lang'] . "/lang.php")) { 296 include $path . $conf['lang'] . "/lang.php"; 297 } 298 if (isset($conf['lang']) && $conf['lang'] != 'en') { 299 if (file_exists($path . $conf['lang'] . "/lang.php")) { 300 include $path . $conf['lang'] . "/lang.php"; 301 } 302 foreach ($config_cascade['lang']['template'] as $config_file) { 303 if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) { 304 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php'); 305 } 306 } 307 } 308 309 if (isset($lang['js'])) { 310 $templatestrings[$tpl] = $lang['js']; 311 } 312 return $templatestrings; 313} 314 315/** 316 * Escapes a String to be embedded in a JavaScript call, keeps \n 317 * as newline 318 * 319 * @param string $string 320 * @return string 321 * 322 * @author Andreas Gohr <andi@splitbrain.org> 323 */ 324function js_escape($string) 325{ 326 return str_replace('\\\\n', '\\n', addslashes($string)); 327} 328 329/** 330 * Adds the given JavaScript code to the window.onload() event 331 * 332 * @param string $func 333 * 334 * @author Andreas Gohr <andi@splitbrain.org> 335 */ 336function js_runonstart($func) 337{ 338 echo "jQuery(function(){ $func; });" . NL; 339} 340