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) 11require_once(DOKU_INC.'inc/init.php'); 12require_once(DOKU_INC.'inc/pageutils.php'); 13require_once(DOKU_INC.'inc/io.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 $edit = (bool) $_REQUEST['edit']; // edit or preview mode? 33 $write = (bool) $_REQUEST['write']; // writable? 34 35 // The generated script depends on some dynamic options 36 $cache = getCacheName('scripts'.$edit.$write,'.js'); 37 38 // Array of needed files 39 $files = array( 40 DOKU_INC.'lib/scripts/events.js', 41 DOKU_INC.'lib/scripts/script.js', 42 DOKU_INC.'lib/scripts/tw-sack.js', 43 DOKU_INC.'lib/scripts/ajax.js', 44 DOKU_INC.'lib/scripts/domLib.js', 45 DOKU_INC.'lib/scripts/domTT.js', 46 ); 47 if($edit && $write){ 48 $files[] = DOKU_INC.'lib/scripts/edit.js'; 49 if($conf['spellchecker']){ 50 $files[] = DOKU_INC.'lib/scripts/spellcheck.js'; 51 } 52 } 53 $files[] = DOKU_TPLINC.'script.js'; 54 55 // get possible plugin scripts 56 $plugins = js_pluginscripts(); 57 58 // check cache age & handle conditional request 59 header('Cache-Control: public, max-age=3600'); 60 header('Pragma: public'); 61 if(js_cacheok($cache,array_merge($files,$plugins))){ 62 http_conditionalRequest(filemtime($cache)); 63 readfile($cache); 64 return; 65 } else { 66 http_conditionalRequest(time()); 67 } 68 69 // start output buffering and build the script 70 ob_start(); 71 72 // add some translation strings and global variables 73 print "var alertText = '".js_escape($lang['qb_alert'])."';"; 74 print "var notSavedYet = '".js_escape($lang['notsavedyet'])."';"; 75 print "var reallyDel = '".js_escape($lang['del_confirm'])."';"; 76 print "var DOKU_BASE = '".DOKU_BASE."';"; 77 78 // load files 79 foreach($files as $file){ 80 @readfile($file); 81 } 82 83 // init stuff 84 js_runonstart("ajax_qsearch.init('qsearch__in','qsearch__out')"); 85 js_runonstart("addEvent(document,'click',closePopups)"); 86 js_runonstart('addTocToggle()'); 87 88 if($edit){ 89 // size controls 90 js_runonstart("initSizeCtl('size__ctl','wiki__text')"); 91 92 if($write){ 93 require_once(DOKU_INC.'inc/toolbar.php'); 94 toolbar_JSdefines('toolbar'); 95 js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)"); 96 97 // add pageleave check 98 js_runonstart("initChangeCheck('".js_escape($lang['notsavedyet'])."')"); 99 100 // add lock timer 101 js_runonstart("locktimer.init(".($conf['locktime'] - 60).",'".js_escape($lang['willexpire'])."',".$conf['usedraft'].")"); 102 103 // load spell checker 104 if($conf['spellchecker']){ 105 js_runonstart("ajax_spell.init('". 106 js_escape($lang['spell_start'])."','". 107 js_escape($lang['spell_stop'])."','". 108 js_escape($lang['spell_wait'])."','". 109 js_escape($lang['spell_noerr'])."','". 110 js_escape($lang['spell_nosug'])."','". 111 js_escape($lang['spell_change'])."')"); 112 } 113 } 114 } 115 116 // load plugin scripts (suppress warnings for missing ones) 117 foreach($plugins as $plugin){ 118 @readfile($plugin); 119 } 120 121 // load user script 122 @readfile(DOKU_CONF.'userscript.js'); 123 124 // add scroll event and tooltip rewriting 125 js_runonstart('updateAccessKeyTooltip()'); 126 js_runonstart('scrollToMarker()'); 127 128 // initialize init pseudo event 129 echo 'if (document.addEventListener) {'; 130 echo ' document.addEventListener("DOMContentLoaded", window.fireoninit, null);'; 131 echo '}'; 132 echo 'addEvent(window,"load",window.fireoninit);'; 133 134 // end output buffering and get contents 135 $js = ob_get_contents(); 136 ob_end_clean(); 137 138 // compress whitespace and comments 139 if($conf['compress']){ 140 $js = js_compress($js); 141 } 142 143 // save cache file 144 io_saveFile($cache,$js); 145 146 // finally send output 147 print $js; 148} 149 150/** 151 * Checks if a JavaScript Cache file still is valid 152 * 153 * @author Andreas Gohr <andi@splitbrain.org> 154 */ 155function js_cacheok($cache,$files){ 156 $ctime = @filemtime($cache); 157 if(!$ctime) return false; //There is no cache 158 159 // some additional files to check 160 $files[] = DOKU_CONF.'dokuwiki.php'; 161 $files[] = DOKU_CONF.'local.php'; 162 $files[] = DOKU_CONF.'userscript.js'; 163 $files[] = __FILE__; 164 165 // now walk the files 166 foreach($files as $file){ 167 if(@filemtime($file) > $ctime){ 168 return false; 169 } 170 } 171 return true; 172} 173 174/** 175 * Returns a list of possible Plugin Scripts (no existance check here) 176 * 177 * @author Andreas Gohr <andi@splitbrain.org> 178 */ 179function js_pluginscripts(){ 180 $list = array(); 181 $plugins = plugin_list(); 182 foreach ($plugins as $p){ 183 $list[] = DOKU_PLUGIN."$p/script.js"; 184 } 185 return $list; 186} 187 188/** 189 * Escapes a String to be embedded in a JavaScript call, keeps \n 190 * as newline 191 * 192 * @author Andreas Gohr <andi@splitbrain.org> 193 */ 194function js_escape($string){ 195 return str_replace('\\\\n','\\n',addslashes($string)); 196} 197 198/** 199 * Adds the given JavaScript code to the window.onload() event 200 * 201 * @author Andreas Gohr <andi@splitbrain.org> 202 */ 203function js_runonstart($func){ 204 echo "addInitEvent(function(){ $func; });\n"; 205} 206 207/** 208 * Strip comments and whitespaces from given JavaScript Code 209 * 210 * This is a rewrite of Nick Galbreaths python tool jsstrip.py which is 211 * released under BSD license. See link for original code. 212 * 213 * @author Nick Galbreath <nickg@modp.com> 214 * @author Andreas Gohr <andi@splitbrain.org> 215 * @link http://modp.com/release/jsstrip/ 216 */ 217function js_compress($s){ 218 $i = 0; 219 $line = 0; 220 $s .= "\n"; 221 $len = strlen($s); 222 223 // items that don't need spaces next to them 224 $chars = '^&|!+\-*\/%=:;,{}()<>% \t\n\r'; 225 226 ob_start(); 227 while($i < $len){ 228 $ch = $s{$i}; 229 230 // multiline comments 231 if($ch == '/' && $s{$i+1} == '*'){ 232 $endC = strpos($s,'*/',$i+2); 233 if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR); 234 $i = $endC + 2; 235 continue; 236 } 237 238 // singleline 239 if($ch == '/' && $s{$i+1} == '/'){ 240 $endC = strpos($s,"\n",$i+2); 241 if($endC === false) trigger_error('Invalid comment', E_USER_ERROR); 242 $i = $endC; 243 continue; 244 } 245 246 // tricky. might be an RE 247 if($ch == '/'){ 248 // rewind, skip white space 249 $j = 1; 250 while($s{$i-$j} == ' '){ 251 $j = $j + 1; 252 } 253 if( ($s{$i-$j} == '=') || ($s{$i-$j} == '(') ){ 254 // yes, this is an re 255 // now move forward and find the end of it 256 $j = 1; 257 while($s{$i+$j} != '/'){ 258 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '/')){ 259 $j = $j + 1; 260 } 261 if($s{$i+$j} == '\\') $j = $j + 2; 262 } 263 echo substr($s,$i,$j+1); 264 $i = $i + $j + 1; 265 continue; 266 } 267 } 268 269 // double quote strings 270 if($ch == '"'){ 271 $j = 1; 272 while( $s{$i+$j} != '"' ){ 273 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '"') ){ 274 $j = $j + 1; 275 } 276 if($s{$i+$j} == '\\') $j = $j + 2; 277 } 278 echo substr($s,$i,$j+1); 279 $i = $i + $j + 1; 280 continue; 281 } 282 283 // single quote strings 284 if($ch == "'"){ 285 $j = 1; 286 while( $s{$i+$j} != "'" ){ 287 while( ($s{$i+$j} != '\\') && ($s{$i+$j} != "'") ){ 288 $j = $j + 1; 289 } 290 if ($s{$i+$j} == '\\') $j = $j + 2; 291 } 292 echo substr($s,$i,$j+1); 293 $i = $i + $j + 1; 294 continue; 295 } 296 297 // newlines 298 if($ch == "\n" || $ch == "\r"){ 299 $i = $i+1; 300 continue; 301 } 302 303 // leading spaces 304 if( ( $ch == ' ' || 305 $ch == "\n" || 306 $ch == "\t" ) && 307 !preg_match('/['.$chars.']/',$s{$i+1}) ){ 308 $i = $i+1; 309 continue; 310 } 311 312 // trailing spaces 313 if( ( $ch == ' ' || 314 $ch == "\n" || 315 $ch == "\t" ) && 316 !preg_match('/['.$chars.']/',$s{$i-1}) ){ 317 $i = $i+1; 318 continue; 319 } 320 321 // other chars 322 echo $ch; 323 $i = $i + 1; 324 } 325 326 327 $out = ob_get_contents(); 328 ob_end_clean(); 329 return $out; 330} 331 332//Setup VIM: ex: et ts=4 enc=utf-8 : 333?> 334