PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/2.1/wp-includes/js/tinymce/tiny_mce_gzip.php

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