| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * $RCSfile: tiny_mce_gzip.php,v $ |
|---|
| 4 | * $Revision: $ |
|---|
| 5 | * $Date: $ |
|---|
| 6 | * |
|---|
| 7 | * @version 1.08 |
|---|
| 8 | * @author Moxiecode |
|---|
| 9 | * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved. |
|---|
| 10 | * |
|---|
| 11 | * This file compresses the TinyMCE JavaScript using GZip and |
|---|
| 12 | * enables the browser to do two requests instead of one for each .js file. |
|---|
| 13 | * Notice: This script defaults the button_tile_map option to true for extra performance. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | @require_once('../../../wp-config.php'); |
|---|
| 17 | |
|---|
| 18 | // gzip_compression(); |
|---|
| 19 | |
|---|
| 20 | function wp_tinymce_lang($path) { |
|---|
| 21 | global $language; |
|---|
| 22 | |
|---|
| 23 | $text = ''; |
|---|
| 24 | |
|---|
| 25 | // Look for xx_YY.js, xx_yy.js, xx.js |
|---|
| 26 | $file = realpath(sprintf($path, $language)); |
|---|
| 27 | if ( file_exists($file) ) |
|---|
| 28 | $text = file_get_contents($file); |
|---|
| 29 | $file = realpath(sprintf($path, strtolower($language))); |
|---|
| 30 | if ( file_exists($file) ) |
|---|
| 31 | $text = file_get_contents($file); |
|---|
| 32 | $file = realpath(sprintf($path, substr($language, 0, 2))); |
|---|
| 33 | if ( file_exists($file) ) |
|---|
| 34 | $text = file_get_contents($file); |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | // Fall back on en.js |
|---|
| 38 | $file = realpath(sprintf($path, 'en')); |
|---|
| 39 | if ( empty($text) && file_exists($file) ) |
|---|
| 40 | $text = file_get_contents($file); |
|---|
| 41 | |
|---|
| 42 | // Send lang file through gettext |
|---|
| 43 | if ( function_exists('__') && strtolower(substr($language, 0, 2)) != 'en' ) { |
|---|
| 44 | $search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem"; |
|---|
| 45 | $replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')"; |
|---|
| 46 | |
|---|
| 47 | $search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem"; |
|---|
| 48 | $replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'"; |
|---|
| 49 | |
|---|
| 50 | $search = array($search1, $search2); |
|---|
| 51 | $replace = array($replace1, $replace2); |
|---|
| 52 | |
|---|
| 53 | $text = preg_replace($search, $replace, $text); |
|---|
| 54 | |
|---|
| 55 | return $text; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | return $text; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | function wp_compact_tinymce_js($text) { |
|---|
| 62 | // This function was custom-made for TinyMCE 2.0, not expected to work with any other JS. |
|---|
| 63 | |
|---|
| 64 | // Strip comments |
|---|
| 65 | $text = preg_replace("!(^|\s+)//.*$!m", '', $text); |
|---|
| 66 | $text = preg_replace("!/\*.*?\*/!s", '', $text); |
|---|
| 67 | |
|---|
| 68 | // Strip leading tabs, carriage returns and unnecessary line breaks. |
|---|
| 69 | $text = preg_replace("!^\t+!m", '', $text); |
|---|
| 70 | $text = str_replace("\r", '', $text); |
|---|
| 71 | $text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text); |
|---|
| 72 | |
|---|
| 73 | return "$text\n"; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | // General options |
|---|
| 78 | $suffix = ""; // Set to "_src" to use source version |
|---|
| 79 | $expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires |
|---|
| 80 | $diskCache = false; // If you enable this option gzip files will be cached on disk. |
|---|
| 81 | $cacheDir = realpath("."); // Absolute directory path to where cached gz files will be stored |
|---|
| 82 | $debug = false; // Enable this option if you need debuging info |
|---|
| 83 | |
|---|
| 84 | // Headers |
|---|
| 85 | header("Content-type: text/javascript; charset: UTF-8"); |
|---|
| 86 | // header("Cache-Control: must-revalidate"); |
|---|
| 87 | header("Vary: Accept-Encoding"); // Handle proxies |
|---|
| 88 | header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT"); |
|---|
| 89 | |
|---|
| 90 | // Get data to load |
|---|
| 91 | $theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : ""; |
|---|
| 92 | $language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : ""; |
|---|
| 93 | $plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : ""; |
|---|
| 94 | $lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en"; |
|---|
| 95 | $index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1; |
|---|
| 96 | $cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug); |
|---|
| 97 | $cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" . $cacheKey . ".gz"; |
|---|
| 98 | $cacheData = ""; |
|---|
| 99 | |
|---|
| 100 | // Patch older versions of PHP < 4.3.0 |
|---|
| 101 | if (!function_exists('file_get_contents')) { |
|---|
| 102 | function file_get_contents($filename) { |
|---|
| 103 | $fd = fopen($filename, 'rb'); |
|---|
| 104 | $content = fread($fd, filesize($filename)); |
|---|
| 105 | fclose($fd); |
|---|
| 106 | return $content; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | // Security check function, can only contain a-z 0-9 , _ - and whitespace. |
|---|
| 111 | function TinyMCE_cleanInput($str) { |
|---|
| 112 | return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_ |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | function TinyMCE_echo($str) { |
|---|
| 116 | global $cacheData, $diskCache; |
|---|
| 117 | |
|---|
| 118 | if ($diskCache) |
|---|
| 119 | $cacheData .= $str; |
|---|
| 120 | else |
|---|
| 121 | echo $str; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | // Only gzip the contents if clients and server support it |
|---|
| 125 | $encodings = array(); |
|---|
| 126 | |
|---|
| 127 | if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) |
|---|
| 128 | $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))); |
|---|
| 129 | |
|---|
| 130 | // Check for gzip header or northon internet securities |
|---|
| 131 | if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) { |
|---|
| 132 | |
|---|
| 133 | } else |
|---|
| 134 | $diskCache = false; |
|---|
| 135 | |
|---|
| 136 | if ($index > -1) { |
|---|
| 137 | // Write main script and patch some things |
|---|
| 138 | if ($index == 0) { |
|---|
| 139 | TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("tiny_mce" . $suffix . ".js")))); // WP |
|---|
| 140 | TinyMCE_echo('TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;'); |
|---|
| 141 | TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;'); |
|---|
| 142 | } else |
|---|
| 143 | TinyMCE_echo('tinyMCE = realTinyMCE;'); |
|---|
| 144 | |
|---|
| 145 | // Do init based on index |
|---|
| 146 | TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);"); |
|---|
| 147 | |
|---|
| 148 | // Load external plugins |
|---|
| 149 | if ($index == 0) |
|---|
| 150 | TinyMCE_echo("tinyMCECompressed.loadPlugins();"); |
|---|
| 151 | |
|---|
| 152 | // Load theme, language pack and theme language packs |
|---|
| 153 | if ($theme) { |
|---|
| 154 | TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")))); // WP |
|---|
| 155 | TinyMCE_echo(wp_tinymce_lang("themes/" . $theme . "/langs/%s.js")); // WP |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /* WP if ($language) WP */ |
|---|
| 159 | TinyMCE_echo(wp_tinymce_lang("langs/%s.js")); // WP |
|---|
| 160 | |
|---|
| 161 | // Load all plugins and their language packs |
|---|
| 162 | $plugins = explode(",", $plugins); |
|---|
| 163 | foreach ($plugins as $plugin) { |
|---|
| 164 | $pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js"); |
|---|
| 165 | /* WP $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js"); WP */ |
|---|
| 166 | |
|---|
| 167 | if ($pluginFile) |
|---|
| 168 | TinyMCE_echo(file_get_contents($pluginFile)); |
|---|
| 169 | |
|---|
| 170 | /* WP if ($languageFile) WP */ |
|---|
| 171 | TinyMCE_echo(wp_tinymce_lang("plugins/" . $plugin . "/langs/%s.js")); // WP |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | // Reset tinyMCE compressor engine |
|---|
| 175 | TinyMCE_echo("tinyMCE = tinyMCECompressed;"); |
|---|
| 176 | |
|---|
| 177 | // Write to cache |
|---|
| 178 | if ($diskCache) { |
|---|
| 179 | // Calculate compression ratio and debug target output path |
|---|
| 180 | if ($debug) { |
|---|
| 181 | $ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0); |
|---|
| 182 | TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');"); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | $cacheData = gzencode($cacheData, 9, FORCE_GZIP); |
|---|
| 186 | |
|---|
| 187 | // Write to file if possible |
|---|
| 188 | $fp = @fopen($cacheFile, "wb"); |
|---|
| 189 | if ($fp) { |
|---|
| 190 | fwrite($fp, $cacheData); |
|---|
| 191 | fclose($fp); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | // Output |
|---|
| 195 | header("Content-Encoding: " . $enc); |
|---|
| 196 | echo $cacheData; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | die; |
|---|
| 200 | } |
|---|
| 201 | ?> |
|---|
| 202 | |
|---|
| 203 | function TinyMCECompressed() { |
|---|
| 204 | this.configs = new Array(); |
|---|
| 205 | this.loadedFiles = new Array(); |
|---|
| 206 | this.externalPlugins = new Array(); |
|---|
| 207 | this.loadAdded = false; |
|---|
| 208 | this.isLoaded = false; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | TinyMCECompressed.prototype.init = function(settings) { |
|---|
| 212 | var elements = document.getElementsByTagName('script'); |
|---|
| 213 | var scriptURL = ""; |
|---|
| 214 | |
|---|
| 215 | for (var i=0; i<elements.length; i++) { |
|---|
| 216 | if (elements[i].src && elements[i].src.indexOf("tiny_mce_gzip.php") != -1) { |
|---|
| 217 | scriptURL = elements[i].src; |
|---|
| 218 | break; |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | settings["theme"] = typeof(settings["theme"]) != "undefined" ? settings["theme"] : "default"; |
|---|
| 223 | settings["plugins"] = typeof(settings["plugins"]) != "undefined" ? settings["plugins"] : ""; |
|---|
| 224 | settings["language"] = typeof(settings["language"]) != "undefined" ? settings["language"] : "en"; |
|---|
| 225 | settings["button_tile_map"] = typeof(settings["button_tile_map"]) != "undefined" ? settings["button_tile_map"] : true; |
|---|
| 226 | this.configs[this.configs.length] = settings; |
|---|
| 227 | this.settings = settings; |
|---|
| 228 | |
|---|
| 229 | scriptURL += (scriptURL.indexOf('?') == -1) ? '?' : '&'; |
|---|
| 230 | scriptURL += "theme=" + escape(this.getOnce(settings["theme"])) + "&language=" + escape(this.getOnce(settings["language"])) + "&plugins=" + escape(this.getOnce(settings["plugins"])) + "&lang=" + settings["language"] + "&index=" + escape(this.configs.length-1); |
|---|
| 231 | document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + scriptURL + '"></script>'); |
|---|
| 232 | |
|---|
| 233 | if (!this.loadAdded) { |
|---|
| 234 | tinyMCE.addEvent(window, "DOMContentLoaded", TinyMCECompressed.prototype.onLoad); |
|---|
| 235 | tinyMCE.addEvent(window, "load", TinyMCECompressed.prototype.onLoad); |
|---|
| 236 | this.loadAdded = true; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | TinyMCECompressed.prototype.onLoad = function() { |
|---|
| 241 | if (tinyMCE.isLoaded) |
|---|
| 242 | return true; |
|---|
| 243 | |
|---|
| 244 | tinyMCE = realTinyMCE; |
|---|
| 245 | TinyMCE_Engine.prototype.onLoad(); |
|---|
| 246 | tinyMCE._addUnloadEvents(); |
|---|
| 247 | |
|---|
| 248 | tinyMCE.isLoaded = true; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | TinyMCECompressed.prototype.addEvent = function(o, n, h) { |
|---|
| 252 | if (o.attachEvent) |
|---|
| 253 | o.attachEvent("on" + n, h); |
|---|
| 254 | else |
|---|
| 255 | o.addEventListener(n, h, false); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | TinyMCECompressed.prototype.getOnce = function(str) { |
|---|
| 259 | var ar = str.replace(/\s+/g, '').split(','); |
|---|
| 260 | |
|---|
| 261 | for (var i=0; i<ar.length; i++) { |
|---|
| 262 | if (ar[i] == '' || ar[i].charAt(0) == '-') { |
|---|
| 263 | ar[i] = null; |
|---|
| 264 | continue; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | // Skip load |
|---|
| 268 | for (var x=0; x<this.loadedFiles.length; x++) { |
|---|
| 269 | if (this.loadedFiles[x] == ar[i]) |
|---|
| 270 | ar[i] = null; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | this.loadedFiles[this.loadedFiles.length] = ar[i]; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | // Glue |
|---|
| 277 | str = ""; |
|---|
| 278 | for (var i=0; i<ar.length; i++) { |
|---|
| 279 | if (ar[i] == null) |
|---|
| 280 | continue; |
|---|
| 281 | |
|---|
| 282 | str += ar[i]; |
|---|
| 283 | |
|---|
| 284 | if (i != ar.length-1) |
|---|
| 285 | str += ","; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | return str; |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | TinyMCECompressed.prototype.loadPlugins = function() { |
|---|
| 292 | var i, ar; |
|---|
| 293 | |
|---|
| 294 | TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript; |
|---|
| 295 | tinyMCE = realTinyMCE; |
|---|
| 296 | |
|---|
| 297 | ar = tinyMCECompressed.externalPlugins; |
|---|
| 298 | for (i=0; i<ar.length; i++) |
|---|
| 299 | tinyMCE.loadPlugin(ar[i].name, ar[i].url); |
|---|
| 300 | |
|---|
| 301 | TinyMCE.prototype.loadScript = function() {}; |
|---|
| 302 | }; |
|---|
| 303 | |
|---|
| 304 | TinyMCECompressed.prototype.loadPlugin = function(n, u) { |
|---|
| 305 | this.externalPlugins[this.externalPlugins.length] = {name : n, url : u}; |
|---|
| 306 | }; |
|---|
| 307 | |
|---|
| 308 | TinyMCECompressed.prototype.importPluginLanguagePack = function(n, v) { |
|---|
| 309 | tinyMCE = realTinyMCE; |
|---|
| 310 | TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript; |
|---|
| 311 | tinyMCE.importPluginLanguagePack(n, v); |
|---|
| 312 | }; |
|---|
| 313 | |
|---|
| 314 | TinyMCECompressed.prototype.addPlugin = function(n, p) { |
|---|
| 315 | tinyMCE = realTinyMCE; |
|---|
| 316 | tinyMCE.addPlugin(n, p); |
|---|
| 317 | }; |
|---|
| 318 | |
|---|
| 319 | var tinyMCE = new TinyMCECompressed(); |
|---|
| 320 | var tinyMCECompressed = tinyMCE; |
|---|