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 // add some global variables 90 print "var DOKU_BASE = '".DOKU_BASE."';"; 91 print "var DOKU_TPL = '".tpl_basedir()."';"; 92 // FIXME: Move those to JSINFO 93 print "var DOKU_UHN = ".((int) useHeading('navigation')).";"; 94 print "var DOKU_UHC = ".((int) useHeading('content')).";"; 95 96 // load JS specific translations 97 $json = new JSON(); 98 $lang['js']['plugins'] = js_pluginstrings(); 99 $templatestrings = js_templatestrings(); 100 if(!empty($templatestrings)) { 101 $lang['js']['template'] = $templatestrings; 102 } 103 echo 'LANG = '.$json->encode($lang['js']).";\n"; 104 105 // load toolbar 106 toolbar_JSdefines('toolbar'); 107 108 // load files 109 foreach($files as $file){ 110 $ismin = (substr($file,-7) == '.min.js'); 111 $debugjs = ($conf['allowdebug'] && strpos($file, DOKU_INC.'lib/scripts/') !== 0); 112 113 echo "\n\n/* XXXXXXXXXX begin of ".str_replace(DOKU_INC, '', $file) ." XXXXXXXXXX */\n\n"; 114 if($ismin) echo "\n/* BEGIN NOCOMPRESS */\n"; 115 if ($debugjs) echo "\ntry {\n"; 116 js_load($file); 117 if ($debugjs) echo "\n} catch (e) {\n logError(e, '".str_replace(DOKU_INC, '', $file)."');\n}\n"; 118 if($ismin) echo "\n/* END NOCOMPRESS */\n"; 119 echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n"; 120 } 121 122 // init stuff 123 if($conf['locktime'] != 0){ 124 js_runonstart("dw_locktimer.init(".($conf['locktime'] - 60).",".$conf['usedraft'].")"); 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 http_cached_finish($cache->cache, $js); 141} 142 143/** 144 * Load the given file, handle include calls and print it 145 * 146 * @author Andreas Gohr <andi@splitbrain.org> 147 */ 148function js_load($file){ 149 if(!@file_exists($file)) return; 150 static $loaded = array(); 151 152 $data = io_readFile($file); 153 while(preg_match('#/\*\s*DOKUWIKI:include(_once)?\s+([\w\.\-_/]+)\s*\*/#',$data,$match)){ 154 $ifile = $match[2]; 155 156 // is it a include_once? 157 if($match[1]){ 158 $base = utf8_basename($ifile); 159 if($loaded[$base]) continue; 160 $loaded[$base] = true; 161 } 162 163 if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile; 164 165 if(@file_exists($ifile)){ 166 $idata = io_readFile($ifile); 167 }else{ 168 $idata = ''; 169 } 170 $data = str_replace($match[0],$idata,$data); 171 } 172 echo "$data\n"; 173} 174 175/** 176 * Returns a list of possible Plugin Scripts (no existance check here) 177 * 178 * @author Andreas Gohr <andi@splitbrain.org> 179 */ 180function js_pluginscripts(){ 181 $list = array(); 182 $plugins = plugin_list(); 183 foreach ($plugins as $p){ 184 $list[] = DOKU_PLUGIN."$p/script.js"; 185 } 186 return $list; 187} 188 189/** 190 * Return an two-dimensional array with strings from the language file of each plugin. 191 * 192 * - $lang['js'] must be an array. 193 * - Nothing is returned for plugins without an entry for $lang['js'] 194 * 195 * @author Gabriel Birke <birke@d-scribe.de> 196 */ 197function js_pluginstrings() 198{ 199 global $conf; 200 $pluginstrings = array(); 201 $plugins = plugin_list(); 202 foreach ($plugins as $p){ 203 if (isset($lang)) unset($lang); 204 if (@file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) { 205 include DOKU_PLUGIN."$p/lang/en/lang.php"; 206 } 207 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) { 208 include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php"; 209 } 210 if (isset($lang['js'])) { 211 $pluginstrings[$p] = $lang['js']; 212 } 213 } 214 return $pluginstrings; 215} 216 217function js_templatestrings() { 218 global $conf; 219 $templatestrings = array(); 220 if (@file_exists(tpl_incdir()."lang/en/lang.php")) { 221 include tpl_incdir()."lang/en/lang.php"; 222 } 223 if (isset($conf['lang']) && $conf['lang']!='en' && @file_exists(tpl_incdir()."lang/".$conf['lang']."/lang.php")) { 224 include tpl_incdir()."lang/".$conf['lang']."/lang.php"; 225 } 226 if (isset($lang['js'])) { 227 $templatestrings[$conf['template']] = $lang['js']; 228 } 229 return $templatestrings; 230} 231 232/** 233 * Escapes a String to be embedded in a JavaScript call, keeps \n 234 * as newline 235 * 236 * @author Andreas Gohr <andi@splitbrain.org> 237 */ 238function js_escape($string){ 239 return str_replace('\\\\n','\\n',addslashes($string)); 240} 241 242/** 243 * Adds the given JavaScript code to the window.onload() event 244 * 245 * @author Andreas Gohr <andi@splitbrain.org> 246 */ 247function js_runonstart($func){ 248 echo "jQuery(function(){ $func; });".NL; 249} 250 251/** 252 * Strip comments and whitespaces from given JavaScript Code 253 * 254 * This is a port of Nick Galbreath's python tool jsstrip.py which is 255 * released under BSD license. See link for original code. 256 * 257 * @author Nick Galbreath <nickg@modp.com> 258 * @author Andreas Gohr <andi@splitbrain.org> 259 * @link http://code.google.com/p/jsstrip/ 260 */ 261function js_compress($s){ 262 $s = ltrim($s); // strip all initial whitespace 263 $s .= "\n"; 264 $i = 0; // char index for input string 265 $j = 0; // char forward index for input string 266 $line = 0; // line number of file (close to it anyways) 267 $slen = strlen($s); // size of input string 268 $lch = ''; // last char added 269 $result = ''; // we store the final result here 270 271 // items that don't need spaces next to them 272 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 273 274 $regex_starters = array("(", "=", "[", "," , ":", "!"); 275 276 $whitespaces_chars = array(" ", "\t", "\n", "\r", "\0", "\x0B"); 277 278 while($i < $slen){ 279 // skip all "boring" characters. This is either 280 // reserved word (e.g. "for", "else", "if") or a 281 // variable/object/method (e.g. "foo.color") 282 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 283 $result .= $s{$i}; 284 $i = $i + 1; 285 } 286 287 $ch = $s{$i}; 288 // multiline comments (keeping IE conditionals) 289 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 290 $endC = strpos($s,'*/',$i+2); 291 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 292 293 // check if this is a NOCOMPRESS comment 294 if(substr($s, $i, $endC+2-$i) == '/* BEGIN NOCOMPRESS */'){ 295 $endNC = strpos($s, '/* END NOCOMPRESS */', $endC+2); 296 if($endNC === false) trigger_error('Found invalid NOCOMPRESS comment', E_USER_ERROR); 297 298 // verbatim copy contents, trimming but putting it on its own line 299 $result .= "\n".trim(substr($s, $i + 22, $endNC - ($i + 22)))."\n"; // BEGIN comment = 22 chars 300 $i = $endNC + 20; // END comment = 20 chars 301 }else{ 302 $i = $endC + 2; 303 } 304 continue; 305 } 306 307 // singleline 308 if($ch == '/' && $s{$i+1} == '/'){ 309 $endC = strpos($s,"\n",$i+2); 310 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 311 $i = $endC; 312 continue; 313 } 314 315 // tricky. might be an RE 316 if($ch == '/'){ 317 // rewind, skip white space 318 $j = 1; 319 while(in_array($s{$i-$j}, $whitespaces_chars)){ 320 $j = $j + 1; 321 } 322 if( in_array($s{$i-$j}, $regex_starters) ){ 323 // yes, this is an re 324 // now move forward and find the end of it 325 $j = 1; 326 while($s{$i+$j} != '/'){ 327 if($s{$i+$j} == '\\') $j = $j + 2; 328 else $j++; 329 } 330 $result .= substr($s,$i,$j+1); 331 $i = $i + $j + 1; 332 continue; 333 } 334 } 335 336 // double quote strings 337 if($ch == '"'){ 338 $j = 1; 339 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 340 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 341 $j += 2; 342 }else{ 343 $j += 1; 344 } 345 } 346 $string = substr($s,$i,$j+1); 347 // remove multiline markers: 348 $string = str_replace("\\\n",'',$string); 349 $result .= $string; 350 $i = $i + $j + 1; 351 continue; 352 } 353 354 // single quote strings 355 if($ch == "'"){ 356 $j = 1; 357 while( $s{$i+$j} != "'" && ($i+$j < $slen)){ 358 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){ 359 $j += 2; 360 }else{ 361 $j += 1; 362 } 363 } 364 $string = substr($s,$i,$j+1); 365 // remove multiline markers: 366 $string = str_replace("\\\n",'',$string); 367 $result .= $string; 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