forked from pi-hole/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
215 lines (182 loc) · 6.17 KB
/
api.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license
*/
$api = true;
require_once 'scripts/pi-hole/php/password.php';
require_once 'scripts/pi-hole/php/FTL.php';
require_once 'scripts/pi-hole/php/database.php';
require_once 'scripts/pi-hole/php/auth.php';
check_cors();
$data = array();
// Common API functions
if (isset($_GET['enable']) && $auth) {
if (isset($_GET['auth'])) {
if ($_GET['auth'] !== $pwhash) {
exit('Not authorized!');
}
} else {
// Skip token validation if explicit auth string is given
check_csrf($_GET['token']);
}
pihole_execute('enable');
$data = array_merge($data, array('status' => 'enabled'));
if (file_exists('../custom_disable_timer')) {
unlink('../custom_disable_timer');
}
} elseif (isset($_GET['disable']) && $auth) {
if (isset($_GET['auth'])) {
if ($_GET['auth'] !== $pwhash) {
exit('Not authorized!');
}
} else {
// Skip token validation if explicit auth string is given
check_csrf($_GET['token']);
}
$disable = intval($_GET['disable']);
// intval returns the integer value on success, or 0 on failure
if ($disable > 0) {
$timestamp = time();
pihole_execute('disable '.$disable.'s');
file_put_contents('../custom_disable_timer', ($timestamp + $disable) * 1000);
} else {
pihole_execute('disable');
if (file_exists('../custom_disable_timer')) {
unlink('../custom_disable_timer');
}
}
$data = array_merge($data, array('status' => 'disabled'));
} elseif (isset($_GET['versions'])) {
// Determine if updates are available for Pi-hole
// using the same script that we use for the footer
// on the dashboard (update notifications are
// suppressed if on development branches)
require 'scripts/pi-hole/php/update_checker.php';
$updates = array('core_update' => $core_update,
'web_update' => $web_update,
'FTL_update' => $FTL_update, );
$current = array('core_current' => $core_current,
'web_current' => $web_current,
'FTL_current' => $FTL_current, );
$latest = array('core_latest' => $core_latest,
'web_latest' => $web_latest,
'FTL_latest' => $FTL_latest, );
$branches = array('core_branch' => $core_branch,
'web_branch' => $web_branch,
'FTL_branch' => $FTL_branch, );
if (isset($versions['DOCKER_VERSION'])) {
// Docker info is available only inside containers
$updates['docker_update'] = $docker_update;
$current['docker_current'] = $docker_current;
$latest['docker_latest'] = $docker_latest;
}
$data = array_merge($data, $updates);
$data = array_merge($data, $current);
$data = array_merge($data, $latest);
$data = array_merge($data, $branches);
} elseif (isset($_GET['setTempUnit'])) {
$unit = strtolower($_GET['setTempUnit']);
if ($unit == 'c' || $unit == 'f' || $unit == 'k') {
pihole_execute('-a -'.$unit);
$result = 'success';
} else {
// invalid unit
$result = 'error';
}
$data = array_merge($data, array('result' => $result));
} elseif (isset($_GET['list'])) {
if (!$auth) {
exit('Not authorized!');
}
if (!isset($_GET['list'])) {
exit('List has not been specified.');
}
switch ($_GET['list']) {
case 'black':
$_POST['type'] = LISTTYPE_BLACKLIST;
break;
case 'regex_black':
$_POST['type'] = LISTTYPE_REGEX_BLACKLIST;
break;
case 'white':
$_POST['type'] = LISTTYPE_WHITELIST;
break;
case 'regex_white':
$_POST['type'] = LISTTYPE_REGEX_WHITELIST;
break;
default:
exit('Invalid list [supported: black, regex_black, white, regex_white]');
}
if (isset($_GET['add'])) {
// Set POST parameters and invoke script to add domain to list
$_POST['domain'] = $_GET['add'];
$_POST['action'] = 'add_domain';
require 'scripts/pi-hole/php/groups.php';
} elseif (isset($_GET['sub'])) {
// Set POST parameters and invoke script to remove domain from list
$_POST['domain'] = $_GET['sub'];
$_POST['action'] = 'delete_domain_string';
require 'scripts/pi-hole/php/groups.php';
} else {
// Set POST parameters and invoke script to get all domains
$_POST['action'] = 'get_domains';
require 'scripts/pi-hole/php/groups.php';
}
return;
} elseif (isset($_GET['customdns']) && $auth) {
if (isset($_GET['auth'])) {
if ($_GET['auth'] !== $pwhash) {
exit('Not authorized!');
}
} else {
// Skip token validation if explicit auth string is given
check_csrf($_GET['token']);
}
switch ($_GET['action']) {
case 'get':
$data = echoCustomDNSEntries();
break;
case 'add':
$data = addCustomDNSEntry();
break;
case 'delete':
$data = deleteCustomDNSEntry();
break;
default:
exit('Wrong action');
}
} elseif (isset($_GET['customcname']) && $auth) {
if (isset($_GET['auth'])) {
if ($_GET['auth'] !== $pwhash) {
exit('Not authorized!');
}
} else {
// Skip token validation if explicit auth string is given
check_csrf($_GET['token']);
}
switch ($_GET['action']) {
case 'get':
$data = echoCustomCNAMEEntries();
break;
case 'add':
$data = addCustomCNAMEEntry();
break;
case 'delete':
$data = deleteCustomCNAMEEntry();
break;
default:
exit('Wrong action');
}
}
// Other API functions
require 'api_FTL.php';
header('Content-type: application/json');
if (isset($_GET['jsonForceObject'])) {
echo json_encode($data, JSON_FORCE_OBJECT);
} else {
echo json_encode($data);
}