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',realpath(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"); 12require_once(DOKU_INC.'inc/init.php'); 13require_once(DOKU_INC.'inc/pageutils.php'); 14require_once(DOKU_INC.'inc/io.php'); 15require_once(DOKU_INC.'inc/JSON.php'); 16 17// Main (don't run when UNIT test) 18if(!defined('SIMPLE_TEST')){ 19 header('Content-Type: text/javascript; charset=utf-8'); 20 js_out(); 21} 22 23 24// ---------------------- functions ------------------------------ 25 26/** 27 * Output all needed JavaScript 28 * 29 * @author Andreas Gohr <andi@splitbrain.org> 30 */ 31function js_out(){ 32 global $conf; 33 global $lang; 34 $edit = (bool) $_REQUEST['edit']; // edit or preview mode? 35 $write = (bool) $_REQUEST['write']; // writable? 36 37 // The generated script depends on some dynamic options 38 $cache = getCacheName('scripts'.$edit.'x'.$write,'.js'); 39 40 // Array of needed files 41 $files = array( 42 DOKU_INC.'lib/scripts/helpers.js', 43 DOKU_INC.'lib/scripts/events.js', 44 DOKU_INC.'lib/scripts/cookie.js', 45 DOKU_INC.'lib/scripts/script.js', 46 DOKU_INC.'lib/scripts/tw-sack.js', 47 DOKU_INC.'lib/scripts/ajax.js', 48 ); 49 if($edit){ 50 if($write){ 51 $files[] = DOKU_INC.'lib/scripts/edit.js'; 52 if($conf['spellchecker']){ 53 $files[] = DOKU_INC.'lib/scripts/spellcheck.js'; 54 } 55 } 56 $files[] = DOKU_INC.'lib/scripts/media.js'; 57 } 58 $files[] = DOKU_TPLINC.'script.js'; 59 60 // get possible plugin scripts 61 $plugins = js_pluginscripts(); 62 63 // check cache age & handle conditional request 64 header('Cache-Control: public, max-age=3600'); 65 header('Pragma: public'); 66 if(js_cacheok($cache,array_merge($files,$plugins))){ 67 http_conditionalRequest(filemtime($cache)); 68 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 69 readfile($cache); 70 return; 71 } else { 72 http_conditionalRequest(time()); 73 } 74 75 // start output buffering and build the script 76 ob_start(); 77 78 // add some global variables 79 print "var DOKU_BASE = '".DOKU_BASE."';"; 80 print "var DOKU_TPL = '".DOKU_TPL."';"; 81 82 //FIXME: move thes into LANG 83 print "var alertText = '".js_escape($lang['qb_alert'])."';"; 84 print "var notSavedYet = '".js_escape($lang['notsavedyet'])."';"; 85 print "var reallyDel = '".js_escape($lang['del_confirm'])."';"; 86 87 // load JS specific translations 88 $json = new JSON(); 89 echo 'LANG = '.$json->encode($lang['js']).";\n"; 90 91 // load files 92 foreach($files as $file){ 93 echo "\n\n/* XXXXXXXXXX begin of $file XXXXXXXXXX */\n\n"; 94 js_load($file); 95 echo "\n\n/* XXXXXXXXXX end of $file XXXXXXXXXX */\n\n"; 96 } 97 98 // init stuff 99 js_runonstart("ajax_qsearch.init('qsearch__in','qsearch__out')"); 100 js_runonstart("addEvent(document,'click',closePopups)"); 101 js_runonstart('addTocToggle()'); 102 103 if($edit){ 104 // size controls 105 js_runonstart("initSizeCtl('size__ctl','wiki__text')"); 106 107 if($write){ 108 require_once(DOKU_INC.'inc/toolbar.php'); 109 toolbar_JSdefines('toolbar'); 110 js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)"); 111 112 // add pageleave check 113 js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')"); 114 115 // add lock timer 116 js_runonstart("locktimer.init(".($conf['locktime'] - 60).",'".js_escape($lang['willexpire'])."',".$conf['usedraft'].")"); 117 118 // load spell checker 119 if($conf['spellchecker']){ 120 js_runonstart("ajax_spell.init('". 121 js_escape($lang['spell_start'])."','". 122 js_escape($lang['spell_stop'])."','". 123 js_escape($lang['spell_wait'])."','". 124 js_escape($lang['spell_noerr'])."','". 125 js_escape($lang['spell_nosug'])."','". 126 js_escape($lang['spell_change'])."')"); 127 } 128 } 129 } 130 131 // load plugin scripts (suppress warnings for missing ones) 132 foreach($plugins as $plugin){ 133 if (@file_exists($plugin)) { 134 echo "\n\n/* XXXXXXXXXX begin of $plugin XXXXXXXXXX */\n\n"; 135 js_load($plugin); 136 echo "\n\n/* XXXXXXXXXX end of $plugin XXXXXXXXXX */\n\n"; 137 } 138 } 139 140 // load user script 141 @readfile(DOKU_CONF.'userscript.js'); 142 143 // add scroll event and tooltip rewriting 144 js_runonstart('updateAccessKeyTooltip()'); 145 js_runonstart('scrollToMarker()'); 146 js_runonstart('focusMarker()'); 147 148 // end output buffering and get contents 149 $js = ob_get_contents(); 150 ob_end_clean(); 151 152 // compress whitespace and comments 153 if($conf['compress']){ 154 $js = js_compress($js); 155 } 156 157 $js .= "\n"; // https://bugzilla.mozilla.org/show_bug.cgi?id=316033 158 159 // save cache file 160 io_saveFile($cache,$js); 161 162 // finally send output 163 print $js; 164} 165 166/** 167 * Load the given file, handle include calls and print it 168 * 169 * @author Andreas Gohr <andi@splitbrain.org> 170 */ 171function js_load($file){ 172 if(!@file_exists($file)) return; 173 static $loaded = array(); 174 175 $data = io_readFile($file); 176 while(preg_match('#/\*\s*DOKUWIKI:include(_once)\s+([\w\./]+)\s*\*/#',$data,$match)){ 177 $ifile = $match[2]; 178 179 // is it a include_once? 180 if($match[1]){ 181 $base = basename($ifile); 182 if($loaded[$base]) continue; 183 $loaded[$base] = true; 184 } 185 186 if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile; 187 188 if(@file_exists($ifile)){ 189 $idata = io_readFile($ifile); 190 }else{ 191 $idata = ''; 192 } 193 $data = str_replace($match[0],$idata,$data); 194 } 195 echo $data; 196} 197 198/** 199 * Checks if a JavaScript Cache file still is valid 200 * 201 * @author Andreas Gohr <andi@splitbrain.org> 202 */ 203function js_cacheok($cache,$files){ 204 if($_REQUEST['purge']) return false; //support purge request 205 206 $ctime = @filemtime($cache); 207 if(!$ctime) return false; //There is no cache 208 209 // some additional files to check 210 $files[] = DOKU_CONF.'dokuwiki.php'; 211 $files[] = DOKU_CONF.'local.php'; 212 $files[] = DOKU_CONF.'userscript.js'; 213 $files[] = __FILE__; 214 215 // now walk the files 216 foreach($files as $file){ 217 if(@filemtime($file) > $ctime){ 218 return false; 219 } 220 } 221 return true; 222} 223 224/** 225 * Returns a list of possible Plugin Scripts (no existance check here) 226 * 227 * @author Andreas Gohr <andi@splitbrain.org> 228 */ 229function js_pluginscripts(){ 230 $list = array(); 231 $plugins = plugin_list(); 232 foreach ($plugins as $p){ 233 $list[] = DOKU_PLUGIN."$p/script.js"; 234 } 235 return $list; 236} 237 238/** 239 * Escapes a String to be embedded in a JavaScript call, keeps \n 240 * as newline 241 * 242 * @author Andreas Gohr <andi@splitbrain.org> 243 */ 244function js_escape($string){ 245 return str_replace('\\\\n','\\n',addslashes($string)); 246} 247 248/** 249 * Adds the given JavaScript code to the window.onload() event 250 * 251 * @author Andreas Gohr <andi@splitbrain.org> 252 */ 253function js_runonstart($func){ 254 echo "addInitEvent(function(){ $func; });".NL; 255} 256 257/** 258 * Strip comments and whitespaces from given JavaScript Code 259 * 260 * This is a port of Nick Galbreath's python tool jsstrip.py which is 261 * released under BSD license. See link for original code. 262 * 263 * @author Nick Galbreath <nickg@modp.com> 264 * @author Andreas Gohr <andi@splitbrain.org> 265 * @link http://code.google.com/p/jsstrip/ 266 */ 267function js_compress($s){ 268 $s = ltrim($s); // strip all initial whitespace 269 $s .= "\n"; 270 $i = 0; // char index for input string 271 $j = 0; // char forward index for input string 272 $line = 0; // line number of file (close to it anyways) 273 $slen = strlen($s); // size of input string 274 $lch = ''; // last char added 275 $result = ''; // we store the final result here 276 277 // items that don't need spaces next to them 278 $chars = "^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'\"[]"; 279 280 while($i < $slen){ 281 // skip all "boring" characters. This is either 282 // reserved word (e.g. "for", "else", "if") or a 283 // variable/object/method (e.g. "foo.color") 284 while ($i < $slen && (strpos($chars,$s[$i]) === false) ){ 285 $result .= $s{$i}; 286 $i = $i + 1; 287 } 288 289 $ch = $s{$i}; 290 // multiline comments (keeping IE conditionals) 291 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 292 $endC = strpos($s,'*/',$i+2); 293 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 294 $i = $endC + 2; 295 continue; 296 } 297 298 // singleline 299 if($ch == '/' && $s{$i+1} == '/'){ 300 $endC = strpos($s,"\n",$i+2); 301 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 302 $i = $endC; 303 continue; 304 } 305 306 // tricky. might be an RE 307 if($ch == '/'){ 308 // rewind, skip white space 309 $j = 1; 310 while($s{$i-$j} == ' '){ 311 $j = $j + 1; 312 } 313 if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ 314 // yes, this is an re 315 // now move forward and find the end of it 316 $j = 1; 317 while($s{$i+$j} != '/'){ 318 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ 319 $j = $j + 1; 320 } 321 if($s{$i+$j} == '\\') $j = $j + 2; 322 } 323 $result .= substr($s,$i,$j+1); 324 $i = $i + $j + 1; 325 continue; 326 } 327 } 328 329 // double quote strings 330 if($ch == '"'){ 331 $j = 1; 332 while( $s{$i+$j} != '"' && ($i+$j < $slen)){ 333 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){ 334 $j += 2; 335 }else{ 336 $j += 1; 337 } 338 } 339 $result .= substr($s,$i,$j+1); 340 $i = $i + $j + 1; 341 continue; 342 } 343 344 // single quote strings 345 if($ch == "'"){ 346 $j = 1; 347 while( $s{$i+$j} != "'" && ($i+$j < $slen)){ 348 if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){ 349 $j += 2; 350 }else{ 351 $j += 1; 352 } 353 } 354 $result .= substr($s,$i,$j+1); 355 $i = $i + $j + 1; 356 continue; 357 } 358 359 // whitespaces 360 if( $ch == ' ' || $ch == "\r" || $ch == "\n" || $ch == "\t" ){ 361 // leading spaces 362 if($i+1 < $slen && (strpos($chars,$s[$i+1]) !== false)){ 363 $i = $i + 1; 364 continue; 365 } 366 // trailing spaces 367 // if this ch is space AND the last char processed 368 // is special, then skip the space 369 $lch = substr($result,-1); 370 if($lch && (strpos($chars,$lch) !== false)){ 371 $i = $i + 1; 372 continue; 373 } 374 // else after all of this convert the "whitespace" to 375 // a single space. It will get appended below 376 $ch = ' '; 377 } 378 379 // other chars 380 $result .= $ch; 381 $i = $i + 1; 382 } 383 384 return trim($result); 385} 386 387//Setup VIM: ex: et ts=4 enc=utf-8 : 388?> 389