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 APIFirewallScheduleUpdate extends APIModel {
21+ # Create our method constructor
22+ public function __construct () {
23+ parent ::__construct ();
24+ $ this ->privileges = ["page-all " , "page-firewall-schedules-edit " ];
25+ $ this ->change_note = "Modified firewall schedule via API " ;
26+ }
27+
28+ public function action () {
29+ # Update this schedule in the config and reload the firewall filter
30+ $ this ->config ["schedules " ]["schedule " ][$ this ->id ] = $ this ->validated_data ;
31+ $ this ->__sort_schedules ();
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_timerange ();
41+ $ this ->__validate_descr ();
42+ }
43+
44+ private function __validate_name () {
45+ # Check for our required `name` payload value
46+ if (isset ($ this ->initial_data ["name " ])) {
47+ if ($ this ->get_schedule_id_by_name ($ this ->initial_data ["name " ], true )) {
48+ $ this ->id = $ this ->get_schedule_id_by_name ($ this ->initial_data ["name " ]);
49+ $ this ->validated_data = $ this ->config ["schedules " ]["schedule " ][$ this ->id ];
50+ } else {
51+ $ this ->errors [] = APIResponse \get (4150 );
52+ }
53+ } else {
54+ $ this ->errors [] = APIResponse \get (4146 );
55+ }
56+ }
57+
58+ private function __validate_timerange () {
59+ # Check for the optional `timerange` payload value
60+ if (isset ($ this ->initial_data ["timerange " ])) {
61+ # Initialize the array to store validated time ranges
62+ $ this ->validated_data ["timerange " ];
63+
64+ # Require at least 1 time range to be configured
65+ if (is_array ($ this ->initial_data ["timerange " ]) and count ($ this ->initial_data ["timerange " ]) >= 1 ) {
66+ # Loop through each requested time range to validate
67+ foreach ($ this ->initial_data ["timerange " ] as $ tr_data ) {
68+ # Validate the time range using the APIFirewallScheduleTimeRangeCreate model
69+ $ time_range = new APIFirewallScheduleTimeRangeCreate ();
70+ $ time_range ->initial_data = $ tr_data ;
71+ $ time_range ->validate_payload (true );
72+
73+ # Add the validated time range if no errors were found
74+ if (empty ($ time_range ->errors )) {
75+ $ this ->validated_data ["timerange " ][] = $ time_range ->validated_data ;
76+ } else {
77+ $ this ->errors = $ this ->errors + $ time_range ->errors ;
78+ }
79+ }
80+ } else {
81+ $ this ->errors [] = APIResponse \get (4162 );
82+ }
83+ }
84+ }
85+
86+ private function __validate_descr () {
87+ # Check for the optional `descr` payload value
88+ if (isset ($ this ->initial_data ["descr " ])) {
89+ $ this ->validated_data ["descr " ] = strval ($ this ->initial_data ["descr " ]);
90+ }
91+ }
92+
93+ public function get_schedule_id_by_name ($ name , $ as_bool =false ) {
94+ # Loop through each schedule configured and check it's name
95+ foreach ($ this ->config ["schedules " ]["schedule " ] as $ id =>$ schedule ) {
96+ # Check if this $schedule's name matches our requested name
97+ if ($ name === $ schedule ["name " ]) {
98+ return ($ as_bool ) ? true : $ id ;
99+ }
100+ }
101+ return ($ as_bool ) ? false : null ;
102+ }
103+
104+ private static function __compare_schedules ($ a , $ b ) {
105+ return strcmp ($ a ['name ' ], $ b ['name ' ]);
106+ }
107+
108+ private function __sort_schedules () {
109+ if (is_array ($ this ->config ['schedules ' ]['schedule ' ])) {
110+ usort ($ this ->config ['schedules ' ]['schedule ' ], ["APIFirewallScheduleCreate " , "__compare_schedules " ]);
111+ }
112+ }
113+ }
0 commit comments