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