File: /var/www/html/keytest/wp-content/themes/ormedia/page-unit-user.php
<?php
//Template Name: unit user reg
global $wpdb;
function encrypt($data) {
$key_256bit = "FlVKQooXnOso8fNiN/cJIz6z8hrFqyo+ErnOtuQ6Up0=";
// Remove the base64 encoding from our key
$encryption_key = base64_decode($key_256bit);
// Generate an initialization vector
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
// Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
// The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
$handled = bin2hex(base64_decode($encrypted));
return (bin2hex($iv).$handled);
}
function decrypt($data) {
$key_256bit = "FlVKQooXnOso8fNiN/cJIz6z8hrFqyo+ErnOtuQ6Up0=";
// Remove the base64 encoding from our key
$encryption_key = base64_decode($key_256bit);
$iv = hex2bin(substr($data,0,32));
$encrypted_data = hex2bin(substr($data,32,strlen($data)-32));
$handled_data = base64_encode($encrypted_data);
$decrypted = openssl_decrypt($handled_data, 'aes-256-cbc', $encryption_key, 0, $iv);
return $decrypted;
}
if($_REQUEST['action'] == 'submit') {
$submit_uid = $_REQUEST['uid'];
$submit_username = $_REQUEST['username'];
$has_user = $wpdb->get_var("SELECT `id` FROM keyman_user where `username` = '$submit_username' limit 1");
$has_user2 = $wpdb->get_var("SELECT `id` FROM keyman_user where `uid` = '$submit_uid' limit 1");
if($has_user > 0 || $has_user2 > 0) {
exit(json_encode([
'rc' => 1
]));
}else {
$res = $wpdb->insert("keyman_user", [
'uid' => $_REQUEST['uid'],
'nickname' => $_REQUEST['nickname'],
'username' => $_REQUEST['username'],
'password' => md5($_REQUEST['password'])
]);
if($res !== false) {
$wpdb->update("keyman_user", [
'owner' => $wpdb->insert_id
], [
'id' => $wpdb->insert_id
]);
exit(json_encode([
'rc' => 0
]));
}else {
exit(json_encode([
'rc' => 2
]));
}
}
}else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KEYMANA UNIT REG</title>
<script src="<?php echo (get_template_directory_uri().'/js/jquery.js')?>"></script>
<style>
.input-line {
font-size: 16px;
color: white;
margin-bottom: 20px;
}
.item-name {
font-weight: 600;
}
* {
margin: 0;
padding: 0;
}
.inputs {
outline: none;
border: 0px;
background-color: rgba(16, 142, 233, 1);
font-size: 16px;
width: 200px;
color: #fff;
border-bottom: 1px solid #fff;
}
.display-none {
display: none !important;
}
</style>
</head>
<body>
<div id="panel" style="width: 100vw;height: 100vh;background-color: rgba(16, 142, 233, 1);display:flex; flex-direction: column;align-items: center;justify-content: center;">
<?php
$unit_id = $_GET['unit_id'];
$unit_id = decrypt($unit_id);
$has_unit = false;
if($unit_id > 0) {
$unit_info = $wpdb->get_row("SELECT a.unit_id, a.block, a.floor, a.unit, b.name_zh from keylab_property_unit_list as a left join keylab_property as b on a.property_id = b.property_id where a.unit_id = $unit_id limit 1");
$has_user = $wpdb->get_var("SELECT `uid` FROM keyman_user where `uid` = $unit_id limit 1");
if(!empty($unit_info) && $has_user != $unit_id) {
$has_unit = true;
}
}
if($has_unit) {
?>
<div class="input-line">
<span class="item-name">單位信息:</span>
<span>
<?php echo $unit_info->name_zh ?> <?php echo $unit_info->block ?> <?php echo $unit_info->floor ?>層 <?php echo $unit_info->unit ?>號
</span>
</div>
<div class="input-line">
<span class="item-name">用戶暱稱:</span>
<span>
<input id="nickname" autocomplete="off" class="inputs" type="text" maxlength="30" />
</span>
</div>
<div class="input-line">
<span class="item-name">
手機號碼:
</span>
<span>
<input id="username" autocomplete="off" class="inputs" type="text" maxlength="30" />
</span>
<br/>
<span class="item-name" style="font-size: 10px;">(用於登入KeyMana App)</span>
</div>
<div class="input-line">
<span class="item-name">登錄密碼:</span>
<span>
<input id="password" autocomplete="off" class="inputs" type="password" maxlength="30" />
</span>
</div>
<div class="input-line">
<span class="item-name">確認密碼:</span>
<span>
<input id="repassword" autocomplete="off" class="inputs" type="password" maxlength="30" />
</span>
</div>
<div style="display: flex;justify-content: center;">
<div onclick="submit()" style="cursor:pointer;border-radius: 5px;display: flex; align-items: center;justify-content: center;width: 100px;height: 40px;background-color: white;color: rgba(16, 142, 233, 1);">
註冊
</div>
</div>
<?php
}else {
?>
<div class="input-line">
<span class="item-name">無法找到對應單位或該單位已註冊</span>
</div>
<?php
}
?>
</div>
<div id="success" class="display-none" style="width: 100vw;height: 100vh;background-color: rgba(16, 142, 233, 1);display:flex; flex-direction: column;align-items: center;justify-content: center;">
<div class="input-line">
<span class="item-name">註冊成功</span>
</div>
</div>
<script>
function submit() {
let uid = <?php echo $unit_id; ?>;
let nickname = $('#nickname').val();
let username = $('#username').val();
let password = $('#password').val();
let repassword = $('#repassword').val();
let obj = {
action: 'submit',
uid,
nickname,
username,
password,
repassword
}
for(let key in obj) {
console.log(obj[key])
if(obj[key].length < 6) {
alert('請檢查所填項長度不小於6位')
return false
}
}
if(password !== repassword) {
alert('密碼不一致')
return false
}
$.post(
'http://keylab.cc/unit-reg',
obj,
function(data){
let res = JSON.parse(data)
console.log(res.rc)
if(res.rc == 0) {
$('#panel').addClass('display-none')
$('#success').removeClass()
}
if(res.rc == 1) {
alert('該手機號已存在')
}
if(res.rc == 2) {
alert('註冊失敗請重試')
}
}
)
}
</script>
</body>
</html>
<?php } ?>