forked from ppetermann/pheal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhealFileCache.php
179 lines (158 loc) · 5.92 KB
/
PhealFileCache.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
MIT License
Copyright (c) 2010 - 2012 Peter Petermann
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Simple filecache for the xml
*/
class PhealFileCache implements PhealCacheInterface
{
/**
* path where to store the xml
* @var string
*/
protected $basepath;
/**
* various options for the filecache
* valid keys are: delimiter, umask, umask_directory
* @var array
*/
protected $options = array(
'delimiter' => ':',
'umask' => 0666,
'umask_directory' => 0777
);
/**
* construct PhealFileCache,
* @param string $basepath optional string on where to store files, defaults to the current/users/home/.pheal/cache/
* @param array $options optional config array, valid keys are: delimiter, umask, umask_directory
*/
public function __construct($basepath = false, $options = array())
{
if(!$basepath)
$basepath = getenv('HOME'). "/.pheal/cache/";
$this->basepath = $basepath;
// Windows systems don't allow : as part of the filename
$this->options['delimiter'] = (strtoupper (substr(PHP_OS, 0,3)) == 'WIN') ? "#" : ":";
// add options
if(is_array($options) && count($options))
$this->options = array_merge($this->options, $options);
}
/**
* create a filename to use
* @param int $userid
* @param string $apikey
* @param string $scope
* @param string $name
* @param array $args
* @return string
*/
protected function filename($userid, $apikey, $scope, $name, $args)
{
// secure input to make sure pheal don't write the files anywhere
// user can define their own apikey/vcode
// maybe this should be tweaked or hashed
$regexp = "/[^a-z0-9,.-_=]/i";
$userid = (int)$userid;
$apikey = preg_replace($regexp,'_',$apikey);
// build cache filename
$argstr = "";
foreach($args as $key => $val) {
if(strlen($val) < 1)
unset($args[$key]);
elseif(!in_array(strtolower($key), array('userid','apikey','keyid','vcode')))
$argstr .= preg_replace($regexp,'_',$key) . $this->options['delimiter'] . preg_replace($regexp,'_',$val) . $this->options['delimiter'];
}
$argstr = substr($argstr, 0, -1);
$filename = "Request" . ($argstr ? "_" . $argstr : "") . ".xml";
$filepath = $this->basepath . ($userid ? "$userid/$apikey/$scope/$name/" : "public/public/$scope/$name/");
if(!file_exists($filepath)) {
// check write access
if(!is_writable($this->basepath))
throw new PhealException(sprintf("Cache directory '%s' isn't writeable", $filepath));
// create cache folder
$oldUmask = umask(0);
mkdir($filepath, $this->options['umask_directory'], true);
umask($oldUmask);
} else {
// check write access
if(!is_writable($filepath))
throw new PhealException(sprintf("Cache directory '%s' isn't writeable", $filepath));
if(file_exists($filename) && !is_writeable($filename))
throw new PhealException(sprintf("Cache file '%s' isn't writeable", $filename));
}
return $filepath . $filename;
}
/**
* Load XML from cache
* @param int $userid
* @param string $apikey
* @param string $scope
* @param string $name
* @param array $args
*/
public function load($userid, $apikey, $scope, $name, $args)
{
$filename = $this->filename($userid, $apikey, $scope, $name, $args);
if(!file_exists($filename))
return false;
$xml = file_get_contents($filename);
if($this->validate_cache($xml))
return $xml;
return false;
}
/**
* validate the cached xml if it is still valid. This contains a name hack
* to work arround EVE API giving wrong cachedUntil values
* @param string $xml
* @return boolean
*/
public function validate_cache($xml)
{
$tz = date_default_timezone_get();
date_default_timezone_set("UTC");
$xml = new SimpleXMLElement($xml);
$dt = (int) strtotime($xml->cachedUntil);
$time = time();
date_default_timezone_set($tz);
return (bool) ($dt > $time);
}
/**
* Save XML from cache
* @param int $userid
* @param string $apikey
* @param string $scope
* @param string $name
* @param array $args
* @param string $xml
*/
public function save($userid,$apikey,$scope,$name,$args,$xml)
{
$filename = $this->filename($userid, $apikey, $scope, $name, $args);
$exists = file_exists($filename);
// save content
file_put_contents($filename, $xml);
// chmod only new files
if(!$exists)
chmod($filename, $this->options['umask']);
}
}