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/events.js', 43 DOKU_INC.'lib/scripts/cookie.js', 44 DOKU_INC.'lib/scripts/script.js', 45 DOKU_INC.'lib/scripts/tw-sack.js', 46 DOKU_INC.'lib/scripts/ajax.js', 47 DOKU_INC.'lib/scripts/domLib.js', 48 DOKU_INC.'lib/scripts/domTT.js', 49 ); 50 if($edit){ 51 if($write){ 52 $files[] = DOKU_INC.'lib/scripts/edit.js'; 53 if($conf['spellchecker']){ 54 $files[] = DOKU_INC.'lib/scripts/spellcheck.js'; 55 } 56 } 57 $files[] = DOKU_INC.'lib/scripts/media.js'; 58 } 59 $files[] = DOKU_TPLINC.'script.js'; 60 61 // get possible plugin scripts 62 $plugins = js_pluginscripts(); 63 64 // check cache age & handle conditional request 65 header('Cache-Control: public, max-age=3600'); 66 header('Pragma: public'); 67 if(js_cacheok($cache,array_merge($files,$plugins))){ 68 http_conditionalRequest(filemtime($cache)); 69 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 70 readfile($cache); 71 return; 72 } else { 73 http_conditionalRequest(time()); 74 } 75 76 // start output buffering and build the script 77 ob_start(); 78 79 // add some global variables 80 print "var DOKU_BASE = '".DOKU_BASE."';"; 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 @readfile($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 echo "\n\n/* XXXXXXXXXX begin of $file XXXXXXXXXX */\n\n"; 134 @readfile($plugin); 135 echo "\n\n/* XXXXXXXXXX end of $file XXXXXXXXXX */\n\n"; 136 } 137 138 // load user script 139 @readfile(DOKU_CONF.'userscript.js'); 140 141 // add scroll event and tooltip rewriting 142 js_runonstart('updateAccessKeyTooltip()'); 143 js_runonstart('scrollToMarker()'); 144 js_runonstart('focusMarker()'); 145 146/* // initialize init pseudo event 147 echo 'if (document.addEventListener) {'.NL; 148 echo ' document.addEventListener("DOMContentLoaded", window.fireoninit, null);'.NL; 149 echo '}'.NL; 150 echo 'addEvent(window,"load",window.fireoninit);'.NL;*/ 151 152 // end output buffering and get contents 153 $js = ob_get_contents(); 154 ob_end_clean(); 155 156 // compress whitespace and comments 157 if($conf['compress']){ 158 $js = js_compress($js); 159 } 160 161 // save cache file 162 io_saveFile($cache,$js); 163 164 // finally send output 165 print $js; 166} 167 168/** 169 * Checks if a JavaScript Cache file still is valid 170 * 171 * @author Andreas Gohr <andi@splitbrain.org> 172 */ 173function js_cacheok($cache,$files){ 174 $ctime = @filemtime($cache); 175 if(!$ctime) return false; //There is no cache 176 177 // some additional files to check 178 $files[] = DOKU_CONF.'dokuwiki.php'; 179 $files[] = DOKU_CONF.'local.php'; 180 $files[] = DOKU_CONF.'userscript.js'; 181 $files[] = __FILE__; 182 183 // now walk the files 184 foreach($files as $file){ 185 if(@filemtime($file) > $ctime){ 186 return false; 187 } 188 } 189 return true; 190} 191 192/** 193 * Returns a list of possible Plugin Scripts (no existance check here) 194 * 195 * @author Andreas Gohr <andi@splitbrain.org> 196 */ 197function js_pluginscripts(){ 198 $list = array(); 199 $plugins = plugin_list(); 200 foreach ($plugins as $p){ 201 $list[] = DOKU_PLUGIN."$p/script.js"; 202 } 203 return $list; 204} 205 206/** 207 * Escapes a String to be embedded in a JavaScript call, keeps \n 208 * as newline 209 * 210 * @author Andreas Gohr <andi@splitbrain.org> 211 */ 212function js_escape($string){ 213 return str_replace('\\\\n','\\n',addslashes($string)); 214} 215 216/** 217 * Adds the given JavaScript code to the window.onload() event 218 * 219 * @author Andreas Gohr <andi@splitbrain.org> 220 */ 221function js_runonstart($func){ 222 echo "addInitEvent(function(){ $func; });".NL; 223} 224 225/** 226 * Strip comments and whitespaces from given JavaScript Code 227 * 228 * This is a rewrite of Nick Galbreaths python tool jsstrip.py which is 229 * released under BSD license. See link for original code. 230 * 231 * @author Nick Galbreath <nickg@modp.com> 232 * @author Andreas Gohr <andi@splitbrain.org> 233 * @link http://modp.com/release/jsstrip/ 234 */ 235function js_compress($s){ 236 $i = 0; 237 $line = 0; 238 $s .= "\n"; 239 $len = strlen($s); 240 241 // items that don't need spaces next to them 242 $chars = '^&|!+\-*\/%=\?:;,{}()<>% \t\n\r'; 243 244 ob_start(); 245 while($i < $len){ 246 $ch = $s{$i}; 247 248 // multiline comments (keeping IE conditionals) 249 if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){ 250 $endC = strpos($s,'*/',$i+2); 251 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 252 $i = $endC + 2; 253 continue; 254 } 255 256 // singleline 257 if($ch == '/' && $s{$i+1} == '/'){ 258 $endC = strpos($s,"\n",$i+2); 259 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 260 $i = $endC; 261 continue; 262 } 263 264 // tricky. might be an RE 265 if($ch == '/'){ 266 // rewind, skip white space 267 $j = 1; 268 while($s{$i-$j} == ' '){ 269 $j = $j + 1; 270 } 271 if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ 272 // yes, this is an re 273 // now move forward and find the end of it 274 $j = 1; 275 while($s{$i+$j} != '/'){ 276 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ 277 $j = $j + 1; 278 } 279 if($s{$i+$j} == '\\') $j = $j + 2; 280 } 281 echo substr($s,$i,$j+1); 282 $i = $i + $j + 1; 283 continue; 284 } 285 } 286 287 // double quote strings 288 if($ch == '"'){ 289 $j = 1; 290 while( $s{$i+$j} != '"' && ($i+$j < $len)){ 291 if( $s{$i+$j} == '\\' && $s{$i+$j+1} == '"' ){ 292 $j += 2; 293 }else{ 294 $j += 1; 295 } 296 } 297 echo substr($s,$i,$j+1); 298 $i = $i + $j + 1; 299 continue; 300 } 301 302 // single quote strings 303 if($ch == "'"){ 304 $j = 1; 305 while( $s{$i+$j} != "'" && ($i+$j < $len)){ 306 if( $s{$i+$j} == '\\' && $s{$i+$j+1} == "'" ){ 307 $j += 2; 308 }else{ 309 $j += 1; 310 } 311 } 312 echo substr($s,$i,$j+1); 313 $i = $i + $j + 1; 314 continue; 315 } 316 317 // newlines 318 if($ch == "\n" || $ch == "\r"){ 319 $i = $i+1; 320 continue; 321 } 322 323 // leading spaces 324 if( ( $ch == ' ' || 325 $ch == "\n" || 326 $ch == "\t" ) && 327 !preg_match('/['.$chars.']/',$s{$i+1}) ){ 328 $i = $i+1; 329 continue; 330 } 331 332 // trailing spaces 333 if( ( $ch == ' ' || 334 $ch == "\n" || 335 $ch == "\t" ) && 336 !preg_match('/['.$chars.']/',$s{$i-1}) ){ 337 $i = $i+1; 338 continue; 339 } 340 341 // other chars 342 echo $ch; 343 $i = $i + 1; 344 } 345 346 347 $out = ob_get_contents(); 348 ob_end_clean(); 349 return $out; 350} 351 352//Setup VIM: ex: et ts=4 enc=utf-8 : 353?> 354