forked from JorgenEvens/WebLab_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
77 lines (59 loc) · 2.09 KB
/
Cache.php
File metadata and controls
77 lines (59 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
class WebLab_Cache {
protected static $_cached;
protected static $_url;
/**
* Initialize cache and check if the current page has been cached.
*/
public static function init() {
if( !empty( $_POST ) ) return; // Cannot cache when data is being delivered
$cache_dir = WebLab_Config::getApplicationConfig()->get( 'Application.Cache.Location', WebLab_Config::OBJECT, false );
if( $cache_dir == null ) return;
self::$_url = WebLab_Parser_URL::getForRequest()->getPath();
$hash = md5( self::$_url );
$path = $cache_dir . DIRECTORY_SEPARATOR . $hash . '.cache';
if( file_exists( $path ) ) {
if( self::_acceptsGzip() ) {
header( 'Content-Encoding:gzip' );
readfile( $path );
} else {
readgzfile( $path );
}
exit();
}
self::$_cached = $path;
}
protected static function _acceptsGzip() {
$s_key = '_gzip_support';
if( !isset( $_SESSION[$s_key] ) ) {
// IIS
$accepted = '';
if( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
$accepted = $_SERVER['HTTP_ACCEPT_ENCODING'];
} // Apache
else if( function_exists( 'getallheaders' ) ){
$headers = getallheaders();
$search = 'accept-encoding:';
foreach( $headers as $header ) {
if( strpos( strtolower( $header ), $search ) !== false ) {
$accepted .= ',' . trim( substr( $header, strpos( $header, ':' )+1 ) );
}
}
}
$_SESSION[$s_key] = ( strpos( ',' . trim( $accepted ) . ',', ',gzip,' ) !== false );
}
return $_SESSION[$s_key];
}
public static function out() {
$cache = WebLab_Config::getApplicationConfig()->get( 'Application.Cache.URLs', WebLab_Config::RAW, false );
if( $cache == null ) {
return;
}
$regex = '#^(' . implode( '|', array_filter( $cache, 'preg_quote' ) ) . ')#i';
if( preg_match( $regex, self::$_url ) && self::$_cached != null ) {
$file = gzopen( self::$_cached, 'w' );
gzwrite( $file, (string)WebLab_Template::getRootTemplate() );
gzclose( $file );
}
}
}