3 * $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
6 * @copyright Copyright 2005-2006, Moxiecode Systems AB, All rights reserved.
8 * This file compresses the TinyMCE JavaScript using GZip and
9 * enables the browser to do two requests instead of one for each .js file.
10 * Notice: This script defaults the button_tile_map option to true for extra performance.
13 @require_once('../../../wp-config.php'); // For get_bloginfo().
16 $plugins = explode(',', getParam("plugins", ""));
17 $languages = explode(',', getParam("languages", ""));
18 $themes = explode(',', getParam("themes", ""));
19 $diskCache = getParam("diskcache", "") == "true";
20 $isJS = getParam("js", "") == "true";
21 $compress = getParam("compress", "true") == "true";
22 $suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
23 $cachePath = realpath("."); // Cache path, this is where the .gz files will be stored
24 $expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
27 $supportsGzip = false;
31 // Custom extra javascripts to pack
33 "some custom .js file",
34 "some custom .js file"
38 $index = getParam("index", -1);
39 $theme = getParam("theme", "");
40 $themes = array($theme);
41 $language = getParam("language", "en");
42 if ( empty($language) )
44 $languages = array($language);
45 if ( $language != strtolower($language) )
46 $languages[] = strtolower($language);
47 if ( $language != substr($language, 0, 2) )
48 $languages[] = substr($language, 0, 2);
54 header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
55 header("Vary: Accept-Encoding"); // Handle proxies
56 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
58 // Is called directly then auto init with default settings
60 echo getFileContents("tiny_mce_gzip.js");
61 echo "tinyMCE_GZ.init({});";
68 die("alert('Real path failed.');");
70 $cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
72 foreach ($custom as $file)
75 $cacheKey = md5($cacheKey);
78 $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
80 $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
83 // Check if it supports gzip
84 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
85 $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
87 if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression') && ini_get('output_handler') != 'ob_gzhandler') {
88 $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
92 // Use cached file disk cache
93 if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
95 header("Content-Encoding: " . $enc);
97 echo getFileContents($cacheFile);
102 // Write main script and patch some things
105 $content .= wp_compact_tinymce_js(getFileContents("tiny_mce" . $suffix . ".js"));
106 $content .= 'TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;';
107 $content .= 'TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;';
109 $content .= 'tinyMCE = realTinyMCE;';
111 // Patch loading functions
112 //$content .= "tinyMCE_GZ.start();";
114 // Do init based on index
115 $content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);";
117 // Load external plugins
119 $content .= "tinyMCECompressed.loadPlugins();";
121 // Add core languages
123 foreach ($languages as $lang)
124 $lang_content .= getFileContents("langs/" . $lang . ".js");
125 if ( empty($lang_content) )
126 $lang_content .= getFileContents("langs/en.js");
127 $content .= $lang_content;
130 foreach ($themes as $theme) {
131 $content .= wp_compact_tinymce_js(getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js"));
134 foreach ($languages as $lang)
135 $lang_content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
136 if ( empty($lang_content) )
137 $lang_content .= getFileContents("themes/" . $theme . "/langs/en.js");
138 $content .= $lang_content;
142 foreach ($plugins as $plugin) {
143 $content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
146 foreach ($languages as $lang)
147 $lang_content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
148 if ( empty($lang_content) )
149 $lang_content .= getFileContents("plugins/" . $plugin . "/langs/en.js");
150 $content .= $lang_content;
154 foreach ($custom as $file)
155 $content .= getFileContents($file);
157 // Reset tinyMCE compressor engine
158 $content .= "tinyMCE = tinyMCECompressed;";
160 // Restore loading functions
161 //$content .= "tinyMCE_GZ.end();";
163 // Generate GZIP'd content
166 header("Content-Encoding: " . $enc);
167 $cacheData = gzencode($content, 9, FORCE_GZIP);
169 $cacheData = $content;
172 if ($diskCache && $cacheKey != "")
173 putFileContents($cacheFile, $cacheData);
178 // Stream uncompressed content
185 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
187 function getParam($name, $def = false) {
188 if (!isset($_GET[$name]))
191 return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
194 function getFileContents($path) {
195 $path = realpath($path);
197 if (!$path || !@is_file($path))
200 if (function_exists("file_get_contents"))
201 return @file_get_contents($path);
204 $fp = @fopen($path, "r");
209 $content .= fgets($fp);
216 function putFileContents($path, $content) {
217 if (function_exists("file_put_contents"))
218 return @file_put_contents($path, $content);
220 $fp = @fopen($path, "wb");
222 fwrite($fp, $content);
228 function wp_compact_tinymce_js($text) {
229 // This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
232 $text = preg_replace("!(^|\s+)//.*$!m", '', $text);
233 $text = preg_replace("!/\*.*?\*/!s", '', $text);
235 // Strip leading tabs, carriage returns and unnecessary line breaks.
236 $text = preg_replace("!^\t+!m", '', $text);
237 $text = str_replace("\r", '', $text);
238 $text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
244 function TinyMCECompressed() {
245 this.configs = new Array();
246 this.loadedFiles = new Array();
247 this.externalPlugins = new Array();
248 this.loadAdded = false;
249 this.isLoaded = false;
252 TinyMCECompressed.prototype.init = function(settings) {
253 var elements = document.getElementsByTagName('script');
256 for (var i=0; i<elements.length; i++) {
257 if (elements[i].src && elements[i].src.indexOf("tiny_mce_gzip.php") != -1) {
258 scriptURL = elements[i].src;
263 settings["theme"] = typeof(settings["theme"]) != "undefined" ? settings["theme"] : "default";
264 settings["plugins"] = typeof(settings["plugins"]) != "undefined" ? settings["plugins"] : "";
265 settings["language"] = typeof(settings["language"]) != "undefined" ? settings["language"] : "en";
266 settings["button_tile_map"] = typeof(settings["button_tile_map"]) != "undefined" ? settings["button_tile_map"] : true;
267 this.configs[this.configs.length] = settings;
268 this.settings = settings;
270 scriptURL += (scriptURL.indexOf('?') == -1) ? '?' : '&';
271 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);
272 document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + scriptURL + '"></script>');
274 if (!this.loadAdded) {
275 tinyMCE.addEvent(window, "DOMContentLoaded", TinyMCECompressed.prototype.onLoad);
276 tinyMCE.addEvent(window, "load", TinyMCECompressed.prototype.onLoad);
277 this.loadAdded = true;
281 TinyMCECompressed.prototype.onLoad = function() {
282 if (tinyMCE.isLoaded)
285 tinyMCE = realTinyMCE;
286 TinyMCE_Engine.prototype.onLoad();
287 tinyMCE._addUnloadEvents();
289 tinyMCE.isLoaded = true;
292 TinyMCECompressed.prototype.addEvent = function(o, n, h) {
294 o.attachEvent("on" + n, h);
296 o.addEventListener(n, h, false);
299 TinyMCECompressed.prototype.getOnce = function(str) {
300 var ar = str.replace(/\s+/g, '').split(',');
302 for (var i=0; i<ar.length; i++) {
303 if (ar[i] == '' || ar[i].charAt(0) == '-') {
309 for (var x=0; x<this.loadedFiles.length; x++) {
310 if (this.loadedFiles[x] == ar[i])
314 this.loadedFiles[this.loadedFiles.length] = ar[i];
319 for (var i=0; i<ar.length; i++) {
325 if (i != ar.length-1)
332 TinyMCECompressed.prototype.loadPlugins = function() {
335 TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
336 tinyMCE = realTinyMCE;
338 ar = tinyMCECompressed.externalPlugins;
339 for (i=0; i<ar.length; i++)
340 tinyMCE.loadPlugin(ar[i].name, ar[i].url);
342 TinyMCE.prototype.loadScript = function() {};
345 TinyMCECompressed.prototype.loadPlugin = function(n, u) {
346 this.externalPlugins[this.externalPlugins.length] = {name : n, url : u};
349 TinyMCECompressed.prototype.importPluginLanguagePack = function(n, v) {
350 tinyMCE = realTinyMCE;
351 TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
352 tinyMCE.importPluginLanguagePack(n, v);
355 TinyMCECompressed.prototype.addPlugin = function(n, p) {
356 tinyMCE = realTinyMCE;
357 tinyMCE.addPlugin(n, p);
360 var tinyMCE = new TinyMCECompressed();
361 var tinyMCECompressed = tinyMCE;