/
contentcache.class.php
165 lines (142 loc) · 4.79 KB
/
contentcache.class.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* @file contentcache.class.php
* Local cache for dynamically generated content/resource.
*
* @section License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Copyright @ 2009-2012 Daniel Swanson <danij@dengine.net>
*/
includeGuard('ContentCache');
class ContentInfo
{
public $modifiedTime = NULL; // Unix timestamp.
}
class ContentCache
{
private $_docRoot = NULL;
public function __construct($docRoot=NULL)
{
$this->_docRoot = $docRoot.'/';
}
public function store($file, $content)
{
if(!isset($file) || $file == '' ||
!isset($content) || $content == '')
return false;
$file = FrontController::absolutePath($this->_docRoot.'/'.$file);
/*try
{*/
if(!file_exists($file))
{
$fh = fopen_recursive($file, 'w');
}
else if(!is_writable($file))
{
throw new Exception(sprintf('file %s not writeable.', $file));
}
else
{
$fh = fopen($file, 'w');
}
if(fwrite($fh, $content) === false)
throw new Exception(sprintf('failed writing to file %s.', $file));
if(!$fh)
throw new Exception(sprintf('failed opening file %s.', $file));
fclose($fh);
/*}
catch(Exception $e)
{
die($e->getMessage());
}*/
return true;
}
/**
* Is the specified content element present in the cache?
*
* @param relPath (String) File name to look up.
* @return (Boolean) TRUE iff the content element is available.
*/
public function isPresent($relPath)
{
return (bool) file_exists(FrontController::nativePath($this->_docRoot."/$relPath"));
}
/**
* Attempt to retrieve a content element from the cache, copying
* its contents into a string.
*
* @param relPath (String) File name to retrieve.
* @return (Boolean) FALSE if the content element could not be
* found, else the content as a string.
*/
public function retrieve($relPath)
{
$path = FrontController::nativePath($this->_docRoot."/$relPath");
if(!$path || !file_exists($path))
throw new Exception(sprintf('file %s not present in content cache.', $relPath));
if($stream = fopen($path, 'r'))
{
$contents = stream_get_contents($stream);
fclose($stream);
return $contents;
}
return "";
}
/**
* Retrieve info about a file in the cache.
*
* @param relPath (String) Name of the file to get info for.
* @param info (ContentInfo) Info record to be populated.
* @return (Boolean) FALSE if the specified file does not exist.
*/
public function getInfo($relPath, &$info)
{
if(!$info instanceof ContentInfo) return FALSE;
$path = FrontController::nativePath($this->_docRoot."/$relPath");
if(!$path || !file_exists($path)) return FALSE;
$info->modifiedTime = filemtime($path);
return TRUE;
}
/**
* Touch a file in the cache (update modified time).
*
* @param relPath (String) Name of the file to touch.
* @return (Boolean) FALSE if the specified file does not exist.
*/
public function touch($relPath)
{
$path = FrontController::nativePath($this->_docRoot."/$relPath");
if(!$path || !file_exists($path)) return FALSE;
touch($path);
return TRUE;
}
/**
* Attempt to import a content element from the cache, outputting
* its contents straight to the output buffer.
*
* @param relPath (String) File name to retrieve.
* @return (Boolean) TRUE iff the content element was imported.
*/
public function import($relPath)
{
$path = FrontController::nativePath($this->_docRoot."/$relPath");
if(!$path || !file_exists($path))
throw new Exception(sprintf('file %s not present in content cache.', $relPath));
return @readfile($path);
}
}