@@ -169,18 +169,21 @@ function api_authorized($req_privs, $read_only=false) {
169169 if (api_authenticate () === true ) {
170170 $ client_config =& getUserEntry ($ client_id );;
171171 $ client_privs = get_user_privileges ($ client_config );
172- // Loop through each of our req privs and ensure the client has them, also check if access is read only
173- foreach ($ req_privs as &$ priv ) {
174- // Check if action is not read only
175- if ($ read_only === false ) {
176- if (array_diff ($ read_priv , $ client_privs ) and in_array ($ priv , $ client_privs , true )) {
177- $ authorized = true ;
178- break ;
179- }
180- } else {
181- if (in_array ($ priv , $ client_privs )) {
182- $ authorized = true ;
183- break ;
172+ // Check if API is in read-only mode
173+ if (is_api_read_only () === $ read_only or $ read_only ) {
174+ // Loop through each of our req privs and ensure the client has them, also check if access is read only
175+ foreach ($ req_privs as &$ priv ) {
176+ // Check if action is not read only
177+ if ($ read_only === false ) {
178+ if (array_diff ($ read_priv , $ client_privs ) and in_array ($ priv , $ client_privs , true )) {
179+ $ authorized = true ;
180+ break ;
181+ }
182+ } else {
183+ if (in_array ($ priv , $ client_privs )) {
184+ $ authorized = true ;
185+ break ;
186+ }
184187 }
185188 }
186189 }
@@ -231,6 +234,17 @@ function api_enabled() {
231234 }
232235}
233236
237+ // Check if the API is in read-only mode
238+ function is_api_read_only () {
239+ // Local variables
240+ $ api_config = get_api_configuration ()[1 ]; // Save our current API config
241+ if (array_key_exists ("readonly " , $ api_config )) {
242+ return true ;
243+ } else {
244+ return false ;
245+ }
246+ }
247+
234248// Check if server IP is allowed to answer API calls. Redirects to login if not
235249function api_whitelist_check () {
236250 global $ config ;
@@ -594,6 +608,33 @@ function sort_firewall_rules($mode=null, $data=null) {
594608 $ config ["filter " ]["rule " ] = $ master_arr ;
595609}
596610
611+ // Sorts nat rules by specified criteria and reloads the filter
612+ function sort_nat_rules ($ mode =null , $ data =null ) {
613+ // Variables
614+ global $ config ;
615+ $ sort_arr = [];
616+ $ master_arr = [];
617+ foreach ($ config ["nat " ]["rule " ] as $ idx => $ fre ) {
618+ $ curr_iface = $ fre ["interface " ]; // Save our current entries interface
619+ // Create our interface array if does not exist
620+ if (!isset ($ sort_arr [$ curr_iface ])) {
621+ $ sort_arr [$ curr_iface ] = [];
622+ }
623+ // Check if user requested this rule to be placed at the top of array
624+ if ($ mode === "top " and $ idx === $ data ) {
625+ array_unshift ($ sort_arr [$ curr_iface ], $ fre );
626+ } else {
627+ $ sort_arr [$ curr_iface ][] = $ fre ;
628+ }
629+ }
630+ foreach ($ sort_arr as $ if ) {
631+ foreach ($ if as $ rule ) {
632+ $ master_arr [] = $ rule ;
633+ }
634+ }
635+ $ config ["nat " ]["rule " ] = $ master_arr ;
636+ }
637+
597638// Checks if inputted routing gateway exists
598639function is_gateway ($ gw ) {
599640 // Local variables
0 commit comments