/plugins/editors/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php

https://bitbucket.org/stager94/skmz-joomla · PHP · 183 lines · 113 code · 38 blank · 32 comment · 37 complexity · 6f59aedbea6bb27e0785494cd2e7b781 MD5 · raw file

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