1
0
Fork 0

Merge pull request #12814 from nupplaphil/bug/config_multi_serialize

Fix multiple serialized values
This commit is contained in:
Hypolite Petovan 2023-02-20 11:54:18 -05:00 committed by GitHub
commit 33a8d2bc3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 8 deletions

View file

@ -61,15 +61,13 @@ class DatabaseConfig implements IManageConfigValues
foreach ($setCache->getAll() as $category => $data) {
foreach ($data as $key => $value) {
$this->cache->set($category, $key, $value, Cache::SOURCE_DATA);
$this->database->insert('config', ['cat' => $category, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
$this->set($category, $key, $value);
}
}
foreach ($delCache->getAll() as $category => $keys) {
foreach ($keys as $key => $value) {
$this->cache->delete($category, $key);
$this->database->delete('config', ['cat' => $category, 'k' => $key]);
$this->delete($category, $key);
}
}
@ -85,6 +83,10 @@ class DatabaseConfig implements IManageConfigValues
/** {@inheritDoc} */
public function set(string $cat, string $key, $value): bool
{
// In case someone or something already serialized a config entry, unserialize it first
// We serialize values just once
$value = SerializeUtil::maybeUnserialize($value);
$this->cache->set($cat, $key, $value, Cache::SOURCE_DATA);
return $this->database->insert('config', ['cat' => $cat, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
}

View file

@ -28,10 +28,24 @@ namespace Friendica\Core\Config\Util;
*/
class SerializeUtil
{
/**
* Checks if the value needs to get unserialized and returns the unserialized value
*
* @param mixed $value A possibly serialized value
*
* @return mixed The unserialized value
*/
public static function maybeUnserialize($value)
{
if (static::isSerialized($value)) {
return @unserialize(trim($value));
// This checks for possible multiple serialized values
while (static::isSerialized($value)) {
$oldValue = $value;
$value = @unserialize($value);
// If there's no change after the unserialize call, break the loop (avoid endless loops)
if ($oldValue === $value) {
break;
}
}
return $value;