@@ -324,28 +324,14 @@ function ip_is_private( $ip )
324
324
return false ;
325
325
}
326
326
327
- /**
328
- * Checks the status for a given IP. API results are cached as transients
329
- *
330
- * @param bool $preauth Wether or not we are checking prior to authorization
331
- *
332
- * @return bool Either returns true, fires $this->kill_login, or includes a math fallback
333
- */
334
- function check_loginability ( $ preauth = false ) {
335
-
336
- $ ip = $ this ->get_ip ();
337
- $ headers = $ this ->get_headers ();
338
- $ header_hash = md5 ( json_encode ( $ headers ) );
339
- $ transient_name = 'jpp_li_ ' . $ header_hash ;
340
- $ transient_value = $ this ->get_transient ( $ transient_name );
327
+ function ip_is_whitelisted ( $ ip ) {
341
328
342
- //Never block login from whitelisted IPs
343
329
if ( defined ( 'JETPACK_IP_ADDRESS_OK ' ) && 'JETPACK_IP_ADDRESS_OK ' == $ ip ) { // found an exact match in wp-config
344
330
return true ;
345
331
}
346
332
347
- $ whitelist = get_site_option ( 'jetpack_protect_whitelist ' , array () );
348
- $ ip_long = inet_pton ( $ ip );
333
+ $ whitelist = get_site_option ( 'jetpack_protect_whitelist ' , array () );
334
+ $ ip_long = inet_pton ( $ ip );
349
335
350
336
if ( ! empty ( $ whitelist ) ) :
351
337
foreach ( $ whitelist as $ item ) :
@@ -364,7 +350,27 @@ function check_loginability( $preauth = false ) {
364
350
365
351
endforeach ;
366
352
endif ;
353
+ return false ;
354
+ }
355
+
356
+ /**
357
+ * Checks the status for a given IP. API results are cached as transients
358
+ *
359
+ * @param bool $preauth Wether or not we are checking prior to authorization
360
+ *
361
+ * @return bool Either returns true, fires $this->kill_login, or includes a math fallback
362
+ */
363
+ function check_loginability ( $ preauth = false ) {
364
+
365
+ $ headers = $ this ->get_headers ();
366
+ $ header_hash = md5 ( json_encode ( $ headers ) );
367
+ $ transient_name = 'jpp_li_ ' . $ header_hash ;
368
+ $ transient_value = $ this ->get_transient ( $ transient_name );
369
+ $ ip = $ this ->get_ip ();
367
370
371
+ if ( $ this ->ip_is_whitelisted ( $ ip ) ) {
372
+ return true ;
373
+ }
368
374
369
375
//Check out our transients
370
376
if ( isset ( $ transient_value ) && $ transient_value [ 'status ' ] == 'ok ' ) {
@@ -564,79 +570,6 @@ public function save_whitelist_ajax() {
564
570
exit ;
565
571
}
566
572
567
- public function save_whitelist ( $ global = false ) {
568
-
569
- global $ current_user ;
570
- $ whitelist = is_array ( $ _POST ['whitelist ' ] ) ? $ _POST ['whitelist ' ] : array ();
571
- $ new_items = array ();
572
-
573
- // validate each item
574
- foreach ( $ whitelist as $ item ) {
575
-
576
- if ( ! isset ( $ item ['range ' ] ) ) {
577
- $ this ->whitelist_error = true ;
578
- break ;
579
- }
580
-
581
- if ( ! in_array ( $ item ['range ' ], array ( '1 ' , '0 ' ) ) ) {
582
- $ this ->whitelist_error = true ;
583
- break ;
584
- }
585
-
586
- $ range = $ item ['range ' ];
587
- $ new_item = new stdClass ();
588
- $ new_item ->range = (bool ) $ range ;
589
- $ new_item ->global = $ global ;
590
- $ new_item ->user_id = $ current_user ->ID ;
591
-
592
- if ( $ range ) {
593
-
594
- if ( ! isset ( $ item ['range_low ' ] ) || ! isset ( $ item ['range_high ' ] ) ) {
595
- $ this ->whitelist_error = true ;
596
- break ;
597
- }
598
-
599
- if ( ! inet_pton ( $ item ['range_low ' ] ) || ! inet_pton ( $ item ['range_high ' ] ) ) {
600
- $ this ->whitelist_error = true ;
601
- break ;
602
- }
603
-
604
- $ new_item ->range_low = $ item ['range_low ' ];
605
- $ new_item ->range_high = $ item ['range_high ' ];
606
-
607
- } else {
608
-
609
- if ( ! isset ( $ item ['ip_address ' ] ) ) {
610
- $ this ->whitelist_error = true ;
611
- break ;
612
- }
613
-
614
- if ( ! inet_pton ( $ item ['ip_address ' ] ) ) {
615
- $ this ->whitelist_error = true ;
616
- break ;
617
- }
618
-
619
- $ new_item ->ip_address = $ item ['ip_address ' ];
620
- }
621
-
622
- $ new_items [] = $ new_item ;
623
-
624
- } // end item loop
625
-
626
- if ( ! empty ( $ this ->whitelist_error ) ) {
627
- return false ;
628
- }
629
-
630
- // merge new items with existing items
631
- $ this ->whitelist = get_site_option ( 'jetpack_protect_whitelist ' , array () );
632
- $ current_user_whitelist = wp_list_filter ( $ this ->whitelist , array ( 'user_id ' => $ current_user ->ID , 'global ' => ! $ global ) );
633
- $ other_user_whtielist = wp_list_filter ( $ this ->whitelist , array ( 'user_id ' => $ current_user ->ID ), 'NOT ' );
634
- $ new_whitelist = array_merge ( $ new_items , $ current_user_whitelist , $ other_user_whtielist );
635
-
636
- update_site_option ( 'jetpack_protect_whitelist ' , $ new_whitelist );
637
- return true ;
638
- }
639
-
640
573
/**
641
574
* Calls over to the api using wp_remote_post
642
575
*
0 commit comments