1+ <?php
2+ require_once ("api/framework/APIBaseModel.inc " );
3+ require_once ("api/framework/APIResponse.inc " );
4+
5+ class APIServicesUnboundDeleteHosts extends APIBaseModel {
6+ # Create our method constructor
7+ public function __construct () {
8+ parent ::__construct ();
9+ $ this ->methods = ["POST " ];
10+ $ this ->privileges = ["page-all " , "page-services-dnsresolver-edithost " ];
11+ $ this ->change_note = "Deleted DNS Resolver host override via API " ;
12+ }
13+
14+ public function action () {
15+ usort ($ this ->validated_data ["hosts_conf " ], "strcmp " );
16+ $ this ->config ["unbound " ]["hosts " ] = $ this ->validated_data ["hosts_conf " ];
17+ $ this ->write_config ();
18+ mark_subsystem_dirty ("unbound " );
19+ # If user requests immediately application
20+ if ($ this ->validated_data ["apply " ] === true ) {
21+ APITools \unbound_reload_config ();
22+ }
23+ // Return success if our function was successful
24+ return APIResponse \get (0 , $ this ->validated_data ["del_list " ]);
25+ }
26+
27+ public function validate_payload () {
28+ if (isset ($ this ->initial_data ['host ' ])) {
29+ $ this ->validated_data ["hostname " ] = trim ($ this ->initial_data ['host ' ]);
30+ $ h_mode = true ;
31+ }
32+ if (isset ($ this ->initial_data ['domain ' ])) {
33+ $ this ->validated_data ["domain " ] = trim ($ this ->initial_data ['domain ' ]);
34+ $ d_mode = true ;
35+ }
36+ if (isset ($ this ->initial_data ['ip ' ])) {
37+ $ this ->validated_data ["ip " ] = trim ($ this ->initial_data ['ip ' ]);
38+ $ i_mode = true ;
39+ }
40+ if ($ this ->initial_data ['aliases ' ] === true ) {
41+ $ a_mode = true ;
42+ }
43+ if ($ this ->initial_data ['apply ' ] === true ) {
44+ $ this ->validated_data ["apply " ] = $ this ->initial_data ['apply ' ];
45+ }
46+ // Determine criteria for deletion
47+ if ($ h_mode and !$ d_mode and !$ i_mode ) {
48+ $ del_mode = "h " ;
49+ } elseif ($ h_mode and $ d_mode and !$ i_mode ) {
50+ $ del_mode = "hd " ;
51+ } elseif ($ h_mode and !$ d_mode and $ i_mode ) {
52+ $ del_mode = "hi " ;
53+ } elseif ($ h_mode and $ d_mode and $ i_mode ) {
54+ $ del_mode = "hdi " ;
55+ } elseif (!$ h_mode and $ d_mode and !$ i_mode ) {
56+ $ del_mode = "d " ;
57+ } elseif (!$ h_mode and $ d_mode and $ i_mode ) {
58+ $ del_mode = "di " ;
59+ } elseif (!$ h_mode and !$ d_mode and $ i_mode ) {
60+ $ del_mode = "i " ;
61+ } else {
62+ $ this ->errors [] = APIResponse \get (2014 );
63+ }
64+
65+ // Check that our configuration is a list and loop through each item, otherwise return ok resp
66+ if (array_key_exists ("hosts " , $ this ->config ["unbound " ]) and is_array ($ this ->config ["unbound " ]["hosts " ])) {
67+ $ this ->validated_data ["del_list " ] = array ("hosts " => array (), "aliases " => array ()); // List of deleted items
68+ $ this ->validated_data ["hosts_conf " ] = &$ this ->config ["unbound " ]["hosts " ]; // Current Unbound host overrides
69+ $ h_count = 0 ; // Define counter for our hosts loop
70+ foreach ($ this ->validated_data ["hosts_conf " ] as $ he ) {
71+ // Check aliases for match if alias mode
72+ if ($ a_mode and is_array ($ he ["aliases " ])) {
73+ $ a_count = 0 ; // Define counter for our aliases loop
74+ // Loop through aliases to check for matches
75+ foreach ($ he ["aliases " ]["item " ] as $ ae ) {
76+ if ($ del_mode === "h " ) {
77+ if ($ this ->validated_data ["hostname " ] === $ ae ["host " ]) {
78+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]["aliases " ]["item " ][$ a_count ]);
79+ $ this ->validated_data ["del_list " ]["aliases " ][] = $ ae ["host " ].". " .$ ae ["domain " ];
80+ }
81+ } elseif ($ del_mode === "d " ) {
82+ if ($ this ->validated_data ["domain " ] === $ ae ["domain " ]) {
83+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]["aliases " ]["item " ][$ a_count ]);
84+ $ this ->validated_data ["del_list " ]["aliases " ][] = $ ae ["host " ].". " .$ ae ["domain " ];
85+ }
86+ } elseif ($ del_mode === "hd " ) {
87+ if ($ this ->validated_data ["hostname " ] === $ ae ["host " ] and $ this ->validated_data ["domain " ] === $ ae ["domain " ]) {
88+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]["aliases " ]["item " ][$ a_count ]);
89+ $ this ->validated_data ["del_list " ]["aliases " ][] = $ ae ["host " ].". " .$ ae ["domain " ];
90+ }
91+ }
92+ // If all aliases were removed, restore aliases key to empty string
93+ if (empty ($ this ->validated_data ["hosts_conf " ][$ h_count ]["aliases " ]["item " ])) {
94+ $ this ->validated_data ["hosts_conf " ][$ h_count ]["aliases " ] = "" ;
95+ }
96+ // Increase our alias counter
97+ $ a_count ++;
98+ }
99+ }
100+ // Check parent host entries
101+ if ($ del_mode === "h " ) {
102+ if ($ this ->validated_data ["hostname " ] === $ he ["host " ]) {
103+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
104+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
105+ }
106+ } elseif ($ del_mode === "d " ) {
107+ if ($ this ->validated_data ["domain " ] === $ he ["domain " ]) {
108+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
109+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
110+ }
111+ } elseif ($ del_mode === "i " ) {
112+ if ($ this ->validated_data ["ip " ] === $ he ["ip " ]) {
113+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
114+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
115+ }
116+ } elseif ($ del_mode === "hd " ) {
117+ if ($ this ->validated_data ["hostname " ] === $ he ["host " ] and $ this ->validated_data ["domain " ] === $ he ["domain " ]) {
118+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
119+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
120+ }
121+ } elseif ($ del_mode === "hi " ) {
122+ if ($ this ->validated_data ["hostname " ] === $ he ["host " ] and $ this ->validated_data ["ip " ] === $ he ["ip " ]) {
123+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
124+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
125+ }
126+ } elseif ($ del_mode === "di " ) {
127+ if ($ this ->validated_data ["domain " ] === $ he ["domain " ] and $ this ->validated_data ["ip " ] === $ he ["ip " ]) {
128+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
129+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
130+ }
131+ } elseif ($ del_mode === "hdi " ) {
132+ if ($ this ->validated_data ["hostname " ] === $ he ["host " ] and $ this ->validated_data ["domain " ] === $ he ["domain " ] and $ this ->validated_data ["ip " ] === $ he ["ip " ]) {
133+ unset($ this ->validated_data ["hosts_conf " ][$ h_count ]);
134+ $ this ->validated_data ["del_list " ]["hosts " ][] = $ he ["host " ].". " .$ he ["domain " ];
135+ }
136+ }
137+ // Increase our host counter
138+ $ h_count ++;
139+ }
140+ } else {
141+ $ this ->errors [] = APIResponse \get (2013 );
142+ }
143+ }
144+ }
0 commit comments