forked from martinlowinski/php-dyndns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
106 lines (89 loc) · 2.35 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/* Wrapper for curl */
function requestCurl($data) {
$ch = curl_init(HOST);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!$data = curl_exec($ch)) {
trigger_error('Curl execution error.' . curl_error($ch), E_USER_ERROR);
return false;
}
curl_close($ch);
return $data;
}
function validIP($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return true;
}
return false;
}
function validIPv6($ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return true;
}
return false;
}
function validDomain($domain) {
return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain) //valid chars check
&& preg_match('/^.{1,253}$/', $domain) //overall length check
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain)); //length of each label
}
function configuredDomain($domain) {
if (defined('DOMAINS')) {
$domains = unserialize(DOMAINS);
return array_key_exists($domain, $domains);
} else {
return $domain == DOMAIN;
}
}
function validCred($pass) {
if ($pass == REMOTE_PASS) {
return true;
}
return false;
}
/* Respond with status & message in JSON */
function respond($status, $msg = '') {
header('Content-type: application/json');
$response = array();
$response['status'] = $status;
if (!empty($msg)) {
$response['msg'] = $msg;
}
$out = json_encode($response);
if ($status != 'success') {
$response['msg'] .= PHP_EOL . 'See ' . LOG . ' for additional information.';
file_put_contents('php://stderr', $out);
exit(1);
} else {
echo $out;
exit();
}
}
/* Custom error handler to log to file */
function dyndns_error_handler($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting
return;
}
$date = date(DATE_W3C);
$str = '';
switch ($errno) {
case E_USER_ERROR:
$str .= "$date ERROR [$errno]: $errstr, Fatal error on line $errline in file $errfile";
break;
case E_USER_WARNING:
$str .= "$date WARNING [$errno]: $errstr\n";
break;
case E_USER_NOTICE:
$str .= "$date NOTICE [$errno]: $errstr\n";
break;
default:
$str .= "$date Unknown error type: [$errno] $errstr\n";
break;
}
file_put_contents(LOG, $str, FILE_APPEND);
/* Don't execute PHP internal error handler */
return true;
}