9 * @copyright Copyright © 20052006, Moxiecode Systems AB, All rights reserved.
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.
17 $suffix = ""; // Set to "_src" to use source version
18 $expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires
19 $diskCache = false; // If you enable this option gzip files will be cached on disk.
20 $cacheDir = realpath("."); // Absolute directory path to where cached gz files will be stored
21 $debug = false; // Enable this option if you need debuging info
24 header("Content-type: text/javascript; charset: UTF-8");
25 // header("Cache-Control: must-revalidate");
26 header("Vary: Accept-Encoding"); // Handle proxies
27 header("Expires: " . gmdate("D, d M Y H:i:s", time() +
$expiresOffset) . " GMT");
30 $theme = isset($_GET['theme']) ?
TinyMCE_cleanInput($_GET['theme']) : "";
31 $language = isset($_GET['language']) ?
TinyMCE_cleanInput($_GET['language']) : "";
32 $plugins = isset($_GET['plugins']) ?
TinyMCE_cleanInput($_GET['plugins']) : "";
33 $lang = isset($_GET['lang']) ?
TinyMCE_cleanInput($_GET['lang']) : "en";
34 $index = isset($_GET['index']) ?
TinyMCE_cleanInput($_GET['index']) : -1;
35 $cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug);
36 $cacheFile = $cacheDir == "" ?
"" : $cacheDir . "/" . "tinymce_" . $cacheKey . ".gz";
39 // Patch older versions of PHP < 4.3.0
40 if (!function_exists('file_get_contents')) {
41 function file_get_contents($filename) {
42 $fd = fopen($filename, 'rb');
43 $content = fread($fd, filesize($filename));
49 // Security check function, can only contain a-z 0-9 , _ - and whitespace.
50 function TinyMCE_cleanInput($str) {
51 return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_
54 function TinyMCE_echo($str) {
55 global $cacheData, $diskCache;
63 // Only gzip the contents if clients and server support it
66 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
67 $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
69 // Check for gzip header or northon internet securities
70 if ((in_array('gzip', $encodings) ||
isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
71 // Use cached file if it exists but not in debug mode
72 if (file_exists($cacheFile) && !$debug) {
73 header("Content-Encoding: gzip");
74 echo file_get_contents($cacheFile);
79 ob_start("ob_gzhandler");
84 // Write main script and patch some things
86 TinyMCE_echo(file_get_contents(realpath("tiny_mce" . $suffix . ".js")));
87 TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
89 TinyMCE_echo('tinyMCE = realTinyMCE;');
91 // Do init based on index
92 TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);");
94 // Load theme, language pack and theme language packs
96 TinyMCE_echo(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")));
97 TinyMCE_echo(file_get_contents(realpath("themes/" . $theme . "/langs/" . $lang . ".js")));
101 TinyMCE_echo(file_get_contents(realpath("langs/" . $language . ".js")));
103 // Load all plugins and their language packs
104 $plugins = explode(",", $plugins);
105 foreach ($plugins as $plugin) {
106 $pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
107 $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js");
110 TinyMCE_echo(file_get_contents($pluginFile));
113 TinyMCE_echo(file_get_contents($languageFile));
116 // Reset tinyMCE compressor engine
117 TinyMCE_echo("tinyMCE = tinyMCECompressed;");
121 // Calculate compression ratio and debug target output path
123 $ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP
)) / strlen($cacheData) * 100.0);
124 TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');");
127 $cacheData = gzencode($cacheData, 9, FORCE_GZIP
);
129 // Write to file if possible
130 $fp = @fopen
($cacheFile, "wb");
132 fwrite($fp, $cacheData);
137 header("Content-Encoding: gzip");
145 function TinyMCECompressed() {
146 this
.configs
= new Array();
147 this
.loadedFiles
= new Array();
148 this
.loadAdded
= false;
149 this
.isLoaded
= false;
152 TinyMCECompressed
.prototype
.init
= function(settings
) {
153 var elements
= document
.getElementsByTagName('script');
156 for (var i
=0; i
<elements
.length
; i++
) {
157 if (elements
[i
].src
&& elements
[i
].src
.indexOf("tiny_mce_gzip.php") != -1) {
158 scriptURL
= elements
[i
].src
;
163 settings
["theme"] = typeof(settings
["theme"]) != "undefined" ? settings
["theme"] : "default";
164 settings
["plugins"] = typeof(settings
["plugins"]) != "undefined" ? settings
["plugins"] : "";
165 settings
["language"] = typeof(settings
["language"]) != "undefined" ? settings
["language"] : "en";
166 settings
["button_tile_map"] = typeof(settings
["button_tile_map"]) != "undefined" ? settings
["button_tile_map"] : true;
167 this
.configs
[this
.configs
.length
] = settings
;
168 this
.settings
= settings
;
170 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);
171 document
.write('<sc'+
'ript language="javascript" type="text/javascript" src="' + scriptURL +
'"></script>');
173 if (!this
.loadAdded
) {
174 tinyMCE
.addEvent(window
, "DOMContentLoaded", TinyMCECompressed
.prototype
.onLoad
);
175 tinyMCE
.addEvent(window
, "load", TinyMCECompressed
.prototype
.onLoad
);
176 this
.loadAdded
= true;
180 TinyMCECompressed
.prototype
.onLoad
= function() {
181 if (tinyMCE
.isLoaded
)
184 tinyMCE
= realTinyMCE
;
185 TinyMCE_Engine
.prototype
.onLoad();
186 tinyMCE
._addUnloadEvents();
187 tinyMCE
.isLoaded
= true;
190 TinyMCECompressed
.prototype
.addEvent
= function(o
, n
, h
) {
192 o
.attachEvent("on" + n
, h
);
194 o
.addEventListener(n
, h
, false);
197 TinyMCECompressed
.prototype
.getOnce
= function(str
) {
198 var ar
= str
.split(',');
200 for (var i
=0; i
<ar
.length
; i++
) {
205 for (var x
=0; x
<this
.loadedFiles
.length
; x++
) {
206 if (this
.loadedFiles
[x
] == ar
[i
])
210 this
.loadedFiles
[this
.loadedFiles
.length
] = ar
[i
];
215 for (var i
=0; i
<ar
.length
; i++
) {
221 if (i
!= ar
.length
-1)
228 var tinyMCE
= new TinyMCECompressed();
229 var tinyMCECompressed
= tinyMCE
;