Skip to content

Commit

Permalink
Merge pull request #11 from jrfnl/Fix-serialization-issues
Browse files Browse the repository at this point in the history
Base64-encode cached data to fix serialization issues
  • Loading branch information
YahnisElsts committed Sep 30, 2014
2 parents 5ca60e9 + 69d36b3 commit 338f373
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions includes/Wpup/FileCache.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php
/**
* A simple file-based cache.
*
* @internal Data is base64 encoded to avoid unserialization issues ('unserialize(): Error at offset') which
* could be caused by:
* - inconsistent line endings
* - unescaped quotes/slashes etc
* - miscounted unicode characters
*
* @see https://github.com/YahnisElsts/wp-update-server/pull/11
*/
class Wpup_FileCache implements Wpup_Cache {
protected $cacheDirectory;
Expand All @@ -18,7 +26,7 @@ public function __construct($cacheDirectory) {
public function get($key) {
$filename = $this->getCacheFilename($key);
if ( is_file($filename) && is_readable($filename) ) {
$cache = unserialize(file_get_contents($filename));
$cache = unserialize(base64_decode(file_get_contents($filename)));
if ( $cache['expiration_time'] < time() ) {
return null; //Cache expired.
} else {
Expand All @@ -41,7 +49,7 @@ public function set($key, $value, $expiration = 0) {
'expiration_time' => time() + $expiration,
'value' => $value,
);
file_put_contents($this->getCacheFilename($key), serialize($cache));
file_put_contents($this->getCacheFilename($key), base64_encode(serialize($cache)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/Wpup/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getMetadata() {
*/
public static function fromArchive($filename, $slug = null, Wpup_Cache $cache = null) {
$modified = filemtime($filename);
$cacheKey = 'metadata-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
$cacheKey = 'metadata-b64-' . $slug . '-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
$metadata = null;

//Try the cache first.
Expand Down

0 comments on commit 338f373

Please sign in to comment.