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