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