modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
api.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryembed.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
querytranscribe.php
3 years ago
rest.php
3 years ago
security.php
3 years ago
ui.php
3 years ago
security.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_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' ], 10, 3 ); |
| 15 | } |
| 16 | if ( !empty( $this->banned_words ) ) { |
| 17 | add_filter( 'mwai_ai_allowed', [ $this, 'check_banned_words' ], 10, 3 ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | function check_banned_ips( $ok, $query, $limits ) { |
| 22 | if ( $ok !== true ) { |
| 23 | return $ok; |
| 24 | } |
| 25 | $ip = $this->core->get_ip_address(); |
| 26 | if ( $this->is_blocked_ip( $ip, $this->banned_ips ) ) { |
| 27 | error_log( "AI Engine blocked IP: $ip" ); |
| 28 | return "Your query has been rejected."; |
| 29 | } |
| 30 | return $ok; |
| 31 | } |
| 32 | |
| 33 | function check_banned_words( $ok, $query, $limits ) { |
| 34 | if ( $ok !== true ) { |
| 35 | return $ok; |
| 36 | } |
| 37 | $text = $query->getLastPrompt(); |
| 38 | foreach ( $this->banned_words as $word ) { |
| 39 | if ( stripos( $text, $word ) !== false ) { |
| 40 | error_log( "AI Engine blocked word: $word" ); |
| 41 | return "Your query has been rejected."; |
| 42 | } |
| 43 | } |
| 44 | return $ok; |
| 45 | } |
| 46 | |
| 47 | function ip_in_range($ip, $range) |
| 48 | { |
| 49 | if (strpos($range, '/') === false) { |
| 50 | $range .= '/32'; // Convert single IP to CIDR notation |
| 51 | } |
| 52 | |
| 53 | list($range_ip, $subnet) = explode('/', $range, 2); |
| 54 | if (filter_var($range_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
| 55 | $ip_bin = ip2long($ip); |
| 56 | $range_ip_bin = ip2long($range_ip); |
| 57 | $subnet_mask = 0xFFFFFFFF << (32 - $subnet); |
| 58 | |
| 59 | return ($ip_bin & $subnet_mask) == ($range_ip_bin & $subnet_mask); |
| 60 | } elseif (filter_var($range_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
| 61 | $ip_bin = inet_pton($ip); |
| 62 | $range_ip_bin = inet_pton($range_ip); |
| 63 | $subnet_mask = str_repeat("\xFF", $subnet >> 3) . str_repeat("\x00", 16 - ($subnet >> 3)); |
| 64 | $subnet_mask[($subnet >> 3)] = chr(0xFF << (8 - ($subnet & 7))); |
| 65 | |
| 66 | return ($ip_bin & $subnet_mask) == ($range_ip_bin & $subnet_mask); |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | function is_blocked_ip($ip, $blocked_ips) |
| 73 | { |
| 74 | foreach ($blocked_ips as $range) { |
| 75 | if ($this->ip_in_range($ip, $range)) { |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return false; |
| 81 | } |
| 82 | } |