chatbot.php
2 years ago
discussions.php
2 years ago
files.php
2 years ago
security.php
2 years ago
tasks.php
2 years ago
utilities.php
2 years ago
security.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_Security { |
| 4 | public $core = null; |
| 5 | public $banned_ips = []; |
| 6 | public $banned_words = []; |
| 7 | |
| 8 | public function __construct( $core ) { |
| 9 | $this->core = $core; |
| 10 | $this->banned_ips = $this->core->get_option( 'banned_ips' ); |
| 11 | $this->banned_words = $this->core->get_option( 'banned_words' ); |
| 12 | |
| 13 | if ( !empty( $this->banned_ips ) ) { |
| 14 | add_filter( 'mwai_ai_allowed', [ $this, 'check_banned_ips' ], 5, 3 ); |
| 15 | } |
| 16 | if ( !empty( $this->banned_words ) ) { |
| 17 | add_filter( 'mwai_ai_allowed', [ $this, 'check_banned_words' ], 5, 3 ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | function check_banned_ips( $ok, $query, $limits ) { |
| 22 | if ( $ok !== true || empty( $this->banned_ips ) ) { |
| 23 | return $ok; |
| 24 | } |
| 25 | if ( is_a( $query, 'Meow_MWAI_Query_Embed' ) ) { |
| 26 | if ( $this->core->can_access_settings() ) { |
| 27 | return $ok; |
| 28 | } |
| 29 | } |
| 30 | $ip = $this->core->get_ip_address(); |
| 31 | if ( $this->is_blocked_ip( $ip, $this->banned_ips ) ) { |
| 32 | error_log( "AI Engine blocked IP: $ip" ); |
| 33 | throw new Exception( "Your query has been rejected." ); |
| 34 | } |
| 35 | return $ok; |
| 36 | } |
| 37 | |
| 38 | function check_banned_words( $ok, $query, $limits ) { |
| 39 | if ( $ok !== true || empty( $this->banned_words ) ) { |
| 40 | return $ok; |
| 41 | } |
| 42 | if ( is_a( $query, 'Meow_MWAI_Query_Embed' ) ) { |
| 43 | if ( $this->core->can_access_settings() ) { |
| 44 | return $ok; |
| 45 | } |
| 46 | } |
| 47 | $text = $query->get_message(); |
| 48 | foreach ( $this->banned_words as $word ) { |
| 49 | // Use preg_quote to escape any special characters in the word |
| 50 | // This is necessary to safely include $word in the regex pattern |
| 51 | $pattern = '/\b' . preg_quote( $word, '/' ) . '\b/i'; |
| 52 | if ( preg_match( $pattern, $text ) ) { |
| 53 | error_log( "AI Engine blocked word: $word" ); |
| 54 | throw new Exception( "Your query has been rejected." ); |
| 55 | } |
| 56 | } |
| 57 | return $ok; |
| 58 | } |
| 59 | |
| 60 | function ip_in_range( $ip, $range ) { |
| 61 | if ( strpos( $range, '/' ) === false ) { |
| 62 | $range .= '/32'; // Convert single IP to CIDR notation |
| 63 | } |
| 64 | list($range_ip, $subnet) = explode('/', $range, 2); |
| 65 | if (filter_var($range_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
| 66 | $ip_bin = ip2long($ip); |
| 67 | $range_ip_bin = ip2long($range_ip); |
| 68 | $subnet_mask = 0xFFFFFFFF << (32 - $subnet); |
| 69 | return ($ip_bin & $subnet_mask) == ($range_ip_bin & $subnet_mask); |
| 70 | } |
| 71 | elseif (filter_var($range_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
| 72 | $ip_bin = inet_pton($ip); |
| 73 | $range_ip_bin = inet_pton($range_ip); |
| 74 | $subnet_mask = str_repeat("\xFF", $subnet >> 3) . str_repeat("\x00", 16 - ($subnet >> 3)); |
| 75 | $subnet_mask[($subnet >> 3)] = chr(0xFF << (8 - ($subnet & 7))); |
| 76 | return ($ip_bin & $subnet_mask) == ($range_ip_bin & $subnet_mask); |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | function is_blocked_ip( $ip, $blocked_ips) { |
| 82 | foreach ( $blocked_ips as $range ) { |
| 83 | if ( $this->ip_in_range( $ip, $range ) ) { |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | } |