@@ -208,11 +208,7 @@ public function whitelist( $args, $assoc_args ) {
208208 */
209209 private function whitelist_create ( $ file , $ user_ips , $ existing_ips ) {
210210
211- $ file_content = '' ;
212- foreach ( $ user_ips as $ ip ) {
213- $ file_content .= "allow $ ip; " . PHP_EOL ;
214- }
215- $ this ->fs ->dumpFile ( $ file , $ file_content );
211+ $ this ->put_ips_to_file ( $ file , $ user_ips );
216212 EE ::success ( sprintf ( 'Created whitelist for `%s` scope with %s IP \'s. ' , $ this ->site_data ->site_url , implode ( ', ' , $ user_ips ) ) );
217213 }
218214
@@ -225,12 +221,8 @@ private function whitelist_create( $file, $user_ips, $existing_ips ) {
225221 */
226222 private function whitelist_append ( $ file , $ user_ips , $ existing_ips ) {
227223
228- $ all_ips = array_unique ( array_merge ( $ user_ips , $ existing_ips ) );
229- $ file_content = '' ;
230- foreach ( $ all_ips as $ individual_ip ) {
231- $ file_content .= "allow $ individual_ip; " . PHP_EOL ;
232- }
233- $ this ->fs ->dumpFile ( $ file , $ file_content );
224+ $ all_ips = array_unique ( array_merge ( $ user_ips , $ existing_ips ) );
225+ $ this ->put_ips_to_file ( $ file , $ all_ips );
234226 EE ::success ( sprintf ( 'Appended %s IP \'s to whitelist of `%s` scope ' , implode ( ', ' , $ user_ips ), $ this ->site_data ->site_url ) );
235227 }
236228
@@ -302,7 +294,7 @@ private function get_ips_from_file( $global ) {
302294 $ file .= $ global ? 'default_acl ' : $ this ->site_data ->site_url . '_acl ' ;
303295 $ existing_ips = [];
304296 if ( $ this ->fs ->exists ( $ file ) ) {
305- $ existing_ips_in_file = array_filter ( explode ( PHP_EOL , trim ( file_get_contents ( $ file ) ) ) , 'strlen ' );
297+ $ existing_ips_in_file = array_slice ( array_filter ( explode ( PHP_EOL , file_get_contents ( $ file ) ), 'trim ' ), 1 , - 1 );
306298 foreach ( $ existing_ips_in_file as $ ip_in_file ) {
307299 $ existing_ips [] = str_replace ( [ 'allow ' , '; ' ], '' , trim ( $ ip_in_file ) );
308300 }
@@ -311,6 +303,22 @@ private function get_ips_from_file( $global ) {
311303 return $ existing_ips ;
312304 }
313305
306+ /**
307+ * Function to put list of ip's into a file.
308+ *
309+ * @param string $file Path of file to write ip's in.
310+ * @param array $ips List of ip's.
311+ */
312+ private function put_ips_to_file ( $ file , $ ips ) {
313+
314+ $ file_content = 'satisfy any; ' . PHP_EOL ;
315+ foreach ( $ ips as $ ip ) {
316+ $ file_content .= "allow $ ip; " . PHP_EOL ;
317+ }
318+ $ file_content .= 'deny all; ' ;
319+ $ this ->fs ->dumpFile ( $ file , $ file_content );
320+ }
321+
314322 /**
315323 * Function to populate basic info from args
316324 *
0 commit comments