forked from opentibiabr/myaac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_account_number.php
51 lines (45 loc) · 1 KB
/
generate_account_number.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
<?php
/**
* Account Number Generator
* Returns json with result
*
* @package MyAAC
* @author Slawkens <[email protected]>
* @author OpenTibiaBR
* @copyright 2023 MyAAC
* @link https://github.com/opentibiabr/myaac
*/
// we need some functions
require '../common.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
$hasNumberColumn = $db->hasColumn('accounts', 'number');
do {
$length = 10;
$min = (int)(1 . str_repeat(0, $length - 1));
$max = (int)str_repeat(9, $length);
try {
$number = random_int($min, $max);
} catch (Exception $e) {
error_('');
}
$query = $db->query('SELECT `id` FROM `accounts` WHERE `' . ($hasNumberColumn ? 'number' : 'id') . '` = ' . $db->quote($number));
} while($query->rowCount() >= 1);
success_($number);
/**
* Output message & exit.
*
* @param string $desc Description
*/
function success_($desc) {
echo json_encode([
'success' => $desc
]);
exit();
}
function error_($desc) {
echo json_encode([
'error' => $desc
]);
exit();
}