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\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 global $conf; 36 global $lang; 37 global $config_cascade; 38 global $INPUT; 39 40 // decide from where to get the template 41 $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t'))); 42 if(!$tpl) $tpl = $conf['template']; 43 44 // array of core files 45 $files = array( 46 DOKU_INC.'lib/scripts/jquery/jquery.cookie.js', 47 DOKU_INC.'inc/lang/'.$conf['lang'].'/jquery.ui.datepicker.js', 48 DOKU_INC."lib/scripts/fileuploader.js", 49 DOKU_INC."lib/scripts/fileuploaderextended.js", 50 DOKU_INC.'lib/scripts/helpers.js', 51 DOKU_INC.'lib/scripts/delay.js', 52 DOKU_INC.'lib/scripts/cookie.js', 53 DOKU_INC.'lib/scripts/script.js', 54 DOKU_INC.'lib/scripts/qsearch.js', 55 DOKU_INC.'lib/scripts/search.js', 56 DOKU_INC.'lib/scripts/tree.js', 57 DOKU_INC.'lib/scripts/index.js', 58 DOKU_INC.'lib/scripts/textselection.js', 59 DOKU_INC.'lib/scripts/toolbar.js', 60 DOKU_INC.'lib/scripts/edit.js', 61 DOKU_INC.'lib/scripts/editor.js', 62 DOKU_INC.'lib/scripts/locktimer.js', 63 DOKU_INC.'lib/scripts/linkwiz.js', 64 DOKU_INC.'lib/scripts/media.js', 65 DOKU_INC.'lib/scripts/compatibility.js', 66# disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 67 DOKU_INC.'lib/scripts/behaviour.js', 68 DOKU_INC.'lib/scripts/page.js', 69 tpl_incdir($tpl).'script.js', 70 ); 71 72 // add possible plugin scripts and userscript 73 $files = array_merge($files,js_pluginscripts()); 74 if(is_array($config_cascade['userscript']['default'])) { 75 foreach($config_cascade['userscript']['default'] as $userscript) { 76 $files[] = $userscript; 77 } 78 } 79 80 // Let plugins decide to either put more scripts here or to remove some 81 Event::createAndTrigger('JS_SCRIPT_LIST', $files); 82 83 // The generated script depends on some dynamic options 84 $cache = new Cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].md5(serialize($files)),'.js'); 85 $cache->setEvent('JS_CACHE_USE'); 86 87 $cache_files = array_merge($files, getConfigFiles('main')); 88 $cache_files[] = __FILE__; 89 90 // check cache age & handle conditional request 91 // This may exit if a cache can be used 92 $cache_ok = $cache->useCache(array('files' => $cache_files)); 93 http_cached($cache->cache, $cache_ok); 94 95 // start output buffering and build the script 96 ob_start(); 97 98 // add some global variables 99 print "var DOKU_BASE = '".DOKU_BASE."';"; 100 print "var DOKU_TPL = '".tpl_basedir($tpl)."';"; 101 print "var DOKU_COOKIE_PARAM = " . json_encode( 102 array( 103 'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'], 104 'secure' => $conf['securecookie'] && is_ssl() 105 )).";"; 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']).";\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()).");\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 if(!file_exists($file)) return; 177 static $loaded = array(); 178 179 $data = io_readFile($file); 180 while(preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#',$data,$match)){ 181 $ifile = $match[2]; 182 183 // is it a include_once? 184 if($match[1]){ 185 $base = \dokuwiki\Utf8\PhpString::basename($ifile); 186 if(array_key_exists($base, $loaded) && $loaded[$base] === true){ 187 $data = str_replace($match[0], '' ,$data); 188 continue; 189 } 190 $loaded[$base] = true; 191 } 192 193 if($ifile[0] != '/') $ifile = dirname($file).'/'.$ifile; 194 195 $idata = ''; 196 if (file_exists($ifile)) { 197 $ismin = (substr($ifile, -7) == '.min.js');; 198 if ($ismin) $idata .= "\n/* BEGIN NOCOMPRESS */\n"; 199 $idata .= io_readFile($ifile); 200 if ($ismin) $idata .= "\n/* END NOCOMPRESS */\n"; 201 } 202 $data = str_replace($match[0],$idata,$data); 203 } 204 echo "$data\n"; 205} 206 207/** 208 * Returns a list of possible Plugin Scripts (no existance check here) 209 * 210 * @author Andreas Gohr <andi@splitbrain.org> 211 * 212 * @return array 213 */ 214function js_pluginscripts(){ 215 $list = array(); 216 $plugins = plugin_list(); 217 foreach ($plugins as $p){ 218 $list[] = DOKU_PLUGIN."$p/script.js"; 219 } 220 return $list; 221} 222 223/** 224 * Return an two-dimensional array with strings from the language file of each plugin. 225 * 226 * - $lang['js'] must be an array. 227 * - Nothing is returned for plugins without an entry for $lang['js'] 228 * 229 * @author Gabriel Birke <birke@d-scribe.de> 230 * 231 * @return array 232 */ 233function js_pluginstrings() { 234 global $conf, $config_cascade; 235 $pluginstrings = array(); 236 $plugins = plugin_list(); 237 foreach($plugins as $p) { 238 $path = DOKU_PLUGIN . $p . '/lang/'; 239 240 if(isset($lang)) unset($lang); 241 if(file_exists($path . "en/lang.php")) { 242 include $path . "en/lang.php"; 243 } 244 foreach($config_cascade['lang']['plugin'] as $config_file) { 245 if(file_exists($config_file . $p . '/en/lang.php')) { 246 include($config_file . $p . '/en/lang.php'); 247 } 248 } 249 if(isset($conf['lang']) && $conf['lang'] != 'en') { 250 if(file_exists($path . $conf['lang'] . "/lang.php")) { 251 include($path . $conf['lang'] . '/lang.php'); 252 } 253 foreach($config_cascade['lang']['plugin'] as $config_file) { 254 if(file_exists($config_file . $p . '/' . $conf['lang'] . '/lang.php')) { 255 include($config_file . $p . '/' . $conf['lang'] . '/lang.php'); 256 } 257 } 258 } 259 260 if(isset($lang['js'])) { 261 $pluginstrings[$p] = $lang['js']; 262 } 263 } 264 return $pluginstrings; 265} 266 267/** 268 * Return an two-dimensional array with strings from the language file of current active template. 269 * 270 * - $lang['js'] must be an array. 271 * - Nothing is returned for template without an entry for $lang['js'] 272 * 273 * @param string $tpl 274 * @return array 275 */ 276function js_templatestrings($tpl) { 277 global $conf, $config_cascade; 278 279 $path = tpl_incdir() . 'lang/'; 280 281 $templatestrings = array(); 282 if(file_exists($path . "en/lang.php")) { 283 include $path . "en/lang.php"; 284 } 285 foreach($config_cascade['lang']['template'] as $config_file) { 286 if(file_exists($config_file . $conf['template'] . '/en/lang.php')) { 287 include($config_file . $conf['template'] . '/en/lang.php'); 288 } 289 } 290 if(isset($conf['lang']) && $conf['lang'] != 'en' && file_exists($path . $conf['lang'] . "/lang.php")) { 291 include $path . $conf['lang'] . "/lang.php"; 292 } 293 if(isset($conf['lang']) && $conf['lang'] != 'en') { 294 if(file_exists($path . $conf['lang'] . "/lang.php")) { 295 include $path . $conf['lang'] . "/lang.php"; 296 } 297 foreach($config_cascade['lang']['template'] as $config_file) { 298 if(file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) { 299 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php'); 300 } 301 } 302 } 303 304 if(isset($lang['js'])) { 305 $templatestrings[$tpl] = $lang['js']; 306 } 307 return $templatestrings; 308} 309 310/** 311 * Escapes a String to be embedded in a JavaScript call, keeps \n 312 * as newline 313 * 314 * @author Andreas Gohr <andi@splitbrain.org> 315 * 316 * @param string $string 317 * @return string 318 */ 319function js_escape($string){ 320 return str_replace('\\\\n','\\n',addslashes($string)); 321} 322 323/** 324 * Adds the given JavaScript code to the window.onload() event 325 * 326 * @author Andreas Gohr <andi@splitbrain.org> 327 * 328 * @param string $func 329 */ 330function js_runonstart($func){ 331 echo "jQuery(function(){ $func; });".NL; 332} 333