Skip to content

Commit

Permalink
Add backward compatibility framework for serialize to json conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele45 committed Jun 19, 2016
1 parent 0d6abfd commit 49df726
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
59 changes: 57 additions & 2 deletions sources/Load.php
Expand Up @@ -1423,10 +1423,31 @@ function loadTheme($id_theme = 0, $initialize = true)
$context['user']['name'] = $txt['guest_title'];

// Set up some additional interface preference context
$context['admin_preferences'] = !empty($options['admin_preferences']) ? json_decode($options['admin_preferences'], true) : array();
if (!empty($options['admin_preferences']))
{
$context['admin_preferences'] = serializeToJson($options['admin_preferences'], function($array_form) {
global $context;

$context['admin_preferences'] = $array_form;
require_once(SUBSDIR . '/Admin.subs.php');
updateAdminPreferences();
});
}
else
{
$context['admin_preferences'] = array();
}

if (!$user_info['is_guest'])
$context['minmax_preferences'] = !empty($options['minmax_preferences']) ? json_decode($options['minmax_preferences'], true) : array();
{
$context['minmax_preferences'] = serializeToJson($options['minmax_preferences'], function($array_form) {
global $settings, $user_info;

// Update the option.
require_once(SUBSDIR . '/Themes.subs.php');
updateThemeOptions(array($settings['theme_id'], $user_info['id'], 'minmax_preferences', json_encode($array_form)));
});
}
// Guest may have collapsed the header, check the cookie to prevent collapse jumping
elseif ($user_info['is_guest'] && isset($_COOKIE['upshrink']))
$context['minmax_preferences'] = array('upshrink' => $_COOKIE['upshrink']);
Expand Down Expand Up @@ -3137,4 +3158,38 @@ function detectServerCores()
}

return 1;
}

/**
* This is necessary to support data stored in the pre-1.0.8 way (i.e. serialized)
*
* @deprecated since 1.0.8
*
* @param string $variable The string to convert
* @param null|callable $save_callback The function that will save the data to the db
* @return mixed[] the array
*/
function serializeToJson($variable, $save_callback = null)
{
$array_form = json_decode($variable, true);

// decoding failed, let's try with unserialize
if ($array_form === null)
{
$array_form = @unserialize($variable);

// If unserialize fails as well, let's just store an empty array
if ($array_form === false)
{
$array_form = array();
}

// Time to update the value if necessary
if ($save_callback !== null)
{
$save_callback($array_form);
}
}

return $array_form;
}
16 changes: 14 additions & 2 deletions sources/admin/ManageThemes.controller.php
Expand Up @@ -1331,7 +1331,13 @@ public function action_jsoption()
// If this is the admin preferences the passed value will just be an element of it.
if ($_GET['var'] == 'admin_preferences')
{
$options['admin_preferences'] = !empty($options['admin_preferences']) ? json_decode($options['admin_preferences'], true) : array();
$options['admin_preferences'] = serializeToJson($options['admin_preferences'], function($array_form) {
global $context;

$context['admin_preferences'] = $array_form;
require_once(SUBSDIR . '/Admin.subs.php');
updateAdminPreferences();
});

// New thingy...
if (isset($_GET['admin_key']) && strlen($_GET['admin_key']) < 5)
Expand All @@ -1343,7 +1349,13 @@ public function action_jsoption()
// If this is the window min/max settings, the passed window name will just be an element of it.
elseif ($_GET['var'] == 'minmax_preferences')
{
$options['minmax_preferences'] = !empty($options['minmax_preferences']) ? json_decode($options['minmax_preferences'], true) : array();
$context['minmax_preferences'] = serializeToJson($options['minmax_preferences'], function($array_form) {
global $settings, $user_info;

// Update the option.
require_once(SUBSDIR . '/Themes.subs.php');
updateThemeOptions(array($settings['theme_id'], $user_info['id'], 'minmax_preferences', json_encode($array_form)));
});

// New value for them
if (isset($_GET['minmax_key']) && strlen($_GET['minmax_key']) < 10)
Expand Down
8 changes: 7 additions & 1 deletion sources/controllers/ModerationCenter.controller.php
Expand Up @@ -77,7 +77,13 @@ public function prepareModcenter()
loadLanguage('ModerationCenter');
loadTemplate(false, 'admin');

$context['admin_preferences'] = !empty($options['admin_preferences']) ? json_decode($options['admin_preferences'], true) : array();
$context['admin_preferences'] = serializeToJson($options['admin_preferences'], function($array_form) {
global $context;

$context['admin_preferences'] = $array_form;
require_once(SUBSDIR . '/Admin.subs.php');
updateAdminPreferences();
});
$context['robot_no_index'] = true;

// Moderation counts for things that this moderator can take care of
Expand Down

0 comments on commit 49df726

Please sign in to comment.