PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vj-admin/tiny_mce/tiny_mce_gzip.php

http://vanilla-journal.googlecode.com/
PHP | 268 lines | 182 code | 52 blank | 34 comment | 49 complexity | f67a38cb770fd78de7550a4dcc8b7df1 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0
  1. <?php
  2. /**
  3. * $RCSfile: tiny_mce_gzip.php,v $
  4. * $Revision: $
  5. * $Date: $
  6. *
  7. * @version 1.08
  8. * @author Moxiecode
  9. * @copyright Copyright Š 2005-2006, Moxiecode Systems AB, All rights reserved.
  10. *
  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.
  14. */
  15. // General options
  16. $suffix = ""; // Set to "_src" to use source version
  17. $expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires
  18. $diskCache = false; // If you enable this option gzip files will be cached on disk.
  19. $cacheDir = realpath("."); // Absolute directory path to where cached gz files will be stored
  20. $debug = false; // Enable this option if you need debuging info
  21. // Headers
  22. header("Content-type: text/javascript; charset: UTF-8");
  23. // header("Cache-Control: must-revalidate");
  24. header("Vary: Accept-Encoding"); // Handle proxies
  25. header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
  26. // Get data to load
  27. $theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : "";
  28. $language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : "";
  29. $plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : "";
  30. $lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en";
  31. $index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1;
  32. $cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug);
  33. $cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" . $cacheKey . ".gz";
  34. $cacheData = "";
  35. // Patch older versions of PHP < 4.3.0
  36. if (!function_exists('file_get_contents')) {
  37. function file_get_contents($filename) {
  38. $fd = fopen($filename, 'rb');
  39. $content = fread($fd, filesize($filename));
  40. fclose($fd);
  41. return $content;
  42. }
  43. }
  44. // Security check function, can only contain a-z 0-9 , _ - and whitespace.
  45. function TinyMCE_cleanInput($str) {
  46. return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_
  47. }
  48. function TinyMCE_echo($str) {
  49. global $cacheData, $diskCache;
  50. if ($diskCache)
  51. $cacheData .= $str;
  52. else
  53. echo $str;
  54. }
  55. // Only gzip the contents if clients and server support it
  56. $encodings = array();
  57. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
  58. $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
  59. // Check for gzip header or northon internet securities
  60. if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
  61. $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
  62. // Use cached file if it exists but not in debug mode
  63. if (file_exists($cacheFile) && !$debug) {
  64. header("Content-Encoding: " . $enc);
  65. echo file_get_contents($cacheFile);
  66. die;
  67. }
  68. if (!$diskCache)
  69. ob_start("ob_gzhandler");
  70. } else
  71. $diskCache = false;
  72. if ($index > -1) {
  73. // Write main script and patch some things
  74. if ($index == 0) {
  75. TinyMCE_echo(file_get_contents(realpath("tiny_mce" . $suffix . ".js")));
  76. TinyMCE_echo('TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;');
  77. TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
  78. } else
  79. TinyMCE_echo('tinyMCE = realTinyMCE;');
  80. // Do init based on index
  81. TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);");
  82. // Load external plugins
  83. if ($index == 0)
  84. TinyMCE_echo("tinyMCECompressed.loadPlugins();");
  85. // Load theme, language pack and theme language packs
  86. if ($theme) {
  87. TinyMCE_echo(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")));
  88. TinyMCE_echo(file_get_contents(realpath("themes/" . $theme . "/langs/" . $lang . ".js")));
  89. }
  90. if ($language)
  91. TinyMCE_echo(file_get_contents(realpath("langs/" . $language . ".js")));
  92. // Load all plugins and their language packs
  93. $plugins = explode(",", $plugins);
  94. foreach ($plugins as $plugin) {
  95. $pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
  96. $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js");
  97. if ($pluginFile)
  98. TinyMCE_echo(file_get_contents($pluginFile));
  99. if ($languageFile)
  100. TinyMCE_echo(file_get_contents($languageFile));
  101. }
  102. // Reset tinyMCE compressor engine
  103. TinyMCE_echo("tinyMCE = tinyMCECompressed;");
  104. // Write to cache
  105. if ($diskCache) {
  106. // Calculate compression ratio and debug target output path
  107. if ($debug) {
  108. $ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0);
  109. TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');");
  110. }
  111. $cacheData = gzencode($cacheData, 9, FORCE_GZIP);
  112. // Write to file if possible
  113. $fp = @fopen($cacheFile, "wb");
  114. if ($fp) {
  115. fwrite($fp, $cacheData);
  116. fclose($fp);
  117. }
  118. // Output
  119. header("Content-Encoding: " . $enc);
  120. echo $cacheData;
  121. }
  122. die;
  123. }
  124. ?>
  125. function TinyMCECompressed() {
  126. this.configs = new Array();
  127. this.loadedFiles = new Array();
  128. this.externalPlugins = new Array();
  129. this.loadAdded = false;
  130. this.isLoaded = false;
  131. }
  132. TinyMCECompressed.prototype.init = function(settings) {
  133. var elements = document.getElementsByTagName('script');
  134. var scriptURL = "";
  135. for (var i=0; i<elements.length; i++) {
  136. if (elements[i].src && elements[i].src.indexOf("tiny_mce_gzip.php") != -1) {
  137. scriptURL = elements[i].src;
  138. break;
  139. }
  140. }
  141. settings["theme"] = typeof(settings["theme"]) != "undefined" ? settings["theme"] : "default";
  142. settings["plugins"] = typeof(settings["plugins"]) != "undefined" ? settings["plugins"] : "";
  143. settings["language"] = typeof(settings["language"]) != "undefined" ? settings["language"] : "en";
  144. settings["button_tile_map"] = typeof(settings["button_tile_map"]) != "undefined" ? settings["button_tile_map"] : true;
  145. this.configs[this.configs.length] = settings;
  146. this.settings = settings;
  147. 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);
  148. document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + scriptURL + '"></script>');
  149. if (!this.loadAdded) {
  150. tinyMCE.addEvent(window, "DOMContentLoaded", TinyMCECompressed.prototype.onLoad);
  151. tinyMCE.addEvent(window, "load", TinyMCECompressed.prototype.onLoad);
  152. this.loadAdded = true;
  153. }
  154. }
  155. TinyMCECompressed.prototype.onLoad = function() {
  156. if (tinyMCE.isLoaded)
  157. return true;
  158. tinyMCE = realTinyMCE;
  159. TinyMCE_Engine.prototype.onLoad();
  160. tinyMCE._addUnloadEvents();
  161. tinyMCE.isLoaded = true;
  162. }
  163. TinyMCECompressed.prototype.addEvent = function(o, n, h) {
  164. if (o.attachEvent)
  165. o.attachEvent("on" + n, h);
  166. else
  167. o.addEventListener(n, h, false);
  168. }
  169. TinyMCECompressed.prototype.getOnce = function(str) {
  170. var ar = str.replace(/\s+/g, '').split(',');
  171. for (var i=0; i<ar.length; i++) {
  172. if (ar[i] == '' || ar[i].charAt(0) == '-') {
  173. ar[i] = null;
  174. continue;
  175. }
  176. // Skip load
  177. for (var x=0; x<this.loadedFiles.length; x++) {
  178. if (this.loadedFiles[x] == ar[i])
  179. ar[i] = null;
  180. }
  181. this.loadedFiles[this.loadedFiles.length] = ar[i];
  182. }
  183. // Glue
  184. str = "";
  185. for (var i=0; i<ar.length; i++) {
  186. if (ar[i] == null)
  187. continue;
  188. str += ar[i];
  189. if (i != ar.length-1)
  190. str += ",";
  191. }
  192. return str;
  193. };
  194. TinyMCECompressed.prototype.loadPlugins = function() {
  195. var i, ar;
  196. TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
  197. tinyMCE = realTinyMCE;
  198. ar = tinyMCECompressed.externalPlugins;
  199. for (i=0; i<ar.length; i++)
  200. tinyMCE.loadPlugin(ar[i].name, ar[i].url);
  201. TinyMCE.prototype.loadScript = function() {};
  202. };
  203. TinyMCECompressed.prototype.loadPlugin = function(n, u) {
  204. this.externalPlugins[this.externalPlugins.length] = {name : n, url : u};
  205. };
  206. TinyMCECompressed.prototype.importPluginLanguagePack = function(n, v) {
  207. tinyMCE = realTinyMCE;
  208. TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
  209. tinyMCE.importPluginLanguagePack(n, v);
  210. };
  211. TinyMCECompressed.prototype.addPlugin = function(n, p) {
  212. tinyMCE = realTinyMCE;
  213. tinyMCE.addPlugin(n, p);
  214. };
  215. var tinyMCE = new TinyMCECompressed();
  216. var tinyMCECompressed = tinyMCE;