File: /var/www/html/wp-content/themes/ormedia/page-api-for-keyalert.php
<?php
/* Template Name: api-for-keyalert-login */
require_once('key_alert_functions.php');
function secu_do_login_4api($login, $password, $push_id)
{
date_default_timezone_set('Asia/Hong_Kong');
global $wpdb;
$obj = new stdClass();
$obj->rc = 1;
$obj->msg = "";
$u = get_secu_user($login);
if ($u == null) {
$obj->rc = -1;
$obj->msg = "login not exists";
insert_log(0, "","","login not exist: ".$login);
} else {
$res = wp_check_password($password, $u->password);
if ($res) {
if (!empty($push_id)) {
$old_push_id = r_push_id($u->id);
if (!empty($old_push_id) && $old_push_id != $push_id) {
// 提示另一设备登录
$data = [
'title' => "logout",
'alert' => "logout",
'extras' => [
'logout' => 1
]
];
send_jpush_msg($old_push_id, $data);
}
u_push_id($u->id, $push_id);
}
$obj->msg = "success";
// $obj->data = $u;
$payload_new = array(
'iss' => 'keylab',
'iat' => time(),
'exp' => time() + 3600,
'nbf' => time() + 3600,
'sub' => 'keylab.cc',
'jti' => md5(uniqid('JWT') . time())
);
$token_new = JwtAuthClass::getToken($payload_new);
// $old_token = r_token($u->id);
// if ($old_token) {
// if (JwtAuthClass::verifyToken($old_token->token) == 1) {
// $obj->data->token = $old_token->token;
// $obj->data->expiry = date('Y-m-d H:i:s', JwtAuthClass::getTokenExp($old_token->token));
// } else {
// u_token($u->id, $token_new);
// $obj->data->token = $token_new;
// $obj->data->expiry = date('Y-m-d H:i:s', JwtAuthClass::getTokenExp($token_new));
// }
// } else {
u_token($u->id, $token_new);
$obj->data->token = $token_new;
$obj->data->expiry = date('Y-m-d H:i:s', JwtAuthClass::getTokenExp($token_new));
// }
insert_log($u->id, $obj->data->token,"","login success: ".$login." exp: ".$obj->data->expiry);
unset($obj->data->token_creation_timestamp);
} else {
$obj->rc = -2;
$obj->msg = "incorrect password";
}
}
return $obj;
}
$login = $_REQUEST['login'];
$password = $_REQUEST['password'];
$push_id = $_REQUEST['push_id'];
$push_id = empty($push_id) ? "" : $push_id;
$rv = secu_do_login_4api($login, $password, $push_id);
// insert_log(0, "", 0, get_request_string(), "登入:rc=" . $rv->rc . ",msg=" . $rv->msg);
echo json_encode($rv);