Skip to content

Commit

Permalink
Start using json even for cookies!
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele45 committed Jul 11, 2016
1 parent f96c556 commit c9bbb4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 7 additions & 9 deletions sources/Load.php
Expand Up @@ -176,19 +176,17 @@ function loadUserSettings()

if (empty($id_member) && isset($_COOKIE[$cookiename]))
{
// Fix a security hole in PHP 4.3.9 and below...
if (preg_match('~^a:[34]:\{i:0;i:\d{1,8};i:1;s:(0|64):"([a-fA-F0-9]{64})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}$~i', $_COOKIE[$cookiename]) == 1)
{
list ($id_member, $password) = @unserialize($_COOKIE[$cookiename]);
$id_member = !empty($id_member) && strlen($password) > 0 ? (int) $id_member : 0;
}
else
$id_member = 0;
list ($id_member, $password) = serializeToJson($_COOKIE[$cookiename], function ($array_from) use ($cookiename) {
$_COOKIE[$cookiename] = json_encode($array_from);
});
$id_member = !empty($id_member) && strlen($password) > 0 ? (int) $id_member : 0;
}
elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && ($_SESSION['USER_AGENT'] == $req->user_agent() || !empty($modSettings['disableCheckUA'])))
{
// @todo Perhaps we can do some more checking on this, such as on the first octet of the IP?
list ($id_member, $password, $login_span) = @unserialize($_SESSION['login_' . $cookiename]);
list ($id_member, $password, $login_span) = serializeToJson($_SESSION['login_' . $cookiename], function ($array_from) use ($cookiename) {
$_SESSION['login_' . $cookiename] = json_encode($array_from);
});
$id_member = !empty($id_member) && strlen($password) == 64 && $login_span > time() ? (int) $id_member : 0;
}

Expand Down
11 changes: 7 additions & 4 deletions sources/subs/Auth.subs.php
Expand Up @@ -46,20 +46,23 @@ function setLoginCookie($cookie_length, $id, $password = '')

// The cookie may already exist, and have been set with different options.
$cookie_state = (empty($modSettings['localCookies']) ? 0 : 1) | (empty($modSettings['globalCookies']) ? 0 : 2);
if (isset($_COOKIE[$cookiename]) && preg_match('~^a:[34]:\{i:0;i:\d{1,8};i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}$~', $_COOKIE[$cookiename]) === 1)

if (isset($_COOKIE[$cookiename]))
{
$array = @unserialize($_COOKIE[$cookiename]);
$array = serializeToJson($_COOKIE[$cookiename], function ($array_from) use ($cookiename) {
$_COOKIE[$cookiename] = json_encode($array_from);
});

// Out with the old, in with the new!
if (isset($array[3]) && $array[3] != $cookie_state)
{
$cookie_url = url_parts($array[3] & 1 > 0, $array[3] & 2 > 0);
elk_setcookie($cookiename, serialize(array(0, '', 0)), time() - 3600, $cookie_url[1], $cookie_url[0]);
elk_setcookie($cookiename, json_encode(array(0, '', 0)), time() - 3600, $cookie_url[1], $cookie_url[0]);
}
}

// Get the data and path to set it on.
$data = serialize(empty($id) ? array(0, '', 0) : array($id, $password, time() + $cookie_length, $cookie_state));
$data = json_encode(empty($id) ? array(0, '', 0) : array($id, $password, time() + $cookie_length, $cookie_state));
$cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));

// Set the cookie, $_COOKIE, and session variable.
Expand Down

0 comments on commit c9bbb4a

Please sign in to comment.