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