/webroot/js/tinymce/tiny_mce_gzip.php
[return to app]1
<?php
2 /**
3 * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z 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 // Set the error reporting to minimal.
14 @error_reporting(E_ERROR | E_WARNING | E_PARSE);
15
16 // Get input
17 $plugins = explode(',', getParam("plugins", ""));
18 $languages = explode(',', getParam("languages", ""));
19 $themes = explode(',', getParam("themes", ""));
20 $diskCache = getParam("diskcache", "") == "true";
21 $isJS = getParam("js", "") == "true";
22 $compress = getParam("compress", "true") == "true";
23 $core = getParam("core", "true") == "true";
24 $suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
25 $cachePath = realpath("."); // Cache path, this is where the .gz files will be stored
26 $expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
27 $content = "";
28 $encodings = array();
29 $supportsGzip = false;
30 $enc = "";
31 $cacheKey = "";
32
33 // Custom extra javascripts to pack
34 $custom = array(/*
35 "some custom .js file",
36 "some custom .js file"
37 */);
38
39 // Headers
40 header("Content-type: text/javascript");
41 header("Vary: Accept-Encoding"); // Handle proxies
42 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
43
44 // Is called directly then auto init with default settings
45 if (!$isJS) {
46 echo getFileContents("tiny_mce_gzip.js");
47 echo "tinyMCE_GZ.init({});";
48 die();
49 }
50
51 // Setup cache info
52 if ($diskCache) {
53 if (!$cachePath)
54 die("alert('Real path failed.');");
55
56 $cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "") . $suffix;
57
58 foreach ($custom as $file)
59 $cacheKey .= $file;
60
61 $cacheKey = md5($cacheKey);
62
63 if ($compress)
64 $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
65 else
66 $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
67 }
68
69 // Check if it supports gzip
70 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
71 $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
72
73 if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) &&
function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
74 $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
75 $supportsGzip = true;
76 }
77
78 // Use cached file disk cache
79 if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
80 if ($compress)
81 header("Content-Encoding: " . $enc);
82
83 echo getFileContents($cacheFile);
84 die();
85 }
86
87 // Add core
88 if ($core == "true") {
89 $content .= getFileContents("tiny_mce" . $suffix . ".js");
90
91 // Patch loading functions
92 $content .= "tinyMCE_GZ.start();";
93 }
94
95 // Add core languages
96 foreach ($languages as $lang)
97 $content .= getFileContents("langs/" . $lang . ".js");
98
99 // Add themes
100 foreach ($themes as $theme) {
101 $content .= getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js");
102
103 foreach ($languages as $lang)
104 $content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
105 }
106
107 // Add plugins
108 foreach ($plugins as $plugin) {
109 $content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
110
111 foreach ($languages as $lang)
112 $content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
113 }
114
115 // Add custom files
116 foreach ($custom as $file)
117 $content .= getFileContents($file);
118
119 // Restore loading functions
120 if ($core == "true")
121 $content .= "tinyMCE_GZ.end();";
122
123 // Generate GZIP'd content
124 if ($supportsGzip) {
125 if ($compress) {
126 header("Content-Encoding: " . $enc);
127 $cacheData = gzencode($content, 9, FORCE_GZIP);
128 } else
129 $cacheData = $content;
130
131 // Write gz file
132 if ($diskCache && $cacheKey != "")
133 putFileContents($cacheFile, $cacheData);
134
135 // Stream to client
136 echo $cacheData;
137 } else {
138 // Stream uncompressed content
139 echo $content;
140 }
141
142 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
143
144 function getParam($name, $def = false) {
145 if (!isset($_GET[$name]))
146 return $def;
147
148 return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
149 }
150
151 function getFileContents($path) {
152 $path = realpath($path);
153
154 if (!$path || !@is_file($path))
155 return "";
156
157 if (function_exists("file_get_contents"))
158 return @file_get_contents($path);
159
160 $content = "";
161 $fp = @fopen($path, "r");
162 if (!$fp)
163 return "";
164
165 while (!feof($fp))
166 $content .= fgets($fp);
167
168 fclose($fp);
169
170 return $content;
171 }
172
173 function putFileContents($path, $content) {
174 if (function_exists("file_put_contents"))
175 return @file_put_contents($path, $content);
176
177 $fp = @fopen($path, "wb");
178 if ($fp) {
179 fwrite($fp, $content);
180 fclose($fp);
181 }
182 }
183 ?>