]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/tiny_mce_gzip.php
Wordpress 2.3.3
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / tiny_mce_gzip.php
1 <?php
2 /**
3  * $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
4  *
5  * @author Moxiecode
6  * @copyright Copyright  2005-2006, Moxiecode Systems AB, All rights reserved.
7  *
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.
11  */
12
13         @require_once('../../../wp-config.php');  // For get_bloginfo().
14
15         // Get input
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
25         $content = "";
26         $encodings = array();
27         $supportsGzip = false;
28         $enc = "";
29         $cacheKey = "";
30
31         // Custom extra javascripts to pack
32         $custom = array(/*
33                 "some custom .js file",
34                 "some custom .js file"
35         */);
36
37         // WP
38         $index = getParam("index", -1);
39         $theme = getParam("theme", "");
40         $themes = array($theme);
41         $language = getParam("language", "en");
42         if ( empty($language) )
43                 $language = 'en';
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);
49         $diskCache = false;
50         $isJS = true;
51         $suffix = '';
52
53         // Headers
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");
57
58         // Is called directly then auto init with default settings
59         if (!$isJS) {
60                 echo getFileContents("tiny_mce_gzip.js");
61                 echo "tinyMCE_GZ.init({});";
62                 die();
63         }
64
65         // Setup cache info
66         if ($diskCache) {
67                 if (!$cachePath)
68                         die("alert('Real path failed.');");
69
70                 $cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
71
72                 foreach ($custom as $file)
73                         $cacheKey .= $file;
74
75                 $cacheKey = md5($cacheKey);
76
77                 if ($compress)
78                         $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
79                 else
80                         $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
81         }
82
83         // Check if it supports gzip
84         if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
85                 $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
86
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";
89                 $supportsGzip = true;
90         }
91
92         // Use cached file disk cache
93         if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
94                 if ($compress)
95                         header("Content-Encoding: " . $enc);
96
97                 echo getFileContents($cacheFile);
98                 die();
99         }
100
101 if ($index > -1) {
102         // Write main script and patch some things
103         if ( $index == 0 ) {
104                 // Add core
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;';
108         } else
109                 $content .= 'tinyMCE = realTinyMCE;';
110
111         // Patch loading functions
112         //$content .= "tinyMCE_GZ.start();";
113
114         // Do init based on index
115         $content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);";
116
117         // Load external plugins
118         if ( $index == 0 )
119                 $content .= "tinyMCECompressed.loadPlugins();";
120
121         // Add core languages
122         $lang_content = '';
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;
128
129         // Add themes
130         foreach ($themes as $theme) {
131                 $content .= wp_compact_tinymce_js(getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js"));
132
133                 $lang_content = '';
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;
139         }
140
141         // Add plugins
142         foreach ($plugins as $plugin) {
143                 $content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
144
145                 $lang_content = '';
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;
151         }
152
153         // Add custom files
154         foreach ($custom as $file)
155                 $content .= getFileContents($file);
156
157         // Reset tinyMCE compressor engine
158         $content .= "tinyMCE = tinyMCECompressed;";
159
160         // Restore loading functions
161         //$content .= "tinyMCE_GZ.end();";
162
163         // Generate GZIP'd content
164         if ($supportsGzip) {
165                 if ($compress) {
166                         header("Content-Encoding: " . $enc);
167                         $cacheData = gzencode($content, 9, FORCE_GZIP);
168                 } else
169                         $cacheData = $content;
170
171                 // Write gz file
172                 if ($diskCache && $cacheKey != "")
173                         putFileContents($cacheFile, $cacheData);
174
175                 // Stream to client
176                 echo $cacheData;
177         } else {
178                 // Stream uncompressed content
179                 echo $content;
180         }
181
182         die;
183 }
184
185         /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
186
187         function getParam($name, $def = false) {
188                 if (!isset($_GET[$name]))
189                         return $def;
190
191                 return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
192         }
193
194         function getFileContents($path) {
195                 $path = realpath($path);
196
197                 if (!$path || !@is_file($path))
198                         return "";
199
200                 if (function_exists("file_get_contents"))
201                         return @file_get_contents($path);
202
203                 $content = "";
204                 $fp = @fopen($path, "r");
205                 if (!$fp)
206                         return "";
207
208                 while (!feof($fp))
209                         $content .= fgets($fp);
210
211                 fclose($fp);
212
213                 return $content;
214         }
215
216         function putFileContents($path, $content) {
217                 if (function_exists("file_put_contents"))
218                         return @file_put_contents($path, $content);
219
220                 $fp = @fopen($path, "wb");
221                 if ($fp) {
222                         fwrite($fp, $content);
223                         fclose($fp);
224                 }
225         }
226
227         // WP specific
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.
230
231                 // Strip comments
232                 $text = preg_replace("!(^|\s+)//.*$!m", '', $text);
233                 $text = preg_replace("!/\*.*?\*/!s", '', $text);
234
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);
239
240                 return "$text\n";
241         }
242 ?>
243
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;
250 }
251
252 TinyMCECompressed.prototype.init = function(settings) {
253         var elements = document.getElementsByTagName('script');
254         var scriptURL = "";
255
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;
259                         break;
260                 }
261         }
262
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;
269
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>');
273
274         if (!this.loadAdded) {
275                 tinyMCE.addEvent(window, "DOMContentLoaded", TinyMCECompressed.prototype.onLoad);
276                 tinyMCE.addEvent(window, "load", TinyMCECompressed.prototype.onLoad);
277                 this.loadAdded = true;
278         }
279 }
280
281 TinyMCECompressed.prototype.onLoad = function() {
282         if (tinyMCE.isLoaded)
283                 return true;
284
285         tinyMCE = realTinyMCE;
286         TinyMCE_Engine.prototype.onLoad();
287         tinyMCE._addUnloadEvents();
288
289         tinyMCE.isLoaded = true;
290 }
291
292 TinyMCECompressed.prototype.addEvent = function(o, n, h) {
293         if (o.attachEvent)
294                 o.attachEvent("on" + n, h);
295         else
296                 o.addEventListener(n, h, false);
297 }
298
299 TinyMCECompressed.prototype.getOnce = function(str) {
300         var ar = str.replace(/\s+/g, '').split(',');
301
302         for (var i=0; i<ar.length; i++) {
303                 if (ar[i] == '' || ar[i].charAt(0) == '-') {
304                         ar[i] = null;
305                         continue;
306                 }
307
308                 // Skip load
309                 for (var x=0; x<this.loadedFiles.length; x++) {
310                         if (this.loadedFiles[x] == ar[i])
311                                 ar[i] = null;
312                 }
313
314                 this.loadedFiles[this.loadedFiles.length] = ar[i];
315         }
316
317         // Glue
318         str = "";
319         for (var i=0; i<ar.length; i++) {
320                 if (ar[i] == null)
321                         continue;
322
323                 str += ar[i];
324
325                 if (i != ar.length-1)
326                         str += ",";
327         }
328
329         return str;
330 };
331
332 TinyMCECompressed.prototype.loadPlugins = function() {
333         var i, ar;
334
335         TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
336         tinyMCE = realTinyMCE;
337
338         ar = tinyMCECompressed.externalPlugins;
339         for (i=0; i<ar.length; i++)
340                 tinyMCE.loadPlugin(ar[i].name, ar[i].url);
341
342         TinyMCE.prototype.loadScript = function() {};
343 };
344
345 TinyMCECompressed.prototype.loadPlugin = function(n, u) {
346         this.externalPlugins[this.externalPlugins.length] = {name : n, url : u};
347 };
348
349 TinyMCECompressed.prototype.importPluginLanguagePack = function(n, v) {
350         tinyMCE = realTinyMCE;
351         TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
352         tinyMCE.importPluginLanguagePack(n, v);
353 };
354
355 TinyMCECompressed.prototype.addPlugin = function(n, p) {
356         tinyMCE = realTinyMCE;
357         tinyMCE.addPlugin(n, p);
358 };
359
360 var tinyMCE = new TinyMCECompressed();
361 var tinyMCECompressed = tinyMCE;