| 16 | | @require_once('../../../wp-config.php'); |
|---|
| 17 | | |
|---|
| 18 | | // gzip_compression(); |
|---|
| 19 | | |
|---|
| 20 | | function wp_tinymce_lang($path) { |
|---|
| 21 | | global $language; |
|---|
| 22 | | |
|---|
| 23 | | $text = ''; |
|---|
| 24 | | |
|---|
| 25 | | // Look for xx_YY.js, xx_yy.js, xx.js |
|---|
| 26 | | $file = realpath(sprintf($path, $language)); |
|---|
| 27 | | if ( file_exists($file) ) |
|---|
| 28 | | $text = file_get_contents($file); |
|---|
| 29 | | $file = realpath(sprintf($path, strtolower($language))); |
|---|
| 30 | | if ( file_exists($file) ) |
|---|
| 31 | | $text = file_get_contents($file); |
|---|
| 32 | | $file = realpath(sprintf($path, substr($language, 0, 2))); |
|---|
| 33 | | if ( file_exists($file) ) |
|---|
| 34 | | $text = file_get_contents($file); |
|---|
| 35 | | |
|---|
| 36 | | |
|---|
| 37 | | // Fall back on en.js |
|---|
| 38 | | $file = realpath(sprintf($path, 'en')); |
|---|
| 39 | | if ( empty($text) && file_exists($file) ) |
|---|
| 40 | | $text = file_get_contents($file); |
|---|
| 41 | | |
|---|
| 42 | | // Send lang file through gettext |
|---|
| 43 | | if ( function_exists('__') && strtolower(substr($language, 0, 2)) != 'en' ) { |
|---|
| 44 | | $search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem"; |
|---|
| 45 | | $replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')"; |
|---|
| 46 | | |
|---|
| 47 | | $search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem"; |
|---|
| 48 | | $replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'"; |
|---|
| 49 | | |
|---|
| 50 | | $search = array($search1, $search2); |
|---|
| 51 | | $replace = array($replace1, $replace2); |
|---|
| 52 | | |
|---|
| 53 | | $text = preg_replace($search, $replace, $text); |
|---|
| 54 | | |
|---|
| 55 | | return $text; |
|---|
| 56 | | } |
|---|
| 57 | | |
|---|
| 58 | | return $text; |
|---|
| 59 | | } |
|---|
| 60 | | |
|---|
| 61 | | function wp_compact_tinymce_js($text) { |
|---|
| 62 | | // This function was custom-made for TinyMCE 2.0, not expected to work with any other JS. |
|---|
| 63 | | |
|---|
| 64 | | // Strip comments |
|---|
| 65 | | $text = preg_replace("!(^|\s+)//.*$!m", '', $text); |
|---|
| 66 | | $text = preg_replace("!/\*.*?\*/!s", '', $text); |
|---|
| 67 | | |
|---|
| 68 | | // Strip leading tabs, carriage returns and unnecessary line breaks. |
|---|
| 69 | | $text = preg_replace("!^\t+!m", '', $text); |
|---|
| 70 | | $text = str_replace("\r", '', $text); |
|---|
| 71 | | $text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text); |
|---|
| 72 | | |
|---|
| 73 | | return "$text\n"; |
|---|
| 74 | | } |
|---|
| 75 | | |
|---|
| 76 | | |
|---|
| 77 | | // General options |
|---|
| 78 | | $suffix = ""; // Set to "_src" to use source version |
|---|
| 79 | | $expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires |
|---|
| 80 | | $diskCache = false; // If you enable this option gzip files will be cached on disk. |
|---|
| 81 | | $cacheDir = realpath("."); // Absolute directory path to where cached gz files will be stored |
|---|
| 82 | | $debug = false; // Enable this option if you need debuging info |
|---|
| 83 | | |
|---|
| 84 | | // Headers |
|---|
| 85 | | header("Content-Type: text/javascript; charset=" . get_bloginfo('charset')); |
|---|
| 86 | | // header("Cache-Control: must-revalidate"); |
|---|
| 87 | | header("Vary: Accept-Encoding"); // Handle proxies |
|---|
| 88 | | header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT"); |
|---|
| 89 | | |
|---|
| 90 | | // Get data to load |
|---|
| 91 | | $theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : ""; |
|---|
| 92 | | $language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : ""; |
|---|
| 93 | | $plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : ""; |
|---|
| 94 | | $lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en"; |
|---|
| 95 | | $index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1; |
|---|
| 96 | | $cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug); |
|---|
| 97 | | $cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" . $cacheKey . ".gz"; |
|---|
| 98 | | $cacheData = ""; |
|---|
| 99 | | |
|---|
| 100 | | // Patch older versions of PHP < 4.3.0 |
|---|
| 101 | | if (!function_exists('file_get_contents')) { |
|---|
| 102 | | function file_get_contents($filename) { |
|---|
| 103 | | $fd = fopen($filename, 'rb'); |
|---|
| 104 | | $content = fread($fd, filesize($filename)); |
|---|
| 105 | | fclose($fd); |
|---|
| 106 | | return $content; |
|---|
| 107 | | } |
|---|
| 108 | | } |
|---|
| 109 | | |
|---|
| 110 | | // Security check function, can only contain a-z 0-9 , _ - and whitespace. |
|---|
| 111 | | function TinyMCE_cleanInput($str) { |
|---|
| 112 | | return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_ |
|---|
| 113 | | } |
|---|
| 114 | | |
|---|
| 115 | | function TinyMCE_echo($str) { |
|---|
| 116 | | global $cacheData, $diskCache; |
|---|
| 117 | | |
|---|
| 118 | | if ($diskCache) |
|---|
| 119 | | $cacheData .= $str; |
|---|
| 120 | | else |
|---|
| 121 | | echo $str; |
|---|
| 122 | | } |
|---|
| 123 | | |
|---|
| 124 | | // Only gzip the contents if clients and server support it |
|---|
| 125 | | $encodings = array(); |
|---|
| 126 | | |
|---|
| 127 | | if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) |
|---|
| 128 | | $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))); |
|---|
| 129 | | |
|---|
| 130 | | // Check for gzip header or northon internet securities |
|---|
| 131 | | if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) { |
|---|
| 132 | | $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip"; |
|---|
| 133 | | |
|---|
| 134 | | // Use cached file if it exists but not in debug mode |
|---|
| 135 | | if (file_exists($cacheFile) && !$debug) { |
|---|
| 136 | | header("Content-Encoding: " . $enc); |
|---|
| 137 | | echo file_get_contents($cacheFile); |
|---|
| 138 | | die; |
|---|
| 139 | | } |
|---|
| 140 | | |
|---|
| 141 | | if (!$diskCache) |
|---|
| 142 | | ob_start("ob_gzhandler"); |
|---|
| 143 | | } else |
|---|
| | 13 | @require_once('../../../wp-config.php'); // For get_bloginfo(). |
|---|
| | 14 | |
|---|
| | 15 | // Get input |
|---|
| | 16 | $plugins = explode(',', getParam("plugins", "")); |
|---|
| | 17 | $languages = explode(',', getParam("languages", "")); |
|---|
| | 18 | $themes = explode(',', getParam("themes", "")); |
|---|
| | 19 | $diskCache = getParam("diskcache", "") == "true"; |
|---|
| | 20 | $isJS = getParam("js", "") == "true"; |
|---|
| | 21 | $compress = getParam("compress", "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 | |
|---|
| | 31 | // Custom extra javascripts to pack |
|---|
| | 32 | $custom = array(/* |
|---|
| | 33 | "some custom .js file", |
|---|
| | 34 | "some custom .js file" |
|---|
| | 35 | */); |
|---|
| | 36 | |
|---|
| | 37 | // WP |
|---|
| | 38 | $index = getParam("index", -1); |
|---|
| | 39 | $theme = getParam("theme", ""); |
|---|
| | 40 | $themes = array($theme); |
|---|
| | 41 | $language = getParam("language", "en"); |
|---|
| | 42 | if ( empty($language) ) |
|---|
| | 43 | $language = 'en'; |
|---|
| | 44 | $languages = array($language); |
|---|
| | 45 | if ( $language != strtolower($language) ) |
|---|
| | 46 | $languages[] = strtolower($language); |
|---|
| | 47 | if ( $language != substr($language, 0, 2) ) |
|---|
| | 48 | $languages[] = substr($language, 0, 2); |
|---|
| | 50 | $isJS = true; |
|---|
| | 51 | $suffix = ''; |
|---|
| | 52 | |
|---|
| | 53 | // Headers |
|---|
| | 54 | header("Content-Type: text/javascript; charset=" . get_bloginfo('charset')); |
|---|
| | 55 | header("Vary: Accept-Encoding"); // Handle proxies |
|---|
| | 56 | header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT"); |
|---|
| | 57 | |
|---|
| | 58 | // Is called directly then auto init with default settings |
|---|
| | 59 | if (!$isJS) { |
|---|
| | 60 | echo getFileContents("tiny_mce_gzip.js"); |
|---|
| | 61 | echo "tinyMCE_GZ.init({});"; |
|---|
| | 62 | die(); |
|---|
| | 63 | } |
|---|
| | 64 | |
|---|
| | 65 | // Setup cache info |
|---|
| | 66 | if ($diskCache) { |
|---|
| | 67 | if (!$cachePath) |
|---|
| | 68 | die("alert('Real path failed.');"); |
|---|
| | 69 | |
|---|
| | 70 | $cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", ""); |
|---|
| | 71 | |
|---|
| | 72 | foreach ($custom as $file) |
|---|
| | 73 | $cacheKey .= $file; |
|---|
| | 74 | |
|---|
| | 75 | $cacheKey = md5($cacheKey); |
|---|
| | 76 | |
|---|
| | 77 | if ($compress) |
|---|
| | 78 | $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz"; |
|---|
| | 79 | else |
|---|
| | 80 | $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js"; |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | // Check if it supports gzip |
|---|
| | 84 | if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) |
|---|
| | 85 | $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))); |
|---|
| | 86 | |
|---|
| | 87 | if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) { |
|---|
| | 88 | $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip"; |
|---|
| | 89 | $supportsGzip = true; |
|---|
| | 90 | } |
|---|
| | 91 | |
|---|
| | 92 | // Use cached file disk cache |
|---|
| | 93 | if ($diskCache && $supportsGzip && file_exists($cacheFile)) { |
|---|
| | 94 | if ($compress) |
|---|
| | 95 | header("Content-Encoding: " . $enc); |
|---|
| | 96 | |
|---|
| | 97 | echo getFileContents($cacheFile); |
|---|
| | 98 | die(); |
|---|
| | 99 | } |
|---|
| 185 | | TinyMCE_echo("tinyMCE = tinyMCECompressed;"); |
|---|
| 186 | | |
|---|
| 187 | | // Write to cache |
|---|
| 188 | | if ($diskCache) { |
|---|
| 189 | | // Calculate compression ratio and debug target output path |
|---|
| 190 | | if ($debug) { |
|---|
| 191 | | $ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0); |
|---|
| 192 | | TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');"); |
|---|
| 193 | | } |
|---|
| 194 | | |
|---|
| 195 | | $cacheData = gzencode($cacheData, 9, FORCE_GZIP); |
|---|
| 196 | | |
|---|
| 197 | | // Write to file if possible |
|---|
| 198 | | $fp = @fopen($cacheFile, "wb"); |
|---|
| | 146 | $content .= "tinyMCE = tinyMCECompressed;"; |
|---|
| | 147 | |
|---|
| | 148 | // Restore loading functions |
|---|
| | 149 | //$content .= "tinyMCE_GZ.end();"; |
|---|
| | 150 | |
|---|
| | 151 | // Generate GZIP'd content |
|---|
| | 152 | if ($supportsGzip) { |
|---|
| | 153 | if ($compress) { |
|---|
| | 154 | header("Content-Encoding: " . $enc); |
|---|
| | 155 | $cacheData = gzencode($content, 9, FORCE_GZIP); |
|---|
| | 156 | } else |
|---|
| | 157 | $cacheData = $content; |
|---|
| | 158 | |
|---|
| | 159 | // Write gz file |
|---|
| | 160 | if ($diskCache && $cacheKey != "") |
|---|
| | 161 | putFileContents($cacheFile, $cacheData); |
|---|
| | 162 | |
|---|
| | 163 | // Stream to client |
|---|
| | 164 | echo $cacheData; |
|---|
| | 165 | } else { |
|---|
| | 166 | // Stream uncompressed content |
|---|
| | 167 | echo $content; |
|---|
| | 168 | } |
|---|
| | 169 | |
|---|
| | 170 | die; |
|---|
| | 171 | } |
|---|
| | 172 | |
|---|
| | 173 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
|---|
| | 174 | |
|---|
| | 175 | function getParam($name, $def = false) { |
|---|
| | 176 | if (!isset($_GET[$name])) |
|---|
| | 177 | return $def; |
|---|
| | 178 | |
|---|
| | 179 | return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_ |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| | 182 | function getFileContents($path) { |
|---|
| | 183 | $path = realpath($path); |
|---|
| | 184 | |
|---|
| | 185 | if (!$path || !@is_file($path)) |
|---|
| | 186 | return ""; |
|---|
| | 187 | |
|---|
| | 188 | if (function_exists("file_get_contents")) |
|---|
| | 189 | return @file_get_contents($path); |
|---|
| | 190 | |
|---|
| | 191 | $content = ""; |
|---|
| | 192 | $fp = @fopen($path, "r"); |
|---|
| | 193 | if (!$fp) |
|---|
| | 194 | return ""; |
|---|
| | 195 | |
|---|
| | 196 | while (!feof($fp)) |
|---|
| | 197 | $content .= fgets($fp); |
|---|
| | 198 | |
|---|
| | 199 | fclose($fp); |
|---|
| | 200 | |
|---|
| | 201 | return $content; |
|---|
| | 202 | } |
|---|
| | 203 | |
|---|
| | 204 | function putFileContents($path, $content) { |
|---|
| | 205 | if (function_exists("file_put_contents")) |
|---|
| | 206 | return @file_put_contents($path, $content); |
|---|
| | 207 | |
|---|
| | 208 | $fp = @fopen($path, "wb"); |
|---|