Changeset 3266376
- Timestamp:
- 04/03/2025 12:51:01 PM (8 months ago)
- File:
-
- 1 edited
-
really-simple-ssl/trunk/class-wp-cli.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
really-simple-ssl/trunk/class-wp-cli.php
r3266361 r3266376 51 51 foreach ( $command_details as $command => $details ) { 52 52 53 if (isset($details['inactive']) && $details['inactive'] === true) {54 continue;55 }53 if (isset($details['inactive']) && $details['inactive'] === true) { 54 continue; 55 } 56 56 57 57 // Do not add Pro commands on free environment … … 312 312 * Activate recommended security headers via CLI 313 313 */ 314 public function activate_security_headers() {315 try {316 foreach (RSSSL()->headers->get_recommended_security_headers() as $header ) {317 if (isset($header['option_name'], $header['recommended_setting'])) {318 rsssl_update_option( $header['option_name'], $header['recommended_setting'] );319 }320 }321 WP_CLI::success( 'Recommended security header settings saved. Run "update_advanced_headers" command to activate them.' );322 do_action('rsssl_update_rules');323 } catch ( Exception $e ) {324 WP_CLI::error( 'Failed to activate security headers: ' . $e->getMessage() );325 }326 }314 public function activate_security_headers() { 315 try { 316 foreach (RSSSL()->headers->get_recommended_security_headers() as $header ) { 317 if (isset($header['option_name'], $header['recommended_setting'])) { 318 rsssl_update_option( $header['option_name'], $header['recommended_setting'] ); 319 } 320 } 321 WP_CLI::success( 'Recommended security header settings saved. Run "update_advanced_headers" command to activate them.' ); 322 do_action('rsssl_update_rules'); 323 } catch ( Exception $e ) { 324 WP_CLI::error( 'Failed to activate security headers: ' . $e->getMessage() ); 325 } 326 } 327 327 328 328 … … 331 331 */ 332 332 public function deactivate_security_headers() 333 {333 { 334 334 try { 335 335 $recommended_headers = RSSSL()->headers->get_recommended_security_headers(); 336 336 337 337 foreach ( $recommended_headers as $header ) { 338 if ( isset( $header['option_name'] ) && isset( $header['disabled_setting'] ) ) {339 rsssl_update_option($header['option_name'], $header['disabled_setting']);340 }338 if ( isset( $header['option_name'] ) && isset( $header['disabled_setting'] ) ) { 339 rsssl_update_option($header['option_name'], $header['disabled_setting']); 340 } 341 341 } 342 342 do_action('rsssl_update_rules'); … … 441 441 try { 442 442 rsssl_update_option( 'enforce_password_security_enabled', false ); 443 rsssl_update_option( 'enforce_frequent_password_change', false );444 rsssl_update_option( 'hide_rememberme', false );443 rsssl_update_option( 'enforce_frequent_password_change', false ); 444 rsssl_update_option( 'hide_rememberme', false ); 445 445 rsssl_update_option( 'enable_hibp_check', false ); 446 446 do_action('rsssl_update_rules'); … … 650 650 /** 651 651 * Reset the 2FA status of a user to disabled 652 *653 * Usage: wp rsssl reset_2fa 123654 *655 * @param array $args User ID should be the first element652 * 653 * Usage: wp rsssl reset_2fa 123 654 * 655 * @param array $args User ID should be the first element 656 656 */ 657 657 public function reset_2fa( $args ): void 658 {659 // When empty array is passed, WP_CLI will return an error660 if ( empty( $args ) ) {661 WP_CLI::error( 'Please provide a user ID.', true );662 }663 $user_id = intval( $args[0] );664 $user = get_user_by('id', $user_id);665 666 if (empty($user)) {667 WP_CLI::error('User not found.', true);668 }669 670 if (!class_exists('Rsssl_Two_Fa_Status')) {671 require_once rsssl_path . '/security/wordpress/two-fa/class-rsssl-two-fa-status.php';672 }673 674 \RSSSL\Security\WordPress\Two_Fa\Rsssl_Two_Fa_Status::reset_user_two_fa($user);675 WP_CLI::success( 'Successfully reset 2FA for user id ' . $user_id );658 { 659 // When empty array is passed, WP_CLI will return an error 660 if ( empty( $args ) ) { 661 WP_CLI::error( 'Please provide a user ID.', true ); 662 } 663 $user_id = intval( $args[0] ); 664 $user = get_user_by('id', $user_id); 665 666 if (empty($user)) { 667 WP_CLI::error('User not found.', true); 668 } 669 670 if (!class_exists('Rsssl_Two_Fa_Status')) { 671 require_once rsssl_path . '/security/wordpress/two-fa/class-rsssl-two-fa-status.php'; 672 } 673 674 \RSSSL\Security\WordPress\Two_Fa\Rsssl_Two_Fa_Status::reset_user_two_fa($user); 675 WP_CLI::success( 'Successfully reset 2FA for user id ' . $user_id ); 676 676 } 677 677 … … 683 683 public function update_advanced_headers() { 684 684 do_action('rsssl_update_rules'); 685 WP_CLI::success( 'Successfully update advanced headers.' ); 686 } 687 688 /** 689 * Add an IP to the firewall blocklist. 690 * 691 * @example wp rsssl add_firewall_ip_block 123.123.123.1 --note="This is a temporary block" 692 * @example wp rsssl add_firewall_ip_block 123.123.123.1 --permanent --note="This is a permanent block" 693 * 694 * @param array $args Should contain IP as the first element 695 * @param array $assoc_args Can contain a note with a 'note' key 696 */ 697 public function add_firewall_ip_block(array $args, array $assoc_args): void 698 { 699 $this->handleFirewallTableEntry($args, $assoc_args, 'blocked', 'add'); 700 } 701 702 /** 703 * Can be used to remove a (temporary) block from the firewall blocklist. 704 * @example wp rsssl remove_firewall_ip_block 123.123.123.1 705 * 706 * @param $args array Should contain the ip address 707 */ 708 public function remove_firewall_ip_block(array $args, array $assoc_args ): void 709 { 710 $this->handleFirewallTableEntry($args, $assoc_args, 'blocked', 'remove'); 711 } 712 713 /** 714 * Return a table of the current blocked IPs with the headers: 715 * IP Address, Note, Permanent 716 */ 717 public function show_blocked_ips() { 718 $columns = [ 719 'ip_address', 720 'note', 721 'permanent', 722 ]; 723 724 $blocked404Model = new Rsssl_404_Block(); 725 $blockedIps = $blocked404Model->get_blocked_ips($columns); 726 727 WP_CLI\Utils\format_items('table', $blockedIps, $columns); 728 } 729 730 /** 731 * Add an IP to the firewall's trusted list. 732 * 733 * Usage: wp rsssl add_firewall_trusted_ip 123.123.123.1 685 WP_CLI::success( 'Successfully update advanced headers.' ); 686 } 687 688 /** 689 * Add an IP to the firewall blocklist. 690 * 691 * @example wp rsssl add_firewall_ip_block 123.123.123.1 --note="This is a temporary block" 692 * @example wp rsssl add_firewall_ip_block 123.123.123.1 --permanent --note="This is a permanent block" 734 693 * 735 694 * @param array $args Should contain IP as the first element 736 695 * @param array $assoc_args Can contain a note with a 'note' key 737 * @uses handleFirewallTableEntry() 696 */ 697 public function add_firewall_ip_block(array $args, array $assoc_args): void 698 { 699 $this->handleFirewallTableEntry($args, $assoc_args, 'blocked', 'add'); 700 } 701 702 /** 703 * Can be used to remove a (temporary) block from the firewall blocklist. 704 * @example wp rsssl remove_firewall_ip_block 123.123.123.1 705 * 706 * @param $args array Should contain the ip address 707 */ 708 public function remove_firewall_ip_block(array $args, array $assoc_args ): void 709 { 710 $this->handleFirewallTableEntry($args, $assoc_args, 'blocked', 'remove'); 711 } 712 713 /** 714 * Return a table of the current blocked IPs with the headers: 715 * IP Address, Note, Permanent 716 */ 717 public function show_blocked_ips() { 718 $columns = [ 719 'ip_address', 720 'note', 721 'permanent', 722 ]; 723 724 $blocked404Model = new Rsssl_404_Block(); 725 $blockedIps = $blocked404Model->get_blocked_ips($columns); 726 727 WP_CLI\Utils\format_items('table', $blockedIps, $columns); 728 } 729 730 /** 731 * Add an IP to the firewall's trusted list. 732 * 733 * Usage: wp rsssl add_firewall_trusted_ip 123.123.123.1 734 * 735 * @param array $args Should contain IP as the first element 736 * @param array $assoc_args Can contain a note with a 'note' key 737 * @uses handleFirewallTableEntry() 738 738 */ 739 739 public function add_firewall_trusted_ip(array $args, array $assoc_args) { 740 $this->handleFirewallTableEntry($args, $assoc_args, 'trusted', 'add');741 } 742 743 /**744 * Remove an IP from the firewall's trusted list.745 *746 * Usage: wp rsssl remove_firewall_trusted_ip 123.123.123.1747 *748 * @param array $args Should contain IP as the first element749 * @param array $assoc_args Can contain a note with a 'note' key750 * @uses handleFirewallTableEntry()751 */752 public function remove_firewall_trusted_ip(array $args, array $assoc_args) {753 $this->handleFirewallTableEntry($args, $assoc_args, 'trusted', 'remove');754 }755 756 /**757 * Add an IP to the LLA's trusted list.758 *759 * Usage: wp rsssl add_lla_trusted_ip 123.123.123.1760 *761 * @param array $args Command arguments.762 * @uses handleLlaTableEntry()763 */764 public function add_lla_trusted_ip( $args ) {765 $this->handleLlaTableEntry($args, 'allowed', 'source_ip', 'add');766 }767 768 /**769 * Add an IP to the LLA's blocklist.770 *771 * Usage: wp rsssl remove_lla_trusted_ip 123.123.123.1772 *773 * @param array $args Command arguments.774 * @uses handleLlaTableEntry()775 */776 public function remove_lla_trusted_ip( $args ) {777 $this->handleLlaTableEntry($args, 'allowed', 'source_ip', 'remove');778 }779 780 /**781 * Remove an IP from the LLA's trusted list.782 *783 * Usage: wp rsssl add_lla_blocked_ip 123.123.123.1784 * Usage: wp rsssl add_lla_blocked_ip 123.123.123.1 --permanent785 *786 * @param array $args Command arguments.787 * @param array $assoc_args Associative arguments.788 * @uses handleLlaTableEntry()789 */790 public function add_lla_blocked_ip( $args, $assoc_args ) {791 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked');792 $this->handleLlaTableEntry($args, $status, 'source_ip', 'add');793 }794 795 /**796 * Remove an IP from the LLA's blocklist.797 *798 * Usage: wp rsssl remove_lla_blocked_ip 123.123.123.1799 * Usage: wp rsssl remove_lla_blocked_ip 123.123.123.1 --permanent800 *801 * @param array $args Command arguments.802 * @param array $assoc_args Associative arguments.803 * @uses handleLlaTableEntry()804 */805 public function remove_lla_blocked_ip( $args, $assoc_args ) {806 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked');807 $this->handleLlaTableEntry($args, $status, 'source_ip', 'remove');808 }809 810 /**811 * Add a username to the LLA's trusted list.812 *813 * Usage: wp rsssl add_lla_trusted_username username814 *815 * @param array $args Command arguments.816 * @uses handleLlaTableEntry()817 */818 public function add_lla_trusted_username( $args ) {819 $this->handleLlaTableEntry($args, 'allowed', 'username', 'add');820 }821 822 /**823 * Remove a username to the LLA's trusted list.824 *825 * Usage: wp rsssl remove_lla_trusted_username username826 *827 * @param array $args Command arguments.828 * @uses handleLlaTableEntry()829 */830 public function remove_lla_trusted_username( $args ) {831 $this->handleLlaTableEntry($args, 'allowed', 'username', 'remove');832 }833 834 /**835 * Add a username to the LLA's blocked list.836 *837 * Usage: wp rsssl add_lla_blocked_username username838 * Usage: wp rsssl add_lla_blocked_username username --permanent839 *840 * @param array $args Command arguments.841 * @param array $assoc_args Associative arguments.842 * @uses handleLlaTableEntry()843 */844 public function add_lla_blocked_username( array $args, array $assoc_args ) {845 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked');846 $this->handleLlaTableEntry($args, $status, 'username', 'add');847 }848 849 /**850 * Remove a username to the LLA's blocked list.851 *852 * Usage: wp rsssl remove_lla_blocked_username username853 * Usage: wp rsssl remove_lla_blocked_username username --permanent854 *855 * @param array $args Command arguments.856 * @param array $assoc_args Associative arguments.857 * @uses handleLlaTableEntry()858 */859 public function remove_lla_blocked_username( $args, $assoc_args ) {860 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked');861 $this->handleLlaTableEntry($args, $status, 'username', 'remove');862 }863 864 /**865 * Handle an action for the firewall table for a specific IP address.866 *867 * @param array $args Command arguments.868 * @param array $assoc_args Associative arguments.869 * @param string $status Should be either 'trusted' or 'blocked'.870 * @param string $action Should be either 'add' or 'remove'.871 *872 * @uses remove_white_list_ip() & add_white_list_ip() from Rsssl_Geo_Block -873 * Those also handle a block request for an IP address.874 */875 protected function handleFirewallTableEntry(array $args, array $assoc_args, string $status, string $action)876 {877 if (rsssl_get_option('enable_firewall', false) !== true) {878 WP_CLI::error('The firewall is not enabled.', true);879 }880 881 if (!in_array($status, ['trusted', 'blocked']) || !in_array($action, ['add', 'remove'])) {882 WP_CLI::error('Could not handle action for the firewall table.', true);883 }884 885 if (empty($args[0])) {886 WP_CLI::error('Please provide an IP address.', true);887 }888 889 $ip = $this->getFilteredIpAddress($args[0]);890 891 // Prepare data for adding to the whitelist.892 $data = [893 'ip_address' => $ip,894 'note' => $assoc_args['note'] ?? '',895 'status' => $status,896 'permanent' => isset($assoc_args['permanent']),897 ];898 899 // Use the Rsssl_Geo_Block class to add the trusted IP.900 if (!class_exists('\RSSSL\Pro\Security\WordPress\Rsssl_Geo_Block')) {901 require_once rsssl_path . 'pro/security/wordpress/rsssl-geo-block.php';902 }903 904 try {905 $geo_block = new \RSSSL\Pro\Security\WordPress\Rsssl_Geo_Block();906 907 // fallback908 $response = ['success' => false, 'message' => 'Something went wrong!'];909 910 if ($action === 'remove') {911 $response = $geo_block->remove_white_list_ip( $data );912 }913 914 if ($action === 'add') {915 $response = $geo_block->add_white_list_ip( $data );916 }917 } catch ( \Exception $e ) {918 WP_CLI::error( 'Failed to handle IP entry: ' . $e->getMessage(), true );919 }920 921 // Handle response.922 if ( $response['success'] ) {923 WP_CLI::success( $response['message'] );924 return;925 }926 927 WP_CLI::error( $response['message'], true );928 }929 930 /**931 * Handle an action for the LLA table for a specific IP address.932 *933 * @param array $args Command arguments.934 * @param string $status Should be either 'allowed' or 'blocked'.935 * @param string $type Should be either 'source_ip' or 'username'.936 * @param string $action Should be either 'add' or 'remove'.937 * @return void938 */939 protected function handleLlaTableEntry(array $args, string $status, string $type, string $action): void940 {941 if (rsssl_get_option('enable_limited_login_attempts', false) !== true) {942 WP_CLI::error('The LLA feature is not enabled.', true);943 }944 945 if (empty($args[0])) {946 WP_CLI::error('Please provide the command the necessary arguments', true);947 }948 949 if (!in_array($status, ['allowed', 'blocked', 'locked']) || !in_array($type, ['source_ip', 'username'])) {950 WP_CLI::error('Something went wrong! Could not handle command.', true);951 }952 953 $value = '';954 if ($type === 'source_ip') {955 $value = $this->getFilteredIpAddress($args[0]);956 }957 958 if ($type === 'username') {959 $value = sanitize_text_field($args[0]);960 }961 962 // Use the Rsssl_Limit_Login_Admin class to add the trusted IP.963 if (!class_exists('\RSSSL\Pro\Security\WordPress\Rsssl_Limit_Login_Admin')) {964 require_once rsssl_path . 'pro/security/wordpress/class-rsssl-limit-login-admin.php';965 }966 967 try {968 $lla = new \RSSSL\Pro\Security\WordPress\Rsssl_Limit_Login_Admin();969 970 // fallback971 $response = ['success' => false, 'message' => 'Something went wrong!'];972 973 if ($action === 'add') {974 $response = $lla->handle_entity([975 'value' => $value,976 'status' => sanitize_text_field($status),977 ], $type);978 }979 980 if ($action === 'remove') {981 $entry = $lla->get_entry($type, $value, $status);982 $response = $lla->delete_entries([983 'id' => $entry['id'],984 ]);985 }986 } catch ( Exception $e ) {987 WP_CLI::error( 'Failed to handle LLA entry: ' . $e->getMessage(), true );988 }989 990 // Handle response.991 if ( $response['success'] ) {992 WP_CLI::success( $response['message'] );993 return;994 }995 996 WP_CLI::error( $response['message'], true );997 }998 999 /**1000 * Return a filtered IP address. Method will exit() if the IP address is1001 * invalid with the WP_CLI error message: Invalid IP address provided.1002 */1003 protected function getFilteredIpAddress(string $originalIp): string1004 {1005 $ip = filter_var($originalIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);1006 if (strpos($originalIp, ':')) {1007 $ip = filter_var($originalIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);1008 }1009 1010 if (empty($ip)) {1011 WP_CLI::error('Invalid IP address provided.', true);1012 }1013 1014 return $ip;1015 }740 $this->handleFirewallTableEntry($args, $assoc_args, 'trusted', 'add'); 741 } 742 743 /** 744 * Remove an IP from the firewall's trusted list. 745 * 746 * Usage: wp rsssl remove_firewall_trusted_ip 123.123.123.1 747 * 748 * @param array $args Should contain IP as the first element 749 * @param array $assoc_args Can contain a note with a 'note' key 750 * @uses handleFirewallTableEntry() 751 */ 752 public function remove_firewall_trusted_ip(array $args, array $assoc_args) { 753 $this->handleFirewallTableEntry($args, $assoc_args, 'trusted', 'remove'); 754 } 755 756 /** 757 * Add an IP to the LLA's trusted list. 758 * 759 * Usage: wp rsssl add_lla_trusted_ip 123.123.123.1 760 * 761 * @param array $args Command arguments. 762 * @uses handleLlaTableEntry() 763 */ 764 public function add_lla_trusted_ip( $args ) { 765 $this->handleLlaTableEntry($args, 'allowed', 'source_ip', 'add'); 766 } 767 768 /** 769 * Add an IP to the LLA's blocklist. 770 * 771 * Usage: wp rsssl remove_lla_trusted_ip 123.123.123.1 772 * 773 * @param array $args Command arguments. 774 * @uses handleLlaTableEntry() 775 */ 776 public function remove_lla_trusted_ip( $args ) { 777 $this->handleLlaTableEntry($args, 'allowed', 'source_ip', 'remove'); 778 } 779 780 /** 781 * Remove an IP from the LLA's trusted list. 782 * 783 * Usage: wp rsssl add_lla_blocked_ip 123.123.123.1 784 * Usage: wp rsssl add_lla_blocked_ip 123.123.123.1 --permanent 785 * 786 * @param array $args Command arguments. 787 * @param array $assoc_args Associative arguments. 788 * @uses handleLlaTableEntry() 789 */ 790 public function add_lla_blocked_ip( $args, $assoc_args ) { 791 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked'); 792 $this->handleLlaTableEntry($args, $status, 'source_ip', 'add'); 793 } 794 795 /** 796 * Remove an IP from the LLA's blocklist. 797 * 798 * Usage: wp rsssl remove_lla_blocked_ip 123.123.123.1 799 * Usage: wp rsssl remove_lla_blocked_ip 123.123.123.1 --permanent 800 * 801 * @param array $args Command arguments. 802 * @param array $assoc_args Associative arguments. 803 * @uses handleLlaTableEntry() 804 */ 805 public function remove_lla_blocked_ip( $args, $assoc_args ) { 806 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked'); 807 $this->handleLlaTableEntry($args, $status, 'source_ip', 'remove'); 808 } 809 810 /** 811 * Add a username to the LLA's trusted list. 812 * 813 * Usage: wp rsssl add_lla_trusted_username username 814 * 815 * @param array $args Command arguments. 816 * @uses handleLlaTableEntry() 817 */ 818 public function add_lla_trusted_username( $args ) { 819 $this->handleLlaTableEntry($args, 'allowed', 'username', 'add'); 820 } 821 822 /** 823 * Remove a username to the LLA's trusted list. 824 * 825 * Usage: wp rsssl remove_lla_trusted_username username 826 * 827 * @param array $args Command arguments. 828 * @uses handleLlaTableEntry() 829 */ 830 public function remove_lla_trusted_username( $args ) { 831 $this->handleLlaTableEntry($args, 'allowed', 'username', 'remove'); 832 } 833 834 /** 835 * Add a username to the LLA's blocked list. 836 * 837 * Usage: wp rsssl add_lla_blocked_username username 838 * Usage: wp rsssl add_lla_blocked_username username --permanent 839 * 840 * @param array $args Command arguments. 841 * @param array $assoc_args Associative arguments. 842 * @uses handleLlaTableEntry() 843 */ 844 public function add_lla_blocked_username( array $args, array $assoc_args ) { 845 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked'); 846 $this->handleLlaTableEntry($args, $status, 'username', 'add'); 847 } 848 849 /** 850 * Remove a username to the LLA's blocked list. 851 * 852 * Usage: wp rsssl remove_lla_blocked_username username 853 * Usage: wp rsssl remove_lla_blocked_username username --permanent 854 * 855 * @param array $args Command arguments. 856 * @param array $assoc_args Associative arguments. 857 * @uses handleLlaTableEntry() 858 */ 859 public function remove_lla_blocked_username( $args, $assoc_args ) { 860 $status = (isset($assoc_args['permanent']) ? 'blocked' : 'locked'); 861 $this->handleLlaTableEntry($args, $status, 'username', 'remove'); 862 } 863 864 /** 865 * Handle an action for the firewall table for a specific IP address. 866 * 867 * @param array $args Command arguments. 868 * @param array $assoc_args Associative arguments. 869 * @param string $status Should be either 'trusted' or 'blocked'. 870 * @param string $action Should be either 'add' or 'remove'. 871 * 872 * @uses remove_white_list_ip() & add_white_list_ip() from Rsssl_Geo_Block - 873 * Those also handle a block request for an IP address. 874 */ 875 protected function handleFirewallTableEntry(array $args, array $assoc_args, string $status, string $action) 876 { 877 if (rsssl_get_option('enable_firewall', false) !== true) { 878 WP_CLI::error('The firewall is not enabled.', true); 879 } 880 881 if (!in_array($status, ['trusted', 'blocked']) || !in_array($action, ['add', 'remove'])) { 882 WP_CLI::error('Could not handle action for the firewall table.', true); 883 } 884 885 if (empty($args[0])) { 886 WP_CLI::error('Please provide an IP address.', true); 887 } 888 889 $ip = $this->getFilteredIpAddress($args[0]); 890 891 // Prepare data for adding to the whitelist. 892 $data = [ 893 'ip_address' => $ip, 894 'note' => $assoc_args['note'] ?? '', 895 'status' => $status, 896 'permanent' => isset($assoc_args['permanent']), 897 ]; 898 899 // Use the Rsssl_Geo_Block class to add the trusted IP. 900 if (!class_exists('\RSSSL\Pro\Security\WordPress\Rsssl_Geo_Block')) { 901 require_once rsssl_path . 'pro/security/wordpress/rsssl-geo-block.php'; 902 } 903 904 try { 905 $geo_block = new \RSSSL\Pro\Security\WordPress\Rsssl_Geo_Block(); 906 907 // fallback 908 $response = ['success' => false, 'message' => 'Something went wrong!']; 909 910 if ($action === 'remove') { 911 $response = $geo_block->remove_white_list_ip( $data ); 912 } 913 914 if ($action === 'add') { 915 $response = $geo_block->add_white_list_ip( $data ); 916 } 917 } catch ( \Exception $e ) { 918 WP_CLI::error( 'Failed to handle IP entry: ' . $e->getMessage(), true ); 919 } 920 921 // Handle response. 922 if ( $response['success'] ) { 923 WP_CLI::success( $response['message'] ); 924 return; 925 } 926 927 WP_CLI::error( $response['message'], true ); 928 } 929 930 /** 931 * Handle an action for the LLA table for a specific IP address. 932 * 933 * @param array $args Command arguments. 934 * @param string $status Should be either 'allowed' or 'blocked'. 935 * @param string $type Should be either 'source_ip' or 'username'. 936 * @param string $action Should be either 'add' or 'remove'. 937 * @return void 938 */ 939 protected function handleLlaTableEntry(array $args, string $status, string $type, string $action): void 940 { 941 if (rsssl_get_option('enable_limited_login_attempts', false) !== true) { 942 WP_CLI::error('The LLA feature is not enabled.', true); 943 } 944 945 if (empty($args[0])) { 946 WP_CLI::error('Please provide the command the necessary arguments', true); 947 } 948 949 if (!in_array($status, ['allowed', 'blocked', 'locked']) || !in_array($type, ['source_ip', 'username'])) { 950 WP_CLI::error('Something went wrong! Could not handle command.', true); 951 } 952 953 $value = ''; 954 if ($type === 'source_ip') { 955 $value = $this->getFilteredIpAddress($args[0]); 956 } 957 958 if ($type === 'username') { 959 $value = sanitize_text_field($args[0]); 960 } 961 962 // Use the Rsssl_Limit_Login_Admin class to add the trusted IP. 963 if (!class_exists('\RSSSL\Pro\Security\WordPress\Rsssl_Limit_Login_Admin')) { 964 require_once rsssl_path . 'pro/security/wordpress/class-rsssl-limit-login-admin.php'; 965 } 966 967 try { 968 $lla = new \RSSSL\Pro\Security\WordPress\Rsssl_Limit_Login_Admin(); 969 970 // fallback 971 $response = ['success' => false, 'message' => 'Something went wrong!']; 972 973 if ($action === 'add') { 974 $response = $lla->handle_entity([ 975 'value' => $value, 976 'status' => sanitize_text_field($status), 977 ], $type); 978 } 979 980 if ($action === 'remove') { 981 $entry = $lla->get_entry($type, $value, $status); 982 $response = $lla->delete_entries([ 983 'id' => $entry['id'], 984 ]); 985 } 986 } catch ( Exception $e ) { 987 WP_CLI::error( 'Failed to handle LLA entry: ' . $e->getMessage(), true ); 988 } 989 990 // Handle response. 991 if ( $response['success'] ) { 992 WP_CLI::success( $response['message'] ); 993 return; 994 } 995 996 WP_CLI::error( $response['message'], true ); 997 } 998 999 /** 1000 * Return a filtered IP address. Method will exit() if the IP address is 1001 * invalid with the WP_CLI error message: Invalid IP address provided. 1002 */ 1003 protected function getFilteredIpAddress(string $originalIp): string 1004 { 1005 $ip = filter_var($originalIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); 1006 if (strpos($originalIp, ':')) { 1007 $ip = filter_var($originalIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); 1008 } 1009 1010 if (empty($ip)) { 1011 WP_CLI::error('Invalid IP address provided.', true); 1012 } 1013 1014 return $ip; 1015 } 1016 1016 1017 1017 /** … … 1157 1157 'pro' => false, 1158 1158 ], 1159 'add_firewall_ip_block' => [1160 'description' => __( 'Add IP block.', 'really-simple-ssl' ),1161 'synopsis' => [1162 [1163 'type' => 'positional',1164 'name' => 'ip_address',1165 'optional' => false,1166 'description' => __( 'The IP to block.', 'really-simple-ssl' ),1167 ],1168 [1169 'type' => 'flag',1170 'name' => 'permanent',1171 'optional' => true,1172 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ),1173 ],1174 [1175 'type' => 'assoc',1176 'name' => 'note',1177 'optional' => true,1178 'description' => __( 'Optional note for the block.', 'really-simple-ssl' ),1179 ],1180 ],1181 'pro' => false,1182 ],1159 'add_firewall_ip_block' => [ 1160 'description' => __( 'Add IP block.', 'really-simple-ssl' ), 1161 'synopsis' => [ 1162 [ 1163 'type' => 'positional', 1164 'name' => 'ip_address', 1165 'optional' => false, 1166 'description' => __( 'The IP to block.', 'really-simple-ssl' ), 1167 ], 1168 [ 1169 'type' => 'flag', 1170 'name' => 'permanent', 1171 'optional' => true, 1172 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ), 1173 ], 1174 [ 1175 'type' => 'assoc', 1176 'name' => 'note', 1177 'optional' => true, 1178 'description' => __( 'Optional note for the block.', 'really-simple-ssl' ), 1179 ], 1180 ], 1181 'pro' => true, 1182 ], 1183 1183 'remove_firewall_ip_block' => [ 1184 1184 'description' => __( 'Remove IP block.', 'really-simple-ssl' ), 1185 'synopsis' => [1186 [1187 'type' => 'positional',1188 'name' => 'ip_address',1189 'optional' => false,1190 'description' => __( 'The IP to remove the block for.', 'really-simple-ssl' ),1191 ],1192 ],1193 'pro' => false,1185 'synopsis' => [ 1186 [ 1187 'type' => 'positional', 1188 'name' => 'ip_address', 1189 'optional' => false, 1190 'description' => __( 'The IP to remove the block for.', 'really-simple-ssl' ), 1191 ], 1192 ], 1193 'pro' => true, 1194 1194 ], 1195 1195 'show_blocked_ips' => [ 1196 1196 'description' => __( 'Show blocked IP\'s.', 'really-simple-ssl' ), 1197 1197 'synopsis' => [], 1198 'pro' => false,1198 'pro' => true, 1199 1199 ], 1200 1200 'add_firewall_trusted_ip' => [ 1201 1201 'description' => __( 'Add a trusted IP to the firewall.', 'really-simple-ssl' ), 1202 1202 'synopsis' => [], 1203 'pro' => false,1204 ], 1205 'remove_firewall_trusted_ip' => [1203 'pro' => true, 1204 ], 1205 'remove_firewall_trusted_ip' => [ 1206 1206 'description' => __( 'Remove a trusted IP from the firewall.', 'really-simple-ssl' ), 1207 1207 'synopsis' => [], 1208 'pro' => false,1209 ], 1210 'add_lla_trusted_ip' => [1211 'description' => __( 'Add a trusted IP to the limit login attempts table.', 'really-simple-ssl' ),1212 'synopsis' => [],1213 'pro' => false,1214 ],1215 'remove_lla_trusted_ip' => [1216 'description' => __( 'Remove a trusted IP from the limit login attempts table.', 'really-simple-ssl' ),1217 'synopsis' => [],1218 'pro' => false,1219 ],1220 'add_lla_blocked_ip' => [1221 'description' => __( 'Add a blocked IP to the limit login attempts table.', 'really-simple-ssl' ),1222 'synopsis' => [1223 [1224 'type' => 'positional',1225 'name' => 'ip_address',1226 'optional' => false,1227 'description' => __( 'The IP to block.', 'really-simple-ssl' ),1228 ],1229 [1230 'type' => 'flag',1231 'name' => 'permanent',1232 'optional' => true,1233 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ),1234 ],1235 ],1236 'pro' => false,1237 ],1238 'remove_lla_blocked_ip' => [1239 'description' => __( 'Remove a blocked IP from the limit login attempts table.', 'really-simple-ssl' ),1240 'synopsis' => [1241 [1242 'type' => 'positional',1243 'name' => 'ip_address',1244 'optional' => false,1245 'description' => __( 'The IP to block.', 'really-simple-ssl' ),1246 ],1247 [1248 'type' => 'flag',1249 'name' => 'permanent',1250 'optional' => true,1251 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ),1252 ],1253 ],1254 'pro' => false,1255 ],1256 'add_lla_trusted_username' => [1257 'description' => __( 'Add a trusted username to the limit login attempts table.', 'really-simple-ssl' ),1258 'synopsis' => [],1259 'pro' => false,1260 ],1261 'remove_lla_trusted_username' => [1262 'description' => __( 'Remove a trusted username from the limit login attempts table.', 'really-simple-ssl' ),1263 'synopsis' => [],1264 'pro' => false,1265 ],1266 'add_lla_blocked_username' => [1267 'description' => __( 'Add a blocked username to the limit login attempts table.', 'really-simple-ssl' ),1268 'synopsis' => [1269 [1270 'type' => 'positional',1271 'name' => 'ip_address',1272 'optional' => false,1273 'description' => __( 'The username to block.', 'really-simple-ssl' ),1274 ],1275 [1276 'type' => 'flag',1277 'name' => 'permanent',1278 'optional' => true,1279 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ),1280 ],1281 ],1282 'pro' => false,1283 ],1284 'remove_lla_blocked_username' => [1285 'description' => __( 'Remove a blocked username from the limit login attempts table.', 'really-simple-ssl' ),1286 'synopsis' => [1287 [1288 'type' => 'positional',1289 'name' => 'username',1290 'optional' => false,1291 'description' => __( 'The username to remove the block for.', 'really-simple-ssl' ),1292 ],1293 [1294 'type' => 'flag',1295 'name' => 'permanent',1296 'optional' => true,1297 'description' => __( 'Flag to remove a permanent block.', 'really-simple-ssl' ),1298 ],1299 ],1300 'pro' => false,1301 ],1208 'pro' => true, 1209 ], 1210 'add_lla_trusted_ip' => [ 1211 'description' => __( 'Add a trusted IP to the limit login attempts table.', 'really-simple-ssl' ), 1212 'synopsis' => [], 1213 'pro' => true, 1214 ], 1215 'remove_lla_trusted_ip' => [ 1216 'description' => __( 'Remove a trusted IP from the limit login attempts table.', 'really-simple-ssl' ), 1217 'synopsis' => [], 1218 'pro' => true, 1219 ], 1220 'add_lla_blocked_ip' => [ 1221 'description' => __( 'Add a blocked IP to the limit login attempts table.', 'really-simple-ssl' ), 1222 'synopsis' => [ 1223 [ 1224 'type' => 'positional', 1225 'name' => 'ip_address', 1226 'optional' => false, 1227 'description' => __( 'The IP to block.', 'really-simple-ssl' ), 1228 ], 1229 [ 1230 'type' => 'flag', 1231 'name' => 'permanent', 1232 'optional' => true, 1233 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ), 1234 ], 1235 ], 1236 'pro' => true, 1237 ], 1238 'remove_lla_blocked_ip' => [ 1239 'description' => __( 'Remove a blocked IP from the limit login attempts table.', 'really-simple-ssl' ), 1240 'synopsis' => [ 1241 [ 1242 'type' => 'positional', 1243 'name' => 'ip_address', 1244 'optional' => false, 1245 'description' => __( 'The IP to block.', 'really-simple-ssl' ), 1246 ], 1247 [ 1248 'type' => 'flag', 1249 'name' => 'permanent', 1250 'optional' => true, 1251 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ), 1252 ], 1253 ], 1254 'pro' => true, 1255 ], 1256 'add_lla_trusted_username' => [ 1257 'description' => __( 'Add a trusted username to the limit login attempts table.', 'really-simple-ssl' ), 1258 'synopsis' => [], 1259 'pro' => true, 1260 ], 1261 'remove_lla_trusted_username' => [ 1262 'description' => __( 'Remove a trusted username from the limit login attempts table.', 'really-simple-ssl' ), 1263 'synopsis' => [], 1264 'pro' => true, 1265 ], 1266 'add_lla_blocked_username' => [ 1267 'description' => __( 'Add a blocked username to the limit login attempts table.', 'really-simple-ssl' ), 1268 'synopsis' => [ 1269 [ 1270 'type' => 'positional', 1271 'name' => 'ip_address', 1272 'optional' => false, 1273 'description' => __( 'The username to block.', 'really-simple-ssl' ), 1274 ], 1275 [ 1276 'type' => 'flag', 1277 'name' => 'permanent', 1278 'optional' => true, 1279 'description' => __( 'Flag to add a permanent block.', 'really-simple-ssl' ), 1280 ], 1281 ], 1282 'pro' => true, 1283 ], 1284 'remove_lla_blocked_username' => [ 1285 'description' => __( 'Remove a blocked username from the limit login attempts table.', 'really-simple-ssl' ), 1286 'synopsis' => [ 1287 [ 1288 'type' => 'positional', 1289 'name' => 'username', 1290 'optional' => false, 1291 'description' => __( 'The username to remove the block for.', 'really-simple-ssl' ), 1292 ], 1293 [ 1294 'type' => 'flag', 1295 'name' => 'permanent', 1296 'optional' => true, 1297 'description' => __( 'Flag to remove a permanent block.', 'really-simple-ssl' ), 1298 ], 1299 ], 1300 'pro' => true, 1301 ], 1302 1302 ]; 1303 1303 }
Note: See TracChangeset
for help on using the changeset viewer.