-
Notifications
You must be signed in to change notification settings - Fork 1
/
flickrcache.class.php
229 lines (191 loc) · 5.62 KB
/
flickrcache.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* File: api-flickrCache
* Handle the JSON formatted Flickr API, and Cache the results to files.
*
* Version:
* 2009.12.13
*
* Copyright:
* 2009 Jay Williams
*
* License:
* Simplified BSD License - http://opensource.org/licenses/bsd-license.php
*/
class FlickrCache extends Flickr
{
/**
* Property: cache_mode
* Whether caching is enabled on the request or not
*/
var $cache_mode;
/**
* Property: cache_ttl
* Length of time, in seconds, the cache will be considered valid
*/
var $cache_ttl;
/**
* Property: cache_path
* Directory to store the cache files
*/
var $cache_path;
/**
* Property: header_mode
* Whether header response will be include in the output
*/
var $header_mode;
/*%******************************************************************************************%*/
// CONSTRUCTOR
/**
* Method: __construct()
* The constructor.
*
* Access:
* public
*
* Parameters:
* key - _string_ (Optional) Your Flickr API Key. If blank, it will look for the <FLICKR_KEY> constant.
* secret_key - _string_ (Optional) Your Flickr API Secret Key. If blank, it will look for the <FLICKR_SECRET_KEY> constant.
* subclass - _string_ (Optional) Don't use this. This is an internal parameter.
*
* Returns:
* boolean FALSE if no valid values are set, otherwise true.
*/
public function __construct($key = null, $secret_key = null, $subclass = null)
{
// Set default values
$this->cache_mode = false;
$this->cache_ttl = 3600;
$this->cache_path = './cache/';
$this->header_mode = false;
return parent::__construct($key, $secret_key, $subclass);
}
/*%******************************************************************************************%*/
// SETTERS
/**
* Method: cache_mode()
* Enables request file caching.
*
* Access:
* public
*
* Parameters:
* enabled - _boolean_ (Optional) Whether cache is enabled or not.
* ttl - _integer_ (Optional) Length of time, in seconds, the cache will be considered valid
* path - _string_ (Optional) Directory to store the cache files
*
* Returns:
* void
*/
public function cache_mode($enabled = true, $ttl = null, $path = null)
{
// Set default values
$this->cache_mode = $enabled;
if ($ttl != null)
$this->cache_ttl = $ttl;
if ($path != null)
$this->cache_path = $path;
// Run cache directory checks
if ($enabled && (!is_dir($this->cache_path) || !is_writable($this->cache_path)))
throw new Flickr_Exception('Cache directory doesn\'t exist or isn\'t writeable');
}
/**
* Method: header_mode()
* Enables header mode within the API. Enabling header mode will include the request header in addition to the body.
*
* Access:
* public
*
* Parameters:
* enabled - _boolean_ (Optional) Whether header mode is enabled or not.
*
* Returns:
* void
*/
public function header_mode($enabled = true)
{
// Set default values
$this->header_mode = $enabled;
}
/*%******************************************************************************************%*/
// MAGIC METHODS
/**
* Handle requests to properties
*/
public function __get($var)
{
// Determine the name of this class
$class_name = get_class($this);
// Re-instantiate this class, passing in the subclass value
$ref = new $class_name($this->key, $this->secret_key, strtolower($var));
$ref->test_mode($this->test_mode); // Make sure this gets passed through.
$ref->cache_mode($this->cache_mode, $this->cache_ttl, $this->cache_path); // Make sure this gets passed through.
$ref->header_mode($this->header_mode); // Make sure this gets passed through.
return $ref;
}
/**
* Handle requests to methods
*/
public function __call($name, $args)
{
// Include default arguments
$default_args = array('format' => 'json', 'nojsoncallback' => 1);
$args[0] = array_merge($default_args, (array)$args[0]);
return parent::__call($name, $args);
}
/*%******************************************************************************************%*/
// REQUEST/RESPONSE
/**
* Method: request()
* Requests the data, parses it, and returns it. Requires RequestCore and SimpleXML.
*
* Parameters:
* url - _string_ (Required) The web service URL to request.
*
* Returns:
* ResponseCore object
*/
public function request($url)
{
if ($this->test_mode)
return $url;
// Generate cache filename
$cache = $this->cache_path . get_class() . '_' . md5($url) . '.cache';
// If cache exists, and is still valid, load it
if($this->cache_mode && file_exists($cache) && (time() - filemtime($cache)) < $this->cache_ttl)
{
$response = (object) json_decode(file_get_contents($cache));
$response->_cached = true; // Add notice that this is a cached file
return $response;
}
if (!class_exists('RequestCore'))
throw new Exception('This class requires RequestCore. http://requestcore.googlecode.com');
$http = new RequestCore($url);
$http->set_useragent(FLICKR_USERAGENT);
$http->send_request();
$response = (object) $this->parse_response($http->get_response_body());
if ($this->header_mode)
$response->_header = $http->get_response_header();
// Cache only successfuly requests
if ($this->cache_mode && $response->stat == 'ok')
{
file_put_contents($cache . '_tmp', json_encode($response));
rename($cache . '_tmp', $cache);
}
return $response;
}
/**
* Method: parse_response()
* Method for parsing the JSON response data.
*
* Parameters:
* data - _string_ (Required) The data to parse.
*
* Returns:
* mixed data
*/
public function parse_response($data)
{
return json_decode($data);
}
}