/forum/web-optimizer/controller/compressor.php
PHP | 3326 lines | 2813 code | 47 blank | 466 comment | 590 complexity | 65414ab144ec54d59b59a2a52faa2117 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * File from WEBO Site SpeedUp, WEBO Software (http://www.webogroup.com/)
- * Gzips and minifies the JavaScript and CSS within the head tags of a page.
- * Can also gzip and minify the page itself
- * and 100+ other cool web performance optimization techniques
- * Based on Web Optimizer, which was based on PHP Speedy
- *
- **/
- class web_optimizer {
- /**
- * Constructor
- * Sets the options and defines the gzip headers
- **/
- function web_optimizer ($options = false) {
- /* initialize chained optimization */
- if(!empty($_GET['web_optimizer_disabled']))
- {
- $this->options['active'] = 0;
- return;
- }
- $this->web_optimizer_stage = round(empty($_GET['web_optimizer_stage']) ? 0 : $_GET['web_optimizer_stage']);
- $this->debug_mode = empty($_GET['web_optimizer_debug']) && empty($_COOKIE['web_optimizer_debug']) ? 0 : 1;
- /* get chained optimization params */
- if (!empty($this->web_optimizer_stage)) {
- $this->username = htmlspecialchars(empty($_GET['username']) ? '' :
- $_GET['username']);
- $this->password = htmlspecialchars(empty($_GET['password']) ? '' :
- $_GET['password']);
- $this->auto_rewrite = round(empty($_GET['auto_rewrite']) ? '' :
- $_GET['auto_rewrite']);
- $this->chained_redirect = 'optimizing.php';
- $this->cache_version = round(empty($_GET['cache_version']) ? '' :
- $_GET['cache_version']);
- /* get major stage number, all stages:
- -1 - system, envelope all <script> to try-catch-document.write
- 0-9 - inilialization, starts in administrative interface
- 10-13 - JS file generation, 1st major stage (common browsers)
- 14-19 - CSS Sprites / data:URI generation, 1st major stage
- 20-24 - CSS file generation + page parsing, 1st major stage
- 25-28 - JS file generation, 2nd major stage (IE 6.0)
- 29-34 - CSS Sprites / mhtml generation, 2nd major stage
- 35-39 - CSS file generation + page parsing, 2nd major stage
- 40-43 - JS file generation, 3rd major stage (IE 7.0)
- 44-49 - CSS Sprites / mhtml generation, 2nd major stage
- 50-54 - CSS file generation + page parsing, 2nd major stage
- 55-58 - JS file generation, 4th major stage (IE 8.0)
- 59-64 - CSS Sprites / data:URI generation, 4th major stage
- 65-69 - CSS file generation + page parsing, 4th major stage
- 70-73 - JS file generation, 5th major stage (IE 7.0 @ Vista)
- 74-79 - CSS Sprites generation, 5th major stage
- 80-84 - CSS file generation + page parsing, 5th major stage
- */
- $this->cache_stage = floor(($this->web_optimizer_stage - 10) / 15);
- }
- /* allow merging of other classes with this one */
- foreach ($options as $key => $value) {
- $this->$key = $value;
- }
- $this->options['active'] = $this->debug_mode ? 1 : $this->options['active'];
- /* disable any actions if not active */
- if (empty($this->options['active'])) {
- return;
- }
- /* define head of the webpage for scripts / styles */
- $this->head = '';
- /* remember current time */
- $this->time = empty($_SERVER['REQUEST_TIME']) ? time() : $_SERVER['REQUEST_TIME'];
- $this->host = $_SERVER['HTTP_HOST'];
- if (strpos($_SERVER['HTTP_HOST'], "www.") !== false ||
- strpos($_SERVER['HTTP_HOST'], "WWW.") !== false) {
- $this->host = substr($this->host, 4);
- }
- /* define PHP version */
- $this->php = $this->options['php'];
- /* skip buffering (need for integration as plugin) */
- $this->buffered = $this->options['buffered'];
- /* Sets User Agent to differ IE from non-IE */
- $this->ua = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
- /* HTTPS or not ? */
- $this->https = empty($_SERVER['HTTPS']) ? '' : 's';
- /* Set options */
- $this->set_options();
- /* Include base plugin class */
- if (is_array($this->options['plugins'])) {
- include_once($this->options['css']['installdir'] . 'libs/php/class.plugin.php');
- }
- /* Remember current page encoding */
- $this->encoding = '';
- /* Define the gzip headers */
- $this->set_gzip_headers();
- /* Deal with flushed content or not? */
- $this->flushed = false;
- $excluded_html_pages = '';
- $included_user_agents = '';
- $retricted_cookie = 0;
- if (!empty($this->options['page']['cache'])) {
- $this->start_cache_engine();
- /* HTML cache ? */
- if (!empty($this->options['page']['cache_ignore']) ||
- !empty($this->options['restricted'])) {
- $list = (empty($this->options['page']['cache_ignore']) ? '' : $this->options['page']['cache_ignore']) .
- (empty($this->options['restricted']) ? '' : ' ' . $this->options['restricted']);
- $excluded_html_pages = preg_replace("/ /", "|", preg_replace("/([\?!\^\$\|\(\)\[\]\{\}])/", "\\\\$1", $list));
- }
- if (!empty($this->options['page']['allowed_user_agents'])) {
- $included_user_agents = preg_replace("/ /", "|", preg_replace("/([\?!\^\$\|\(\)\[\]\{\}])/", "\\\\$1", $this->options['page']['allowed_user_agents']));
- }
- if (!empty($this->options['page']['exclude_cookies'])) {
- $cookies = explode(" ", $this->options['page']['exclude_cookies']);
- foreach ($cookies as $cookie) {
- if (isset($_COOKIE[$cookie])) {
- $retricted_cookie = 1;
- }
- }
- }
- }
- /* cache if
- - option is enabled,
- - don't parse excluded pages,
- - or parse included USER AGENTS,
- - don't parse pages with excluded coockies,
- - flush or gzip for HTML are disabled,
- - headers have not been sent,
- - page is requested by GET,
- - no chained optimization,
- - no debug mode,
- - external cache restriction.
- */
- $this->cache_me = !empty($this->options['page']['cache']) &&
- (empty($this->options['page']['cache_ignore']) ||
- !preg_match("!" . $excluded_html_pages . "!is", $_SERVER['REQUEST_URI']) ||
- preg_match("!" . $included_user_agents . "!is", $this->ua)) &&
- !$retricted_cookie &&
- (empty($this->options['page']['gzip']) ||
- empty($this->options['page']['flush'])) &&
- !headers_sent() &&
- (getenv('REQUEST_METHOD') == 'GET') &&
- empty($this->web_optimizer_stage) &&
- !$this->debug_mode &&
- empty($this->no_cache);
- /* check if we can get out cached page */
- if (!empty($this->cache_me)) {
- $this->uri = $this->convert_request_uri();
- /* gzip cached content before output? (plugins have onCache) */
- $gzip_me = is_array($this->options['plugins']);
- $cache_plain_key = $this->view->ensure_trailing_slash($this->uri) . 'index' . $this->ua_mod . '.html';
- $cache_key = $cache_plain_key .
- ($this->options['page']['flush'] ||
- empty($this->encoding_ext) ||
- $gzip_me ? '' : $this->encoding_ext);
- $timestamp = $this->cache_engine->get_mtime($cache_key);
- /* try to get from cache non-gzipped page if gzipped one doesn't exist */
- if (!$timestamp && !$this->options['page']['flush'] && !empty($this->encoding_ext) && !$gzip_me) {
- $timestamp = $this->cache_engine->get_mtime($cache_plain_key);
- $gzip_me = 1;
- }
- if ($timestamp && $this->time - $timestamp < $this->options['page']['cache_timeout']) {
- $content = $this->cache_engine->get_entry($gzip_me ? $cache_plain_key : $cache_key);
- if (class_exists('JUtility'))
- {
- $token = JUtility::getToken();
- $content = str_replace('##WSS_JTOKEN_WSS##', $token, $content);
- }
- /* execute plugin-specific logic */
- if (is_array($this->options['plugins'])) {
- foreach ($this->options['plugins'] as $plugin) {
- $plugin_file =
- $this->options['css']['installdir'] .
- 'plugins/' . $plugin . '.php';
- if (@is_file($plugin_file)) {
- include_once($plugin_file);
- $web_optimizer_plugin = new $plugin;
- $content =
- $web_optimizer_plugin->onAfterOptimization($content);
- }
- }
- }
- if ($gzip_me) {
- $cnt = $this->create_gz_compress($content,
- in_array($this->encoding, array('gzip', 'x-gzip')));
- if (!empty($cnt)) {
- $content = $cnt;
- /* skip gzip if we can't compress content */
- } else {
- $this->options['page']['gzip'] = 0;
- $this->encoding = '';
- }
- }
- $hash = crc32($content) .
- (empty($this->encoding) ? '' : '-' . str_replace("x-", "", $this->encoding));
- /* check for return visits */
- if ((isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
- stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"') ||
- (isset($_SERVER['HTTP_IF_MATCH']) &&
- stripslashes($_SERVER['HTTP_IF_MATCH']) == '"' . $hash . '"')) {
- /* return visit and no modifications, so do not send anything */
- header ("HTTP/1.0 304 Not Modified");
- header ("Content-Length: 0");
- while (@ob_end_clean());
- die();
- }
- /* set ETag, thx to merzmarkus */
- header("ETag: \"" . $hash . "\"");
- /* set content-type */
- if (!empty($this->options['charset'])) {
- header("Content-Type: text/html; charset=" . $this->compress_options['charset']);
- }
- if (empty($this->web_optimizer_stage) &&
- $this->options['page']['clientside_cache']) {
- /* not really GMT but is valid locally */
- $ExpStr = date("D, d M Y H:i:s",
- $this->time + $this->options['page']['clientside_timeout']) . " GMT";
- header("Cache-Control: " .
- ($this->options['page']['gzip'] ? 'private' : 'public') .
- ", max-age=" .
- $this->options['page']['clientside_timeout']);
- header("Expires: " . $ExpStr);
- }
- /* check if cached content must be gzipped, can't gzip twice via php for flush */
- if ($this->options['page']['gzip'] && !$this->options['page']['flush']) {
- $this->set_gzip_header();
- }
- while (@ob_end_clean());
- echo $content;
- /* content is a head part, flush it after */
- if ($this->options['page']['flush']) {
- flush();
- $this->flushed = true;
- } else {
- die();
- }
- }
- }
- /* change some hosts if HTTPS is used */
- if ($this->https && !empty($this->options['page']['parallel_https'])) {
- $this->options['javascript']['host'] =
- $this->options['css']['host'] =
- $this->options['page']['parallel_hosts'] =
- $this->options['page']['parallel_https'];
- }
- /* number of external files calls to process */
- $this->initial_files = array();
- /* set internal encoding */
- $this->charset = empty($wss_encoding) ? 'utf8' : $wss_encoding;
- /* prepare escaped host */
- $this->host_escaped = str_replace('.', '\.', $this->host);
- /* activate application */
- $this->options['active'] = 1;
- if ($this->buffered) {
- /* Start things off */
- $this->start();
- }
- }
- /**
- * Write installation progress to JavaScript file
- *
- **/
- function write_progress ($progress) {
- $this->write_file($this->options['javascript']['cachedir'] . 'progress.html', $progress);
- }
- /**
- * Options are read from config.webo.php
- **/
- function set_options () {
- /* Set paths with new options */
- $this->options['document_root'] = empty($this->options['document_root']) ? '' : $this->options['document_root'];
- $this->view->set_paths($this->options['document_root']);
- /* Set local root if chained optimization */
- if (!empty($this->web_optimizer_stage)) {
- $this->view->paths['full']['current_directory'] = $this->view->paths['full']['document_root'];
- $this->view->paths['relative']['current_directory'] = $this->view->paths['relative']['document_root'];
- $_SERVER['REQUEST_URI'] = '/';
- /* force User Agent on chained optimization */
- $mods = array(
- /* all common browsers except IE */
- '',
- /* IE 6.0, when will it die? */
- '.ie6',
- /* IE 7.0 */
- '.ie7',
- /* IE 8.0 */
- '.ie8',
- /* Mobile Agents */
- '.ma'
- );
- $this->ua_mod = $mods[$this->cache_stage];
- }
- $this->premium = $this->view->validate_license($this->options['license'], $this->options['html_cachedir'], $this->options['host']);
- $this->set_user_agent();
- $webo_cachedir = $this->view->unify_dir_separator(realpath(dirname(__FILE__) . '/../') . '/');
- /* ensure trailing slashes */
- $this->options['html_cachedir'] = $this->view->ensure_trailing_slash($this->options['html_cachedir']);
- $this->options['css_cachedir'] = $this->view->ensure_trailing_slash($this->options['css_cachedir']);
- $this->options['javascript_cachedir'] = $this->view->ensure_trailing_slash($this->options['javascript_cachedir']);
- /* normalize host */
- if (!empty($this->options['host'])) {
- $this->options['host'] = preg_replace("!^https?://!", "", $this->options['host']);
- }
- /* Read in options */
- $full_options = array(
- "javascript" => array(
- "cachedir" => $this->options['javascript_cachedir'],
- "cachedir_relative" => str_replace($this->options['document_root'], "/", $this->options['javascript_cachedir']),
- "installdir" => $webo_cachedir,
- "gzip" => $this->options['gzip']['javascript'] &&
- ((!$this->options['htaccess']['mod_gzip'] &&
- !$this->options['htaccess']['mod_deflate'] &&
- (!$this->options['htaccess']['mod_rewrite'] ||
- !$this->options['htaccess']['mod_mime'] ||
- !$this->options['htaccess']['mod_expires'])) ||
- !$this->options['htaccess']['enabled']),
- "gzip_level" => round($this->options['gzip']['javascript_level']),
- "minify" => $this->options['minify']['javascript'],
- "minify_body" => $this->options['minify']['javascript_body'],
- "minify_with" => $this->options['minify']['with_jsmin'] ?
- 'jsmin' : ($this->options['minify']['with_yui'] ?
- 'yui' : ($this->options['minify']['with_packer'] ?
- 'packer' : '')),
- "minify_try" => $this->options['external_scripts']['include_try'],
- "minify_exclude" => $this->options['external_scripts']['minify_exclude'],
- "remove_duplicates" => $this->options['external_scripts']['duplicates'],
- "far_future_expires" => $this->options['far_future_expires']['javascript'] &&
- !$this->options['htaccess']['mod_expires'],
- "far_future_expires_php" => $this->options['far_future_expires']['javascript'],
- "far_future_expires_rewrite" => $this->options['htaccess']['mod_rewrite'] &&
- $this->options['htaccess']['enabled'] &&
- $this->options['far_future_expires']['javascript'],
- "unobtrusive_body" => $this->options['unobtrusive']['body'] &&
- !$this->options['unobtrusive']['all'],
- "external_scripts" => $this->options['external_scripts']['on'] &&
- $this->options['minify']['javascript'],
- "inline_scripts" => $this->options['external_scripts']['inline'] &&
- $this->options['minify']['javascript'],
- "external_scripts_head_end" => $this->options['external_scripts']['head_end'],
- "external_scripts_exclude" => $this->options['external_scripts']['ignore_list'],
- "dont_check_file_mtime" => $this->options['performance']['mtime'],
- "file" => $this->options['minify']['javascript_file'],
- "host" => $this->options['minify']['javascript_host'],
- "https" => $this->premium > 1 ? $this->options['parallel']['https'] : ''
- ),
- "css" => array(
- "cachedir" => $this->options['css_cachedir'],
- "cachedir_relative" => str_replace($this->options['document_root'], "/", $this->options['css_cachedir']),
- "installdir" => $webo_cachedir,
- "gzip" => $this->options['gzip']['css'] &&
- ((!$this->options['htaccess']['mod_gzip'] &&
- !$this->options['htaccess']['mod_deflate'] &&
- (!$this->options['htaccess']['mod_rewrite'] ||
- !$this->options['htaccess']['mod_mime'] ||
- !$this->options['htaccess']['mod_expires'])) ||
- !$this->options['htaccess']['enabled']),
- "gzip_level" => round($this->options['gzip']['css_level']),
- "minify" => $this->options['minify']['css'],
- "minify_body" => $this->options['minify']['css_body'],
- "minify_with" => $this->options['minify']['css_min'] == 2 ?
- 'tidy' : ($this->options['minify']['css_min'] ? 'basic' : ''),
- "far_future_expires" => $this->options['far_future_expires']['css'] &&
- !$this->options['htaccess']['mod_expires'],
- "far_future_expires_php" => $this->options['far_future_expires']['css'],
- "far_future_expires_rewrite" => $this->options['htaccess']['mod_rewrite'] &&
- $this->options['htaccess']['enabled'] &&
- $this->options['far_future_expires']['css'],
- "data_uris" => $this->options['data_uris']['on'],
- /* disable mhtml for IE7- under HTTPS */
- "data_uris_mhtml" => $this->options['data_uris']['mhtml'] &&
- (!$this->https || (!strpos($this->ua, 'MSIE 6') && !strpos($this->ua, 'MSIE 7'))),
- "data_uris_separate" => $this->options['data_uris']['separate'] &&
- ((!empty($this->ua_mod) &&
- $this->options['data_uris']['mhtml']) ||
- (empty($this->ua_mod) &&
- $this->options['data_uris']['on'])),
- "data_uris_domloaded" => $this->options['data_uris']['domloaded'],
- "data_uris_size" => round($this->options['data_uris']['size']),
- "data_uris_mhtml_size" => round($this->options['data_uris']['mhtml_size']),
- "data_uris_exclude" => $this->options['data_uris']['ignore_list'],
- "data_uris_exclude_mhtml" => $this->options['data_uris']['additional_list'],
- "css_sprites" => $this->options['css_sprites']['enabled'],
- "css_sprites_expires_rewrite" => (!$this->options['htaccess']['mod_rewrite'] ||
- !$this->options['htaccess']['enabled']) &&
- $this->options['far_future_expires']['images'],
- "css_sprites_ignore" => $this->options['css_sprites']['ignore'],
- "css_sprites_exclude" => $this->options['css_sprites']['ignore_list'],
- "truecolor_in_jpeg" => $this->options['css_sprites']['truecolor_in_jpeg'],
- "aggressive" => $this->options['css_sprites']['aggressive'],
- "no_ie6" => $this->options['css_sprites']['no_ie6'],
- "dimensions_limited" => round($this->options['css_sprites']['dimensions_limited']),
- "css_sprites_extra_space" => $this->options['css_sprites']['extra_space'],
- "punypng" => (!empty($this->options['punypng']) ? $this->options['punypng'] : '') &&
- ($this->premium > 1),
- "css_restore_properties" => $this->options['performance']['restore_properties'] &&
- ($this->premium > 1),
- "unobtrusive_body" => false,
- "parallel" => $this->options['parallel']['enabled'],
- "parallel_hosts" => $this->options['parallel']['allowed_list'],
- "external_scripts" => $this->options['external_scripts']['css'],
- "inline_scripts" => $this->options['external_scripts']['css_inline'],
- "external_scripts_exclude" => $this->options['external_scripts']['additional_list'],
- "include_code" => $this->options['external_scripts']['include_code'],
- "dont_check_file_mtime" => $this->options['performance']['mtime'],
- "file" => $this->options['minify']['css_file'],
- "host" => $this->options['minify']['css_host'],
- "https" => $this->premium > 1 ? $this->options['parallel']['https'] : ''
- ),
- "page" => array(
- "cachedir" => $this->options['html_cachedir'],
- "cache_engine" => $this->options['performance']['cache_engine'],
- "cache_engine_options" => $this->options['performance']['cache_engine_options'],
- "cachedir_relative" => str_replace($this->options['document_root'], "/", $this->options['html_cachedir']),
- "installdir" => $webo_cachedir,
- "host" => $this->options['host'],
- "gzip" => $this->options['gzip']['page'] &&
- ((!$this->options['htaccess']['mod_gzip'] &&
- !$this->options['htaccess']['mod_deflate']) ||
- !$this->options['htaccess']['enabled']),
- "gzip_noie" => $this->options['gzip']['noie'],
- "gzip_level" => round($this->options['gzip']['page_level']),
- "gzip_cookie" => $this->options['gzip']['cookie'],
- "minify" => $this->options['minify']['page'],
- "minify_aggressive" => $this->options['minify']['html_one_string'],
- "remove_comments" => $this->options['minify']['html_comments'],
- "dont_check_file_mtime" => $this->options['performance']['mtime'],
- "cache_images" => $this->options['far_future_expires']['images'],
- "far_future_expires_rewrite" => (!($this->options['htaccess']['mod_rewrite'] ||
- $this->options['htaccess']['mod_expires']) ||
- !$this->options['htaccess']['enabled']) &&
- $this->options['far_future_expires']['images'],
- "far_future_expires_external" => $this->options['far_future_expires']['external'],
- "clientside_cache" => $this->options['far_future_expires']['html'],
- "clientside_timeout" => $this->options['far_future_expires']['html_timeout'],
- "cache" => $this->options['html_cache']['enabled'],
- "cache_timeout" => $this->options['html_cache']['timeout'],
- "flush" => $this->options['html_cache']['flush_only'],
- "flush_size" => $this->options['html_cache']['flush_size'],
- "cache_ignore" => $this->options['html_cache']['ignore_list'],
- "cache_params" => $this->options['html_cache']['params'],
- "allowed_user_agents" => $this->options['html_cache']['allowed_list'],
- "exclude_cookies" => $this->options['html_cache']['additional_list'],
- "parallel" => $this->options['parallel']['enabled'],
- "parallel_hosts" => $this->options['parallel']['allowed_list'],
- "parallel_satellites" => $this->options['parallel']['additional'],
- "parallel_satellites_hosts" => $this->options['parallel']['additional_list'],
- "parallel_ignore" => $this->options['parallel']['ignore_list'],
- "parallel_css" => $this->options['parallel']['css'],
- "parallel_javascript" => $this->options['parallel']['javascript'],
- "parallel_ftp" => $this->options['parallel']['ftp'],
- "parallel_https" => $this->options['parallel']['https'],
- "unobtrusive_informers" => $this->options['unobtrusive']['informers'] &&
- ($this->premium > 1),
- "unobtrusive_counters" => $this->options['unobtrusive']['counters'] &&
- ($this->premium > 1),
- "unobtrusive_ads" => $this->options['unobtrusive']['ads'] &&
- ($this->premium > 1),
- "unobtrusive_all" => $this->options['unobtrusive']['all'] &&
- ($this->premium > 1),
- "unobtrusive_iframes" => $this->options['unobtrusive']['iframes'] &&
- ($this->premium > 1),
- "unobtrusive_onload" => $this->options['unobtrusive']['on'] &&
- ($this->premium > 1),
- "unobtrusive_inline" => $this->options['unobtrusive']['on'] == 2 &&
- ($this->premium > 1),
- "footer" => $this->premium ? $this->options['footer']['text'] : 1,
- "footer_image" => $this->options['footer']['image'],
- "footer_text" => $this->options['footer']['link'],
- "footer_style" => $this->options['footer']['css_code'],
- "spot" => $this->premium ? $this->options['footer']['spot'] : 1,
- "counter" => $this->options['footer']['counter'],
- "htaccess_username" => $this->options['external_scripts']['user'],
- "htaccess_password" => $this->options['external_scripts']['pass'],
- "html_tidy" => $this->options['performance']['plain_string'],
- "sprites" => $this->options['css_sprites']['html_sprites'],
- "dimensions_limited" => round($this->options['css_sprites']['html_limit']),
- "per_page" => $this->options['css_sprites']['html_page']
- ),
- "document_root" => $this->options['document_root'],
- "document_root_relative" => str_replace("//", "/", str_replace($this->options['document_root'], "/", $this->options['website_root'])),
- "website_root" => $this->options['website_root'],
- "cache_version" => round($this->options['performance']['cache_version']),
- "uniform_cache" => $this->options['performance']['uniform_cache'],
- "plugins" => ($this->premium > 1) &&
- !empty($this->options['plugins']) ? explode(" ", $this->options['plugins']) : '',
- "restricted" => ($this->premium > 1) &&
- !empty($this->options['restricted']) ? $this->options['restricted'] : '',
- "days_to_delete" => round($this->options['performance']['delete_old']),
- "charset" => $this->options['charset']
- );
- $this->lc = $this->options['license'];
- /* overwrite other options array that we passed in */
- $this->options = $full_options;
- }
- /**
- * Start saving the output buffer
- *
- **/
- function start () {
- ob_start();
- ob_start();
- ob_implicit_flush(0);
- }
- /**
- * Compress passes content directly
- *
- **/
- function compress ($content) {
- $this->finish($content);
- }
- /**
- * Do work and return output buffer
- *
- **/
- function finish ($content = false) {
- /* disable any actions if not active */
- if (empty($this->options['active'])) {
- return $content;
- }
- if ($content === false) {
- $this->content = ob_get_clean();
- /* clear all other buffers */
- while (@ob_end_clean());
- } else {
- $this->content = $content;
- }
- /* execute plugin-specific logic, BeforeOptimization event */
- if (is_array($this->options['plugins'])) {
- foreach ($this->options['plugins'] as $plugin) {
- $plugin_file =
- $this->options['css']['installdir'] .
- 'plugins/' . $plugin . '.php';
- if (@is_file($plugin_file)) {
- include_once($plugin_file);
- $web_optimizer_plugin = new $plugin;
- $this->content =
- $web_optimizer_plugin->onBeforeOptimization($this->content);
- }
- }
- }
- $skip = 0;
- if (function_exists('get_headers')) {
- $headers = headers_list();
- /* define if Content-Type is text/html and allow it */
- foreach ($headers as $head) {
- $header = strtolower($head);
- if (strpos($header, 'content-type:') !== false || strpos($header, 'location:') !== false) {
- $skip++;
- }
- if (strpos($header, 'text/html') || strpos($header, 'application/xhtml+xml')) {
- $skip--;
- }
- if (strpos($header, 'content-base') !== false) {
- $this->basehref = substr($head, 14);
- }
- }
- }
- /* also skip AJAX requests with X-Requested-With: XMLHttpRequest */
- if (!$skip &&
- !empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
- $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
- $skip = 1;
- }
- /* also skip some CMS-ralted parameters */
- if (!$skip && !empty($_GET['no_html'])) {
- $skip = 1;
- }
- /* skip some extensions */
- if (!$skip && !empty($_SERVER['QUERY_STRING'])) {
- $query = explode('.', $_SERVER['QUERY_STRING']);
- $ext = strtolower($query[count($query) - 1]);
- if (in_array($ext, array('pdf', 'doc', 'xls', 'docx', 'xlsx'))) {
- $skip = 1;
- }
- }
- /* skip some known cases of non-HTML content */
- if (!$skip) {
- /* reduce amount of viewing content, accelerate 'fast check' by 1% */
- $spot = substr($this->content, 0, 60);
- if (strpos($spot, '<methodResponse') !== false ||
- strpos($spot, '<rss') !== false ||
- strpos($spot, '<feed') !== false ||
- strpos($spot, '<urlset') !== false ||
- strpos($spot, '<smf') !== false ||
- strpos($spot, '{') === 0 ||
- strlen($this->content) < 200) {
- $skip = 1;
- }
- }
- /* restrict URL from configuration */
- if (!$skip &&
- !empty($this->options['restricted'])) {
- if (preg_match("@" . preg_replace("/ /", "|",
- preg_replace("/([\?!\^\$\|\(\)\[\]\{\}])/",
- "\\\\$1",
- $this->options['restricted'])) . "@",
- $_SERVER['REQUEST_URI'])) {
- $skip = 1;
- }
- }
- /* skip RSS, SMF xml format */
- if (!$skip) {
- /* find all files in head to process */
- $this->get_script_array();
- /* Run the functions specified in options */
- if (is_array($this->options)) {
- foreach ($this->options as $func => $option) {
- if (method_exists($this, $func)) {
- if (!empty($option['gzip']) ||
- !empty($option['minify']) ||
- !empty($option['far_future_expires']) ||
- !empty($option['parallel']) ||
- !empty($option['unobtrusive_all']) ||
- !empty($option['unobtrusive_ads']) ||
- !empty($option['unobtrusive_counters']) ||
- !empty($option['unobtrusive_informers']) ||
- !empty($option['unobtrusive_iframes']) ||
- !empty($option['cache']) ||
- !empty($option['sprites'])) {
- if (!empty($this->web_optimizer_stage)) {
- $this->write_progress($this->web_optimizer_stage++);
- }
- $this->$func($option, $func);
- }
- }
- }
- }
- if (!empty($this->web_optimizer_stage)) {
- $this->write_progress($this->web_optimizer_stage);
- /* redirect to installation page if chained optimization if finished */
- if ($this->web_optimizer_stage > 85) {
- if ($this->chained_redirect === 'optimizing.php') {
- $this->write_progress(97);
- header('Location: ../index.php?page=install_stage_3&Submit=1&web_optimizer_stage=97&wss__password=' .
- $this->password);
- }
- /* else redirect to the next stage */
- } else {
- header('Location: ' . $this->chained_redirect . '?web_optimizer_stage=' .
- $this->web_optimizer_stage .
- '&password=' .
- $this->password .
- '&web_optimizer_debug=1');
- }
- while (@ob_end_clean());
- die();
- }
- }
- /* remove marker for styles */
- $this->content = str_replace('@@@WSSSTYLES@@@', '', $this->content);
- /* Return content to requestor */
- if ($content) {
- return $this->content;
- /* or echo content to the browser */
- } else {
- /* HTTP/1.0 needs Content-Length sometimes. With PHP4 we can't check when exactly. */
- if (!empty($this->encoding)) {
- header('Content-Length: ' . strlen($this->content));
- }
- echo $this->content;
- /* It's obvious to send anything right after gzipped content */
- if (!empty($this->encoding)) {
- while (@ob_end_clean());
- die();
- }
- }
- }
- /**
- * GZIP and minify the javascript as required
- *
- **/
- function javascript ($options,$type) {
- /* prepare list of files to process */
- $script_files = array();
- foreach ($this->initial_files as $file) {
- if (!empty($file['tag']) && $file['tag'] == 'script') {
- $script_files[] = $file;
- }
- }
- if (!empty($options['minify']) && !empty($script_files)) {
- $this->content = $this->do_compress(
- array(
- 'cachedir' => $options['cachedir'],
- 'cachedir_relative' => $options['cachedir_relative'],
- 'installdir' => $options['installdir'],
- 'host' => $options['host'],
- 'tag' => 'script',
- 'type' => 'text/javascript',
- 'ext' => 'js',
- 'src' => 'src',
- 'self_close' => false,
- 'gzip' => $options['gzip'],
- 'gzip_level' => $options['gzip_level'],
- 'minify' => $options['minify'],
- 'minify_body' => $options['minify_body'],
- 'minify_with' => $options['minify_with'],
- 'minify_try' => $options['minify_try'],
- 'minify_exclude' => $options['minify_exclude'],
- 'remove_duplicates' => $options['remove_duplicates'],
- 'far_future_expires' => $options['far_future_expires'],
- 'far_future_expires_php' => $options['far_future_expires_php'],
- 'far_future_expires_rewrite' => $options['far_future_expires_rewrite'],
- 'header' => $type,
- 'css_sprites' => false,
- 'css_sprites_exclude' => false,
- 'aggressive' => false,
- 'no_ie6' => false,
- 'dimensions_limited' => false,
- 'css_sprites_extra_space' => false,
- 'data_uris' => false,
- 'mhtml' => false,
- 'unobtrusive_body' => $options['unobtrusive_body'],
- 'external_scripts' => $options['external_scripts'],
- 'inline_scripts' => $options['inline_scripts'],
- 'external_scripts_head_end' => $options['external_scripts_head_end'],
- 'external_scripts_exclude' => $options['external_scripts_exclude'],
- 'dont_check_file_mtime' => $options['dont_check_file_mtime'],
- 'file' => $options['file'],
- 'https' => $options['https']
- ),
- $this->content,
- $script_files
- );
- }
- }
- /**
- * GZIP and minify the CSS as required
- *
- **/
- function css ($options, $type) {
- /* prepare list of files to process */
- $link_files = array();
- foreach ($this->initial_files as $file) {
- if (!empty($file['tag']) && $file['tag'] == 'link') {
- $link_files[] = $file;
- }
- }
- if (!empty($options['minify']) && !empty($link_files)) {
- /* Compress separately for each media type*/
- $this->content = $this->do_compress(
- array(
- 'cachedir' => $options['cachedir'],
- 'cachedir_relative' => $options['cachedir_relative'],
- 'installdir' => $options['installdir'],
- 'host' => $options['host'],
- 'tag' => 'link',
- 'type' => 'text/css',
- 'ext' => 'css',
- 'src' => 'href',
- 'rel' => 'stylesheet',
- 'data_uris' => $options['data_uris'],
- 'mhtml' => $options['data_uris_mhtml'],
- 'data_uris_separate' => $options['data_uris_separate'],
- 'data_uris_domloaded' => $options['data_uris_domloaded'],
- 'data_uris_size' => $options['data_uris_size'],
- 'data_uris_exclude' => $options['data_uris_exclude'],
- 'mhtml' => $options['data_uris_mhtml'],
- 'mhtml_size' => $options['data_uris_mhtml_size'],
- 'mhtml_exclude' => $options['data_uris_exclude_mhtml'],
- 'css_sprites' => $options['css_sprites'],
- 'css_sprites_ignore' => $options['css_sprites_ignore'],
- 'css_sprites_exclude' => $options['css_sprites_exclude'],
- 'truecolor_in_jpeg' => $options['truecolor_in_jpeg'],
- 'aggressive' => $options['aggressive'],
- 'no_ie6' => $options['no_ie6'],
- 'dimensions_limited' => $options['dimensions_limited'],
- 'css_sprites_extra_space' => $options['css_sprites_extra_space'],
- 'css_sprites_expires_rewrite' => $options['css_sprites_expires_rewrite'],
- 'punypng' => $options['punypng'],
- 'css_restore_properties' => $options['css_restore_properties'],
- 'self_close' => true,
- 'gzip' => $options['gzip'],
- 'gzip_level' => $options['gzip_level'],
- 'minify' => $options['minify'],
- 'minify_body' => $options['minify_body'],
- 'minify_with' => $options['minify_with'],
- 'far_future_expires' => $options['far_future_expires'],
- 'far_future_expires_php' => $options['far_future_expires_php'],
- 'far_future_expires_rewrite' => $options['far_future_expires_rewrite'],
- 'header' => $type,
- 'unobtrusive_body' => $options['unobtrusive_body'],
- 'parallel' => $options['parallel'],
- 'parallel_hosts' => $options['parallel_hosts'],
- 'external_scripts' => $options['external_scripts'],
- 'inline_scripts' => $options['inline_scripts'],
- 'external_scripts_exclude' => $options['external_scripts_exclude'],
- 'include_code' => $options['include_code'],
- 'dont_check_file_mtime' => $options['dont_check_file_mtime'],
- 'file' => $options['file'],
- 'https' => $options['https']
- ),
- $this->content,
- $link_files
- );
- }
- }
- /**
- * GZIP and minify the page itself as required
- *
- **/
- function page ($options, $type) {
- if (empty($this->web_optimizer_stage) && $options['clientside_cache'] && empty($this->flushed)) {
- /* not really GMT but is valid locally */
- $ExpStr = date("D, d M Y H:i:s",
- $this->time + $this->options['page']['clientside_timeout']) . " GMT";
- header("Cache-Control: private, max-age=" .
- $this->options['page']['clientside_timeout']);
- header("Expires: " . $ExpStr);
- }
- /* move informers, counters, ads, and iframes before </body> */
- $this->replace_informers($options);
- /* Minify page itself or parse multiple hosts */
- if (!empty($options['minify']) ||
- (!empty($options['parallel']) &&
- !empty($options['parallel_hosts'])) ||
- !empty($options['unobtrusive_all']) ||
- !empty($this->options['page']['far_future_expires_rewrite']) ||
- !empty($this->options['page']['far_future_expires_external']) ||
- !empty($this->options['page']['sprites'])) {
- $this->content = $this->trimwhitespace($this->content);
- }
- /* remove marker for styles and BOM */
- $this->content = str_replace(array("@@@WSSSTYLES@@@", "@@@WSSSCRIPT@@@", ""), "", $this->content);
- /* Add script to check gzip possibility */
- if (!empty($options['gzip_cookie']) && empty($_COOKIE['_wo_gzip_checked']) && empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
- $cookie = '<script type="text/javascript" src="' . $options['cachedir_relative'] . 'wo.cookie.php"></script>';
- if ($options['html_tidy'] && ($bodypos = strpos($this->content, '</body>'))) {
- $this->content = substr_replace($this->content, $cookie, $bodypos, 0);
- } elseif ($options['html_tidy'] && ($bodypos = strpos($this->content, '</BODY>'))) {
- $this->content = substr_replace($this->content, $cookie, $bodypos, 0);
- } else {
- $this->content = preg_replace('@(</body>)@is', $cookie . "$1", $this->content);
- /* a number of engines doesn't set </body> */
- if (!strpos($this->content, "wo.cookie")) {
- $this->content .= $cookie;
- }
- }
- }
- /* execute plugin-specific logic, AfterOptimization event */
- if (is_array($this->options['plugins'])) {
- foreach ($this->options['plugins'] as $plugin) {
- $plugin_file =
- $this->options['css']['installdir'] .
- 'plugins/' . $plugin . '.php';
- if (@is_file($plugin_file)) {
- include_once($plugin_file);
- $web_optimizer_plugin = new $plugin;
- $this->content =
- $web_optimizer_plugin->onAfterOptimization($this->content);
- }
- }
- }
- if (!empty($this->web_optimizer_stage)) {
- $this->write_progress($this->web_optimizer_stage++);
- }
- /* check if we need to store cached page */
- if (!empty($this->cache_me)) {
- /* add client side replacement for WordPress comment fields */
- if (defined('WP_CACHE')) {
- foreach ($_COOKIE as $key => $value) {
- if (strpos($key, 'comment_author') === 0) {
- $this->content = str_replace('value="'. urldecode($value) .'"', 'value=""', $this->content);
- }
- }
- $this->content = preg_replace("@(</body>)@is", '<script type="text/javascript">(function(){var a=document.cookie.split(";"),b,c=0,d,e;while(b=a[c++]){if(b.indexOf("comment_author_")!=-1){d=b.split("=");e=document.getElementById(d[0].replace(/(_?[a-f0-9]{32,}|\s?comment_author_)/g,"")||"author");if(e){e.value=unescape(d[1].replace(/\+/g," "))}}}}())</script>$1', $this->content);
- }
- if (class_exists('JUtility'))
- {
- $token = JUtility::getToken();
- $this->content = str_replace($token, '##WSS_JTOKEN_WSS##', $this->content);
- }
- /* prepare flushed part of content */
- if (!empty($options['flush']) && empty($this->encoding)) {
- if (empty($options['flush_size'])) {
- if ($this->options['page']['html_tidy'] && ($headpos = strpos($source, '</head>'))) {
- $content_to_write = substr($this->content, 0, $headpos + 7);
- } elseif ($this->options['page']['html_tidy'] && ($headpos = strpos($source, '</HEAD>'))) {
- $content_to_write = substr($this->content, 0, $headpos + 7);
- } else {
- $content_to_write = preg_replace("!(.*<\/head>).*!is", "$1", $this->content);
- }
- } else {
- $content_to_write =
- substr($this->content, 0, $options['flush_size']);
- }
- }
- $ordinary_cache_key = $this->view->ensure_trailing_slash($this->uri) . 'index' . $this->ua_mod . '.html';
- $cache_key = $ordinary_cache_key . (empty($this->encoding_ext) ? '' : $this->encoding_ext);
- $timestamp = $this->cache_engine->get_mtime($cache_key);
- /* set ETag, thx to merzmarkus */
- if (empty($options['flush'])) {
- header("ETag: \"" .
- md5($this->content) .
- (empty($this->encoding) ? '' : '-' .
- str_replace("x-", "", $this->encoding)) .
- "\"");
- }
- if (empty($timestamp) || $this->time - $timestamp > $options['cache_timeout']) {
- if (!empty($options['gzip']) && !empty($this->encoding)) {
- $content_to_write = $this->create_gz_compress($this->content,
- in_array($this->encoding, array('gzip', 'x-gzip')));
- /* or just write full or non-gzipped content */
- } elseif (empty($options['flush']) || !empty($this->encoding)) {
- $content_to_write = $this->content;
- }
- /* don't create empty files */
- if (!empty($content_to_write)) {
- $this->cache_engine->put_entry($cache_key, $content_to_write, $this->time);
- }
- /* create uncompressed file for plugins */
- if (is_array($this->options['plugins']) &&
- !empty($this->encoding_ext)) {
- $this->cache_engine->put_entry($ordinary_cache_key, $this->content, $this->time);
- }
- }
- }
- /* execute plugin-specific logic, Cache event */
- if (is_array($this->options['plugins'])) {
- foreach ($this->options['plugins'] as $plugin) {
- $plugin_file =
- $this->options['css']['installdir'] .
- 'plugins/' . $plugin . '.php';
- if (@is_file($plugin_file)) {
- include_once($plugin_file);
- $web_optimizer_plugin = new $plugin;
- $this->content =
- $web_optimizer_plugin->onCache($this->content);
- }
- }
- }
- /* strip from content flushed part */
- if (!empty($this->flushed)) {
- if (empty($options['flush_size'])) {
- $options['flush_size'] = strlen($content_to_write);
- }
- $this->content = substr($this->content, $options['flush_size']);
- }
- /* Gzip page itself */
- if(!empty($options['gzip']) && !empty($this->encoding)) {
- $content = $this->create_gz_compress($this->content,
- in_array($this->encoding, array('gzip', 'x-gzip')));
- if (!empty($content)) {
- $this->set_gzip_header();
- $this->content = $content;
- }
- }
- }
- /**
- * Write content to file
- *
- **/
- function write_file ($file, $content, $upload = 0, $mime = '') {
- if (@function_exists('file_put_contents')) {
- @file_put_contents($file, $content);
- } else {
- $fp = @fopen($file, "a");
- if ($fp) {
- /* block file from writing */
- @flock($fp, LOCK_EX);
- /* erase content and move to the beginning */
- @ftruncate($fp, 0);
- @fseek($fp, 0);
- @fwrite($fp, $content);
- @fclose($fp);
- }
- }
- @touch($file, $this->time);
- @chmod($file, octdec("0644"));
- if ($upload && !empty($this->options['page']['parallel_ftp'])) {
- $this->view->upload_cdn($file,
- $this->options['document_root'],
- $this->options['page']['parallel_ftp'],
- $mime,
- $this->options['page']['host']);
- }
- }
- /**
- * Adds multiple hosts to HTML for images
- *
- **/
- function add_multiple_hosts ($content, $hosts, $satellites, $satellites_hosts) {
- /* limit by 4 */
- if (count($hosts) > 4) {
- $hosts = array($hosts[0], $hosts[1], $hosts[2], $hosts[3]);
- }
- if (count($satellites_hosts) > 4) {
- $satellites_hosts = array($satellites_hosts[0], $satellites_hosts[1], $satellites_hosts[2], $satellites_hosts[3]);
- }
- $count = count($hosts);
- $count_satellites = count($satellites_hosts);
- $replaced = array();
- $IMG = strpos($content, '<IMG');
- if (!empty($this->options['page']['html_tidy']) && !$IMG) {
- $_content = $content;
- while ($pos = strpos($_content, '<img')) {
- $len = strpos(substr($_content, $pos), '>');
- /* gets image tag w/o the closing >, it's OK */
- $imgs[] = array(substr($_content, $pos, $len));
- $_content = substr_replace($_content, '', $pos, $len);
- }
- } elseif (empty($this->options['page']['html_tidy']) || $IMG) {
- preg_match_all("!<img[^>]+>!is", $content, $imgs, PREG_SET_ORDER);
- }
- if (!empty($this->options['page']['sprites']) && !empty($imgs)) {
- require($this->options['css']['installdir'] . 'libs/php/html.sprites.php');
- $html_sprites = new html_sprites($imgs, $this->options, $this);
- $content = $html_sprites->process($content);
- }
- if (!empty($imgs)) {
- $ignore_list = explode(" ", $this->options['page']['parallel_ignore']);
- $ignore_sprites = explode(" ", $this->options['css']['css_sprites_exclude']);
- foreach ($imgs as $image) {
- if (!empty($this->options['page']['html_tidy']) && ($pos=strpos($image[0], ' src="'))) {
- $old_src = substr($image[0], $pos+6, strpos(substr($image[0], $pos+6), '"'));
- } elseif (!empty($this->options['page']['html_tidy']) && ($pos=strpos($image[0], " src='"))) {
- $old_src = substr($image[0], $pos+6, strpos(substr($image[0], $pos+6), "'"));
- } else {
- $old_src = preg_replace("!^['\"\s]*(.*?)['\"\s]*$!is", "$1", preg_replace("!.*[\"'\s]src\s*=\s*(\"[^\"]+\"|'[^']+'|[\S]+).*!is", "$1", $image[0]));
- }
- $old_src = $this->convert_basehref($old_src);
- $old_src_param = ($old_src_param_pos = strpos($old_src, '?')) ? substr($old_src, $old_src_param_pos) : '';
- /* image file name to check through ignore list */
- $img = preg_replace("@.*/@", "", $old_src);
- $absolute_src = $this->convert_path_to_absolute($old_src,
- array('file' => $_SERVER['REQUEST_URI']));
- if (empty($replaced[$image[0]])) {
- if (!empty($this->options['page']['sprites']) &&
- ((!in_array($img, $ignore_sprites) && empty($this->options['css']['css_sprites_ignore'])) ||
- (in_array($img, $ignore_sprites) && !empty($this->options['css']['css_sprites_ignore']))) &&
- !empty($html_sprites->css_images[$absolute_src]) && !empty($html_sprites->css_images[$absolute_src][2]) &&
- (empty($this->ua_mod) || $this->ua_mod != '.ie6' || empty($this->options['css']['no_ie6']))) {
- $class = substr($html_sprites->css_images[$absolute_src][8], 1);
- if (!empty($this->options['page']['html_tidy']) &&
- (strpos($image[0], 'class') || strpos($image[0], 'CLASS'))) {
- if ($pos=strpos($image[0], ' class="')) {
- $end = strpos(substr($image[0], $pos + 7), '"');
- $new_image = substr($image[0], 0, $pos + 7 + $end) .
- ' ' . $class . substr($image[0], $pos + 7 + $end);
- } elseif ($pos=strpos($image[0], " class='")) {
- $end = strpos(substr($image[0], $pos + 7), "'");
- $new_image = substr($image[0], 0, $pos + 7 + $end) .
- ' ' . $class . substr($image[0], $end);
- } else {
- $new_image = preg_replace("!(.*['\"\s]class\s*=\s*)([\"'])?([^\"']+)([\"'])?([\s/>])(.*)!is", "$1$2$3 " .
- $class . "$4$5$6", $image[0]);
- }
- } elseif (preg_match("@['\"\s]class\s*=\s*['\"]@is", $image[0])) {
- $new_image = preg_replace("!(.*['\"\s]class\s*=\s*)([\"'])?([^\"']+)([\"'])?([\s/>])(.*)!is", "$1$2$3 " .
- $class . "$4$5$6", $image[0]);
- } elseif (preg_match("@['\"\s]class\s*=\s*@is", $image[0])) {
- $new_image = preg_replace("!(.*['\"\s]class\s*=\s*)([^\s]+)\s!is", "$1\"$2 " .
- $class . "\" ", $image[0]);
- } else {
- $new_image = substr($image[0], 0, 4) . ' class="' .
- $class . '"' . substr($image[0], 4);
- }
- /* add transparent GIF or data:URI chunk */
- $new_src = (empty($this->ua_mod) ||
- substr($this->ua_mod, 3, 1) > 7) &&
- !$this->options['uniform_cache'] ?
- 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' :
- ((empty($this->options['page']['far_future_expires_rewrite']) ?
- '' : $this->options['page']['cachedir_relative'] . 'wo.static.php?') .
- $this->options['page']['cachedir_relative'] . '0.gif');
- /* are we operating with multiple hosts */
- } elseif (!empty($this->options['page']['parallel']) &&
- !empty($this->options['page']['parallel_hosts']) &&
- (!count($ignore_list) || !in_array(str_replace($old_src_param, '', $img), $ignore_list))) {
- /* skip images on different hosts */
- if (preg_match("!//(www\.)?" . $this->host_escaped . "/+!i", $old_src) || strpos($old_src, '//') === false) {
- /* using secure host */
- if ($this->https && !empty($this->options['page']['parallel_https'])) {
- $new_host = $this->options['page']['parallel_https'];
- } else {
- /* calculating unique sum from image src */
- $sum = 0;
- $i = ceil(strlen($old_src)/2);
- while (isset($old_src{$i++})) {
- $sum += ord($old_src{$i-1});
- }
- $host = $hosts[$sum%$count];
- /* if we have dot in the distribution host - it's a domain name */
- $new_host = $host .
- ((strpos($host, '.') === false) ?
- '.' . $this->host : '');
- }
- $new_src = "//" .
- $new_host .
- $absolute_src .
- preg_replace("!(www\.)?" . $this->host_escaped . "!i",
- $new_host, $old_src_param);
- } elseif ($count_satellites && !empty($satellites_hosts[0]) && empty($replaced[$old_src])) {
- $img_host = preg_replace("@(https?:)?//(www\.)?([^/]+)/.*@", "$3", $old_src);
- /* check if we can distribute this image through satellites' hosts */
- if (in_array($img_host, $satellites)) {
- $new_src = preg_replace("@(https?://)(www\.)?([^/]+)/@", "$1" . $hosts[strlen($old_src)%$count] . ".$3/", $old_src);
- }
- }
- /* or replacing images with rewrite to Expires setter? */
- } elseif (!empty($this->options['page']['far_future_expires_rewrite']) ||
- !empty($this->options['page']['far_future_expires_external'])) {
- /* add static proxy for external images */
- if (!$absolute_src &&
- $this->options['page']['far_future_expires_external']) {
- $absolute_src = $old_src;
- }
- /* do not touch dynamic images / styles / scripts -- how we can handle them? */
- if ($absolute_src &&
- (preg_match("@\.(bmp|gif|png|ico|jpe?g)$@is", $absolute_src) ||
- !empty($this->options['page']['far_future_expires_external']))) {
- $new_src =
- $this->options['page']['cachedir_relative'] .
- 'wo.static.php?' . $absolute_src;
- }
- }
- if (!empty($new_src)) {
- if (empty($new_image)) {
- /* prevent replacing images from oher domains with the same file name */
- $new_src_image = str_replace($old_src, $new_src, $image[0]);
- } else {
- /* replace src in image with new class */
- $new_src_image = str_replace($old_src, $new_src, $new_image);
- }
- $content = str_replace($image[0], $new_src_image, $content);
- $new_src = '';
- $new_image = '';
- }
- $replaced[$image[0]] = 1;
- }
- }
- }
- return $content;
- }
- /**
- * Returns GZIP compressed content string with header
- *
- **/
- function create_gz_compress ($content, $force_gzip = true) {
- if (!empty($this->encoding)) {
- if (!empty($force_gzip) && function_exists('gzcompress')) {
- $size = strlen($content);
- $crc = crc32($content);
- $cnt = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
- $content = gzcompress($content, $this->options['page']['gzip_level']);
- $content = substr($content, 0, -4);
- $cnt .= $content;
- $cnt .= pack('V', $crc);
- $cnt .= pack('V', $size);
- } elseif (empty($force_gzip) && function_exists('gzdeflate')) {
- $cnt = gzdeflate($content, $this->options['page']['gzip_level']);
- }
- return $cnt;
- } else {
- return false;
- }
- }
- /**
- * Sets current encoding for HTML document
- *
- **/
- function set_gzip_encoding () {
- if (!empty($_SERVER["HTTP_ACCEPT_ENCODING"]) && !empty($this->options['page']['gzip'])) {
- $gzip_no_ie = !in_array($this->ua_mod, array('.ie6', '.ie7')) || empty($this->options['page']['gzip_noie']);
- $ae = strtolower($_SERVER["HTTP_ACCEPT_ENCODING"]);
- if (strpos($ae, 'x-gzip') !== false && $gzip_no_ie) {
- $this->encoding = 'x-gzip';
- $this->encoding_ext = '.gz';
- } elseif ((strpos($ae, 'gzip') !== false || !empty($_COOKIE['_wo_gzip'])) && $gzip_no_ie) {
- $this->encoding = 'gzip';
- $this->encoding_ext = '.gz';
- } elseif (strpos($ae, 'x-deflate') !== false) {
- $this->encoding = 'x-deflate';
- $this->encoding_ext = '…
Large files files are truncated, but you can click here to view the full file