-
Notifications
You must be signed in to change notification settings - Fork 24
/
function.php
57 lines (47 loc) · 1.49 KB
/
function.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
55
56
57
<?php
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle) {
return $needle !== '' && substr($haystack, -strlen($needle)) === (string)$needle;
}
}
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
if (!function_exists('filterInputPostGet')) {
function filterInputPostGet($name, $default = null) {
return filter_input(INPUT_POST, $name, FILTER_SANITIZE_STRING) ?? filter_input(INPUT_GET, $name, FILTER_SANITIZE_STRING) ?? $default;
}
}
function getIP() {
$manual_ip = filterInputPostGet('ip');
if (empty($manual_ip)) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
$ip = trim(explode(',', $ip)[0]);
}
else {
$ip = $manual_ip;
}
if ( ! filter_var($ip, FILTER_VALIDATE_IP)) {
throw new Exception('No valid IP');
}
return $ip;
}
function getRecordType($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$recordType = 'A';
}
else if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$recordType = 'AAAA';
}
else {
throw new Exception('Unknown IP type');
}
return $recordType;
}