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