You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
949 B
PHTML
34 lines
949 B
PHTML
5 years ago
|
<?php
|
||
|
|
||
|
/*
|
||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||
|
*/
|
||
|
|
||
|
class LoginKey {
|
||
|
|
||
|
public static function generate(string $appname): string {
|
||
|
global $database;
|
||
|
do {
|
||
|
$code = base64_encode(random_bytes(32));
|
||
|
} while ($database->has('userloginkeys', ['key' => $code]));
|
||
|
|
||
|
$database->insert('userloginkeys', ['key' => $code, 'expires' => date("Y-m-d H:i:s", time() + 600), 'appname' => $appname]);
|
||
|
|
||
|
return $code;
|
||
|
}
|
||
|
|
||
|
public static function getuid(string $code): int {
|
||
|
global $database;
|
||
|
if (!$database->has('userloginkeys', ["AND" => ['key' => $code, 'uid[!]' => null]])) {
|
||
|
throw new Exception();
|
||
|
}
|
||
|
|
||
|
$uid = $database->get('userloginkeys', 'uid', ['key' => $code]);
|
||
|
|
||
|
return $uid;
|
||
|
}
|
||
|
|
||
|
}
|