|
| 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 | +class APIFirewallTrafficShaperQueueDelete extends APIModel { |
| 20 | + public $parent_id; |
| 21 | + |
| 22 | + # Create our method constructor |
| 23 | + public function __construct() { |
| 24 | + parent::__construct(); |
| 25 | + $this->privileges = ["page-all", "page-firewall-trafficshaper-queues"]; |
| 26 | + $this->change_note = "Deleted firewall traffic shaper queue via API"; |
| 27 | + } |
| 28 | + |
| 29 | + public function action() { |
| 30 | + unset($this->config["shaper"]["queue"][$this->parent_id]["queue"][$this->id]); |
| 31 | + $this->write_config(); |
| 32 | + mark_subsystem_dirty('shaper'); |
| 33 | + |
| 34 | + # Only reload the filter immediately if it was requested by the client |
| 35 | + if ($this->initial_data["apply"] === true) { |
| 36 | + # Reload the filter, reset RRD logs and mark the subsystem as applied |
| 37 | + filter_configure(); |
| 38 | + system("rm -f /var/db/rrd/*queuedrops.rrd"); |
| 39 | + system("rm -f /var/db/rrd/*queues.rrd"); |
| 40 | + enable_rrd_graphing(); |
| 41 | + clear_subsystem_dirty('shaper'); |
| 42 | + } |
| 43 | + return APIResponse\get(0, $this->validated_data); |
| 44 | + } |
| 45 | + |
| 46 | + public function validate_payload() { |
| 47 | + $this->__validate_interface(); |
| 48 | + $this->__validate_name(); |
| 49 | + } |
| 50 | + |
| 51 | + private function __validate_interface() { |
| 52 | + # Check for our required `interface` payload value |
| 53 | + if (isset($this->initial_data["interface"])) { |
| 54 | + $this->initial_data["interface"] = APITools\get_pfsense_if_id($this->initial_data["interface"]); |
| 55 | + |
| 56 | + # Check that the requested interface exists |
| 57 | + if ($this->initial_data["interface"]) { |
| 58 | + # Check that a traffic shaper is configured for this interface |
| 59 | + if ($this->get_shaper_id_by_interface($this->initial_data["interface"], true)) { |
| 60 | + # Set the interface this queue will be applied to |
| 61 | + $this->parent_id = $this->get_shaper_id_by_interface($this->initial_data["interface"]); |
| 62 | + } else { |
| 63 | + $this->errors[] = APIResponse\get(4122); |
| 64 | + } |
| 65 | + } else { |
| 66 | + $this->errors[] = APIResponse\get(4111); |
| 67 | + } |
| 68 | + } else { |
| 69 | + $this->errors[] = APIResponse\get(4110); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private function __validate_name() { |
| 74 | + # Check for our required `name` payload value |
| 75 | + if (isset($this->initial_data["name"])) { |
| 76 | + # Loop through each queue within the interface's shaper and search for a match |
| 77 | + foreach ($this->config["shaper"]["queue"][$this->parent_id]["queue"] as $id=>$queue) { |
| 78 | + # Check if this queue matches the requested queue to delete |
| 79 | + if ($this->initial_data["name"] === $queue["name"]) { |
| 80 | + $this->id = intval($id); |
| 81 | + $this->validated_data = $this->config["shaper"]["queue"][$this->parent_id]["queue"][$this->id]; |
| 82 | + } |
| 83 | + } |
| 84 | + # If no ID was found set does not exist error |
| 85 | + if (!isset($this->id)) { |
| 86 | + $this->errors[] = APIResponse\get(4145); |
| 87 | + } |
| 88 | + } else { |
| 89 | + $this->errors[] = APIResponse\get(4123); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public function get_shaper_id_by_interface($interface, $as_bool=false) { |
| 94 | + # Loop through each configured shaper |
| 95 | + foreach ($this->config["shaper"]["queue"] as $id=>$shaper) { |
| 96 | + # Check if this is the shaper for our interface, if so return it's ID |
| 97 | + if ($interface === $shaper["interface"]) { |
| 98 | + return ($as_bool) ? true : intval($id); |
| 99 | + } |
| 100 | + } |
| 101 | + return ($as_bool) ? false : null; |
| 102 | + } |
| 103 | +} |
0 commit comments