-
Notifications
You must be signed in to change notification settings - Fork 1
/
start_bind9_api.php
725 lines (641 loc) · 24.8 KB
/
start_bind9_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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
// Include the Swoole extension
if (!extension_loaded('swoole')) {
die('Swoole extension must be installed');
}
require_once 'helpers.php';
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Badcow\DNS\Classes;
use Badcow\DNS\Zone;
use Badcow\DNS\Rdata\Factory;
use Badcow\DNS\ResourceRecord;
use Badcow\DNS\AlignedBuilder;
use Namingo\Rately\Rately;
// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$logFilePath = '/var/log/namingo/bind9_api.log';
$log = setupLogger($logFilePath, 'BIND9_API');
// Initialize the PDO connection pool
$pool = new Swoole\Database\PDOPool(
(new Swoole\Database\PDOConfig())
->withDriver($_ENV['DB_TYPE'])
->withHost($_ENV['DB_HOST'])
->withPort($_ENV['DB_PORT'])
->withDbName($_ENV['DB_DATABASE'])
->withUsername($_ENV['DB_USERNAME'])
->withPassword($_ENV['DB_PASSWORD'])
->withCharset('utf8mb4')
);
// Handler Functions
function handleLogin($request, $pdo) {
$body = json_decode($request->rawContent(), true);
$username = trim($body['username'] ?? '');
$password = $body['password'] ?? '';
if (empty($username) || empty($password)) {
return [400, ['error' => 'Username and password are required']];
}
try {
// Prepare statement to fetch user securely
$stmt = $pdo->prepare('SELECT id, password FROM users WHERE username = :username LIMIT 1');
$stmt->execute(['username' => $username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// Check if user exists and verify password
if (!$user || !password_verify($password, $user['password'])) {
// Authentication failed
return [401, ['error' => 'Invalid credentials']];
}
// Generate secure session token
$token = bin2hex(random_bytes(32)); // Generates a 64-character token
// Store session token in the database
$stmt = $pdo->prepare('
INSERT INTO sessions (user_id, token, ip_address, user_agent, created_at, expires_at)
VALUES (:user_id, :token, :ip_address, :user_agent, NOW(), DATE_ADD(NOW(), INTERVAL 1 HOUR))
');
$ipAddress = inet_pton($request->server['remote_addr'] ?? ''); // Store IP address in binary format
$userAgent = substr($request->header['user-agent'] ?? '', 0, 255); // Limit user agent length to 255 characters
$stmt->execute([
'user_id' => $user['id'],
'token' => $token,
'ip_address' => $ipAddress,
'user_agent' => $userAgent
]);
// Return session token to the client
return [200, ['token' => $token]];
} catch (Exception $e) {
// Log the exception internally without exposing details to the client
error_log('Login error: ' . $e->getMessage());
return [500, ['error' => 'Internal server error']];
}
}
function handleGetZones() {
$zoneDir = $_ENV['BIND9_ZONE_DIR'];
$files = glob("$zoneDir/*.zone");
$zones = array_map(function($file) {
return basename($file, '.zone');
}, $files);
return [200, ['zones' => $zones]];
}
/**
* Handle adding a new zone.
*
* @param Swoole\Http\Request $request
* @return array [status_code, response_body]
*/
function handleAddZone($request, $pdo) {
$body = json_decode($request->rawContent(), true);
$zoneName = trim($body['zone'] ?? '');
if (!$zoneName) {
return [400, ['error' => 'Zone name is required']];
}
// Validate zone name (basic validation)
if (!preg_match('/^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $zoneName)) {
return [400, ['error' => 'Invalid zone name format']];
}
$zoneDir = $_ENV['BIND9_ZONE_DIR'];
$zoneFile = "$zoneDir/$zoneName.zone";
if (file_exists($zoneFile)) {
return [409, ['error' => 'Zone already exists']];
}
// Create the zone using Badcow\DNS
try {
$zone = new Zone($zoneName.'.');
$serialNumber = getCurrentSerialNumber($pdo, $zoneName);
if (!$serialNumber) {
// No serial number found, insert the initial one
$serialNumber = insertInitialSerialNumber($pdo, $zoneName);
} else {
// Update the serial number
$serialNumber = updateSerialNumber($pdo, $zoneName);
}
// Add default SOA and NS records
$soa = new ResourceRecord;
$soa->setName('@');
$soa->setClass(Classes::INTERNET);
$soa->setRdata(Factory::Soa(
$_ENV['NS1'],
$_ENV['SOA_EMAIL'],
$serialNumber,
$_ENV['REFRESH'],
$_ENV['RETRY'],
$_ENV['EXPIRE'],
$_ENV['MIN_TTL']
));
$zone->addResourceRecord($soa);
// Add NS records (NS1 to NS13)
for ($i = 1; $i <= 13; $i++) {
$nsKey = 'NS' . $i;
if (isset($_ENV[$nsKey])) {
$nsRecord = new ResourceRecord;
$nsRecord->setName('@');
$nsRecord->setClass(Classes::INTERNET);
$nsRecord->setRdata(Factory::Ns($_ENV[$nsKey]));
$zone->addResourceRecord($nsRecord);
}
}
saveZone($zone);
} catch (Exception $e) {
return [500, ['error' => 'Failed to create zone file: ' . $e->getMessage()]];
}
// Update named.conf.local
try {
addZoneToConfig($zoneName, $zoneFile);
} catch (Exception $e) {
// Clean up by removing the created zone file
unlink($zoneFile);
return [500, ['error' => 'Failed to update named.conf.local: ' . $e->getMessage()]];
}
// Reload BIND9 to apply changes
try {
reloadBIND9();
} catch (Exception $e) {
return [500, ['error' => 'Failed to reload BIND9: ' . $e->getMessage()]];
}
return [201, ['message' => 'Zone created successfully']];
}
/**
* Handle deleting an existing zone.
*
* @param string $zoneName
* @return array [status_code, response_body]
*/
function handleDeleteZone($zoneName) {
$zoneName = trim($zoneName);
if (!$zoneName) {
return [400, ['error' => 'Zone name is required']];
}
// Validate zone name (basic validation)
if (!preg_match('/^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $zoneName)) {
return [400, ['error' => 'Invalid zone name format']];
}
$zoneDir = $_ENV['BIND9_ZONE_DIR'];
$zoneFile = "$zoneDir/$zoneName.zone";
if (!file_exists($zoneFile)) {
return [404, ['error' => 'Zone file does not exist']];
}
// Remove zone block from named.conf.local
try {
removeZoneFromConfig($zoneName);
} catch (Exception $e) {
return [500, ['error' => 'Failed to update named.conf.local: ' . $e->getMessage()]];
}
// Delete the zone file
if (!unlink($zoneFile)) {
return [500, ['error' => 'Failed to delete zone file']];
}
// Reload BIND9 to apply changes
try {
reloadBIND9();
} catch (Exception $e) {
return [500, ['error' => 'Failed to reload BIND9: ' . $e->getMessage()]];
}
return [200, ['message' => 'Zone deleted successfully']];
}
function handleGetRecords($zoneName) {
try {
$zone = loadZone($zoneName);
} catch (Exception $e) {
return [404, ['error' => $e->getMessage()]];
}
$records = [];
foreach ($zone->getResourceRecords() as $record) {
$records[] = [
'name' => $record->getName(),
'type' => $record->getType(),
'ttl' => $record->getTtl(),
'rdata' => $record->getRdata()->toText()
];
}
return [200, ['records' => $records]];
}
function handleAddRecord($zoneName, $request) {
try {
$zone = loadZone($zoneName);
} catch (Exception $e) {
return [404, ['error' => $e->getMessage()]];
}
$body = json_decode($request->rawContent(), true);
$name = $body['name'] ?? '';
$type = strtoupper($body['type'] ?? '');
$ttl = $body['ttl'] ?? 3600;
$rdata = $body['rdata'] ?? '';
if (!$name || !$type || !$rdata) {
return [400, ['error' => 'Missing required fields']];
}
// Check if the record already exists in the zone
foreach ($zone->getResourceRecords() as $existingRecord) {
if ($existingRecord->getName() === $name && $existingRecord->getRdata()->getType() === $type) {
// Compare the Rdata based on type
switch ($type) {
case 'A':
case 'AAAA':
if ($existingRecord->getRdata()->getAddress() === $rdata) {
return [400, ['error' => 'Record already exists']];
}
break;
case 'CNAME':
case 'NS':
case 'PTR':
if ($existingRecord->getRdata()->getTarget() === $rdata) {
return [400, ['error' => 'Record already exists']];
}
break;
case 'MX':
if ($existingRecord->getRdata()->getExchange() === $rdata['exchange'] &&
$existingRecord->getRdata()->getPreference() == $rdata['preference']) {
return [400, ['error' => 'Record already exists']];
}
break;
case 'SOA':
$soa = $existingRecord->getRdata();
if ($soa->getMname() === $rdata['mname'] &&
$soa->getRname() === $rdata['rname'] &&
$soa->getSerial() == $rdata['serial'] &&
$soa->getRefresh() == $rdata['refresh'] &&
$soa->getRetry() == $rdata['retry'] &&
$soa->getExpire() == $rdata['expire'] &&
$soa->getMinimum() == $rdata['minimum']) {
return [400, ['error' => 'Record already exists']];
}
break;
case 'SPF':
case 'TXT':
if ($existingRecord->getRdata()->getText() === $rdata) {
return [400, ['error' => 'Record already exists']];
}
break;
case 'DS':
if ($existingRecord->getRdata()->getKeyTag() == $rdata['keytag'] &&
$existingRecord->getRdata()->getAlgorithm() == $rdata['algorithm'] &&
$existingRecord->getRdata()->getDigestType() == $rdata['digestType'] &&
$existingRecord->getRdata()->getDigest() === $rdata['digest']) {
return [400, ['error' => 'Record already exists']];
}
break;
default:
return [400, ['error' => 'Unsupported record type']];
}
}
}
// Create a new DNS resource record
$record = new ResourceRecord;
$record->setName($name);
if (is_numeric($ttl)) {
$record->setTtl($ttl);
}
$record->setClass(Classes::INTERNET);
// Dynamically create Rdata based on type
try {
// Mapping record types to Factory methods
$factoryMethods = [
'A' => 'A',
'AAAA' => 'AAAA',
'CNAME' => 'CNAME',
'MX' => 'MX',
'NS' => 'NS',
'PTR' => 'PTR',
'SOA' => 'SOA',
'TXT' => 'TXT',
'SPF' => 'SPF',
'DS' => 'DS',
];
$normalizedType = strtoupper($type);
if (!isset($factoryMethods[$normalizedType])) {
return [400, ['error' => 'Unsupported record type']];
}
$methodName = $factoryMethods[$normalizedType];
if ($type === 'MX') {
$preference = $rdata['preference'];
$exchange = $rdata['exchange'];
$rdataInstance = \Badcow\DNS\Rdata\Factory::MX($preference, $exchange);
} else if ($type === 'DS') {
$keytag = $rdata['keytag'];
$algorithm = $rdata['algorithm'];
$digestType = $rdata['digestType'];
$digest = $rdata['digest'];
$rdataInstance = \Badcow\DNS\Rdata\Factory::DS($keytag, $algorithm, hex2bin($digest), $digestType);
} else {
$rdataInstance = \Badcow\DNS\Rdata\Factory::$methodName($rdata);
}
$record->setRdata($rdataInstance);
} catch (Exception $e) {
return [400, ['error' => 'Invalid RDATA: ' . $e->getMessage()]];
}
// Add the record to the zone and save
$zone->addResourceRecord($record);
saveZone($zone);
reloadBIND9();
return [201, ['message' => 'Record added successfully']];
}
/**
* Handle updating an existing DNS record.
*
* @param string $zoneName The name of the DNS zone.
* @param Swoole\Http\Request $request The HTTP request containing update details.
* @return array [status_code, response_body]
*/
function handleUpdateRecord($zoneName, $request) {
try {
$zone = loadZone($zoneName);
} catch (Exception $e) {
return [404, ['error' => $e->getMessage()]];
}
$body = json_decode($request->rawContent(), true);
// Extract identifying information for the record to be updated
$currentName = trim($body['current_name'] ?? '');
$currentType = strtoupper(trim($body['current_type'] ?? ''));
$currentRdata = trim($body['current_rdata'] ?? '');
// Extract new data for the record
$newName = trim($body['new_name'] ?? $currentName);
$newTtl = isset($body['new_ttl']) ? intval($body['new_ttl']) : 3600;
$newRdata = trim($body['new_rdata'] ?? $currentRdata);
$newComment = trim($body['new_comment'] ?? '');
if (!$currentName || !$currentType || !$currentRdata) {
return [400, ['error' => 'Current record name, type, and rdata are required for identification']];
}
// Find the record by current_name, current_type, and current_rdata
$recordToUpdate = null;
foreach ($zone->getResourceRecords() as $record) {
if (
strtolower($record->getName()) === strtolower($currentName) &&
strtoupper($record->getType()) === strtoupper($currentType) &&
strtolower($record->getRdata()->toText()) === strtolower($currentRdata)
) {
$recordToUpdate = $record;
break;
}
}
if (!$recordToUpdate) {
return [404, ['error' => 'Record not found']];
}
// Update the record with new values
if ($newName) {
$recordToUpdate->setName($newName);
}
if ($newTtl) {
$recordToUpdate->setTtl($newTtl);
}
if ($newRdata) {
// Dynamically create Rdata based on type
try {
// Mapping record types to Factory methods
$factoryMethods = [
'A' => 'A',
'AAAA' => 'AAAA',
'CNAME' => 'CNAME',
'MX' => 'MX',
'NS' => 'NS',
'PTR' => 'PTR',
'SOA' => 'SOA',
'TXT' => 'TXT',
'SPF' => 'SPF',
'DS' => 'DS',
];
$normalizedType = strtoupper($currentType);
if (!isset($factoryMethods[$normalizedType])) {
return [400, ['error' => 'Unsupported record type']];
}
$methodName = $factoryMethods[$normalizedType];
if ($currentType === 'MX') {
$preference = $newRdata['preference'];
$exchange = $newRdata['exchange'];
$rdataInstance = \Badcow\DNS\Rdata\Factory::MX($preference, $exchange);
} else if ($currentType === 'DS') {
$keytag = $newRdata['keytag'];
$algorithm = $newRdata['algorithm'];
$digestType = $newRdata['digestType'];
$digest = $newRdata['digest'];
$rdataInstance = \Badcow\DNS\Rdata\Factory::DS($keytag, $algorithm, hex2bin($digest), $digestType);
} else {
$rdataInstance = \Badcow\DNS\Rdata\Factory::$methodName($newRdata);
}
$record->setRdata($rdataInstance);
} catch (Exception $e) {
return [400, ['error' => 'Invalid RDATA: ' . $e->getMessage()]];
}
}
if ($newComment) {
$recordToUpdate->setComment($newComment);
}
// Save the updated zone
try {
saveZone($zone);
} catch (Exception $e) {
return [500, ['error' => 'Failed to save updated zone: ' . $e->getMessage()]];
}
// Reload BIND9 to apply changes
try {
reloadBIND9();
} catch (Exception $e) {
return [500, ['error' => 'Failed to reload BIND9: ' . $e->getMessage()]];
}
return [200, ['message' => 'Record updated successfully']];
}
/**
* Handle deleting an existing DNS record.
*
* @param string $zoneName The name of the DNS zone.
* @param Swoole\Http\Request $request The HTTP request containing deletion details.
* @return array [status_code, response_body]
*/
function handleDeleteRecord($zoneName, $request) {
try {
$zone = loadZone($zoneName);
} catch (Exception $e) {
return [404, ['error' => $e->getMessage()]];
}
$body = json_decode($request->rawContent(), true);
// Extract identifying information for the record to be deleted
$recordName = trim($body['name'] ?? '');
$recordType = strtoupper(trim($body['type'] ?? ''));
if ($recordType === 'DS' || $recordType === 'MX') {
$recordRdata = $body['rdata'] ?? '';
} else {
$recordRdata = trim($body['rdata'] ?? '');
}
if (!$recordName || !$recordType || !$recordRdata) {
return [400, ['error' => 'Record name, type, and rdata are required for identification']];
}
// Find the record by name, type, and rdata
$recordToDelete = null;
foreach ($zone->getResourceRecords() as $record) {
if (
strtolower($record->getName()) === strtolower($recordName) &&
strtoupper($record->getType()) === strtoupper($recordType)
) {
// Handle DS record comparison
if ($recordType === 'DS') {
$dsRecord = $record->getRdata();
if ($dsRecord->getKeyTag() == $recordRdata['keytag'] &&
$dsRecord->getAlgorithm() == $recordRdata['algorithm'] &&
$dsRecord->getDigestType() == $recordRdata['digestType'] &&
$dsRecord->getDigest() === $recordRdata['digest']) {
$recordToDelete = $record;
break;
}
} elseif ($recordType === 'MX') {
$mxRecord = $record->getRdata();
if ($mxRecord->getExchange() === $recordRdata['exchange'] &&
$mxRecord->getPreference() == $recordRdata['preference']) {
$recordToDelete = $record;
break;
}
} else {
// Handle other record types
if (strtolower($record->getRdata()->toText()) === strtolower($recordRdata)) {
$recordToDelete = $record;
break;
}
}
}
}
if (!$recordToDelete) {
return [404, ['error' => 'Record not found']];
}
// Remove the record from the zone
$zone->remove($recordToDelete);
// Save the updated zone
try {
saveZone($zone);
} catch (Exception $e) {
return [500, ['error' => 'Failed to save updated zone: ' . $e->getMessage()]];
}
// Reload BIND9 to apply changes
try {
reloadBIND9();
} catch (Exception $e) {
return [500, ['error' => 'Failed to reload BIND9: ' . $e->getMessage()]];
}
return [200, ['message' => 'Record deleted successfully']];
}
// Initialize Swoole HTTP Server
$server = new Server("0.0.0.0", 7650);
$server->set([
'daemonize' => false,
'log_file' => '/var/log/namingo/bind9-api.log',
'log_level' => SWOOLE_LOG_INFO,
'worker_num' => swoole_cpu_num() * 2,
'pid_file' => '/var/run/bind9-api.pid',
'max_request' => 1000,
'dispatch_mode' => 1,
'open_tcp_nodelay' => true,
'max_conn' => 1024,
'buffer_output_size' => 2 * 1024 * 1024, // 2MB
'heartbeat_check_interval' => 60,
'heartbeat_idle_time' => 600, // 10 minutes
'package_max_length' => 2 * 1024 * 1024, // 2MB
'reload_async' => true,
'http_compression' => true
]);
$rateLimiter = new Rately();
$log->info('BIND9 api server started at http://127.0.0.1:7650');
$server->on("request", function (Request $request, Response $response) use ($pool, $log, $rateLimiter) {
// Set CORS headers if needed
$response->header("Content-Type", "application/json");
// Get a PDO connection from the pool
$pdo = $pool->get();
$remoteAddr = $request->server['remote_addr'];
if (!isIpWhitelisted($remoteAddr, $pdo)) {
if (($_ENV['RATELY'] == true) && ($rateLimiter->isRateLimited('bind9_api', $remoteAddr, $_ENV['RATE_LIMIT'], $_ENV['RATE_PERIOD']))) {
$log->error('rate limit exceeded for ' . $remoteAddr);
$response->header('Content-Type', 'application/json');
$response->status(429);
$response->end(json_encode(['error' => 'Rate limit exceeded. Please try again later.']));
}
}
try {
$path = $request->server['request_uri'];
$method = $request->server['request_method'];
// Routing
// Authentication route does not require auth
if ($path === '/login' && $method === 'POST') {
list($status, $body) = handleLogin($request, $pdo);
$response->status($status);
$response->end(json_encode($body));
return;
}
// All other routes require authentication
$user = authenticate($request, $pdo, $log);
if (!$user) {
$response->status(401);
$response->end(json_encode(['error' => 'Unauthorized']));
return;
}
// Zones Management
if ($path === '/zones') {
if ($method === 'GET') {
list($status, $body) = handleGetZones();
$response->status($status);
$response->end(json_encode($body));
return;
} elseif ($method === 'POST') {
list($status, $body) = handleAddZone($request, $pdo);
$response->status($status);
$response->end(json_encode($body));
return;
}
}
// Delete Zone: DELETE /zones/{zone}
if (preg_match('#^/zones/([^/]+)$#', $path, $matches) && $method === 'DELETE') {
$zoneName = $matches[1];
list($status, $body) = handleDeleteZone($zoneName);
$response->status($status);
$response->end(json_encode($body));
return;
}
// Records Management
// GET /zones/{zone}/records
if (preg_match('#^/zones/([^/]+)/records$#', $path, $matches)) {
$zoneName = $matches[1];
if ($method === 'GET') {
list($status, $body) = handleGetRecords($zoneName);
$response->status($status);
$response->end(json_encode($body));
return;
} elseif ($method === 'POST') {
list($status, $body) = handleAddRecord($zoneName, $request);
$response->status($status);
$response->end(json_encode($body));
return;
}
}
// Update/Delete Record: PUT or DELETE /zones/{zone}/records/{record_id}
if (preg_match('#^/zones/([^/]+)/records/([^/]+)$#', $path, $matches)) {
$zoneName = $matches[1];
$recordId = $matches[2];
if ($method === 'PUT') {
list($status, $body) = handleUpdateRecord($zoneName, $request);
$response->status($status);
$response->end(json_encode($body));
return;
} elseif ($method === 'DELETE') {
list($status, $body) = handleDeleteRecord($zoneName, $request);
$response->status($status);
$response->end(json_encode($body));
return;
}
}
// If no route matched
$log->info('Path Not Found');
$response->status(404);
$response->end(json_encode(['error' => 'Path Not Found']));
} catch (PDOException $e) {
// Handle database exceptions
$log->error('Database error: ' . $e->getMessage());
$response->status(500);
$response->header('Content-Type', 'application/json');
$response->end(json_encode(['Database error:' => $e->getMessage()]));
} catch (Throwable $e) {
// Catch any other exceptions or errors
$log->error('Error: ' . $e->getMessage());
$response->status(500);
$response->header('Content-Type', 'application/json');
$response->end(json_encode(['Error:' => $e->getMessage()]));
} finally {
// Return the connection to the pool
$pool->put($pdo);
}
});
$server->start();