PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

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