1+ <?php
2+ // Copyright 2021 Jared Hendrickson
3+ //
4+ // Licensed under the Apache License, Version 2.0 (the "License");
5+ // you may not use this file except in compliance with the License.
6+ // You may obtain a copy of the License at
7+ //
8+ // http://www.apache.org/licenses/LICENSE-2.0
9+ //
10+ // Unless required by applicable law or agreed to in writing, software
11+ // distributed under the License is distributed on an "AS IS" BASIS,
12+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ // See the License for the specific language governing permissions and
14+ // limitations under the License.
15+
16+ require_once ("api/framework/APIModel.inc " );
17+ require_once ("api/framework/APIResponse.inc " );
18+
19+
20+ class APIFirewallTrafficShaperLimiterBandwidthDelete extends APIModel {
21+ private $ parent_id ;
22+
23+ # Create our method constructor
24+ public function __construct () {
25+ parent ::__construct ();
26+ $ this ->privileges = ["page-all " , "page-firewall-trafficshaper-limiter " ];
27+ $ this ->change_note = "Deleted firewall traffic shaper limiter bandwidth via API " ;
28+ }
29+
30+ public function action () {
31+ unset($ this ->config ["dnshaper " ]["queue " ][$ this ->parent_id ]["bandwidth " ]["item " ][$ this ->id ]);
32+ $ this ->write_config ();
33+ filter_configure ();
34+
35+ return APIResponse \get (0 , $ this ->validated_data );
36+ }
37+
38+ public function validate_payload () {
39+ $ this ->__validate_name ();
40+ $ this ->__validate_id ();
41+ }
42+
43+ private function __validate_name () {
44+ # Check for our required `name` payload value
45+ if (isset ($ this ->initial_data ["name " ])) {
46+ if ($ this ->get_limiter_id_by_name ($ this ->initial_data ["name " ], true )) {
47+ $ this ->parent_id = $ this ->get_limiter_id_by_name ($ this ->initial_data ["name " ]);
48+ } else {
49+ $ this ->errors [] = APIResponse \get (4209 );
50+ }
51+ } else {
52+ $ this ->errors [] = APIResponse \get (4167 );
53+ }
54+ }
55+
56+ private function __validate_id () {
57+ # Check for our required `id` payload value
58+ if (isset ($ this ->initial_data ["id " ])) {
59+ $ id = intval ($ this ->initial_data ["id " ]);
60+
61+ # Ensure a bandwidth object with this ID exists
62+ if (array_key_exists ($ id , $ this ->config ["dnshaper " ]["queue " ][$ this ->parent_id ]["bandwidth " ]["item " ])) {
63+ $ this ->id = $ id ;
64+ $ this ->validated_data = $ this ->config ["dnshaper " ]["queue " ][$ this ->parent_id ]["bandwidth " ]["item " ][$ id ];
65+ } else {
66+ $ this ->errors [] = APIResponse \get (4216 );
67+ }
68+ } else {
69+ $ this ->errors [] = APIResponse \get (4215 );
70+ }
71+ }
72+
73+ public function get_limiter_id_by_name ($ name , $ as_bool =false ) {
74+ # Loop through each configured limiter and see if it uses this name
75+ foreach ($ this ->config ["dnshaper " ]["queue " ] as $ id =>$ limiter ) {
76+ # Check for matching names
77+ if ($ name === $ limiter ["name " ]) {
78+ return ($ as_bool ) ? true : $ id ;
79+ }
80+ }
81+ return ($ as_bool ) ? false : null ;
82+ }
83+
84+ }
0 commit comments