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