Menu

[r565]: / functions / classes / class.PowerDNS.php  Maximize  Restore  History

Download this file

1330 lines (1177 with data), 35.6 kB

   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
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
<?php
/**
* phpIPAM PowerDNS class to work with PowerDNS
*
* https://wiki.powerdns.com/trac/wiki/fields
*
*/
class PowerDNS extends Common_functions {
/* variables */
public $error = false; // connection error string
public $db_settings; // (obj) db settings
public $defaults; // (obj) defaults settings
public $limit; // number of results
public $orderby; // order field
public $orderdir; // $order direction
public $domain_types; // (obj) types of domain
public $record_types; // (obj) record types
// cache
private $domains_cache = array(); // array of domains - index = id
/* objects */
protected $Database; // Database object - phpipam
protected $Database_pdns; // Database object - pdns
/**
* __construct method
*
* @access public
*/
public function __construct (Database_PDO $Database) {
# initialize Result
$this->Result = new Result ();
# initialize object
$this->Database = $Database;
# Log object
$this->Log = new Logging ($this->Database);
// get settings
$this->get_settings ();
// set database
$this->db_set ();
// set domain types
$this->set_domain_types ();
//set record types
$this->set_record_types ();
// set uery values
$this->set_query_values ();
// set ttl values
$this->set_ttl_values ();
}
/* @database settings ---------- */
/**
* Sets database connection
*
* @access private
* @return void
*/
private function db_set () {
// decode values form powerDNS
$this->db_settings = strlen($this->settings->powerDNS)>10 ? json_decode($this->settings->powerDNS) : json_decode($this->db_set_db_settings ());
// set connection
$this->Database_pdns = new Database_PDO ($this->db_settings->username, $this->db_settings->password, $this->db_settings->host, $this->db_settings->port, $this->db_settings->name);
}
/**
* Sets default values for database connection and othern parameters
*
* @access private
* @return void
*/
private function db_set_db_settings () {
$this->defaults = new StdClass ();
// database
$this->defaults->host = "127.0.0.1";
$this->defaults->name = "pdns";
$this->defaults->username = "pdns";
$this->defaults->password = "pdns";
$this->defaults->port = 3306;
$this->defaults->autoserial = "No";
// default values
$this->defaults->ns = "localhost";
$this->defaults->hostmaster = $this->settings->siteAdminMail;
$this->defaults->refresh = 180;
$this->defaults->retry = 3600;
$this->defaults->expire = 604800;
$this->defaults->nxdomain_ttl = 180;
$this->defaults->ttl = 86400;
// return
return json_encode($this->defaults);
}
/**
* Checks database connection with given parameters
*
* @access public
* @return void
*/
public function db_check () {
try { $this->Database_pdns->connect(); }
catch (Exception $e) {
// error
$this->error = $e->getMessage();
return false;
}
// ok
return true;
}
/**
* Returns Id of last insert
*
* @access public
* @return void
*/
public function get_last_db_id () {
return $this->Database_pdns->lastInsertId ();
}
/**
* Returns domain types
*
* @access private
* @return void
*/
private function set_domain_types () {
$types = array(
"NATIVE",
"MASTER",
"SLAVE",
"SUPERSLAVE"
);
# save
$this->domain_types = (object) $types;
}
/**
* Sets array of record types
*
* https://doc.powerdns.com/md/types/
*
* For now only basic record types are available because of validations.
* If some other record is required uncomment it, note that input will not be validated
*
* @access private
* @return void
*/
private function set_record_types () {
// set array
$record_types[] = "A";
$record_types[] = "AAAA";
$record_types[] = "MX";
$record_types[] = "CNAME";
$record_types[] = "PTR";
$record_types[] = "TXT";
$record_types[] = "NS";
$record_types[] = "SOA";
$record_types[] = "SPF";
// $record_types[] = "DS";
// $record_types[] = "SSHFP";
// $record_types[] = "SRV";
// $record_types[] = "DNSKEY";
// $record_types[] = "NSEC";
// $record_types[] = "RRSIG";
// $record_types[] = "AFSDB";
// $record_types[] = "CERT";
// $record_types[] = "HINFO";
// $record_types[] = "KEY";
// $record_types[] = "LOC";
// $record_types[] = "NAPTR";
// $record_types[] = "RP";
// $record_types[] = "TLSA";
// save
$this->record_types = (object) $record_types;
}
/**
* Sets default values for TTL
*
* @access private
* @return void
*/
private function set_ttl_values () {
// set array
$ttl[60] = "1 minute";
$ttl[180] = "3 minutes";
$ttl[300] = "5 minutes";
$ttl[600] = "10 minutes";
$ttl[900] = "15 minutes";
$ttl[1800] = "30 minutes";
$ttl[2700] = "45 minutes";
$ttl[3600] = "1 hour";
$ttl[7400] = "2 hours";
$ttl[21600] = "6 hours";
$ttl[43200] = "12 hours";
$ttl[86400] = "24 hours";
$ttl[604800]= "1 week";
// save
$this->ttl = (object) $ttl;
}
/**
* set_query_values function.
*
* @access public
* @param int $limit (default: 1000000)
* @param string $orderby (default: "id")
* @param string $orderdir (default: "asc")
* @return void
*/
public function set_query_values ($limit = 1000000, $orderby = "id", $orderdir = "asc") {
$this->limit = $limit; // number of results
$this->orderby = $orderby; // order field
$this->orderdir = $orderdir; // $order direction
}
/* @domains ---------- */
/**
* Edit domain wrapper
*
* @access public
* @param mixed $action
* @param mixed $values
* @return void
*/
public function domain_edit ($action, $values) {
# strip tags
$values = $this->strip_input_tags ($values);
# execute based on action
if($action=="add") { return $this->domain_add ($values); }
elseif($action=="edit") { return $this->domain_change ($values); }
elseif($action=="delete") { return $this->domain_delete ($values); }
else { return $this->Result->show("danger", _("Invalid action"), true); }
}
/**
* Creates new domain
*
* @access private
* @param mixed $values
* @return void
*/
private function domain_add ($values) {
# null empty values
$values = $this->reformat_empty_array_fields ($values, null);
# execute
try { $this->Database_pdns->insertObject("domains", $values); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS domain create", "Failed to create PowerDNS domain: ".$e->getMessage()."<hr>".$this->array_to_log((array) $values), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
return false;
}
// write log
$this->Log->write( "PowerDNS domain create", "New PowerDNS domain created<hr>".$this->array_to_log((array) $values), 0);
# ok
return true;
}
/**
* Edits domain
*
* @access private
* @param mixed $values
* @return void
*/
private function domain_change ($values) {
# null empty values
$values = $this->reformat_empty_array_fields ($values, null);
# execute
try { $this->Database_pdns->updateObject("domains", $values); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS domain edit", "Failed to edit PowerDNS domain: ".$e->getMessage()."<hr>".$this->array_to_log((array) $values), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
return false;
}
// write log
$this->Log->write( "PowerDNS domain edit", "PowerDNS domain edited<hr>".$this->array_to_log((array) $values), 0);
# ok
return true;
}
/**
* Deletes domain
*
* @access private
* @param mixed $values
* @return void
*/
private function domain_delete ($values) {
# save old
$old_domain = $this->fetch_domain ($values['id']);
# execute
try { $this->Database_pdns->deleteRow("domains", "id", $values['id']); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS domain delete", "Failed to delete PowerDNS domain: ".$e->getMessage()."<hr>".$this->array_to_log((array) $old_domain), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage(), false);
return false;
}
// write log
$this->Log->write( "PowerDNS domain delete", "PowerDNS domain deleted<hr>".$this->array_to_log((array) $old_domain), 0);
return true;
}
/**
* Fetches all domains
*
* @access public
* @return void
*/
public function fetch_all_domains () {
# fetch
try { $res = $this->Database_pdns->getObjects("domains", "id"); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# cache
if (sizeof($res)>0) {
foreach ($res as $r) { $this->domains_cache[$r->id] = $r; }
}
# result
return sizeof($res)>0 ? $res : false;
}
/**
* Fetches all forward domains
*
* @access public
* @return void
*/
public function fetch_all_forward_domains () {
# fetch
try { $res = $this->Database_pdns->findObjects("domains", "name", "%.arpa", "name", true, true, true); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# cache
if (sizeof($res)>0) {
foreach ($res as $r) { $this->domains_cache[$r->id] = $r; }
}
# result
return sizeof($res)>0 ? $res : false;
}
/**
* Fetches all reverse IPv4 domains
*
* @access public
* @return void
*/
public function fetch_reverse_v4_domains () {
# fetch
try { $res = $this->Database_pdns->findObjects("domains", "name", "%.in-addr.arpa", "name", true, true); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# cache
if (sizeof($res) > 0) {
foreach ($res as $r) { $this->domains_cache[$r->id] = $r; }
}
# result
return sizeof($res) > 0 ? $res : false;
}
/**
* Fetches all reverse IPv6 domains
*
* @access public
* @return void
*/
public function fetch_reverse_v6_domains () {
# fetch
try { $res = $this->Database_pdns->findObjects("domains", "name", "%.ip6.arpa", "name", true, true); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# cache
if (sizeof($res) > 0) {
foreach ($res as $r) { $this->domains_cache[$r->id] = $r; }
}
# result
return sizeof($res) > 0 ? $res : false;
}
/**
* Fetches domain record by id (numberic) of name (varchar)
*
* @access public
* @param mixed $id
* @return void
*/
public function fetch_domain ($id) {
# numeric of hostname
return is_numeric($id) ? $this->fetch_domain_by_id ($id) : $this->fetch_domain_by_name ($id);
}
/**
* Fetches domain details by id
*
* @access public
* @param mixed $id
* @return void
*/
public function fetch_domain_by_id ($id) {
# chcek cache
if (array_key_exists($id, $this->domains_cache)) { return $this->domains_cache[$id]; }
# fetch
try { $domain = $this->Database_pdns->getObject("domains", $id); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# cache
$this->domains_cache[$domain->id] = $domain;
# result
return sizeof($domain)>0 ? $domain : false;
}
/**
* Fetches domain details by name
*
* @access public
* @param mixed $name
* @return void
*/
public function fetch_domain_by_name ($name) {
# fetch
try { $domain = $this->Database_pdns->findObjects("domains", "name", $name); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# cache
$this->domains_cache[$domain->id] = $domain;
# result
return sizeof($domain[0])>0 ? $domain[0] : false;
}
/**
* Returns number of records for domain
*
* @access public
* @param mixed $domain_id
* @return void
*/
public function count_domain_records ($domain_id) {
# fetch
try { $res = $this->Database_pdns->numObjectsFilter("records", "domain_id", $domain_id); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return $res;
}
/**
* Conts number of records types
*
* @access public
* @param mixed $domain_id
* @param string $type (default: "PTR")
* @return void
*/
public function count_domain_records_by_type ($domain_id, $type="PTR") {
// query
$query = "select count(*) as `cnt` from `records` where `domain_id` = ? and `type` = ?;";
// fetch
try { $records = $this->Database_pdns->getObjectsQuery($query, array($domain_id, $type)); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return $records[0]->cnt;
}
/* records -------------------- */
/**
* Fetches all records for some domain
*
* @access public
* @param mixed $domain_id
* @return void
*/
public function fetch_all_domain_records ($domain_id) {
$query = "select * from `records` where `domain_id` = ? order by $this->orderby $this->orderdir limit $this->limit;";
// fetch
try { $records = $this->Database_pdns->getObjectsQuery($query, array($domain_id)); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return sizeof($records)>0 ? $records : false;
}
/**
* Fetches all records by type.
*
* @access public
* @param mixed $domain_id
* @param mixed $type
* @return void
*/
public function fetch_domain_records_by_type ($domain_id, $type) {
// query
$query = "select * from `records` where `domain_id` = ? and `type` = ? order by $this->orderby $this->orderdir limit $this->limit;";
// fetch
try { $records = $this->Database_pdns->getObjectsQuery($query, array($domain_id, $type)); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return sizeof($records)>0 ? $records : false;
}
/**
* Fetches record from database
*
* @access public
* @param mixed $record_id
* @return void
*/
public function fetch_record ($record_id) {
// validate int
$this->validate_integer ($record_id);
// fetch
try { $record = $this->Database_pdns->getObject("records", $record_id); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return sizeof($record)>0 ? $record : false;
}
/**
* Searches records for specific domainid for type and name values
*
* @access public
* @param mixed $domain_id
* @param mixed $type
* @param mixed $name
* @return void
*/
public function search_record_domain_type_name ($domain_id, $type, $name) {
// query
$query = "select * from `records` where `domain_id` = ? and `type` = ? and `name` = ? limit 1;";
// fetch
try { $records = $this->Database_pdns->getObjectQuery($query, array($domain_id, $type, $name)); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return sizeof($records)>0 ? $records : false;
}
/**
* Searches for domain record
*
* @access public
* @param string $field (default: "content")
* @param mixed $value (default: null)
* @param string $sortField (default: 'id')
* @param bool $sortAsc (default: true)
* @return void
*/
public function search_records ($field = "content", $value = null, $sortField = 'id', $sortAsc = true) {
// fetch
try { $records = $this->Database_pdns->findObjects("records", $field, $value, $sortField, $sortAsc); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return sizeof($records)>0 ? $records : false;
}
/**
* Edit PowerDNS record
*
* @access public
* @param mixed $action
* @param mixed $values
* @return void
*/
public function record_edit ($action, $values) {
# strip tags
$values = $this->strip_input_tags ($values);
# execute based on action
if($action=="add") { return $this->add_domain_record ($values); }
elseif($action=="edit") { return $this->update_domain_record ($values['domain_id'], $values); }
elseif($action=="delete") { return $this->remove_domain_record ($values['domain_id'], $values['id']); }
else { return $this->Result->show("danger", _("Invalid action"), true); }
}
/**
* Create new record
*
* @access public
* @param (object) $record
* @param (boolean) $print_success
* @return void
*/
public function add_domain_record ($record, $print_success = true) {
# null empty values
$record = $this->reformat_empty_array_fields ($record, null);
# execute
try { $this->Database_pdns->insertObject("records", $record); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS record create", "Failed to create PowerDNS domain record: ".$e->getMessage()."<hr>".$this->array_to_log((array) $record), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage(), true);
return false;
}
// write log
$this->Log->write( "PowerDNS record create", "New PowerDNS domain record created<hr>".$this->array_to_log((array) $record), 0);
# print ?
if ($print_success)
$this->Result->show("success", _("Record created"));
// save id
$this->lastId = $this->Database_pdns->lastInsertId ();
# soa update
$this->update_soa_serial ($record['domain_id']);
# ok
return true;
}
/**
* Updates domain record and SOA serial
*
* @access public
* @param mixed $domain_id
* @param mixed (array) $content
* @param (boolean) $print_success
* @return void
*/
public function update_domain_record ($domain_id, $content, $print_success=true) {
// validate domain
if ($this->fetch_domain ($domain_id)===false) { $this->Result->show("danger", "Invalid domain id", true); }
// remove domain_id if set !
unset($content->domain_id);
// remove record
$this->update_domain_record_content ($content);
// save id
$this->lastId = $this->Database_pdns->lastInsertId ();
// update SOA serial
$this->update_soa_serial ($domain_id);
# print ?
if ($print_success)
$this->Result->show("success", _("Record updated"));
// ok
return true;
}
/**
* Removes domain record
*
* @access public
* @param mixed $domain_id
* @param mixed $record_id
* @param (boolean) $print_success
* @return void
*/
public function remove_domain_record ($domain_id, $record_id, $print_success=true) {
// validate domain
if ($this->fetch_domain ($domain_id)===false) { $this->Result->show("danger", "Invalid domain id", true); }
// remove record
$this->remove_domain_record_by_id ($record_id);
// update SOA serial
$this->update_soa_serial ($domain_id);
# print ?
if ($print_success)
$this->Result->show("success", _("Record deleted"));
// ok
return true;
}
/**
* Removes specified domain record by id
*
* @access public
* @param mixed $id
* @return void
*/
public function remove_domain_record_by_id ($id) {
# fetch old records
$old_record = $this->fetch_record ($id);
# execute
try { $this->Database_pdns->deleteRow("records", "id", $id); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS record delete", "Failed to delete PowerDNS domain record: ".$e->getMessage()."<hr>".$this->array_to_log((array) $old_record), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage(), true);
return false;
}
// write log
$this->Log->write( "PowerDNS record delete", "PowerDNS domain record deleted<hr>".$this->array_to_log((array) $old_record), 0);
# ok
return true;
}
/**
* Updates content of specific record
*
* @access public
* @param mixed $content
* @return void
*/
public function update_domain_record_content ($content) {
# execute
try { $this->Database_pdns->updateObject("records", $content); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS record update", "Failed to update PowerDNS domain record: ".$e->getMessage()."<hr>".$this->array_to_log((array) $content), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage(), true);
return false;
}
// write log
$this->Log->write( "PowerDNS record updated", "PowerDNS domain record updated<hr>".$this->array_to_log((array) $content), 0);
# ok
return true;
}
/**
* Updates SOA serial
*
* @access private
* @param mixed $domain_id
* @param mixed $serial
* @return void
*/
private function update_soa_serial ($domain_id, $serial = false) {
// fetch record
$soa = $this->fetch_domain_records_by_type ($domain_id, "SOA");
// if not set exit
if ($soa === false) { return false; }
else { $soa = $soa[0]; }
// update serial it not autoserial
$soa_serial = explode(" ", $soa->content);
$soa_serial[2] = $this->db_settings->autoserial=="Yes" ? 0 : (int) $soa_serial[2]+1;
// if serail set override it
if($serial!==false) { $soa_serial[2] = $serial; }
// set update content
$content = array(
"id"=>$soa->id,
"content"=>implode(" ", $soa_serial),
"change_date"=>$soa_serial[2]
);
// update
$this->update_domain_record_content ($content);
}
/**
* Updates all SOA serials if it changes from autoserial false to true
*
* @access public
* @param bool $autoserial (Default : No)
* @return void
*/
public function update_all_soa_serials ($autoserial = "No") {
// fetch all domains
$all_domains = $this->fetch_all_domains ();
// set new serial
$serial = $autoserial==="Yes" ? 0 : date("Ymd")."01";
// set new serial
if ($all_domains !== false) {
foreach ($all_domains as $d) {
$this->update_soa_serial ($d->id, $serial);
}
}
}
/**
* Create default records for domain if requested
*
* - SOA record (primary hostmaster serial refresh retry expire default_ttl)
* - One entry for each NS
*
* @access public
* @param (array) $values
* @return void
*/
public function create_default_records ($values) {
// get last id
$this->lastId = $this->Database_pdns->lastInsertId ();
// set defaults
$this->db_set_db_settings ();
// content
$soa[] = array_shift(explode(";", $values['ns']));
$soa[] = str_replace ("@", ".", $values['hostmaster']);
$soa[] = $this->set_default_change_date ();
$soa[] = $this->validate_refresh ($values['refresh']);
$soa[] = $this->validate_integer ($values['retry']);
$soa[] = $this->validate_integer ($values['expire']);
$soa[] = $this->validate_nxdomain_ttl ($values['nxdomain_ttl']);
// formulate SOA value
$records[] = $this->formulate_new_record ($this->lastId, $values['name'], "SOA", implode(" ", $soa), $values['ttl']);
// formulate NS records
$ns = explode(";", $values['ns']);
if (sizeof($ns)>0) {
foreach($ns as $s) {
// validate
if($this->validate_hostname($s)===false) { $this->Result->show("danger", "Invalid NS". " $n", true); }
// save
$records[] = $this->formulate_new_record ($this->lastId, $values['name'], "NS", $s, $values['ttl']);
}
}
// create records
foreach($records as $r) {
$this->add_domain_record ($r, false);
}
// all good, print it !
$this->Result->show("success", "Default records created", false);
}
/**
* Creates object with values for new record
*
* @access public
* @param mixed $domain_id
* @param mixed $name (default: null)
* @param mixed $type
* @param mixed $content
* @param mixed $ttl
* @param mixed $prio (default: null)
* @param int $disabled (default: 0)
* @return void
*/
public function formulate_new_record ($domain_id, $name=null, $type, $content, $ttl, $prio=null, $disabled = 0) {
// initiate class
$record = new StdClass ();
// set record details
$record->domain_id = $this->validate_record_domain_id ($domain_id); // sets domain id
$record->name = $this->validate_record_name ($name); // record name
$record->type = $this->validate_record_type ($type); // record type
$record->content = $content; // record content
$record->ttl = $this->validate_ttl ($ttl); // ttl validation
$record->prio = $this->validate_prio ($prio); // priority, default NULL
$record->change_date= $this->set_default_change_date (); // sets default change date
$record->disabled = $disabled; // enables of disables record
// return record
return (array) $record;
}
/**
* Validates edit of record
*
* @access public
* @param mixed $name (default: null)
* @param mixed $type (default: null)
* @param mixed $content (default: null)
* @param mixed $ttl (default: null)
* @param mixed $prio (default: null)
* @param int $disabled (default: 0)
* @param mixed $old_date (default: null)
* @return void
*/
public function formulate_update_record ($name=null, $type=null, $content=null, $ttl=null, $prio=null, $disabled=null, $old_date=null) {
// initiate class
$record = new StdClass ();
// set record details
if (!is_null($name)) $record->name = $this->validate_record_name ($name); // record name
if (!is_null($type)) $record->type = $this->validate_record_type ($type); // record type
if (!is_null($content)) $record->content = $content; // record content
if (!is_null($ttl)) $record->ttl = $this->validate_ttl ($ttl); // ttl validation
if (!is_null($prio)) $record->prio = $this->validate_prio ($prio); // priority, default NULL
$record->change_date= $this->update_record_change_date ($old_date); // updates change date
if (!is_null($disabled))$record->disabled = $disabled; // enables of disables record
// return record
return (array) $record;
}
/**
* Validate domain id
*
* - must be an integer
* - domain must already exist
*
* @access private
* @param mixed $domain_id
* @return void
*/
private function validate_record_domain_id ($domain_id) {
// integer
if (!is_numeric($domain_id)) { $this->Result->show("danger", _("Domain id must be an integer"), true); }
// check for domain record
if ($this->fetch_domain ($domain_id)===false) { $this->Result->show("danger", _("Domain does not exist"), true); }
// ok
return $domain_id;
}
/**
* Validates record name
*
* - if not null validate hostname
*
* @access private
* @param mixed $name
* @return void
*/
private function validate_record_name ($name) {
// null is ok, otherwise URI is required
if (strlen($name)>0 && !$this->validate_hostname($name)){ $this->Result->show("danger", _("Invalid record name"), true); }
// ok
return $name;
}
/**
* Validates record type
*
* - check against permitted record types
*
* @access private
* @param mixed $type
* @return void
*/
private function validate_record_type ($type) {
// if set check, otherwise ognore
if(isset($type)) {
// check record type
if(!in_array($type, (array) $this->record_types)) { $this->Result->show("danger", _("Invalid record type"), true); }
}
// ok
return $type;
}
/**
* Validate TTL value
*
* - numeric
* - between 0 and 2147483647
*
* @access private
* @param mixed $ttl
* @return void
*/
private function validate_ttl ($ttl) {
// check numberfic
if(!is_numeric($ttl)) { $this->Result->show("danger", "Invalid TTL", true); }
// check range
if(0 > $ttl || $ttl > 2147483647) { $this->Result->show("danger", "TTL range is from 0 to 2147483647", true); }
// ok
return $ttl;
}
/**
* Validates nxdomain ttl
*
* @access private
* @param mixed $ttl
* @return void
*/
private function validate_nxdomain_ttl ($ttl) {
// check numberfic
if(!is_numeric($ttl)) { $this->Result->show("danger", "Invalid NXDOMAIN TTL", true); }
// check range
if(0 > $ttl || $ttl > 10800) { $this->Result->show("danger", "NXDOMAIN TTL range is from 0 to 10800", true); }
// ok
return $ttl;
}
/**
* Validate refresh SOA value
*
* @access private
* @param mixed $refresh
* @return void
*/
private function validate_refresh ($refresh) {
// check numberfic
if(!is_numeric($refresh)) { $this->Result->show("danger", "Invalid refresh TTL", true); }
// check range
if(1200 > $refresh || $refresh > 2147483647) { $this->Result->show("danger", "refresh TTL range is from 1200 to 2147483647", true); }
// ok
return $refresh;
}
/**
* Validates priority
*
* @access private
* @param mixed $prio
* @return void
*/
private function validate_prio ($prio) {
// validate numbric
if(!is_null($prio) && strlen($prio)>0) {
if(!is_numeric($prio)) { $this->Result->show("danger", "Invalid priority value", true); }
// range
if(0 > $prio || $prio > 1000) { $this->Result->show("danger", "Priority range is from 0 to 1000", true); }
}
// ok
return $prio;
}
/**
* Validates integer
*
* @access private
* @param mixed $int
* @return void
*/
private function validate_integer ($int) {
// validate numbric
if(strlen($int)>0 && !is_null($int) && $int!==false) {
if(!is_numeric($int)) { $this->Result->show("danger", "Invalid integer value", true); }
}
// ok
return $int;
}
/**
* Sets default change date for record
*
* - 2015032701
*
* @access private
* @return void
*/
private function set_default_change_date () {
return date("Ymd")."00";
}
/**
* Updates change date for record
*
* @access private
* @param mixed $current_date (default: null)
* @return void
*/
private function update_record_change_date ($current_date=null) {
// not set
if (is_null($current_date)) { return $this->set_default_change_date (); }
// split to date / sequence
$date = substr($current_date, 0,8);
$seq = substr($current_date, 8,9);
// date same ++, otherwise default
if ($date==date('Ymd')) { return $current_date+1; }
else { return $this->set_default_change_date (); }
}
/**
* Updates all existing domain records if domain name changes !
*
* @access public
* @param mixed $domain_id
* @param mixed $name
* @return void
*/
public function update_all_records ($domain_id, $name) {
// set query
$query = "update `records` set `name` = replace(`name`, ?, ?) where where `domain_id` = $domain_id;";
}
/**
* Removes all domain records
*
* @access public
* @param mixed $domain_id
* @return void
*/
public function remove_all_records ($domain_id) {
// execute
try { $res = $this->Database_pdns->runQuery("delete from `records` where `domain_id` = ?;", array($domain_id)); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS domain truncate", "Failed to remove all PowerDNS domain records: ".$e->getMessage()."<hr>".$this->array_to_log((array) $domain_id), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
// write log
$this->Log->write( "PowerDNS domain truncate", "PowerDNS domain records truncated<hr>".$this->array_to_log((array) $domain_id), 0);
# ok
$this->Result->show("success", _("All records for domain removed"));
return true;
}
/* @PTR reverse functions -------------------- */
/**
* Returns PTR zone from ip address / subnet based on IP version
*
* @access public
* @param mixed $ip
* @return void
*/
public function get_ptr_zone_name ($ip, $mask) {
return $this->identify_address ($ip)=="IPv4" ? $this->get_ptr_zone_name_v4 ($ip, $mask) : $this->get_ptr_zone_name_v6 ($ip, $mask);
}
/**
* Returns PTR zone for IPv4 records
*
* @access public
* @param mixed $ip
* @return void
*/
public function get_ptr_zone_name_v4 ($ip, $mask) {
// check mask to see how many IP bits to remove
$bits = $mask<23 ? 2 : 1;
// to array
$zone = explode(".", $ip);
// create name
if ($bits==1) { return $zone[2].".".$zone[1].".".$zone[0].".in-addr.arpa"; }
else { return $zone[1].".".$zone[0].".in-addr.arpa"; }
}
/**
* Returns PTR zone for IPv6 record
*
* @access public
* @param mixed $ip
* @return void
*/
public function get_ptr_zone_name_v6 ($ip, $mask) {
// PEAR for IPv6
$this->initialize_pear_net_IPv6 ();
// remove netmask and ::
$subnet = $this->Net_IPv6->removeNetmaskSpec($ip);
$subnet = rtrim($subnet, "::");
// to array
$ip = explode(":", $subnet);
// if 0 than add 4 nulls
foreach ($ip as $k=>$i) {
$ip[$k] = str_pad($i, 4, "0", STR_PAD_LEFT);
}
// to array and reverse
$zone = array_reverse(str_split(implode("", $ip)));
return implode(".", $zone).".ip6.arpa";
}
/**
* Set name for PTR host record
*
* @access public
* @param mixed $ip
* @return void
*/
public function get_ip_ptr_name ($ip) {
// set zone prefix and reverse content
if ($this->identify_address ($ip)=="IPv4") {
$prefix = ".in-addr.arpa";
$zone = array_reverse(explode(".", $ip));
}
else {
// PEAR for IPv6
$this->initialize_pear_net_IPv6 ();
// uncompress and remove netmask
$ip = $this->Net_IPv6->uncompress($ip);
$ip = $this->Net_IPv6->removeNetmaskSpec($ip);
// to array
$ip = explode(":", $ip);
// if 0 than add 4 nulls
foreach ($ip as $k=>$i) {
$ip[$k] = str_pad($i, 4, "0", STR_PAD_LEFT);
}
$ip = str_split(implode("", $ip));
$prefix = ".ip6.arpa";
$zone = array_reverse($ip);
}
// return
return implode(".", $zone).$prefix;
}
/**
* Checks if record exists
*
* @access public
* @param mixed $domain_id
* @param mixed $name
* @param mixed $type
* @param mixed $content
* @return void
*/
public function record_exists ($domain_id, $name, $type, $content) {
// execute
try { $res = $this->Database_pdns->getObjectQuery("select count(*) as `cnt` from `records` where `domain_id` = ? and `name`=? and `type`=? and `content`=?;", array($domain_id, $name, $type, $content)); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# ok
return $res->cnt >0 ? true : false;
}
/**
* Checks if record exists by id
*
* @access private
* @param mixed $ptr_id (default: 0)
* @return void
*/
public function record_id_exists ($ptr_id = 0) {
# 0 or dalse
if (@$ptr_id==0 || $ptr_id===false) { return false; }
# fetch
try { $count = $this->Database_pdns->numObjectsFilter("records", "id", $ptr_id); }
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
# result
return $count==0 ? false : true;
}
/**
* Removes all PTR records for domain
*
* @access public
* @param mixed $domain_id
* @return void
*/
public function remove_all_ptr_records ($domain_id) {
// execute
try { $res = $this->Database_pdns->runQuery("delete from `records` where `domain_id` = ? and `type` = 'PTR';", array($domain_id)); }
catch (Exception $e) {
// write log
$this->Log->write( "PowerDNS records delete", "Failed to delete all PowerDNS domain PTR records: ".$e->getMessage()."<hr>".$this->array_to_log((array) $domain_id), 2);
$this->Result->show("danger", _("Error: ").$e->getMessage());
return false;
}
// write log
$this->Log->write( "PowerDNS records delete", "All PTR records for PowerDNS removed<hr>".$this->array_to_log((array) $domain_id), 0);
# ok
return true;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.