This repository has been archived by the owner on Dec 17, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpArmoryCache.class.php
441 lines (364 loc) · 16 KB
/
phpArmoryCache.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* phpArmory is an embeddable class to retrieve XML data from the WoW armory.
*
* phpArmory is an embeddable PHP5 class, which allow you to fetch XML data
* from the World of Warcraft armory in order to display arena teams,
* characters, guilds, and items on a web page.
* @author Daniel S. Reichenbach <[email protected]>
* @copyright Copyright (c) 2008, Daniel S. Reichenbach
* @license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
* @link https://github.com/marenkay/phparmory/tree
* @package phpArmory
* @version 0.4.2
*/
/**
* phpArmory5Cache extends phpArmory5, thus we require the base class file.
*/
require_once('phpArmory.class.php');
/**
* phpArmory5Cache class
*
* A class to fetch and cache unserialized XML data from the World of Warcraft
* armory site.
* @package phpArmory
* @subpackage classes
*/
class phpArmory5Cache extends phpArmory5 {
/**
* Current version of the phpArmory5Cache class.
* @access protected
* @var string Contains the current class version.
*/
protected $version = "0.4.2";
/**
* Current state of the phpArmory5Cache class. Allowed values are alpha, beta,
* and release.
* @access protected
* @var string Contains the current versions' state.
*/
protected $version_state = "release";
/**
* The current cache ID in use.
* @access protected
* @var string Contains the cache ID.
*/
protected $cacheID = "";
/**
* Selected data storage for the class. Can be "file" or "mysql".
* @access protected
* @var string Contains the selected data storage.
*/
protected $dataStore = "file";
/**
* The path to the cache directory (must chmod 777 to make it writeable).
* @access protected
* @var string Contains the path where to store cache files.
*/
protected $dataPath = "./cache";
/**
* MySQL connection string.
* @access protected
* @var string Contains the MySQL connection string.
*/
protected $mysqlString = "mysql://username:password@localhost/databasename";
/**
* MySQL cache table.
* @access protected
* @var string Contains the MySQL cache table.
*/
protected $mysqlTable = "armory_cache";
/**
* The time between cache updates in seconds
* @access protected
* @var integer Contains the time delay between updates in seconds. Default is 4 hours.
*/
protected $updateInterval = 14400;
/**
* phpArmory5Cache class constructor.
* @access public
* @param string $areaName
* @param string $dataStore
* @param string $mysqlString
* @param string $mysqlTable
* @param int $downloadRetries
* @return mixed $result Returns TRUE if the class could be instantiated properly. Returns FALSE and an error string, if the class could not be instantiated.
*/
public function __construct($areaName = NULL, $dataStore = NULL, $dataPath = NULL, $mysqlString = NULL, $downloadRetries = NULL) {
parent::__construct($areaName, $downloadRetries);
if(($dataStore==NULL)&&($this->dataStore)){
$dataStore = $this->dataStore;
} else {
$this->dataStore = $dataStore;
}
if(($mysqlString==NULL)&&($this->mysqlString)){
$mysqlString = $this->mysqlString;
} else {
$this->mysqlString = $mysqlString;
}
if(($mysqlTable==NULL)&&($this->mysqlTable)){
$mysqlTable = $this->mysqlTable;
} else {
$this->mysqlTable = $mysqlTable;
}
switch($this->dataStore) {
case 'file':
if(($dataPath==NULL)&&($this->dataPath)){
$dataPath = $this->dataPath;
} else {
$this->dataPath = $dataPath;
}
break;
case 'mysql':
if (!extension_loaded('mysql') || !extension_loaded('mysqli')) {
self::triggerError("Either PHP extension \"mysql\" or \"mysqli\" extension is required to use this class.");
} else {
if(($dataPath==NULL)&&($this->mysqlTable)){
$dataPath = $this->mysqlTable;
} else {
$this->mysqlTable = $dataPath;
}
$conn = @parse_url($this->mysqlString);
$this->mysqlString = mysql_connect($conn['host'], $conn['user'], $conn['pass']) or die("Failed to connect to database");
mysql_select_db(str_replace('/', '', $conn['path']), $this->mysqlString) or die("Unable to select database table");
$query = "CREATE TABLE IF NOT EXISTS `".$this->mysqlTable."` (
`cache_id` VARCHAR(100) NOT NULL DEFAULT '',
`cache_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`cache_xml` TEXT,
PRIMARY KEY `cache_id` (`cache_id`))";
mysql_query($query, $this->mysqlString) or self::triggerError("Unable to create the cache table");
}
break;
default:
die("Invalid dataStore defined.");
break;
}
parent::setArea($areaName);
}
/**
* Raise a PHP error.
* @access protected
* @param string $userError The error string to output.
*/
protected function triggerError ($userError = NULL) {
if (is_string($userError)) {
trigger_error("phpArmoryCache " . $this->version . " - " . $this->version_state . ": " . $userError, E_USER_ERROR);
}
}
/**
* Raise a PHP warning if the class is used from the command line.
* @access protected
* @param string $userWarning The warning string to output.
*/
protected function triggerWarning ($userWarning = NULL) {
if (is_string($userWarning)) {
$sapi_type = substr(php_sapi_name(), 0, 3);
if ($sapi_type == 'cli') {
trigger_error("phpArmoryCache " . $this->version . " - " . $this->version_state . ": " . $userWarning, E_USER_WARNING);
}
}
}
/**
* Raise a PHP notice if the class is used from the command line.
* @access protected
* @param string $userNotice The notice string to output.
*/
protected function triggerNotice ($userNotice = NULL) {
if (is_string($userNotice)) {
$sapi_type = substr(php_sapi_name(), 0, 3);
if ($sapi_type == 'cli') {
trigger_error("phpArmoryCache " . $this->version . " - " . $this->version_state . ": " . $userNotice, E_USER_NOTICE);
}
}
}
/**
* Retrieve cached data.
* @access public
* @param string $cacheID The unique ID of the retrieved data.
* @return mixed $result Returns a string containg the cached XML data, or FALSE if there is no cached data.
*/
public function getCachedData($cacheID) {
switch($this->dataStore) {
case "file":
$filename = $this->dataPath."/".$cacheID;
if (file_exists($filename)) {
if (time()-filemtime($filename) > $this->updateInterval) {
// Cache is out of date, remove the old file
self::triggerNotice("Cached file data for " . $cacheID . " is outdated.");
@unlink($filename);
} else {
// Return the cached XML as an array
$array = unserialize(file_get_contents($filename));
self::triggerNotice("Cached file data for " . $cacheID . " is valid.");
return $array;
}
}
break;
case "mysql":
$query = "SELECT cache_xml, UNIX_TIMESTAMP(cache_time) AS cache_time FROM `".$this->mysqlTable."` WHERE cache_id = '".$cacheID."'";
$result = mysql_query($query, $this->mysqlString) or self::triggerError("Unable to select cache from database");
if ($result && mysql_num_rows($result)) {
if (time()-mysql_result($result, 0, 'cache_time') > $this->updateInterval) {
$query = "DELETE FROM `".$this->mysqlTable."` WHERE cache_id = '".$cacheID."'";
mysql_query($query, $this->mysqlString);
self::triggerNotice("Cached mysql data for " . $cacheID . " is outdated.");
} else {
// Return the cached XML as an array
self::triggerNotice("Cached mysql data for " . $cacheID . " is valid.");
return parent::convertXmlToArray(mysql_result($result, 0, 'cache_xml'));
}
}
break;
}
}
/**
* Saved retrieved data to cache.
* @access public
* @param string $cacheID The unique ID of the retrieved data.
* @param string $xml The retrieved XML data to store.
* @return bool $result Returns TRUE if $xml could be cached, and FALSE if it failed to be saved.
*/
public function setCachedData($cacheID, $xml) {
switch($this->dataStore){
case "file":
$filename = $this->dataPath."/".$cacheID;
$handle = fopen($filename, 'w') or self::triggerError("Can not open file (" . $filename . ")");
fwrite($handle, $xml) or self::triggerError("Can not write to file (" . $filename . ")");
self::triggerNotice("Successfully cached " . $cacheID . " in file mode.");
fclose($handle);
break;
case "mysql":
if (get_magic_quotes_gpc()) $xml = stripslashes($xml);
$xml = mysql_escape_string($xml);
$query = "REPLACE INTO `".$this->mysqlTable."` (cache_id, cache_xml) VALUES('".$cacheID."','".$xml."')";
mysql_query($query, $this->mysqlString) or self::triggerError("Unable to save to database " . mysql_error());
self::triggerNotice("Successfully cached " . $cacheID . " in mysql mode.");
break;
}
}
/**
* Provides information on a specific arena team.
* @access public
* @param string $arenaName The arena teams' name.
* @param string $realmName The arena teams' realm name.
* @return array $result Returns an array containing arenaTeamData if $arenaTeamName and $realmName are valid, otherwise FALSE.
*/
public function getArenaTeamData($arenaTeamName = NULL, $realmName = NULL) {
$this->cacheID = "a".md5($arenaTeamName.$realmName);
$cached = $this->getCachedData($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getArenaTeamData($arenaTeamName, $realmName);
if ( $this->cacheID && is_array($cached) ) {
$scached = serialize($cached);
$this->setCachedData($this->cacheID, $scached);
unset($this->cacheID);
} else {
return FALSE;
}
return $cached;
} else {
return $cached;
}
}
/**
* Provides information on a specific character.
* @access public
* @param string $characterName The characters' name.
* @param string $realmName The characters' realm name.
* @param bool $onlyBasicData If true, only the basic character data will be fetched.
* @return array $result Returns an array containing characterData if $characterName and $realmName are valid, otherwise FALSE.
*/
public function getCharacterData($characterName = NULL, $realmName = NULL, $onlyBasicData = false) {
$this->cacheID = "c".md5($characterName.$realmName);
$cached = $this->getCachedData($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getCharacterData($characterName, $realmName, $onlyBasicData);
if ( $this->cacheID && is_array($cached) ) {
$scached = serialize($cached);
$this->setCachedData($this->cacheID, $scached);
unset($this->cacheID);
} else {
return FALSE;
}
return $cached;
} else {
return $cached;
}
}
/**
* Provides information on a specific guild.
* @access public
* @param string $guildName The guilds' name.
* @param string $realmName The guilds' realm name.
* @return array $result Returns an array containing guildData if $guildName and $realmName are valid, otherwise FALSE.
*/
public function getGuildData($guildName = NULL, $realmName = NULL) {
$this->cacheID = "g".md5($guildName.$realmName);
$cached = $this->getCachedData($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getGuildData($guildName, $realmName);
if ( $this->cacheID && is_array($cached) ) {
$scached = serialize($cached);
$this->setCachedData($this->cacheID, $scached);
unset($this->cacheID);
} else {
return FALSE;
}
return $cached;
} else {
return $cached;
}
}
/**
* Provides information on a specific item by querying its' ID.
* @access public
* @param int $itemID The items' ID.
* @return array $result Returns an array containing itemData if $itemID is valid, otherwise FALSE.
*/
public function getItemData($itemID) {
$this->cacheID = "i".md5($itemID);
$cached = $this->getCachedData($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getItemData($itemID);
if ( $this->cacheID && is_array($cached) ) {
$scached = serialize($cached);
$this->setCachedData($this->cacheID, $scached);
unset($this->cacheID);
} else {
return FALSE;
}
return $cached;
} else {
return $cached;
}
}
/**
* Provides information on a specific item by querying its' name.
* @access public
* @param string $itemName The items' name.
* @param string $itemFilter An associative array of search paramters.
* @return mixed $result Returns an array containing itemData if $itemName is valid, otherwise FALSE.
*/
public function getItemDataByName($itemName, $itemFilter = NULL) {
if ($filter&&is_array($filter)) {
$this->cacheID = "s".md5($itemName.implode('', $filter));
} else {
$this->cacheID = "s".md5($itemName);
}
$cached = $this->getCachedData($this->cacheID);
if (!is_array($cached)) {
$cached = parent::getItemDataByName($itemName, $itemFilter);
if ( $this->cacheID && is_array($cached) ) {
$scached = serialize($cached);
$this->setCachedData($this->cacheID, $scached);
unset($this->cacheID);
} else {
return FALSE;
}
return $cached;
} else {
return $cached;
}
}
}
?>