1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13: if (TinyMCE_Compressor::getParam("js")) {
14:
15: $tinyMCECompressor = new TinyMCE_Compressor(array(
16: 17: 18: 19: 20: 21: 22: 23:
24: ));
25:
26:
27: $tinyMCECompressor->handleRequest();
28: }
29:
30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: class TinyMCE_Compressor {
47: private $files, $settings;
48: private static $defaultSettings = array(
49: "plugins" => "",
50: "themes" => "",
51: "languages" => "",
52: "disk_cache" => false,
53: "expires" => "30d",
54: "cache_dir" => "",
55: "compress" => true,
56: "files" => "",
57: "source" => true,
58: );
59:
60: 61: 62: 63: 64:
65: public function __construct($settings = array()) {
66: $this->settings = array_merge(self::$defaultSettings, $settings);
67:
68: if (empty($this->settings["cache_dir"])) {
69: $this->settings["cache_dir"] = dirname(__FILE__);
70: }
71: }
72:
73: 74: 75: 76: 77:
78: public function &addFile($file) {
79: $this->files .= ($this->files ? "," : "") . $file;
80:
81: return $this;
82: }
83:
84: 85: 86:
87: public function handleRequest() {
88: $files = array();
89: $supportsGzip = false;
90: $expiresOffset = $this->parseTime($this->settings["expires"]);
91: $tinymceDir = dirname(__FILE__);
92:
93:
94: $plugins = self::getParam("plugins");
95: if ($plugins) {
96: $this->settings["plugins"] = $plugins;
97: }
98:
99: $plugins = preg_split('/,/', $this->settings["plugins"], -1, PREG_SPLIT_NO_EMPTY);
100:
101:
102: $themes = self::getParam("themes");
103: if ($themes) {
104: $this->settings["themes"] = $themes;
105: }
106:
107: $themes = preg_split('/,/', $this->settings["themes"], -1, PREG_SPLIT_NO_EMPTY);
108:
109:
110: $languages = self::getParam("languages");
111: if ($languages) {
112: $this->settings["languages"] = $languages;
113: }
114:
115: $languages = preg_split('/,/', $this->settings["languages"], -1, PREG_SPLIT_NO_EMPTY);
116:
117:
118: $tagFiles = self::getParam("files");
119: if ($tagFiles) {
120: $this->settings["files"] = $tagFiles;
121: }
122:
123:
124: $diskCache = self::getParam("diskcache");
125: if ($diskCache) {
126: $this->settings["disk_cache"] = ($diskCache === "true");
127: }
128:
129:
130: $src = self::getParam("src");
131: if ($src) {
132: $this->settings["source"] = ($src === "true");
133: }
134:
135:
136: if (self::getParam("core", "true") === "true") {
137: $files[] = "tinymce";
138: }
139:
140:
141: foreach ($languages as $language) {
142: $files[] = "langs/" . $language;
143: }
144:
145:
146: foreach ($plugins as $plugin) {
147: $files[] = "plugins/" . $plugin . "/plugin";
148:
149: foreach ($languages as $language) {
150: $files[] = "plugins/" . $plugin . "/langs/" . $language;
151: }
152: }
153:
154:
155: foreach ($themes as $theme) {
156: $files[] = "themes/" . $theme . "/theme";
157:
158: foreach ($languages as $language) {
159: $files[] = "themes/" . $theme . "/langs/" . $language;
160: }
161: }
162:
163:
164: $allFiles = array_merge($files, preg_split('/,/', $this->settings['files'], -1, PREG_SPLIT_NO_EMPTY));
165:
166:
167: for ($i = 0; $i < count($allFiles); $i++) {
168: $file = $allFiles[$i];
169:
170: if ($this->settings["source"] && file_exists($file . ".js")) {
171: $file .= ".js";
172: } else if (file_exists($file . ".min.js")) {
173: $file .= ".min.js";
174: } else {
175: $file = "";
176: }
177:
178: $allFiles[$i] = $file;
179: }
180:
181:
182: $hash = md5(implode('', $allFiles));
183:
184:
185: $zlibOn = ini_get('zlib.output_compression') || (ini_set('zlib.output_compression', 0) === false);
186: $encodings = (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) ? strtolower($_SERVER['HTTP_ACCEPT_ENCODING']) : "";
187: $encoding = preg_match( '/\b(x-gzip|gzip)\b/', $encodings, $match) ? $match[1] : "";
188:
189:
190: if (isset($_SERVER['---------------'])) {
191: $encoding = "x-gzip";
192: }
193:
194: $supportsGzip = $this->settings['compress'] && !empty($encoding) && !$zlibOn && function_exists('gzencode');
195:
196:
197: $cacheFile = $this->settings["cache_dir"] . "/tinymce.gzip-" . $hash . ($supportsGzip ? ".gz" : ".js");
198:
199:
200: header("Content-type: text/javascript");
201: header("Vary: Accept-Encoding");
202: header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
203: header("Cache-Control: public, max-age=" . $expiresOffset);
204:
205: if ($supportsGzip) {
206: header("Content-Encoding: " . $encoding);
207: }
208:
209:
210: if ($this->settings['disk_cache'] && file_exists($cacheFile)) {
211: readfile($cacheFile);
212: return;
213: }
214:
215:
216: $buffer = "var tinyMCEPreInit={base:'" . dirname($_SERVER["SCRIPT_NAME"]) . "',suffix:'.min'};";
217:
218:
219: foreach ($allFiles as $file) {
220: if ($file) {
221: $fileContents = $this->getFileContents($tinymceDir . "/" . $file);
222:
223: $buffer .= $fileContents;
224: }
225: }
226:
227:
228: $buffer .= 'tinymce.each("' . implode(',', $files) . '".split(","),function(f){tinymce.ScriptLoader.markDone(tinyMCE.baseURL+"/"+f+".js");});';
229:
230:
231: if ($supportsGzip) {
232: $buffer = gzencode($buffer, 9, FORCE_GZIP);
233: }
234:
235:
236: if ($this->settings["disk_cache"]) {
237: @file_put_contents($cacheFile, $buffer);
238: }
239:
240:
241: echo $buffer;
242: }
243:
244: 245: 246: 247: 248: 249: 250:
251: public static function renderTag($tagSettings, $return = false) {
252: $settings = array_merge(self::$defaultSettings, $tagSettings);
253:
254: if (empty($settings["cache_dir"])) {
255: $settings["cache_dir"] = dirname(__FILE__);
256: }
257:
258: $scriptSrc = $settings["url"] . "?js=1";
259:
260:
261: if (isset($settings["plugins"])) {
262: $scriptSrc .= "&plugins=" . (is_array($settings["plugins"]) ? implode(',', $settings["plugins"]) : $settings["plugins"]);
263: }
264:
265:
266: if (isset($settings["themes"])) {
267: $scriptSrc .= "&themes=" . (is_array($settings["themes"]) ? implode(',', $settings["themes"]) : $settings["themes"]);
268: }
269:
270:
271: if (isset($settings["languages"])) {
272: $scriptSrc .= "&languages=" . (is_array($settings["languages"]) ? implode(',', $settings["languages"]) : $settings["languages"]);
273: }
274:
275:
276: if (isset($settings["disk_cache"])) {
277: $scriptSrc .= "&diskcache=" . ($settings["disk_cache"] === true ? "true" : "false");
278: }
279:
280:
281: 282: 283: 284: 285: 286:
287: if (isset($tagSettings["files"])) {
288: $scriptSrc .= "&files=" .(is_array($settings["files"]) ? implode(',', $settings["files"]) : $settings["files"]);
289: }
290:
291:
292: if (isset($settings["source"])) {
293: $scriptSrc .= "&src=" . ($settings["source"] === true ? "true" : "false");
294: }
295:
296: $scriptTag = '<script src="' . htmlspecialchars($scriptSrc) . '"></script>';
297:
298: if ($return) {
299: return $scriptTag;
300: } else {
301: echo $scriptTag;
302: }
303: }
304:
305: 306: 307: 308: 309: 310: 311:
312: public static function getParam($name, $default = "") {
313: if (!isset($_GET[$name])) {
314: return $default;
315: }
316:
317: return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]);
318: }
319:
320: 321: 322: 323: 324: 325:
326: private function parseTime($time) {
327: $multipel = 1;
328:
329:
330: if (strpos($time, "h") > 0) {
331: $multipel = 3600;
332: }
333:
334:
335: if (strpos($time, "d") > 0) {
336: $multipel = 86400;
337: }
338:
339:
340: if (strpos($time, "m") > 0) {
341: $multipel = 2592000;
342: }
343:
344:
345: return intval($time) * $multipel;
346: }
347:
348: 349: 350: 351: 352: 353:
354: private function getFileContents($file) {
355: $content = file_get_contents($file);
356:
357:
358: if (substr($content, 0, 3) === pack("CCC", 0xef, 0xbb, 0xbf)) {
359: $content = substr($content, 3);
360: }
361:
362: return $content;
363: }
364: }
365: ?>