Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / editor / tinymce / jscripts / tiny_mce / tiny_mce_gzip.php
blobce3fa787587d24e8b088fc0ac710b8ee3ac24a99
1 <?php // $Id$
2 /**
3 * $RCSfile$
4 * $Revision$
5 * $Date$
7 * @version 1.07
8 * @author Moxiecode
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.
16 // General options
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
23 // Headers
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");
29 // Get data to load
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";
37 $cacheData = "";
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));
44 fclose($fd);
45 return $content;
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;
57 if ($diskCache)
58 $cacheData .= $str;
59 else
60 echo $str;
63 // Only gzip the contents if clients and server support it
64 $encodings = array();
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);
75 die;
78 if (!$diskCache)
79 ob_start("ob_gzhandler");
80 } else
81 $diskCache = false;
83 if ($index > -1) {
84 // Write main script and patch some things
85 if ($index == 0) {
86 TinyMCE_echo(file_get_contents(realpath("tiny_mce" . $suffix . ".js")));
87 TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
88 } else
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
95 if ($theme) {
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")));
100 if ($language)
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");
109 if ($pluginFile)
110 TinyMCE_echo(file_get_contents($pluginFile));
112 if ($languageFile)
113 TinyMCE_echo(file_get_contents($languageFile));
116 // Reset tinyMCE compressor engine
117 TinyMCE_echo("tinyMCE = tinyMCECompressed;");
119 // Write to cache
120 if ($diskCache) {
121 // Calculate compression ratio and debug target output path
122 if ($debug) {
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");
131 if ($fp) {
132 fwrite($fp, $cacheData);
133 fclose($fp);
136 // Output
137 header("Content-Encoding: gzip");
138 echo $cacheData;
141 die;
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');
154 var scriptURL = "";
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;
159 break;
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)
182 return true;
184 tinyMCE = realTinyMCE;
185 TinyMCE_Engine.prototype.onLoad();
186 tinyMCE._addUnloadEvents();
187 tinyMCE.isLoaded = true;
190 TinyMCECompressed.prototype.addEvent = function(o, n, h) {
191 if (o.attachEvent)
192 o.attachEvent("on" + n, h);
193 else
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++) {
201 if (ar[i] == '')
202 continue;
204 // Skip load
205 for (var x=0; x<this.loadedFiles.length; x++) {
206 if (this.loadedFiles[x] == ar[i])
207 ar[i] = null;
210 this.loadedFiles[this.loadedFiles.length] = ar[i];
213 // Glue
214 str = "";
215 for (var i=0; i<ar.length; i++) {
216 if (ar[i] == null)
217 continue;
219 str += ar[i];
221 if (i != ar.length-1)
222 str += ",";
225 return str;
228 var tinyMCE = new TinyMCECompressed();
229 var tinyMCECompressed = tinyMCE;