Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/web_services/classes/ElggHMACCache.php (2 issues)

1
<?php
2
/**
3
 * ElggHMACCache
4
 * Store cached data in a temporary database, only used by the HMAC stuff.
5
 *
6
 * @package    Elgg.Core
7
 * @subpackage HMAC
8
 */
9
class ElggHMACCache extends ElggCache {
10
	/**
11
	 * Set the Elgg cache.
12
	 *
13
	 * @param int $max_age Maximum age in seconds, 0 if no limit.
14
	 */
15
	function __construct($max_age = 0) {
16
		$this->setVariable("max_age", $max_age);
17
	}
18
19
	/**
20
	 * Save a key
21
	 *
22
	 * @param string $key          Name
23
	 * @param string $data         Value
24
	 * @param int    $expire_after Number of seconds to expire cache after
25
	 *
26
	 * @return boolean
27
	 */
28
	public function save($key, $data, $expire_after = null) {
29
		$dbprefix = elgg_get_config('dbprefix');
30
		$key = sanitise_string($key);
31
		$time = time();
32
33
		$query = "INSERT into {$dbprefix}hmac_cache (hmac, ts) VALUES ('$key', '$time')";
34
		return insert_data($query);
0 ignored issues
show
Bug Best Practice introduced by Cash Costello
The expression return insert_data($query) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
35
	}
36
37
	/**
38
	 * Load a key
39
	 *
40
	 * @param string $key    Name
41
	 * @param int    $offset Offset
42
	 * @param int    $limit  Limit
43
	 *
44
	 * @return string
45
	 */
46
	public function load($key, $offset = 0, $limit = null) {
47
		$dbprefix = elgg_get_config('dbprefix');
48
		$key = sanitise_string($key);
49
50
		$row = get_data_row("SELECT * from {$dbprefix}hmac_cache where hmac='$key'");
51
		if ($row) {
52
			return $row->hmac;
53
		}
54
55
		return false;
56
	}
57
58
	/**
59
	 * Invalidate a given key.
60
	 *
61
	 * @param string $key Name
62
	 *
63
	 * @return bool
64
	 */
65
	public function delete($key) {
66
		$dbprefix = elgg_get_config('dbprefix');
67
		$key = sanitise_string($key);
68
69
		return delete_data("DELETE from {$dbprefix}hmac_cache where hmac='$key'");
0 ignored issues
show
Bug Best Practice introduced by jdalsem
The expression return delete_data(EncapsedNode) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
70
	}
71
72
	/**
73
	 * Clear out all the contents of the cache.
74
	 *
75
	 * Not currently implemented in this cache type.
76
	 *
77
	 * @return true
78
	 */
79
	public function clear() {
80
		return true;
81
	}
82
83
	/**
84
	 * Clean out old stuff.
85
	 *
86
	 */
87
	public function __destruct() {
88
		$dbprefix = elgg_get_config('dbprefix');
89
		$time = time();
90
		$age = (int) $this->getVariable("max_age");
91
92
		$expires = $time - $age;
93
94
		delete_data("DELETE from {$dbprefix}hmac_cache where ts<$expires");
95
	}
96
}
97