b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (CVS HEAD) [ class tree: main ] [ index: main ] [ all elements ]

Source for file tiny_mce_gzip.php

Documentation is available at tiny_mce_gzip.php

  1. <?php
  2. /**
  3.  * $Id: tiny_mce_gzip.php,v 1.1 2009/03/21 22:26:32 fplanque Exp $
  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.  
  13.     // Set the error reporting to minimal.
  14.     @error_reporting(E_ERROR E_WARNING E_PARSE);
  15.  
  16.     // Get input
  17.     $plugins explode(','getParam("plugins"""));
  18.     $languages explode(','getParam("languages"""));
  19.     $themes explode(','getParam("themes"""));
  20.     $diskCache getParam("diskcache"""== "true";
  21.     $isJS getParam("js"""== "true";
  22.     $compress getParam("compress""true"== "true";
  23.     $core getParam("core""true"== "true";
  24.     $suffix getParam("suffix""_src"== "_src" "_src" "";
  25.  
  26.     $cachePath dirname(__FILE__).'/../../../cache/plugins/tinymce'// Cache path, this is where the .gz files will be stored
  27.  
  28.     $expiresOffset 3600 24 10// Cache for 10 days in browser cache
  29.     $content "";
  30.     $encodings array();
  31.     $supportsGzip false;
  32.     $enc "";
  33.     $cacheKey "";
  34.  
  35.     // Custom extra javascripts to pack
  36.     $custom array(/*
  37.         "some custom .js file",
  38.         "some custom .js file"
  39.     */);
  40.  
  41.     // Headers
  42.     header("Content-type: text/javascript");
  43.     header("Vary: Accept-Encoding");  // Handle proxies
  44.     header("Expires: " gmdate("D, d M Y H:i:s"time($expiresOffset" GMT");
  45.  
  46.     // Is called directly then auto init with default settings
  47.     if (!$isJS{
  48.         echo getFileContents("tiny_mce_gzip.js");
  49.         echo "tinyMCE_GZ.init({});";
  50.         die();
  51.     }
  52.  
  53.     // Setup cache info
  54.     if ($diskCache{
  55.         if (!$cachePath || !is_readable($cachePath))
  56.             die("alert('Compressor Real path failed. (tiny_mce_gzip.php) $cachePath');");
  57.  
  58.         $cacheKey getParam("plugins"""getParam("languages"""getParam("themes"""$suffix;
  59.  
  60.         foreach ($custom as $file)
  61.             $cacheKey .= $file;
  62.  
  63.         // Add modification date of core JS file, so that the disk cache expires on
  64.         // TinyMCE upgrade/changes:
  65.         $last_mod_time @filemtime('tiny_mce'.$suffix.'.js');
  66.         $cacheKey .= $last_mod_time;
  67.  
  68.         $cacheKey md5($cacheKey);
  69.  
  70.         $cacheFile $cachePath.'/tiny_mce_'.$cacheKey;
  71.         if ($compress)
  72.             $cacheFile .= '.gz';
  73.         else
  74.             $cacheFile .= '.js';
  75.  
  76.         // Remove old cache files on every ~1000th request:
  77.         ifrand(0,1000010 )
  78.         {
  79.             foreachglob($cachePath.'/tiny_mce_*.{gz,js}'GLOB_NOSORT|GLOB_BRACEas $old_cache_file )
  80.             {
  81.                 iffilectime($old_cache_file$last_mod_time )
  82.                 {
  83.                     @unlink($old_cache_file);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     // Check if it supports gzip
  90.     if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
  91.         $encodings explode(','strtolower(preg_replace("/\s+/"""$_SERVER['HTTP_ACCEPT_ENCODING'])));
  92.  
  93.     if ((in_array('gzip'$encodings|| in_array('x-gzip'$encodings|| isset($_SERVER['---------------'])) && function_exists('ob_gzhandler'&& !ini_get('zlib.output_compression')) {
  94.         $enc in_array('x-gzip'$encodings"x-gzip" "gzip";
  95.         $supportsGzip true;
  96.     }
  97.  
  98.     // Use cached file disk cache
  99.     if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
  100.         if ($compress)
  101.             header("Content-Encoding: " $enc);
  102.  
  103.         echo getFileContents($cacheFile);
  104.         die();
  105.     }
  106.  
  107.     // Add core
  108.     if ($core == "true"{
  109.         $content .= getFileContents("tiny_mce" $suffix ".js");
  110.  
  111.         // Patch loading functions
  112.         $content .= "tinyMCE_GZ.start();";
  113.     }
  114.  
  115.     // Add core languages
  116.     foreach ($languages as $lang)
  117.         $content .= getFileContents("langs/" $lang ".js");
  118.  
  119.     // Add themes
  120.     foreach ($themes as $theme{
  121.         $content .= getFileContents"themes/" $theme "/editor_template" $suffix ".js");
  122.  
  123.         foreach ($languages as $lang)
  124.             $content .= getFileContents("themes/" $theme "/langs/" $lang ".js");
  125.     }
  126.  
  127.     // Add plugins
  128.     foreach ($plugins as $plugin{
  129.         $content .= getFileContents("plugins/" $plugin "/editor_plugin" $suffix ".js");
  130.  
  131.         foreach ($languages as $lang)
  132.             $content .= getFileContents("plugins/" $plugin "/langs/" $lang ".js");
  133.     }
  134.  
  135.     // Add custom files
  136.     foreach ($custom as $file)
  137.         $content .= getFileContents($file);
  138.  
  139.     // Restore loading functions
  140.     if ($core == "true")
  141.         $content .= "tinyMCE_GZ.end();";
  142.  
  143.     // Generate GZIP'd content
  144.     if ($supportsGzip{
  145.         if ($compress{
  146.             header("Content-Encoding: " $enc);
  147.             $cacheData gzencode($content9FORCE_GZIP);
  148.         else
  149.             $cacheData $content;
  150.  
  151.         // Write gz file
  152.         if ($diskCache && $cacheKey != "")
  153.             putFileContents($cacheFile$cacheData);
  154.  
  155.         // Stream to client
  156.         echo $cacheData;
  157.     else {
  158.         // Stream uncompressed content
  159.         echo $content;
  160.     }
  161.  
  162.     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  163.  
  164.     function getParam($name$def false{
  165.         if (!isset($_GET[$name]))
  166.             return $def;
  167.  
  168.         return preg_replace("/[^0-9a-z\-_,]+/i"""$_GET[$name])// Remove anything but 0-9,a-z,-_
  169.     }
  170.  
  171.     function getFileContents($path{
  172.         $path realpath($path);
  173.  
  174.         if (!$path || !@is_file($path))
  175.             return "";
  176.  
  177.         if (function_exists("file_get_contents"))
  178.             return @file_get_contents($path);
  179.  
  180.         $content "";
  181.         $fp @fopen($path"r");
  182.         if (!$fp)
  183.             return "";
  184.  
  185.         while (!feof($fp))
  186.             $content .= fgets($fp);
  187.  
  188.         fclose($fp);
  189.  
  190.         return $content;
  191.     }
  192.  
  193.     function putFileContents($path$content{
  194.         if (function_exists("file_put_contents"))
  195.             return @file_put_contents($path$content);
  196.  
  197.         $fp @fopen($path"wb");
  198.         if ($fp{
  199.             fwrite($fp$content);
  200.             fclose($fp);
  201.         }
  202.     }
  203. ?>

Documentation generated on Sat, 06 Mar 2010 03:58:19 +0100 by phpDocumentor 1.4.2. This site is hosted and maintained by Daniel HAHLER (Contact).