-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
54 lines (42 loc) · 1.13 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
(c) Copyright 2019, Pongsakorn Ritrugsa <[email protected]>
This project is licensed under the terms of the MIT license.
*/
function make_bad_request($msg) {
http_response_code(400);
echo json_encode(['message' => $msg]);
}
function get_idcard_lastdigit($idcard) {
// if too long or too short
if(strlen($idcard) > 13 || strlen($idcard) < 12) return -1;
// calculate for check digit
$sum = 0;
for($i = 0; $i < 12; $i++) {
$sum += (int)$idcard[$i] * (13 - $i);
}
$mod = $sum % 11;
$check_digit = (11 - $mod) % 10;
return $check_digit;
}
function get_generated_idcard() {
// start with 9 is invalid in the real world.
$idcard = "9";
// append with the 11-digit random numbers
for ($i=1; $i <= 11; $i++) {
$idcard .= (string)rand(0, 9);
}
// append with the last digit
$idcard .= (string)get_idcard_lastdigit($idcard);
return $idcard;
}
function is_idcard_valid($idcard) {
if (!is_13digits($idcard)) return false;
return (get_idcard_lastdigit($idcard) == $idcard[12]);
}
function is_13digits($str) {
preg_match('/^\d{13,13}$/', $str, $regex_matches);
if (empty($regex_matches))
return false;
return true;
}