Skip to content

Commit ae642c7

Browse files
author
Jordan Hall
committed
Simplify cache retrieval and perform additional checks on cache object format
1 parent 9deb3e9 commit ae642c7

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/DOFileCache.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public function get($key)
115115
return false;
116116
}
117117

118+
// Check cache object format
119+
if (!isset($cacheObj->content) || !isset($cacheObj->expiryTimestamp)) {
120+
return false;
121+
}
122+
123+
// Check cache content is serialized
124+
if (!$this->isSerialized($cacheObj->content)) {
125+
return false;
126+
}
127+
118128
if (!function_exists('sys_getloadavg') && $this->config['unixLoadUpperThreshold'] !== -1) {
119129
throw new Exception('Your PHP installation does not support `sys_getloadavg` (Windows?). Please set `unixLoadUpperThreshold` to `-1` in your DOFileCache config.');
120130
}
@@ -127,23 +137,45 @@ public function get($key)
127137

128138
if ($cacheObj->expiryTimestamp > time() || $unixLoad[0] >= $this->config['unixLoadUpperThreshold']) {
129139
// Cache item has not yet expired or system load is too high
130-
$content = $cacheObj->content;
131-
132-
if (($unserializedContent = unserialize($content)) !== false) {
133-
// Normal unserialization
134-
$content = $unserializedContent;
135-
} elseif ($content == serialize(false)) {
136-
// Edge case to handle boolean false being stored
137-
$content = false;
138-
}
139-
140-
return $content;
140+
return unserialize($cacheObj->content);
141141
} else {
142142
// Cache item has expired
143143
return false;
144144
}
145145
}
146146

147+
/**
148+
* Check if the string contains serialized data
149+
*
150+
* @param string $string
151+
*
152+
* @return bool
153+
*/
154+
public function isSerialized($string)
155+
{
156+
if (!is_string($string)) {
157+
return false;
158+
}
159+
160+
if ($string === 'N;') {
161+
return true;
162+
}
163+
164+
if (strlen($string) < 4) {
165+
return false;
166+
}
167+
168+
if ($string[1] !== ':') {
169+
return false;
170+
}
171+
172+
if (!in_array($string[0], ['s', 'a', 'O', 'b', 'i', 'd'])) {
173+
return false;
174+
}
175+
176+
return (@unserialize($string) !== false);
177+
}
178+
147179
/**
148180
* Remove a value from the cache.
149181
*

0 commit comments

Comments
 (0)