PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/tiny_mce/tiny_mce_gzip.php

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