Go to the documentation of this file.00001 <?php
00010 class module_compress_controller extends module_abstract {
00011
00012 protected function execCssExt($prm=null) {
00013 $file = $prm[0];
00014 if (request::get('text'))
00015 $file.= DS.request::get('text');
00016 response::getInstance()->showFile(file::nyroExists(array(
00017 'name'=>'module'.DS.nyro::getCfg()->compressModule.DS.'css'.DS.$file,
00018 'realName'=>true,
00019 'type'=>'other'
00020 )));
00021 }
00022
00023 protected function execJs($prm=null) {
00024 $this->compress('js', $prm);
00025 }
00026
00027 protected function execCss($prm=null) {
00028 $this->compress('css', $prm);
00029 }
00030
00037 protected function compress($type, $prm) {
00038 if ($type == 'js') {
00039 $conf = array_merge_recursive($this->cfg->all, $this->cfg->js);
00040 } else if ($type == 'css') {
00041 $conf = array_merge_recursive($this->cfg->all, $this->cfg->css);
00042 }
00043
00044 $key = $type.'--'.md5(serialize($prm)).'--'.md5(serialize($conf));
00045 $supportsGzip = false;
00046 if ($conf['compress']) {
00047 $encodings = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))) : array();
00048 if ($conf['gzip_compress'] && (in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('gzencode') && !ini_get('zlib.output_compression')) {
00049 $enc = in_array('x-gzip', $encodings) ? 'x-gzip' : 'gzip';
00050 $supportsGzip = true;
00051 $key = 'gzip-'.$key;
00052 }
00053 }
00054 $content = null;
00055 $cache = cache::getInstance($this->cfg->cache);
00056 if (!$conf['disk_cache'] || !$cache->get($content, array('id'=>$key))) {
00057 foreach($prm as $file) {
00058 $f = file::nyroExists(array(
00059 'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$type.'_'.$file,
00060 'type'=>'tpl',
00061 'tplExt'=>$type
00062 ));
00063 if ($f) {
00064 if ($conf['php'])
00065 $content.= file::fetch($f);
00066 else
00067 $content.= file::read($f);
00068 }
00069 }
00070
00071 if ($conf['compress']) {
00072 if ($type == 'js') {
00073 lib::load('jsMin');
00074 $content = JSMin::minify($content);
00075 } else if ($type == 'css') {
00076 lib::load('cssMin');
00077 $content = CssMin::minify($content, $conf['filters'], $conf['plugins']);
00078 }
00079 if ($supportsGzip)
00080 $content = gzencode($content, 9, FORCE_GZIP);
00081 }
00082 $cache->save();
00083 }
00084
00085 $resp = response::getInstance();
00086
00087
00088 if ($conf['compress']) {
00089 $resp->setCompress(false);
00090 $resp->addHeader('Vary', 'Accept-Encoding');
00091 if ($conf['etags'] || preg_match('/MSIE/i', $_SERVER['HTTP_USER_AGENT'])) {
00092
00093 $resp->addHeader('ETag', md5($content));
00094 }
00095 $parseTime = $this->_parseTime($conf['expires_offset']);
00096 $resp->addHeader('Expires', gmdate('D, d M Y H:i:s', time() + $parseTime).' GMT');
00097 $resp->addHeader('Cache-Control', 'public, max-age='.$parseTime);
00098
00099 if ($type == 'js') {
00100
00101 if (!isset($_GET['gz']) && $supportsGzip && $conf['patch_ie'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
00102
00103 $url = $_SERVER['REQUEST_URI'];
00104
00105 if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'])
00106 $url.= '?'.$_SERVER['QUERY_STRING'].'&gz=1';
00107 else
00108 $url.= '?gz=1';
00109
00110
00111 echo 'var gz;try {gz = new XMLHttpRequest();} catch(gz) { try {gz = new ActiveXObject("Microsoft.XMLHTTP");}';
00112 echo 'catch (gz) {gz = new ActiveXObject("Msxml2.XMLHTTP");}}';
00113 echo 'gz.open("GET", "'.$url.'", false);gz.send(null);eval(gz.responseText);';
00114 die();
00115 }
00116 }
00117
00118 if ($supportsGzip)
00119 $resp->addHeader('Content-Encoding', $enc);
00120 }
00121
00122 $resp->sendText($content);
00123 }
00124
00125 protected function _parseTime($time) {
00126 $multipel = 1;
00127
00128
00129 if (strpos($time, "h") != false)
00130 $multipel = 60 * 60;
00131
00132
00133 if (strpos($time, "d") != false)
00134 $multipel = 24 * 60 * 60;
00135
00136
00137 if (strpos($time, "m") != false)
00138 $multipel = 24 * 60 * 60 * 30;
00139
00140
00141 return intval($time) * $multipel;
00142 }
00143
00144 }