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