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 = new cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.js'); 36 $cache->_event = 'JS_CACHE_USE'; 37 38 // load minified version for some files 39 $min = $conf['compress'] ? '.min' : ''; 40 41 // array of core files 42 $files = array( 43 DOKU_INC."lib/scripts/jquery/jquery$min.js", 44 DOKU_INC.'lib/scripts/jquery/jquery.cookie.js', 45 DOKU_INC."lib/scripts/jquery/jquery-ui$min.js", 46 DOKU_INC."lib/scripts/jquery/jquery-migrate$min.js", 47 DOKU_INC."lib/scripts/fileuploader.js", 48 DOKU_INC."lib/scripts/fileuploaderextended.js", 49 DOKU_INC.'lib/scripts/helpers.js', 50 DOKU_INC.'lib/scripts/delay.js', 51 DOKU_INC.'lib/scripts/cookie.js', 52 DOKU_INC.'lib/scripts/script.js', 53 DOKU_INC.'lib/scripts/tw-sack.js', 54 DOKU_INC.'lib/scripts/qsearch.js', 55 DOKU_INC.'lib/scripts/tree.js', 56 DOKU_INC.'lib/scripts/index.js', 57 DOKU_INC.'lib/scripts/drag.js', 58 DOKU_INC.'lib/scripts/textselection.js', 59 DOKU_INC.'lib/scripts/toolbar.js', 60 DOKU_INC.'lib/scripts/edit.js', 61 DOKU_INC.'lib/scripts/editor.js', 62 DOKU_INC.'lib/scripts/locktimer.js', 63 DOKU_INC.'lib/scripts/linkwiz.js', 64 DOKU_INC.'lib/scripts/media.js', 65# deprecated DOKU_INC.'lib/scripts/compatibility.js', 66# disabled for FS#1958 DOKU_INC.'lib/scripts/hotkeys.js', 67 DOKU_INC.'lib/scripts/behaviour.js', 68 DOKU_INC.'lib/scripts/page.js', 69 tpl_incdir().'script.js', 70 ); 71 72 // add possible plugin scripts and userscript 73 $files = array_merge($files,js_pluginscripts()); 74 if(isset($config_cascade['userscript']['default'])){ 75 $files[] = $config_cascade['userscript']['default']; 76 } 77 78 $cache_files = array_merge($files, getConfigFiles('main')); 79 $cache_files[] = __FILE__; 80 81 // check cache age & handle conditional request 82 // This may exit if a cache can be used 83 $cache_ok = $cache->useCache(array('files' => $cache_files)); 84 http_cached($cache->cache, $cache_ok); 85 86 // start output buffering and build the script 87 ob_start(); 88 89 $json = new JSON(); 90 // add some global variables 91 print "var DOKU_BASE = '".DOKU_BASE."';"; 92 print "var DOKU_TPL = '".tpl_basedir()."';"; 93 print "var DOKU_COOKIE_PARAM = " . $json->encode( 94 array( 95 'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'], 96 'secure' => $conf['securecookie'] && is_ssl() 97 )).";"; 98 // FIXME: Move those to JSINFO 99 print "var DOKU_UHN = ".((int) useHeading('navigation')).";"; 100 print "var DOKU_UHC = ".((int) useHeading('content')).";"; 101 102 // load JS specific translations 103 $lang['js']['plugins'] = js_pluginstrings(); 104 $templatestrings = js_templatestrings(); 105 if(!empty($templatestrings)) { 106 $lang['js']['template'] = $templatestrings; 107 } 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 $ismin = (substr($file,-7) == '.min.js'); 116 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC.'lib/scripts/') !== 0); 117 118 echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n"; 119 if($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 120 if ($debugjs) echo "\ntry {\n"; 121 js_load($file); 122 if ($debugjs) echo "\n} catch (e) {\n logError(e, '".str_replace(DOKU_INC, '', $file)."');\n}\n"; 123 if($ismin) echo "\n/* END NOCOMPRESS */\n"; 124 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 125 } 126 127 // init stuff 128 if($conf['locktime'] != 0){ 129 js_runonstart("dw_locktimer.init(".($conf['locktime'] - 60).",".$conf['usedraft'].")"); 130 } 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 http_cached_finish($cache->cache, $js); 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 = utf8_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\n"; 178} 179 180/** 181 * Returns a list of possible Plugin Scripts (no existance check here) 182 * 183 * @author Andreas Gohr <andi@splitbrain.org> 184 */ 185function js_pluginscripts(){ 186 $list = array(); 187 $plugins = plugin_list(); 188 foreach ($plugins as $p){ 189 $list[] = DOKU_PLUGIN."$p/script.js"; 190 } 191 return $list; 192} 193 194/** 195 * Return an two-dimensional array with strings from the language file of each plugin. 196 * 197 * - $lang['js'] must be an array. 198 * - Nothing is returned for plugins without an entry for $lang['js'] 199 * 200 * @author Gabriel Birke <birke@d-scribe.de> 201 */ 202function js_pluginstrings() { 203 global $conf; 204 $pluginstrings = array(); 205 $plugins = plugin_list(); 206 foreach ($plugins as $p){ 207 if (isset($lang)) unset($lang); 208 if (@file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) { 209 include DOKU_PLUGIN."$p/lang/en/lang.php"; 210 } 211 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) { 212 include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php"; 213 } 214 if (isset($lang['js'])) { 215 $pluginstrings[$p] = $lang['js']; 216 } 217 } 218 return $pluginstrings; 219} 220 221function js_templatestrings() { 222 global $conf; 223 $templatestrings = array(); 224 if (@file_exists(tpl_incdir()."lang/en/lang.php")) { 225 include tpl_incdir()."lang/en/lang.php"; 226 } 227 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(tpl_incdir()."lang/".$conf['lang']."/lang.php")) { 228 include tpl_incdir()."lang/".$conf['lang']."/lang.php"; 229 } 230 if (isset($lang['js'])) { 231 $templatestrings[$conf['template']] = $lang['js']; 232 } 233 return $templatestrings; 234} 235 236/** 237 * Escapes a String to be embedded in a JavaScript call, keeps \n 238 * as newline 239 * 240 * @author Andreas Gohr <andi@splitbrain.org> 241 */ 242function js_escape($string){ 243 return str_replace('\\\\n','\\n',addslashes($string)); 244} 245 246/** 247 * Adds the given JavaScript code to the window.onload() event 248 * 249 * @author Andreas Gohr <andi@splitbrain.org> 250 */ 251function js_runonstart($func){ 252 echo "jQuery(function(){ $func; });".NL; 253} 254 255/** 256 * Strip comments and whitespaces from given JavaScript Code 257 * 258 * This is a port of Nick Galbreath's python tool jsstrip.py which is 259 * released under BSD license. See link for original code. 260 * 261 * @author Nick Galbreath <nickg@modp.com> 262 * @author Andreas Gohr <andi@splitbrain.org> 263 * @link http://code.google.com/p/jsstrip/ 264 */ 265function js_compress($s){ 266 $s = ltrim($s); // strip all initial whitespace 267 $s .= "\n"; 268 $i = 0; // char index for input string 269 $j = 0; // char forward index for input string 270 $line = 0; // line number of file (close to it anyways) 271 $slen = strlen($s); // size of input string 272 $lch = ''; // last char added 273 $result = ''; // we store the final result here 274 275 // items that don't need spaces next to them 276 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 277 278 $regex_starters = array("(", "=", "[", "," , ":", "!"); 279 280 $whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B"); 281 282 while($i < $slen){ 283 // skip all "boring" characters. This is either 284 // reserved word (e.g. "for", "else", "if") or a 285 // variable/object/method (e.g. "foo.color") 286 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 287 $result .= $s{$i}; 288 $i = $i + 1; 289 } 290 291 $ch = $s{$i}; 292 // multiline comments (keeping IE conditionals) 293 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 294 $endC = strpos($s,'*/',$i+2); 295 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 296 297 // check if this is a NOCOMPRESS comment 298 if(substr($s, $i, $endC+2-$i) == '/* BEGIN NOCOMPRESS */'){ 299 $endNC = strpos($s, '/* END NOCOMPRESS */', $endC+2); 300 if($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR); 301 302 // verbatim copy contents, trimming but putting it on its own line 303 $result .= "\n".trim(substr($s, $i + 22, $endNC - ($i + 22)))."\n"; // BEGIN comment = 22 chars 304 $i = $endNC + 20; // END comment = 20 chars 305 }else{ 306 $i = $endC + 2; 307 } 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 if($s{$i+$j} == '\\') $j = $j + 2; 332 else $j++; 333 } 334 $result .= substr($s,$i,$j+1); 335 $i = $i + $j + 1; 336 continue; 337 } 338 } 339 340 // double quote strings 341 if($ch == '"'){ 342 $j = 1; 343 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 344 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 345 $j += 2; 346 }else{ 347 $j += 1; 348 } 349 } 350 $string = substr($s,$i,$j+1); 351 // remove multiline markers: 352 $string = str_replace("\\\n",'',$string); 353 $result .= $string; 354 $i = $i + $j + 1; 355 continue; 356 } 357 358 // single quote strings 359 if($ch == "'"){ 360 $j = 1; 361 while( $s{$i+$j} != "'" && ($i+$j < $slen)){ 362 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){ 363 $j += 2; 364 }else{ 365 $j += 1; 366 } 367 } 368 $string = substr($s,$i,$j+1); 369 // remove multiline markers: 370 $string = str_replace("\\\n",'',$string); 371 $result .= $string; 372 $i = $i + $j + 1; 373 continue; 374 } 375 376 // whitespaces 377 if( $ch == ' ' || $ch == "\r" || $ch == "\n" || $ch == "\t" ){ 378 // leading spaces 379 if($i+1 < $slen && (strpos($chars,$s[$i+1]) !== false)){ 380 $i = $i + 1; 381 continue; 382 } 383 // trailing spaces 384 // if this ch is space AND the last char processed 385 // is special, then skip the space 386 $lch = substr($result,-1); 387 if($lch && (strpos($chars,$lch) !== false)){ 388 $i = $i + 1; 389 continue; 390 } 391 // else after all of this convert the "whitespace" to 392 // a single space. It will get appended below 393 $ch = ' '; 394 } 395 396 // other chars 397 $result .= $ch; 398 $i = $i + 1; 399 } 400 401 return trim($result); 402} 403 404//Setup VIM: ex: et ts=4 : 405