This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
TemplateResource.class.php
317 lines (290 loc) · 8.96 KB
/
TemplateResource.class.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* Copyright (c) 2011 Cyso Managed Hosting < development [at] cyso . nl >
*
* This file is part of TonicDNS.
*
* TonicDNS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TonicDNS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TonicDNS. If not, see <http://www.gnu.org/licenses/>.
*
* @package resources
* @license http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Template Resource.
* @uri /template
* @uri /template/:identifier
*/
class TemplateResource extends TokenResource {
/**
* Retrieves an existing DNS template.
*
* ### Response: ###
*
* ~~~
* [
* {
* "identifier": <string>,
* "description": <string>,
* "entries": [ {
* "name": <string>,
* "type": <string>,
* "content": <string>,
* "ttl": <int>,
* "priority": <int>
* },0..n ]
* },0..n
* ]
* ~~~
*
* ### Errors (request without identifier): ###
*
* * 500 - Failed to connect to database or query execution error.
*
* ### Errors (request with identifier): ###
*
* * 500 - Failed to connect to database or query execution error.
* * 404 - Could not find template.
*
* @access public
* @param mixed $request Request parameters
* @param string $identifier Template identifier
* @return Response DNS template data if successful, error message otherwise.
*/
public function get($request, $identifier = null) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if (empty($identifier)) {
return TemplateFunctions::get_all_templates($response);
} else {
$validator = new TemplateValidator();
$validator->identifier = $identifier;
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
return TemplateFunctions::get_template($response, $identifier);
}
}
/**
* Create a new DNS template.
*
* ### Request: ###
*
* ~~~
* {
* "identifier": <string>,
* "description": <string>,
* "entries": [ {
* "name": <string>,
* "type": <string>,
* "content": <string>,
* "ttl": <int>,
* "priority": <int>
* },0..n ]
* }
* ~~~
*
* ### Response: ###
*
* ~~~
* true
* ~~~
*
* ### Errors: ###
*
* * 508 - Invalid request, missing required parameters or input validation failed.
* * 500 - Failed to connect to database or query execution error.
* * 409 - Template already exists.
*
* @access public
* @param mixed $request Request parameters
* @return Response True if request was successful, error message otherwise.
*/
public function put($request) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if ($data == null) {
$response->code = Response::BADREQUEST;
$response->error = "Request body was malformed. Ensure the body is in valid format.";
$response->error_detail = "BODY_MALFORMED";
return $response;
}
if (!isset($data->identifier) || !isset($data->description) || !isset($data->entries) || empty($data->entries)) {
$response->code = Response::BADREQUEST;
$response->error = "Identifier, description and/or entries were missing or invalid. Ensure that the body is in valid format and all required parameters are present.";
$response->error_detail = "MISSING_REQUIRED_PARAMETERS";
return $response;
}
$validator = new TemplateValidator($data);
$validator->mode_override = "add";
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
return TemplateFunctions::create_template($response, $data);
}
/**
* Update an existing DNS template. This method will overwrite the entire Template.
*
* ### Request: ###
*
* ~~~
* {
* "identifier": <string>,
* "description": <string>,
* "entries": [ {
* "name": <string>,
* "type": <string>,
* "content": <string>,
* "ttl": <int optional>,
* "priority": <int optional>
* },0..n ]
* }
* ~~~
*
* ### Response: ###
*
* ~~~
* true
* ~~~
*
* ### Errors: ###
*
* * 508 - Invalid request, missing required parameters or input validation failed.
* * 500 - Failed to connect to database or query execution error.
* * 404 - Could not find template.
*
* @access public
* @param mixed $request Request parameters
* @param string $identifier Template identifier
* @return Response True if request was successful, error message otherwise.
*/
public function post($request, $identifier = null) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if ($data == null) {
$response->code = Response::BADREQUEST;
$response->error = "Request body was malformed. Ensure the body is in valid format.";
$response->error_detail = "BODY_MALFORMED";
return $response;
}
if (empty($identifier) || !isset($data->identifier) || !isset($data->entries) || empty($data->entries)) {
$response->code = Response::BADREQUEST;
$response->error = "Identifier and/or entries were missing or invalid. Ensure that the body is in valid format and all required parameters are present.";
$response->error_detail = "MISSING_REQUIRED_PARAMETERS";
return $response;
}
$validator = new TemplateValidator($data);
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
return TemplateFunctions::modify_template($response, $identifier, $data);
}
/**
* Delete an existing DNS template.
*
* ### Response: ###
*
* ~~~
* true
* ~~~
*
* ### Errors: ###
*
* * 508 - Invalid request, missing required parameters or input validation failed.
* * 500 - Failed to connect to database or query execution error.
* * 404 - Could not find template.
*
* @access public
* @param mixed $request Request parameters
* @param string $identifier Template identifier
* @return Response True if template was deleted, error message otherwise.
*/
public function delete($request, $identifier = null) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if (empty($identifier)) {
$response->code = Response::BADREQUEST;
$response->error = "Identifier and/or entries were missing or invalid. Ensure that the body is in valid format and all required parameters are present.";
$response->error_detail = "BODY_MALFORMED";
return $response;
}
$validator = new TemplateValidator();
$validator->identifier = $identifier;
if (!$validator->validates()) {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
return $response;
}
return TemplateFunctions::delete_template($response, $identifier);
}
/**
* Validates the properties of a template.
*
* To validator Template records, use the RecordResource.
*
* ### Request: ###
*
* ~~~
* {
* "identifier": <string>,
* "description": <string>
* }
* ~~~
*
* ### Response: ###
*
* ~~~
* true
* ~~~
*
* ### Errors: ###
*
* * 508 - Invalid request, missing required parameters or input validation failed.
*
* @access public
* @param mixed $request Request parameters
* @return Response True if record is valid, error message with parse errors otherwise.
*/
public function validate($request) {
$response = new FormattedResponse($request);
$data = $request->parseData();
if (empty($data) || !isset($data->identifier) || !isset($data->description)) {
$response->code = Response::BADREQUEST;
$response->error = "Request body was malformed. Ensure that all mandatory properties have been set.";
$response->error_detail = "BODY_MALFORMED";
return $response;
}
$validator = new TemplateValidator($data);
if ($validator->validates()) {
$response->code = Response::OK;
$response->body = true;
$response->log_message = "Template was successfully validated.";
} else {
$response->code = Response::BADREQUEST;
$response->error = $validator->getFormattedErrors();
$response->error_detail = $validator->getErrorDetails();
}
return $response;
}
}
?>