nyroFwk  0.2
nyro/module/compress/controller.class.php
Go to the documentation of this file.
1 <?php
11 
12  protected function execCssExt($prm=null) {
13  $file = $prm[0];
14  if (request::get('text'))
15  $file.= DS.request::get('text');
16  response::getInstance()->showFile(file::nyroExists(array(
17  'name'=>'module'.DS.nyro::getCfg()->compressModule.DS.'css'.DS.$file,
18  'realName'=>true,
19  'type'=>'other'
20  )));
21  }
22 
23  protected function execJs($prm=null) {
24  $this->compress('js', $prm);
25  }
26 
27  protected function execCss($prm=null) {
28  $this->compress('css', $prm);
29  }
30 
37  protected function compress($type, $prm) {
38  $resp = response::getInstance();
39 
40  if ($type == 'js') {
41  $conf = $this->cfg->all;
42  factory::mergeCfg($conf, $this->cfg->js);
43  } else if ($type == 'css') {
44  $conf = $this->cfg->all;
45  factory::mergeCfg($conf, $this->cfg->css);
46  }
47 
48  $key = $type.'--'.md5(serialize($prm)).'--'.md5(serialize($conf));
49  $supportsGzip = false;
50  if ($conf['compress']) {
51  $encodings = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))) : array();
52  if ($conf['gzip_compress'] && (in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('gzencode') && !ini_get('zlib.output_compression')) {
53  $enc = in_array('x-gzip', $encodings) ? 'x-gzip' : 'gzip';
54  $supportsGzip = true;
55  $key = 'gzip-'.$key;
56  }
57  }
58  $content = null;
59  $cache = cache::getInstance($this->cfg->cache);
60  $cacheDate = $cache->get($content, array('id'=>$key));
61 
62  if (!$conf['disk_cache'] || !$cacheDate) {
63  foreach($prm as $file) {
64  $f = file::nyroExists(array(
65  'name'=>'module_'.nyro::getCfg()->compressModule.'_'.$type.'_'.$file,
66  'type'=>'tpl',
67  'tplExt'=>$type
68  ));
69  if ($f) {
70  if ($conf['php'])
71  $content.= file::fetch($f);
72  else
73  $content.= file::read($f);
74  }
75  }
76 
77  if ($conf['compress']) {
78  if ($type == 'js') {
79  lib::load('jsMin');
80  $content = JSMin::minify($content);
81  } else if ($type == 'css') {
82  lib::load('cssMin');
83  $content = CssMin::minify($content, $conf['filters'], $conf['plugins']);
84  }
85  if ($supportsGzip)
86  $content = gzencode($content, 9, FORCE_GZIP);
87  }
88  $cache->save();
89  } else if ($cacheDate) {
90  $resp->addHeader('Age', time() - $cacheDate);
91  }
92 
93  /* @var $resp response_http */
94  if ($conf['compress']) {
95  $resp->setCompress(false);
96  $resp->addHeader('Vary', 'Accept-Encoding'); // Handle proxies
97  if ($conf['etags'] || preg_match('/MSIE/i', $_SERVER['HTTP_USER_AGENT'])) {
98  // We need to use etags on IE since it will otherwise always load the contents
99  $resp->addHeader('ETag', md5($content));
100  }
101  $parseTime = $this->_parseTime($conf['expires_offset']);
102  $resp->addHeader('Expires', gmdate('D, d M Y H:i:s', time() + $parseTime).' GMT');
103  $resp->addHeader('Cache-Control', 'public, max-age='.$parseTime);
104 
105  if ($type == 'js') {
106  // Output explorer workaround or compressed file
107  if (!isset($_GET['gz']) && $supportsGzip && $conf['patch_ie'] && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
108  // Build request URL
109  $url = $_SERVER['REQUEST_URI'];
110 
111  if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'])
112  $url.= '?'.$_SERVER['QUERY_STRING'].'&gz=1';
113  else
114  $url.= '?gz=1';
115 
116  // This script will ensure that the gzipped script gets loaded on IE versions with the Gzip request chunk bug
117  echo 'var gz;try {gz = new XMLHttpRequest();} catch(gz) { try {gz = new ActiveXObject("Microsoft.XMLHTTP");}';
118  echo 'catch (gz) {gz = new ActiveXObject("Msxml2.XMLHTTP");}}';
119  echo 'gz.open("GET", "'.$url.'", false);gz.send(null);eval(gz.responseText);';
120  die();
121  }
122  }
123 
124  if ($supportsGzip)
125  $resp->addHeader('Content-Encoding', $enc);
126  }
127 
128  $resp->sendText($content);
129  }
130 
131  protected function _parseTime($time) {
132  $multipel = 1;
133 
134  // Hours
135  if (strpos($time, "h") != false)
136  $multipel = 60 * 60;
137 
138  // Days
139  if (strpos($time, "d") != false)
140  $multipel = 24 * 60 * 60;
141 
142  // Months
143  if (strpos($time, "m") != false)
144  $multipel = 24 * 60 * 60 * 30;
145 
146  // Trim string
147  return intval($time) * $multipel;
148  }
149 
150 }
static get($get=null)
static read($file)
Definition: file.class.php:242
static getInstance(array $cfg=array())
Definition: cache.class.php:30
static getCfg()
Definition: nyro.class.php:133
if(!defined('NYROROOT')) define('NYROROOT' ROOT DS
Definition: start.inc.php:56
static getInstance()
static nyroExists($prm)
Definition: file.class.php:137
static mergeCfg(array &$prm, array $cfg)
static fetch($file, array $vars=array())
Definition: file.class.php:441
static load($name)
Definition: lib.class.php:51
$f
Definition: list.php:6
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13