Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • idna_convert
  • SearchResultModule
  • TinyMCE_Compressor

Functions

  • buildCategoryArray
  • decDate
  • getJobFileName
  • getLastActialRunTime
  • getLastScheduledRunTime
  • logMessage
  • lTrimZeros
  • markLastRun
  • parseCronFile
  • parseElement
  • runJob
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * tinymce.gzip.php
  4:  *
  5:  * Copyright, Moxiecode Systems AB
  6:  * Released under LGPL License.
  7:  *
  8:  * License: http://tinymce.moxiecode.com/license
  9:  * Contributing: http://tinymce.moxiecode.com/contributing
 10:  */
 11: 
 12: // Handle incoming request if it's a script call
 13: if (TinyMCE_Compressor::getParam("js")) {
 14:     // Default settings
 15:     $tinyMCECompressor = new TinyMCE_Compressor(array(
 16:     /*
 17:      * Add any site-specific defaults here that you may wish to implement. For example:
 18:      *
 19:      *  "languages" => "en",
 20:      *  "cache_dir" => realpath(dirname(__FILE__) . "/../../_cache"),
 21:      *  "files"     => "somescript,anotherscript",
 22:      *  "expires"   => "1m",
 23:      */
 24:     ));
 25: 
 26:     // Handle request, compress and stream to client
 27:     $tinyMCECompressor->handleRequest();
 28: }
 29: 
 30: /**
 31:  * This class combines and compresses the TinyMCE core, plugins, themes and
 32:  * language packs into one disk cached gzipped request. It improves the loading speed of TinyMCE dramatically but
 33:  * still provides dynamic initialization.
 34:  *
 35:  * Example of direct usage:
 36:  * require_once("../js/tinymce/tinymce.gzip.php");
 37:  *
 38:  * // Renders script tag with compressed scripts
 39:  * TinyMCE_Compressor::renderTag(array(
 40:  *    "url" => "../js/tinymce/tinymce.gzip.php",
 41:  *    "plugins" => "pagebreak,style",
 42:  *    "themes" => "advanced",
 43:  *    "languages" => "en"
 44:  * ));
 45:  */
 46: class TinyMCE_Compressor {
 47:     private $files, $settings;
 48:     private static $defaultSettings = array(
 49:         "plugins"    => "",
 50:         "themes"     => "",
 51:         "languages"  => "",
 52:         "disk_cache" => false,
 53:         "expires"    => "30d",
 54:         "cache_dir"  => "",
 55:         "compress"   => true,
 56:         "files"      => "",
 57:         "source"     => true,
 58:     );
 59: 
 60:     /**
 61:      * Constructs a new compressor instance.
 62:      *
 63:      * @param Array $settings Name/value array with non-default settings for the compressor instance.
 64:      */
 65:     public function __construct($settings = array()) {
 66:         $this->settings = array_merge(self::$defaultSettings, $settings);
 67: 
 68:         if (empty($this->settings["cache_dir"])) {
 69:             $this->settings["cache_dir"] = dirname(__FILE__);
 70:         }
 71:     }
 72: 
 73:     /**
 74:      * Adds a file to the concatenation/compression process.
 75:      *
 76:      * @param String $path Path to the file to include in the compressed package/output.
 77:      */
 78:     public function &addFile($file) {
 79:         $this->files .= ($this->files ? "," : "") . $file;
 80: 
 81:         return $this;
 82:     }
 83: 
 84:     /**
 85:      * Handles the incoming HTTP request and sends back a compressed script depending on settings and client support.
 86:      */
 87:     public function handleRequest() {
 88:         $files = array();
 89:         $supportsGzip = false;
 90:         $expiresOffset = $this->parseTime($this->settings["expires"]);
 91:         $tinymceDir = dirname(__FILE__);
 92: 
 93:         // Plugins
 94:         $plugins = self::getParam("plugins");
 95:         if ($plugins) {
 96:             $this->settings["plugins"] = $plugins;
 97:         }
 98: 
 99:         $plugins = preg_split('/,/', $this->settings["plugins"], -1, PREG_SPLIT_NO_EMPTY);
100: 
101:         // Themes
102:         $themes = self::getParam("themes");
103:         if ($themes) {
104:             $this->settings["themes"] = $themes;
105:         }
106: 
107:         $themes = preg_split('/,/', $this->settings["themes"], -1, PREG_SPLIT_NO_EMPTY);
108: 
109:         // Languages
110:         $languages = self::getParam("languages");
111:         if ($languages) {
112:             $this->settings["languages"] = $languages;
113:         }
114: 
115:         $languages = preg_split('/,/', $this->settings["languages"], -1, PREG_SPLIT_NO_EMPTY);
116: 
117:         // Files
118:         $tagFiles = self::getParam("files");
119:         if ($tagFiles) {
120:             $this->settings["files"] = $tagFiles;
121:         }
122: 
123:         // Diskcache option
124:         $diskCache = self::getParam("diskcache");
125:         if ($diskCache) {
126:             $this->settings["disk_cache"] = ($diskCache === "true");
127:         }
128: 
129:         // Source or minified version
130:         $src = self::getParam("src");
131:         if ($src) {
132:             $this->settings["source"] = ($src === "true");
133:         }
134: 
135:         // Add core js
136:         if (self::getParam("core", "true") === "true") {
137:             $files[] = "tinymce";
138:         }
139: 
140:         // Add core languages
141:         foreach ($languages as $language) {
142:             $files[] = "langs/" . $language;
143:         }
144: 
145:         // Add plugins
146:         foreach ($plugins as $plugin) {
147:             $files[] = "plugins/" . $plugin . "/plugin";
148: 
149:             foreach ($languages as $language) {
150:                 $files[] = "plugins/" . $plugin . "/langs/" . $language;
151:             }
152:         }
153: 
154:         // Add themes
155:         foreach ($themes as $theme) {
156:             $files[] = "themes/" . $theme . "/theme";
157: 
158:             foreach ($languages as $language) {
159:                 $files[] = "themes/" . $theme . "/langs/" . $language;
160:             }
161:         }
162: 
163:         // Add any specified files.
164:         $allFiles = array_merge($files, preg_split('/,/', $this->settings['files'], -1, PREG_SPLIT_NO_EMPTY));
165: 
166:         // Process source files
167:         for ($i = 0; $i < count($allFiles); $i++) {
168:             $file = $allFiles[$i];
169: 
170:             if ($this->settings["source"] && file_exists($file . ".js")) {
171:                 $file .= ".js";
172:             } else if (file_exists($file . ".min.js"))  {
173:                 $file .= ".min.js";
174:             } else {
175:                 $file = "";
176:             }
177: 
178:             $allFiles[$i] = $file;
179:         }
180: 
181:         // Generate hash for all files
182:         $hash = md5(implode('', $allFiles));
183: 
184:         // Check if it supports gzip
185:         $zlibOn = ini_get('zlib.output_compression') || (ini_set('zlib.output_compression', 0) === false);
186:         $encodings = (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) ? strtolower($_SERVER['HTTP_ACCEPT_ENCODING']) : "";
187:         $encoding = preg_match( '/\b(x-gzip|gzip)\b/', $encodings, $match) ? $match[1] : "";
188: 
189:         // Is northon antivirus header
190:         if (isset($_SERVER['---------------'])) {
191:             $encoding = "x-gzip";
192:         }
193: 
194:         $supportsGzip = $this->settings['compress'] && !empty($encoding) && !$zlibOn && function_exists('gzencode');
195: 
196:         // Set cache file name
197:         $cacheFile = $this->settings["cache_dir"] . "/tinymce.gzip-" . $hash . ($supportsGzip ? ".gz" : ".js");
198: 
199:         // Set headers
200:         header("Content-type: text/javascript");
201:         header("Vary: Accept-Encoding");  // Handle proxies
202:         header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
203:         header("Cache-Control: public, max-age=" . $expiresOffset);
204: 
205:         if ($supportsGzip) {
206:             header("Content-Encoding: " . $encoding);
207:         }
208: 
209:         // Use cached file
210:         if ($this->settings['disk_cache'] && file_exists($cacheFile)) {
211:             readfile($cacheFile);
212:             return;
213:         }
214: 
215:         // Set base URL for where tinymce is loaded from
216:         $buffer = "var tinyMCEPreInit={base:'" . dirname($_SERVER["SCRIPT_NAME"]) . "',suffix:'.min'};";
217: 
218:         // Load all tinymce script files into buffer
219:         foreach ($allFiles as $file) {
220:             if ($file) {
221:                 $fileContents = $this->getFileContents($tinymceDir . "/" . $file);
222: //              $buffer .= "\n//-FILE-$tinymceDir/$file (". strlen($fileContents) . " bytes)\n";
223:                 $buffer .= $fileContents;
224:             }
225:         }
226: 
227:         // Mark all themes, plugins and languages as done
228:         $buffer .= 'tinymce.each("' . implode(',', $files) . '".split(","),function(f){tinymce.ScriptLoader.markDone(tinyMCE.baseURL+"/"+f+".js");});';
229: 
230:         // Compress data
231:         if ($supportsGzip) {
232:             $buffer = gzencode($buffer, 9, FORCE_GZIP);
233:         }
234: 
235:         // Write cached file
236:         if ($this->settings["disk_cache"]) {
237:             @file_put_contents($cacheFile, $buffer);
238:         }
239: 
240:         // Stream contents to client
241:         echo $buffer;
242:     }
243: 
244:     /**
245:      * Renders a script tag that loads the TinyMCE script.
246:      *
247:      * @param Array $settings Name/value array with settings for the script tag.
248:      * @param Bool  $return   The script tag is return instead of being output if true
249:      * @return String the tag is returned if $return is true  
250:      */
251:     public static function renderTag($tagSettings, $return = false) {
252:         $settings = array_merge(self::$defaultSettings, $tagSettings);
253: 
254:         if (empty($settings["cache_dir"])) {
255:             $settings["cache_dir"] = dirname(__FILE__);
256:         }
257: 
258:         $scriptSrc = $settings["url"] . "?js=1";
259: 
260:         // Add plugins
261:         if (isset($settings["plugins"])) {
262:             $scriptSrc .= "&plugins=" . (is_array($settings["plugins"]) ? implode(',', $settings["plugins"]) : $settings["plugins"]);
263:         }
264: 
265:         // Add themes
266:         if (isset($settings["themes"])) {
267:             $scriptSrc .= "&themes=" . (is_array($settings["themes"]) ? implode(',', $settings["themes"]) : $settings["themes"]);
268:         }
269: 
270:         // Add languages
271:         if (isset($settings["languages"])) {
272:             $scriptSrc .= "&languages=" . (is_array($settings["languages"]) ? implode(',', $settings["languages"]) : $settings["languages"]);
273:         }
274: 
275:         // Add disk_cache
276:         if (isset($settings["disk_cache"])) {
277:             $scriptSrc .= "&diskcache=" . ($settings["disk_cache"] === true ? "true" : "false");
278:         }
279: 
280:         // Add any explicitly specified files if the default settings have been overriden by the tag ones
281:         /*
282:          * Specifying tag files will override (rather than merge with) any site-specific ones set in the 
283:          * TinyMCE_Compressor object creation.  Note that since the parameter parser limits content to alphanumeric
284:          * only base filenames can be specified.  The file extension is assumed to be ".js" and the directory is
285:          * the TinyMCE root directory.  A typical use of this is to include a script which initiates the TinyMCE object. 
286:          */
287:         if (isset($tagSettings["files"])) {
288:             $scriptSrc .= "&files=" .(is_array($settings["files"]) ? implode(',', $settings["files"]) : $settings["files"]);
289:         }
290: 
291:         // Add src flag
292:         if (isset($settings["source"])) {
293:             $scriptSrc .= "&src=" . ($settings["source"] === true ? "true" : "false");
294:         }
295: 
296:         $scriptTag = '<script src="' . htmlspecialchars($scriptSrc) . '"></script>';
297: 
298:         if ($return) {
299:             return $scriptTag;
300:         } else {
301:             echo $scriptTag;
302:         }
303:     }
304: 
305:     /**
306:      * Returns a sanitized query string parameter.
307:      *
308:      * @param String $name Name of the query string param to get.
309:      * @param String $default Default value if the query string item shouldn't exist.
310:      * @return String Sanitized query string parameter value.
311:      */
312:     public static function getParam($name, $default = "") {
313:         if (!isset($_GET[$name])) {
314:             return $default;
315:         }
316: 
317:         return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Sanatize for security, remove anything but 0-9,a-z,-_,
318:     }
319: 
320:     /**
321:      * Parses the specified time format into seconds. Supports formats like 10h, 10d, 10m.
322:      *
323:      * @param String $time Time format to convert into seconds.
324:      * @return Int Number of seconds for the specified format.
325:      */
326:     private function parseTime($time) {
327:         $multipel = 1;
328: 
329:         // Hours
330:         if (strpos($time, "h") > 0) {
331:             $multipel = 3600;
332:         }
333: 
334:         // Days
335:         if (strpos($time, "d") > 0) {
336:             $multipel = 86400;
337:         }
338: 
339:         // Months
340:         if (strpos($time, "m") > 0) {
341:             $multipel = 2592000;
342:         }
343: 
344:         // Trim string
345:         return intval($time) * $multipel;
346:     }
347: 
348:     /**
349:      * Returns the contents of the script file if it exists and removes the UTF-8 BOM header if it exists.
350:      *
351:      * @param String $file File to load.
352:      * @return String File contents or empty string if it doesn't exist.
353:      */
354:     private function getFileContents($file) {
355:         $content = file_get_contents($file);
356: 
357:         // Remove UTF-8 BOM
358:         if (substr($content, 0, 3) === pack("CCC", 0xef, 0xbb, 0xbf)) {
359:             $content = substr($content, 3);
360:         }
361: 
362:         return $content;
363:     }
364: }
365: ?>
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0