diff --git a/misuzu.php b/misuzu.php index 9136ea2a..9f44402f 100644 --- a/misuzu.php +++ b/misuzu.php @@ -36,7 +36,6 @@ $errorHandler->register(); require_once 'src/array.php'; require_once 'src/audit_log.php'; -require_once 'src/base32.php'; require_once 'src/changelog.php'; require_once 'src/colour.php'; require_once 'src/comments.php'; @@ -431,14 +430,14 @@ MIG; if(!empty($_COOKIE['msz_uid']) && !empty($_COOKIE['msz_sid']) && ctype_digit($_COOKIE['msz_uid']) && ctype_xdigit($_COOKIE['msz_sid']) && strlen($_COOKIE['msz_sid']) === 64) { - $_COOKIE['msz_auth'] = base64url_encode(user_session_cookie_pack($_COOKIE['msz_uid'], $_COOKIE['msz_sid'])); + $_COOKIE['msz_auth'] = Base64::decode(user_session_cookie_pack($_COOKIE['msz_uid'], $_COOKIE['msz_sid']), true); setcookie('msz_auth', $_COOKIE['msz_auth'], strtotime('1 year'), '/', '', !empty($_SERVER['HTTPS']), true); setcookie('msz_uid', '', -3600, '/', '', !empty($_SERVER['HTTPS']), true); setcookie('msz_sid', '', -3600, '/', '', !empty($_SERVER['HTTPS']), true); } if(!empty($_COOKIE['msz_auth']) && is_string($_COOKIE['msz_auth'])) { - $cookieData = user_session_cookie_unpack(base64url_decode($_COOKIE['msz_auth'])); + $cookieData = user_session_cookie_unpack(Base64::decode($_COOKIE['msz_auth'], true)); if(!empty($cookieData) && user_session_start($cookieData['user_id'], $cookieData['session_token'])) { $userDisplayInfo = DB::prepare(' diff --git a/public/auth/login.php b/public/auth/login.php index 8c70e721..d5f4d983 100644 --- a/public/auth/login.php +++ b/public/auth/login.php @@ -96,7 +96,7 @@ while(!empty($_POST['login']) && is_array($_POST['login'])) { user_session_start($userData->user_id, $sessionKey); $cookieLife = strtotime(user_session_current('session_expires')); - $cookieValue = base64url_encode(user_session_cookie_pack($userData->user_id, $sessionKey)); + $cookieValue = Base64::encode(user_session_cookie_pack($userData->user_id, $sessionKey), true); setcookie('msz_auth', $cookieValue, $cookieLife, '/', '', !empty($_SERVER['HTTPS']), true); if(!is_local_url($loginRedirect)) { diff --git a/public/auth/twofactor.php b/public/auth/twofactor.php index d9eab165..5ce88cd3 100644 --- a/public/auth/twofactor.php +++ b/public/auth/twofactor.php @@ -69,7 +69,7 @@ while(!empty($twofactor)) { user_session_start($tokenInfo['user_id'], $sessionKey); $cookieLife = strtotime(user_session_current('session_expires')); - $cookieValue = base64url_encode(user_session_cookie_pack($tokenInfo['user_id'], $sessionKey)); + $cookieValue = Base64::encode(user_session_cookie_pack($tokenInfo['user_id'], $sessionKey), true); setcookie('msz_auth', $cookieValue, $cookieLife, '/', '', !empty($_SERVER['HTTPS']), true); if(!is_local_url($redirect)) { diff --git a/public/proxy.php b/public/proxy.php index 3e37e3ce..9799873d 100644 --- a/public/proxy.php +++ b/public/proxy.php @@ -24,7 +24,7 @@ if(empty($proxyHash) || empty($proxyUrl)) { return; } -$proxyUrlDecoded = base64url_decode($proxyUrl); +$proxyUrlDecoded = Base64::decode($proxyUrl, true); $parsedUrl = parse_url($proxyUrlDecoded); if(empty($parsedUrl['scheme']) diff --git a/src/Base32.php b/src/Base32.php new file mode 100644 index 00000000..b1447a06 --- /dev/null +++ b/src/Base32.php @@ -0,0 +1,41 @@ +> $shift) : ''; + } + + return $out; + } + + public static function encode(string $data): string { + $bin = ''; + $encoded = ''; + $length = strlen($data); + + for($i = 0; $i < $length; $i++) { + $bin .= sprintf('%08b', ord($data[$i])); + } + + $bin = str_split($bin, 5); + $last = array_pop($bin); + $bin[] = str_pad($last, 5, '0', STR_PAD_RIGHT); + + foreach($bin as $part) { + $encoded .= self::CHARS[bindec($part)]; + } + + return $encoded; + } +} diff --git a/src/Base64.php b/src/Base64.php new file mode 100644 index 00000000..9b8d1c60 --- /dev/null +++ b/src/Base64.php @@ -0,0 +1,28 @@ +> $shift) : ''; - } - - return $out; -} - -function base32_encode(string $data): string { - $bin = ''; - $encoded = ''; - $length = strlen($data); - - for($i = 0; $i < $length; $i++) { - $bin .= sprintf('%08b', ord($data[$i])); - } - - $bin = str_split($bin, 5); - $last = array_pop($bin); - $bin[] = str_pad($last, 5, '0', STR_PAD_RIGHT); - - foreach($bin as $part) { - $encoded .= MSZ_BASE32_CHARS[bindec($part)]; - } - - return $encoded; -} diff --git a/src/otp.php b/src/otp.php index d4b8039a..a69e9b41 100644 --- a/src/otp.php +++ b/src/otp.php @@ -1,4 +1,5 @@