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) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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);
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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'");
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