-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-code.php
46 lines (42 loc) · 1.27 KB
/
get-code.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
<?php
include("config.php");
include("common.php");
spl_autoload_register(function($classes){
include 'classes/'.$classes.".php";
});
$cache = new Cache();
header('Content-type: application/json; charset=utf-8');
if ($cache->cleanUp($cachePath)) {
$useCode = generateUniqueCode(5, $cachePath);
} else {
die("{\"error\":\"Unable to perform code management due to server error.\"}");
}
if ($useCode) {
$codeObj = new stdClass;
$codeObj->code = $useCode;
$codeObj->useUrl = $useUrl . "activate";
if ($cache->createLoginCache($codeObj, $cachePath)) {
echo json_encode($codeObj);
} else {
die("{\"error\":\"Unable to generate code due to server error.\"}");
}
} else {
die("{\"error\":\"Unable to read codes due to server error.\"}");
}
function generateUniqueCode($length, $cachePath) {
$useCode = "";
do {
$useCode = generateCode($length);
} while (is_file($cachePath . $useCode . ".json"));
return $useCode;
}
function generateCode($length) {
$characters = 'BCDFGHJKLMNPQRSTUVWXZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
?>