Skip to content

Commit 338f373

Browse files
committed
Merge pull request #11 from jrfnl/Fix-serialization-issues
Base64-encode cached data to fix serialization issues
2 parents 5ca60e9 + 69d36b3 commit 338f373

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

includes/Wpup/FileCache.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<?php
22
/**
33
* A simple file-based cache.
4+
*
5+
* @internal Data is base64 encoded to avoid unserialization issues ('unserialize(): Error at offset') which
6+
* could be caused by:
7+
* - inconsistent line endings
8+
* - unescaped quotes/slashes etc
9+
* - miscounted unicode characters
10+
*
11+
* @see https://github.com/YahnisElsts/wp-update-server/pull/11
412
*/
513
class Wpup_FileCache implements Wpup_Cache {
614
protected $cacheDirectory;
@@ -18,7 +26,7 @@ public function __construct($cacheDirectory) {
1826
public function get($key) {
1927
$filename = $this->getCacheFilename($key);
2028
if ( is_file($filename) && is_readable($filename) ) {
21-
$cache = unserialize(file_get_contents($filename));
29+
$cache = unserialize(base64_decode(file_get_contents($filename)));
2230
if ( $cache['expiration_time'] < time() ) {
2331
return null; //Cache expired.
2432
} else {
@@ -41,7 +49,7 @@ public function set($key, $value, $expiration = 0) {
4149
'expiration_time' => time() + $expiration,
4250
'value' => $value,
4351
);
44-
file_put_contents($this->getCacheFilename($key), serialize($cache));
52+
file_put_contents($this->getCacheFilename($key), base64_encode(serialize($cache)));
4553
}
4654

4755
/**

includes/Wpup/Package.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getMetadata() {
7575
*/
7676
public static function fromArchive($filename, $slug = null, Wpup_Cache $cache = null) {
7777
$modified = filemtime($filename);
78-
$cacheKey = 'metadata-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
78+
$cacheKey = 'metadata-b64-' . $slug . '-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
7979
$metadata = null;
8080

8181
//Try the cache first.

0 commit comments

Comments
 (0)