use json rather than serialize

master
Jean-Christian Paul Denis 2023-03-05 22:58:55 +01:00
parent c1f8c9666c
commit 3f347f0330
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
4 changed files with 47 additions and 13 deletions

View File

@ -36,7 +36,7 @@ use ArrayObject;
*/
abstract class Action
{
/** @var array<string> Current module */
/** @var array<string,mixed> Current module */
protected $module = [];
/** @var string Current full path */
@ -87,7 +87,7 @@ abstract class Action
$settings = dcCore::app()->blog->settings->get(Core::id())->get('settings_' . $this->class_name);
if (null != $settings) {
$settings = unserialize($settings);
$settings = json_decode($settings, true);
}
$this->settings = is_array($settings) ? $settings : [];
@ -237,7 +237,7 @@ abstract class Action
{
dcCore::app()->blog->settings->get(Core::id())->put(
'settings_' . $this->class_name,
serialize($this->settings),
json_encode($this->settings),
'string',
null,
true,

View File

@ -79,6 +79,11 @@ class Core
return basename(dirname(__DIR__));
}
public static function name()
{
return __('improve');
}
public function getLogs(): array
{
return $this->logs;
@ -95,8 +100,8 @@ class Core
return 0;
}
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcLog::LOG_TABLE_NAME);
$cur->log_msg = serialize($this->logs);
$cur->log_table = 'improve';
$cur->log_msg = json_encode($this->logs);
$cur->log_table = self::id();
$id = dcCore::app()->log->addLog($cur);
return $id;
@ -104,13 +109,13 @@ class Core
public function readLogs(int $id): array
{
$rs = dcCore::app()->log->getLogs(['log_table' => 'improve', 'log_id' => $id, 'limit' => 1]);
$rs = dcCore::app()->log->getLogs(['log_table' => self::id(), 'log_id' => $id, 'limit' => 1]);
if ($rs->isEmpty()) {
return [];
}
dcCore::app()->log->delLogs($rs->__get('log_id'));
$res = unserialize($rs->__get('log_msg'));
$res = json_decode($rs->__get('log_msg'), true);
return is_array($res) ? $res : [];
}
@ -122,7 +127,7 @@ class Core
return [];
}
$lines = [];
foreach ($logs['improve'] as $path => $tools) {
foreach ($logs[self::id()] as $path => $tools) {
$l_types = [];
foreach (['success', 'warning', 'error'] as $type) {
$l_tools = [];
@ -198,7 +203,7 @@ class Core
}
foreach ($workers as $action) {
// trace all path and action in logs
$this->logs['improve'][__('Begin')][] = $action->id();
$this->logs[self::id()][__('Begin')][] = $action->id();
// info: set current module
$action->setModule($module);
$action->setPath(__('Begin'), '', true);
@ -215,7 +220,7 @@ class Core
}
foreach ($workers as $action) {
// trace all path and action in logs
$this->logs['improve'][$file[0]][] = $action->id();
$this->logs[self::id()][$file[0]][] = $action->id();
// info: set current path
$action->setPath($file[0], $file[1], $file[2]);
}
@ -254,7 +259,7 @@ class Core
}
foreach ($workers as $action) {
// trace all path and action in logs
$this->logs['improve'][__('End')][] = $action->id();
$this->logs[self::id()][__('End')][] = $action->id();
// info: set current module
$action->setPath(__('End'), '', true);
// action: close module

View File

@ -16,6 +16,7 @@ namespace Dotclear\Plugin\improve;
/* dotclear */
use dcCore;
use dcNamespace;
use dcNsProcess;
/* php */
@ -33,7 +34,7 @@ class Install extends dcNsProcess
private static $default_settings = [[
'disabled',
'List of hidden action modules',
'tab;newline;endoffile',
'cssheader;tab;newline;endoffile',
'string',
]];
@ -52,6 +53,7 @@ class Install extends dcNsProcess
try {
self::update_0_8_0();
self::update_1_1_0();
self::putSettings();
return true;
@ -88,4 +90,31 @@ class Install extends dcNsProcess
}
}
}
/** Update improve < 1.1 : use json_(en|de)code rather than (un)serialize */
private static function update_1_1_0(): void
{
if (version_compare(dcCore::app()->getVersion(Core::id()) ?? '0', '1.1', '<')) {
foreach (['setting_', 'preferences'] as $key) {
$record = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' .
"WHERE setting_ns = '" . dcCore::app()->con->escape(Core::id()) . "' " .
"AND setting_id LIKE '" . $key . "%' "
);
while ($record->fetch()) {
try {
$value = @unserialize($record->f('setting_value'));
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME);
$cur->setting_value = json_encode(is_array($value) ? $value : []);
$cur->update(
"WHERE setting_id = '" . $record->f('setting_id') . "' and setting_ns = '" . dcCore::app()->con->escape($record->f('setting_ns')) . "' " .
'AND blog_id ' . (null === $record->f('blog_id') ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->f('blog_id')) . "' "))
);
} catch(Exception) {
}
}
}
}
}
}

View File

@ -89,7 +89,7 @@ class Manage extends dcNsProcess
if (!empty(self::$type)) {
$preferences = dcCore::app()->blog->settings->get(Core::id())->get('preferences');
if (is_string($preferences)) {
$preferences = unserialize($preferences);
$preferences = json_decode($preferences, true);
if (is_array($preferences)) {
return $all ? $preferences : (array_key_exists(self::$type, $preferences) ? $preferences[self::$type] : []);
}