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 9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 11if(!defined('NL')) define('NL',"\n"); 12if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here 13require_once(DOKU_INC.'inc/init.php'); 14 15// Main (don't run when UNIT test) 16if(!defined('SIMPLE_TEST')){ 17 header('Content-Type: application/javascript; charset=utf-8'); 18 js_out(); 19} 20 21 22// ---------------------- functions ------------------------------ 23 24/** 25 * Output all needed JavaScript 26 * 27 * @author Andreas Gohr <andi@splitbrain.org> 28 */ 29function js_out(){ 30 global $conf; 31 global $lang; 32 global $config_cascade; 33 global $INPUT; 34 35 // decide from where to get the template 36 $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t'))); 37 if(!$tpl) $tpl = $conf['template']; 38 39 // array of core files 40 $files = array( 41 DOKU_INC.'lib/scripts/jquery/jquery.cookie.js', 42 DOKU_INC.'inc/lang/'.$conf['lang'].'/jquery.ui.datepicker.js', 43 DOKU_INC."lib/scripts/fileuploader.js", 44 DOKU_INC."lib/scripts/fileuploaderextended.js", 45 DOKU_INC.'lib/scripts/helpers.js', 46 DOKU_INC.'lib/scripts/delay.js', 47 DOKU_INC.'lib/scripts/cookie.js', 48 DOKU_INC.'lib/scripts/script.js', 49 DOKU_INC.'lib/scripts/qsearch.js', 50 DOKU_INC.'lib/scripts/tree.js', 51 DOKU_INC.'lib/scripts/index.js', 52 DOKU_INC.'lib/scripts/textselection.js', 53 DOKU_INC.'lib/scripts/toolbar.js', 54 DOKU_INC.'lib/scripts/edit.js', 55 DOKU_INC.'lib/scripts/editor.js', 56 DOKU_INC.'lib/scripts/locktimer.js', 57 DOKU_INC.'lib/scripts/linkwiz.js', 58 DOKU_INC.'lib/scripts/media.js', 59 DOKU_INC.'lib/scripts/compatibility.js', 60# disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 61 DOKU_INC.'lib/scripts/behaviour.js', 62 DOKU_INC.'lib/scripts/page.js', 63 tpl_incdir($tpl).'script.js', 64 ); 65 66 // add possible plugin scripts and userscript 67 $files = array_merge($files,js_pluginscripts()); 68 if(!empty($config_cascade['userscript']['default'])) { 69 foreach($config_cascade['userscript']['default'] as $userscript) { 70 $files[] = $userscript; 71 } 72 } 73 74 // Let plugins decide to either put more scripts here or to remove some 75 trigger_event('JS_SCRIPT_LIST', $files); 76 77 // The generated script depends on some dynamic options 78 $cache = new cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].md5(serialize($files)),'.js'); 79 $cache->_event = 'JS_CACHE_USE'; 80 81 $cache_files = array_merge($files, getConfigFiles('main')); 82 $cache_files[] = __FILE__; 83 84 // check cache age & handle conditional request 85 // This may exit if a cache can be used 86 $cache_ok = $cache->useCache(array('files' => $cache_files)); 87 http_cached($cache->cache, $cache_ok); 88 89 // start output buffering and build the script 90 ob_start(); 91 92 $json = new JSON(); 93 // add some global variables 94 print "var DOKU_BASE = '".DOKU_BASE."';"; 95 print "var DOKU_TPL = '".tpl_basedir($tpl)."';"; 96 print "var DOKU_COOKIE_PARAM = " . $json->encode( 97 array( 98 'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'], 99 'secure' => $conf['securecookie'] && is_ssl() 100 )).";"; 101 // FIXME: Move those to JSINFO 102 print "var DOKU_UHN = ".((int) useHeading('navigation')).";"; 103 print "var DOKU_UHC = ".((int) useHeading('content')).";"; 104 105 // load JS specific translations 106 $lang['js']['plugins'] = js_pluginstrings(); 107 $templatestrings = js_templatestrings($tpl); 108 if(!empty($templatestrings)) { 109 $lang['js']['template'] = $templatestrings; 110 } 111 echo 'LANG = '.$json->encode($lang['js']).";\n"; 112 113 // load toolbar 114 toolbar_JSdefines('toolbar'); 115 116 // load files 117 foreach($files as $file){ 118 if(!file_exists($file)) continue; 119 $ismin = (substr($file,-7) == '.min.js'); 120 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC.'lib/scripts/') !== 0); 121 122 echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n"; 123 if($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 124 if ($debugjs) echo "\ntry {\n"; 125 js_load($file); 126 if ($debugjs) echo "\n} catch (e) {\n logError(e, '".str_replace(DOKU_INC, '', $file)."');\n}\n"; 127 if($ismin) echo "\n/* END NOCOMPRESS */\n"; 128 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 129 } 130 131 // init stuff 132 if($conf['locktime'] != 0){ 133 js_runonstart("dw_locktimer.init(".($conf['locktime'] - 60).",".$conf['usedraft'].")"); 134 } 135 // init hotkeys - must have been done after init of toolbar 136# disabled for FS#1958 js_runonstart('initializeHotkeys()'); 137 138 // end output buffering and get contents 139 $js = ob_get_contents(); 140 ob_end_clean(); 141 142 // strip any source maps 143 stripsourcemaps($js); 144 145 // compress whitespace and comments 146 if($conf['compress']){ 147 $js = js_compress($js); 148 } 149 150 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 151 152 http_cached_finish($cache->cache, $js); 153} 154 155/** 156 * Load the given file, handle include calls and print it 157 * 158 * @author Andreas Gohr <andi@splitbrain.org> 159 * 160 * @param string $file filename path to file 161 */ 162function js_load($file){ 163 if(!file_exists($file)) return; 164 static $loaded = array(); 165 166 $data = io_readFile($file); 167 while(preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#',$data,$match)){ 168 $ifile = $match[2]; 169 170 // is it a include_once? 171 if($match[1]){ 172 $base = utf8_basename($ifile); 173 if(array_key_exists($base, $loaded) && $loaded[$base] === true){ 174 $data = str_replace($match[0], '' ,$data); 175 continue; 176 } 177 $loaded[$base] = true; 178 } 179 180 if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile; 181 182 if(file_exists($ifile)){ 183 $idata = io_readFile($ifile); 184 }else{ 185 $idata = ''; 186 } 187 $data = str_replace($match[0],$idata,$data); 188 } 189 echo "$data\n"; 190} 191 192/** 193 * Returns a list of possible Plugin Scripts (no existance check here) 194 * 195 * @author Andreas Gohr <andi@splitbrain.org> 196 * 197 * @return array 198 */ 199function js_pluginscripts(){ 200 $list = array(); 201 $plugins = plugin_list(); 202 foreach ($plugins as $p){ 203 $list[] = DOKU_PLUGIN."$p/script.js"; 204 } 205 return $list; 206} 207 208/** 209 * Return an two-dimensional array with strings from the language file of each plugin. 210 * 211 * - $lang['js'] must be an array. 212 * - Nothing is returned for plugins without an entry for $lang['js'] 213 * 214 * @author Gabriel Birke <birke@d-scribe.de> 215 * 216 * @return array 217 */ 218function js_pluginstrings() { 219 global $conf, $config_cascade; 220 $pluginstrings = array(); 221 $plugins = plugin_list(); 222 foreach($plugins as $p) { 223 $path = DOKU_PLUGIN . $p . '/lang/'; 224 225 if(isset($lang)) unset($lang); 226 if(file_exists($path . "en/lang.php")) { 227 include $path . "en/lang.php"; 228 } 229 foreach($config_cascade['lang']['plugin'] as $config_file) { 230 if(file_exists($config_file . $p . '/en/lang.php')) { 231 include($config_file . $p . '/en/lang.php'); 232 } 233 } 234 if(isset($conf['lang']) && $conf['lang'] != 'en') { 235 if(file_exists($path . $conf['lang'] . "/lang.php")) { 236 include($path . $conf['lang'] . '/lang.php'); 237 } 238 foreach($config_cascade['lang']['plugin'] as $config_file) { 239 if(file_exists($config_file . $p . '/' . $conf['lang'] . '/lang.php')) { 240 include($config_file . $p . '/' . $conf['lang'] . '/lang.php'); 241 } 242 } 243 } 244 245 if(isset($lang['js'])) { 246 $pluginstrings[$p] = $lang['js']; 247 } 248 } 249 return $pluginstrings; 250} 251 252/** 253 * Return an two-dimensional array with strings from the language file of current active template. 254 * 255 * - $lang['js'] must be an array. 256 * - Nothing is returned for template without an entry for $lang['js'] 257 * 258 * @param string $tpl 259 * @return array 260 */ 261function js_templatestrings($tpl) { 262 global $conf, $config_cascade; 263 264 $path = tpl_incdir() . 'lang/'; 265 266 $templatestrings = array(); 267 if(file_exists($path . "en/lang.php")) { 268 include $path . "en/lang.php"; 269 } 270 foreach($config_cascade['lang']['template'] as $config_file) { 271 if(file_exists($config_file . $conf['template'] . '/en/lang.php')) { 272 include($config_file . $conf['template'] . '/en/lang.php'); 273 } 274 } 275 if(isset($conf['lang']) && $conf['lang'] != 'en' && file_exists($path . $conf['lang'] . "/lang.php")) { 276 include $path . $conf['lang'] . "/lang.php"; 277 } 278 if(isset($conf['lang']) && $conf['lang'] != 'en') { 279 if(file_exists($path . $conf['lang'] . "/lang.php")) { 280 include $path . $conf['lang'] . "/lang.php"; 281 } 282 foreach($config_cascade['lang']['template'] as $config_file) { 283 if(file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) { 284 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php'); 285 } 286 } 287 } 288 289 if(isset($lang['js'])) { 290 $templatestrings[$tpl] = $lang['js']; 291 } 292 return $templatestrings; 293} 294 295/** 296 * Escapes a String to be embedded in a JavaScript call, keeps \n 297 * as newline 298 * 299 * @author Andreas Gohr <andi@splitbrain.org> 300 * 301 * @param string $string 302 * @return string 303 */ 304function js_escape($string){ 305 return str_replace('\\\\n','\\n',addslashes($string)); 306} 307 308/** 309 * Adds the given JavaScript code to the window.onload() event 310 * 311 * @author Andreas Gohr <andi@splitbrain.org> 312 * 313 * @param string $func 314 */ 315function js_runonstart($func){ 316 echo "jQuery(function(){ $func; });".NL; 317} 318 319/** 320 * Strip comments and whitespaces from given JavaScript Code 321 * 322 * This is a port of Nick Galbreath's python tool jsstrip.py which is 323 * released under BSD license. See link for original code. 324 * 325 * @author Nick Galbreath <nickg@modp.com> 326 * @author Andreas Gohr <andi@splitbrain.org> 327 * @link http://code.google.com/p/jsstrip/ 328 * 329 * @param string $s 330 * @return string 331 */ 332function js_compress($s){ 333 $s = ltrim($s); // strip all initial whitespace 334 $s .= "\n"; 335 $i = 0; // char index for input string 336 $j = 0; // char forward index for input string 337 $line = 0; // line number of file (close to it anyways) 338 $slen = strlen($s); // size of input string 339 $lch = ''; // last char added 340 $result = ''; // we store the final result here 341 342 // items that don't need spaces next to them 343 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 344 345 // items which need a space if the sign before and after whitespace is equal. 346 // E.g. '+ ++' may not be compressed to '+++' --> syntax error. 347 $ops = "+-"; 348 349 $regex_starters = array("(", "=", "[", "," , ":", "!", "&", "|"); 350 351 $whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B"); 352 353 while($i < $slen){ 354 // skip all "boring" characters. This is either 355 // reserved word (e.g. "for", "else", "if") or a 356 // variable/object/method (e.g. "foo.color") 357 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 358 $result .= $s{$i}; 359 $i = $i + 1; 360 } 361 362 $ch = $s{$i}; 363 // multiline comments (keeping IE conditionals) 364 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 365 $endC = strpos($s,'*/',$i+2); 366 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 367 368 // check if this is a NOCOMPRESS comment 369 if(substr($s, $i, $endC+2-$i) == '/* BEGIN NOCOMPRESS */'){ 370 $endNC = strpos($s, '/* END NOCOMPRESS */', $endC+2); 371 if($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR); 372 373 // verbatim copy contents, trimming but putting it on its own line 374 $result .= "\n".trim(substr($s, $i + 22, $endNC - ($i + 22)))."\n"; // BEGIN comment = 22 chars 375 $i = $endNC + 20; // END comment = 20 chars 376 }else{ 377 $i = $endC + 2; 378 } 379 continue; 380 } 381 382 // singleline 383 if($ch == '/' && $s{$i+1} == '/'){ 384 $endC = strpos($s,"\n",$i+2); 385 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 386 $i = $endC; 387 continue; 388 } 389 390 // tricky. might be an RE 391 if($ch == '/'){ 392 // rewind, skip white space 393 $j = 1; 394 while(in_array($s{$i-$j}, $whitespaces_chars)){ 395 $j = $j + 1; 396 } 397 if( in_array($s{$i-$j}, $regex_starters) ){ 398 // yes, this is an re 399 // now move forward and find the end of it 400 $j = 1; 401 while($s{$i+$j} != '/'){ 402 if($s{$i+$j} == '\\') $j = $j + 2; 403 else $j++; 404 } 405 $result .= substr($s,$i,$j+1); 406 $i = $i + $j + 1; 407 continue; 408 } 409 } 410 411 // double quote strings 412 if($ch == '"'){ 413 $j = 1; 414 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 415 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 416 $j += 2; 417 }else{ 418 $j += 1; 419 } 420 } 421 $string = substr($s,$i,$j+1); 422 // remove multiline markers: 423 $string = str_replace("\\\n",'',$string); 424 $result .= $string; 425 $i = $i + $j + 1; 426 continue; 427 } 428 429 // single quote strings 430 if($ch == "'"){ 431 $j = 1; 432 while( $s{$i+$j} != "'" && ($i+$j < $slen)){ 433 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){ 434 $j += 2; 435 }else{ 436 $j += 1; 437 } 438 } 439 $string = substr($s,$i,$j+1); 440 // remove multiline markers: 441 $string = str_replace("\\\n",'',$string); 442 $result .= $string; 443 $i = $i + $j + 1; 444 continue; 445 } 446 447 // whitespaces 448 if( $ch == ' ' || $ch == "\r" || $ch == "\n" || $ch == "\t" ){ 449 $lch = substr($result,-1); 450 451 // Only consider deleting whitespace if the signs before and after 452 // are not equal and are not an operator which may not follow itself. 453 if ($i+1 < $slen && ((!$lch || $s[$i+1] == ' ') 454 || $lch != $s[$i+1] 455 || strpos($ops,$s[$i+1]) === false)) { 456 // leading spaces 457 if($i+1 < $slen && (strpos($chars,$s[$i+1]) !== false)){ 458 $i = $i + 1; 459 continue; 460 } 461 // trailing spaces 462 // if this ch is space AND the last char processed 463 // is special, then skip the space 464 if($lch && (strpos($chars,$lch) !== false)){ 465 $i = $i + 1; 466 continue; 467 } 468 } 469 470 // else after all of this convert the "whitespace" to 471 // a single space. It will get appended below 472 $ch = ' '; 473 } 474 475 // other chars 476 $result .= $ch; 477 $i = $i + 1; 478 } 479 480 return trim($result); 481} 482 483//Setup VIM: ex: et ts=4 : 484