Skip to content

Commit

Permalink
Fix decode with previously cached versions
Browse files Browse the repository at this point in the history
Closes #84ti
  • Loading branch information
stevebauman committed Dec 28, 2023
1 parent fb5b765 commit 35e262b
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Cache/CacheDefinitionCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stevebauman\Purify\Cache;

use HTMLPurifier_Definition;
use HTMLPurifier_DefinitionCache;
use Illuminate\Support\Facades\Cache;

Expand Down Expand Up @@ -31,8 +32,8 @@ public function __construct($type)
/**
* Adds a definition object to the cache.
*
* @param \HTMLPurifier_Definition $def
* @param \HTMLPurifier_Config $config
* @param HTMLPurifier_Definition $def
* @param \HTMLPurifier_Config $config
*
* @return bool|void
*/
Expand All @@ -54,8 +55,8 @@ public function add($def, $config)
/**
* Unconditionally saves a definition object to the cache.
*
* @param \HTMLPurifier_Definition $def
* @param \HTMLPurifier_Config $config
* @param HTMLPurifier_Definition $def
* @param \HTMLPurifier_Config $config
*
* @return bool|void
*/
Expand All @@ -73,8 +74,8 @@ public function set($def, $config)
/**
* Replace an object in the cache.
*
* @param \HTMLPurifier_Definition $def
* @param \HTMLPurifier_Config $config
* @param HTMLPurifier_Definition $def
* @param \HTMLPurifier_Config $config
*
* @return bool|void
*/
Expand All @@ -98,7 +99,7 @@ public function replace($def, $config)
*
* @param \HTMLPurifier_Config $config
*
* @return bool|\HTMLPurifier_Definition
* @return bool|HTMLPurifier_Definition
*/
public function get($config)
{
Expand Down Expand Up @@ -162,7 +163,7 @@ public function cleanup($config)
/**
* Encode the definition for storage.
*
* @param \HTMLPurifier_Definition $def
* @param HTMLPurifier_Definition $def
*
* @return string
*/
Expand All @@ -176,15 +177,17 @@ protected function encode($def)
*
* @param string $def
*
* @return \HTMLPurifier_Definition
* @return HTMLPurifier_Definition
*/
protected function decode($def)
{
// Backwards compatibility with previously cached definitions.
if (! $encoded = base64_decode($def)) {
return unserialize($def);
// Backwards compatibility with old cache definitions.
$instance = @unserialize($def);

if ($instance instanceof HTMLPurifier_Definition) {
return $instance;
}

return unserialize($encoded);
return unserialize(base64_decode($def));
}
}

0 comments on commit 35e262b

Please sign in to comment.