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: text/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 34 // The generated script depends on some dynamic options 35 $cache = getCacheName('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.js'); 36 37 // array of core files 38 $files = array( 39 DOKU_INC.'lib/scripts/helpers.js', 40 DOKU_INC.'lib/scripts/events.js', 41 DOKU_INC.'lib/scripts/delay.js', 42 DOKU_INC.'lib/scripts/cookie.js', 43 DOKU_INC.'lib/scripts/script.js', 44 DOKU_INC.'lib/scripts/tw-sack.js', 45 DOKU_INC.'lib/scripts/ajax.js', 46 DOKU_INC.'lib/scripts/index.js', 47 DOKU_INC.'lib/scripts/drag.js', 48 DOKU_INC.'lib/scripts/textselection.js', 49 DOKU_INC.'lib/scripts/toolbar.js', 50 DOKU_INC.'lib/scripts/edit.js', 51 DOKU_INC.'lib/scripts/locktimer.js', 52 DOKU_INC.'lib/scripts/linkwiz.js', 53 DOKU_INC.'lib/scripts/media.js', 54 DOKU_INC.'lib/scripts/subscriptions.js', 55# disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 56 DOKU_TPLINC.'script.js', 57 ); 58 59 // add possible plugin scripts and userscript 60 $files = array_merge($files,js_pluginscripts()); 61 if(isset($config_cascade['userscript']['default'])){ 62 $files[] = $config_cascade['userscript']['default']; 63 } 64 65 // check cache age & handle conditional request 66 header('Cache-Control: public, max-age=3600'); 67 header('Pragma: public'); 68 if(js_cacheok($cache,$files)){ 69 http_conditionalRequest(filemtime($cache)); 70 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 71 72 // finally send output 73 if ($conf['gzip_output'] && http_gzip_valid($cache)) { 74 header('Vary: Accept-Encoding'); 75 header('Content-Encoding: gzip'); 76 readfile($cache.".gz"); 77 } else { 78 if (!http_sendfile($cache)) readfile($cache); 79 } 80 return; 81 } else { 82 http_conditionalRequest(time()); 83 } 84 85 // start output buffering and build the script 86 ob_start(); 87 88 // add some global variables 89 print "var DOKU_BASE = '".DOKU_BASE."';"; 90 print "var DOKU_TPL = '".DOKU_TPL."';"; 91 print "var DOKU_UHN = ".((int) useHeading('navigation')).";"; 92 print "var DOKU_UHC = ".((int) useHeading('content')).";"; 93 94 // load JS specific translations 95 $json = new JSON(); 96 $lang['js']['plugins'] = js_pluginstrings(); 97 echo 'LANG = '.$json->encode($lang['js']).";\n"; 98 99 // load toolbar 100 toolbar_JSdefines('toolbar'); 101 102 // load files 103 foreach($files as $file){ 104 echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n"; 105 js_load($file); 106 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 107 } 108 109 110 // init stuff 111 js_runonstart("addEvent(document,'click',closePopups)"); 112 js_runonstart('addTocToggle()'); 113 js_runonstart("initSizeCtl('size__ctl','wiki__text')"); 114 js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)"); 115 if($conf['locktime'] != 0){ 116 js_runonstart("locktimer.init(".($conf['locktime'] - 60).",'".js_escape($lang['willexpire'])."',".$conf['usedraft'].", 'wiki__text')"); 117 } 118 js_runonstart('scrollToMarker()'); 119 js_runonstart('focusMarker()'); 120 // init hotkeys - must have been done after init of toolbar 121# disabled for FS#1958 js_runonstart('initializeHotkeys()'); 122 123 // end output buffering and get contents 124 $js = ob_get_contents(); 125 ob_end_clean(); 126 127 // compress whitespace and comments 128 if($conf['compress']){ 129 $js = js_compress($js); 130 } 131 132 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 133 134 // save cache file 135 io_saveFile($cache,$js); 136 if(function_exists('gzopen')) io_saveFile("$cache.gz",$js); 137 138 // finally send output 139 if ($conf['gzip_output']) { 140 header('Vary: Accept-Encoding'); 141 header('Content-Encoding: gzip'); 142 print gzencode($js,9,FORCE_GZIP); 143 } else { 144 print $js; 145 } 146} 147 148/** 149 * Load the given file, handle include calls and print it 150 * 151 * @author Andreas Gohr <andi@splitbrain.org> 152 */ 153function js_load($file){ 154 if(!@file_exists($file)) return; 155 static $loaded = array(); 156 157 $data = io_readFile($file); 158 while(preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#',$data,$match)){ 159 $ifile = $match[2]; 160 161 // is it a include_once? 162 if($match[1]){ 163 $base = basename($ifile); 164 if($loaded[$base]) continue; 165 $loaded[$base] = true; 166 } 167 168 if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile; 169 170 if(@file_exists($ifile)){ 171 $idata = io_readFile($ifile); 172 }else{ 173 $idata = ''; 174 } 175 $data = str_replace($match[0],$idata,$data); 176 } 177 echo $data; 178} 179 180/** 181 * Checks if a JavaScript Cache file still is valid 182 * 183 * @author Andreas Gohr <andi@splitbrain.org> 184 */ 185function js_cacheok($cache,$files){ 186 if(isset($_REQUEST['purge'])) return false; //support purge request 187 188 $ctime = @filemtime($cache); 189 if(!$ctime) return false; //There is no cache 190 191 // some additional files to check 192 $files = array_merge($files, getConfigFiles('main')); 193 $files[] = DOKU_CONF.'userscript.js'; 194 $files[] = __FILE__; 195 196 // now walk the files 197 foreach($files as $file){ 198 if(@filemtime($file) > $ctime){ 199 return false; 200 } 201 } 202 return true; 203} 204 205/** 206 * Returns a list of possible Plugin Scripts (no existance check here) 207 * 208 * @author Andreas Gohr <andi@splitbrain.org> 209 */ 210function js_pluginscripts(){ 211 $list = array(); 212 $plugins = plugin_list(); 213 foreach ($plugins as $p){ 214 $list[] = DOKU_PLUGIN."$p/script.js"; 215 } 216 return $list; 217} 218 219/** 220 * Return an two-dimensional array with strings from the language file of each plugin. 221 * 222 * - $lang['js'] must be an array. 223 * - Nothing is returned for plugins without an entry for $lang['js'] 224 * 225 * @author Gabriel Birke <birke@d-scribe.de> 226 */ 227function js_pluginstrings() 228{ 229 global $conf; 230 $pluginstrings = array(); 231 $plugins = plugin_list(); 232 foreach ($plugins as $p){ 233 if (isset($lang)) unset($lang); 234 if (@file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) { 235 include DOKU_PLUGIN."$p/lang/en/lang.php"; 236 } 237 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) { 238 include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php"; 239 } 240 if (isset($lang['js'])) { 241 $pluginstrings[$p] = $lang['js']; 242 } 243 } 244 return $pluginstrings; 245} 246 247/** 248 * Escapes a String to be embedded in a JavaScript call, keeps \n 249 * as newline 250 * 251 * @author Andreas Gohr <andi@splitbrain.org> 252 */ 253function js_escape($string){ 254 return str_replace('\\\\n','\\n',addslashes($string)); 255} 256 257/** 258 * Adds the given JavaScript code to the window.onload() event 259 * 260 * @author Andreas Gohr <andi@splitbrain.org> 261 */ 262function js_runonstart($func){ 263 echo "addInitEvent(function(){ $func; });".NL; 264} 265 266/** 267 * Strip comments and whitespaces from given JavaScript Code 268 * 269 * This is a port of Nick Galbreath's python tool jsstrip.py which is 270 * released under BSD license. See link for original code. 271 * 272 * @author Nick Galbreath <nickg@modp.com> 273 * @author Andreas Gohr <andi@splitbrain.org> 274 * @link http://code.google.com/p/jsstrip/ 275 */ 276function js_compress($s){ 277 $s = ltrim($s); // strip all initial whitespace 278 $s .= "\n"; 279 $i = 0; // char index for input string 280 $j = 0; // char forward index for input string 281 $line = 0; // line number of file (close to it anyways) 282 $slen = strlen($s); // size of input string 283 $lch = ''; // last char added 284 $result = ''; // we store the final result here 285 286 // items that don't need spaces next to them 287 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 288 289 $regex_starters = array("(", "=", "[", "," , ":"); 290 291 $whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B"); 292 293 while($i < $slen){ 294 // skip all "boring" characters. This is either 295 // reserved word (e.g. "for", "else", "if") or a 296 // variable/object/method (e.g. "foo.color") 297 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 298 $result .= $s{$i}; 299 $i = $i + 1; 300 } 301 302 $ch = $s{$i}; 303 // multiline comments (keeping IE conditionals) 304 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 305 $endC = strpos($s,'*/',$i+2); 306 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 307 $i = $endC + 2; 308 continue; 309 } 310 311 // singleline 312 if($ch == '/' && $s{$i+1} == '/'){ 313 $endC = strpos($s,"\n",$i+2); 314 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 315 $i = $endC; 316 continue; 317 } 318 319 // tricky. might be an RE 320 if($ch == '/'){ 321 // rewind, skip white space 322 $j = 1; 323 while(in_array($s{$i-$j}, $whitespaces_chars)){ 324 $j = $j + 1; 325 } 326 if( in_array($s{$i-$j}, $regex_starters) ){ 327 // yes, this is an re 328 // now move forward and find the end of it 329 $j = 1; 330 while($s{$i+$j} != '/'){ 331 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ 332 $j = $j + 1; 333 } 334 if($s{$i+$j} == '\\') $j = $j + 2; 335 } 336 $result .= substr($s,$i,$j+1); 337 $i = $i + $j + 1; 338 continue; 339 } 340 } 341 342 // double quote strings 343 if($ch == '"'){ 344 $j = 1; 345 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 346 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 347 $j += 2; 348 }else{ 349 $j += 1; 350 } 351 } 352 $result .= substr($s,$i,$j+1); 353 $i = $i + $j + 1; 354 continue; 355 } 356 357 // single quote strings 358 if($ch == "'"){ 359 $j = 1; 360 while( $s{$i+$j} != "'" && ($i+$j < $slen)){ 361 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){ 362 $j += 2; 363 }else{ 364 $j += 1; 365 } 366 } 367 $result .= substr($s,$i,$j+1); 368 $i = $i + $j + 1; 369 continue; 370 } 371 372 // whitespaces 373 if( $ch == ' ' || $ch == "\r" || $ch == "\n" || $ch == "\t" ){ 374 // leading spaces 375 if($i+1 < $slen && (strpos($chars,$s[$i+1]) !== false)){ 376 $i = $i + 1; 377 continue; 378 } 379 // trailing spaces 380 // if this ch is space AND the last char processed 381 // is special, then skip the space 382 $lch = substr($result,-1); 383 if($lch && (strpos($chars,$lch) !== false)){ 384 $i = $i + 1; 385 continue; 386 } 387 // else after all of this convert the "whitespace" to 388 // a single space. It will get appended below 389 $ch = ' '; 390 } 391 392 // other chars 393 $result .= $ch; 394 $i = $i + 1; 395 } 396 397 return trim($result); 398} 399 400//Setup VIM: ex: et ts=4 : 401