27 $this->headers = $this->cfg->headers;
38 return $this->cfg->statusCfg[$this->cfg->status];
40 return $this->cfg->status;
50 if (array_key_exists($status, $this->cfg->statusCfg)) {
51 $this->cfg->status = $status;
63 return $this->cfg->compress;
72 $this->cfg->compress = (boolean) $compress;
81 return $this->cfg->layout;
90 $this->cfg->layout = $layout;
99 return $this->cfg->ajaxLayout;
108 $this->cfg->ajaxLayout = $ajaxLayout;
119 public function addHeader($name, $value, $replace =
true) {
120 if ($name ==
'Content-Type')
122 if ($replace || !$this->
hasHeader($name)) {
123 $this->headers[$name] = $value;
138 if ($replace || !$this->
hasHeader(
'Content-Type')) {
139 if (array_key_exists($value, $this->cfg->contentTypeCfg))
140 $value = $this->cfg->getInArray(
'contentTypeCfg', $value);
141 else if (strpos($value,
'/') ===
false)
142 $value =
'text/'.$value;
143 $this->headers[
'Content-Type'] = $value.
'; charset='.$this->cfg->charset;
153 $this->
addHeader(
'Expires', gmdate(
'D, j M Y H:i:s', strtotime(
'+32 days')).
' GMT');
166 return $this->headers[$name];
177 return array_key_exists($name, $this->headers);
184 $this->headers = $this->cfg->headers;
191 if (!headers_sent()) {
193 foreach($this->headers as $name=>$value) {
194 header($name.
': '.$value);
215 if ($this->cfg->compress && !ob_get_length())
216 ob_start(
'ob_gzhandler');
224 public function send($headerOnly =
false) {
225 if (!headers_sent()) {
232 $layout =
request::isAjax()? $this->cfg->ajaxLayout : $this->cfg->layout;
242 'cache'=>array(
'auto'=>
false)
244 $tpl->set(
'content', $this->content);
245 $ret = $tpl->fetch();
252 return $this->
getAttr(
'globalCache');
263 if (!is_array($vars) || !isset($vars[
'status']) || ! is_array($vars[
'headers']))
264 throw new Exception(
'Error while retrieving from cache');
267 if (isset($vars[
'headers'][
'Content-Type'])) {
268 $this->headers[
'Content-Type'] = $vars[
'headers'][
'Content-Type'];
269 unset($vars[
'headers'][
'Content-Type']);
272 foreach($vars[
'headers'] as $k=>$v)
298 public function sendFile($file, $name = null, $delete =
false) {
299 $name = $name ? $name : basename($file);
313 $this->cfg->compress =
false;
316 $this->
addHeader(
'Cache-Control',
'must-revalidate, post-check=0, pre-check=0');
317 $this->
addHeader(
'Content-Type',
'application/force-download');
318 $this->
addHeader(
'Content-Disposition',
'attachment; filename='.$name.
'');
319 $this->
addHeader(
'Last-Modified', gmdate(
'D, j M Y H:i:s').
' GMT',
true);
332 if (strpos($type,
'audio') === 0 || strpos($type,
'video') === 0) {
335 $this->cfg->compress =
false;
337 $this->
addHeader(
'Last-Modified', gmdate(
'D, j M Y H:i:s', filemtime($file)).
' GMT',
true);
338 $this->
addHeader(
'Content-Type', $type,
true);
339 $this->
addHeader(
'Cache-Control',
'public',
false);
355 protected function mediaDownload($file, $forceDownload =
false, $fileName = null, $delete =
false) {
356 $fileName = $fileName ? $fileName : basename($file);
357 if(strstr($_SERVER[
'HTTP_USER_AGENT'],
'MSIE'))
358 $fileName = preg_replace(
'/\./',
'%2e', $fileName, substr_count($fileName,
'.') - 1);
359 $fileModified = filemtime($file);
360 $fileSize = filesize($file);
362 $audio = strpos($fileType,
'audio') === 0;
363 $video = strpos($fileType,
'video') === 0;
367 $bufferSize = 8*1024;
368 $partialDownload =
false;
369 $httpRangeDownload =
false;
371 if (isset($_SERVER[
'HTTP_RANGE'])) {
372 $range = explode(
'-', substr($_SERVER[
'HTTP_RANGE'], strlen(
'bytes=')));
374 $seekStart = intval($range[0]);
375 $seekEnd = $range[1] > 0 ? intval($range[1]) : -1;
376 $partialDownload =
true;
377 $httpRangeDownload =
true;
379 $partialDownload =
true;
380 $httpRangeDownload =
true;
383 if ($seekEnd < $seekStart)
384 $seekEnd = $fileSize - 1;
386 $contentLength = $seekEnd - $seekStart + 1;
388 if(!$fileHandle = fopen($file,
'rb'))
392 header(
'Pragma: public');
393 if ($forceDownload) {
394 if (ini_get(
'zlib.output_compression'))
395 ini_set(
'zlib.output_compression',
'Off');
397 header(
'Expires: 0');
398 header(
'Cache-Control: must-revalidate, post-check=0, pre-check=0');
399 header(
'Cache-Control: private',
false);
401 header(
'Content-Type: application/force-download');
402 header(
'Content-Type: application/octet-stream');
403 header(
'Content-Type: application/download');
404 header(
'Content-type: '.$fileType);
405 header(
'Content-Disposition: attachment; filename='.$fileName.
'');
407 header(
'Cache-Control: public');
408 header(
'Content-type: '.$fileType);
409 header(
'Content-Disposition: inline; filename='.$fileName.
'');
411 header(
'Last-Modified: '.date(
'D, d M Y H:i:s \G\M\T', $fileModified));
412 header(
"Content-Transfer-Encoding: binary\n");
413 if ($httpRangeDownload) {
414 header(
'HTTP/1.0 206 Partial Content');
415 header(
'Status: 206 Partial Content');
416 header(
'Accept-Ranges: bytes');
417 header(
'Content-Range: bytes '.$seekStart.
'-'.$seekEnd.
'/'.$fileSize);
419 header(
'Content-Length: '.$contentLength);
421 if ($seekStart > 0) {
422 $partialDownload =
true;
423 fseek($fileHandle, $seekStart);
424 if ($fileType ==
'video/x-flv')
425 echo
'FLV', pack(
'C', 1), pack(
'C', 1), pack(
'N', 9), pack(
'N', 9);
434 $throttle = $video ? 320 : ($audio ? 84 : 0);
435 $burst = 1024 * ($video ? 500 : ($audio ? 120 : 0));
436 while (!(connection_aborted() || connection_status() == 1) && $bytesSent < $contentLength) {
438 if ($bytesSent >= $burst)
442 if ($bytesSent + $bufferSize > $contentLength)
443 $bufferSize = $contentLength - $bytesSent;
446 echo fread($fileHandle, $bufferSize);
447 $bytesSent+= $bufferSize;
453 if($speed && ($bytesSent - $burst > $speed * $chunk*1024)) {
478 $tmp =
"/*\n[comment]\n*/\n";
482 $tmp =
"<!--\n[comment]\n-->\n";
485 return str_replace(
'[comment]', $comment, $tmp);
509 public function error($url = null, $number = 404) {
sendFile($file, $name=null, $delete=false)
setContentType($value, $replace=true)
mediaDownload($file, $forceDownload=false, $fileName=null, $delete=false)
sendFileAsString($file, $name)
redirect($url, $status=301)
setVarsFromGlobalCache($vars)
error($url=null, $number=404)
static isMobile($test=null)
addHeader($name, $value, $replace=true)
setAjaxlayout($ajaxLayout)
static get($className, array $cfg=array())