__( 'Malware analysis', 'ai-guardwall' ), 'hardening' => __( 'Security hardening advice', 'ai-guardwall' ), 'general' => __( 'Both', 'ai-guardwall' ), ); } /** * Insert (or refresh) the bundled files. * * @since 1.0.0 */ public static function register_bundled() { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ); $dir = plugin_dir_path( __FILE__ ) . 'data/knowledge/'; $files = array( 'malware-cleanup-instruction' => array( __( 'Malware detection & cleanup instructions', 'ai-guardwall' ), 'malware' ), 'wordpress-security-guide' => array( __( 'WordPress security guide', 'ai-guardwall' ), 'hardening' ), ); foreach ( $files as $slug => $info ) { $path = $dir . $slug . '.md'; if ( ! is_readable( $path ) ) { continue; } $content = (string) file_get_contents( $path ); $existing = $wpdb->get_row( $wpdb->prepare( "SELECT id, source FROM %i WHERE slug = %s LIMIT 1", $table, $slug ), ARRAY_A ); if ( $existing ) { if ( 'bundled' === $existing['source'] ) { $wpdb->update( $table, array( 'content' => $content ), array( 'id' => (int) $existing['id'] ) ); } continue; } $wpdb->insert( $table, array( 'name' => $info[0], 'slug' => $slug, 'source' => 'bundled', 'purpose' => $info[1], 'content' => $content, 'active' => 1, 'created_at' => gmdate( 'Y-m-d H:i:s' ), ) ); } } public static function all() { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, name, slug, source, purpose, active, created_at, CHAR_LENGTH(content) AS chars FROM %i ORDER BY source = 'bundled' DESC, id ASC", $table ), ARRAY_A ); return $rows ? $rows : array(); } public static function get( $id ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ); return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i WHERE id = %d", $table, (int) $id ), ARRAY_A ); } /** * Add an imported file. * * @since 1.0.0 * @param string $name Display name. * @param string $content Markdown. * @param string $purpose malware|hardening|general. * @return int|WP_Error */ public static function add( $name, $content, $purpose = 'malware' ) { global $wpdb; $content = str_replace( "\0", '', (string) $content ); $content = wp_check_invalid_utf8( $content, true ); if ( strlen( $content ) > self::MAX_FILE_BYTES ) { return new WP_Error( 'svabg_knowledge_size', __( 'Knowledge files can be up to 512 KB.', 'ai-guardwall' ) ); } if ( '' === trim( $content ) ) { return new WP_Error( 'svabg_knowledge_empty', __( 'The file is empty.', 'ai-guardwall' ) ); } $name = sanitize_text_field( $name ); if ( '' === $name && preg_match( '/^\s*#\s*(.+)$/m', $content, $m ) ) { $name = sanitize_text_field( $m[1] ); } if ( '' === $name ) { $name = __( 'Imported knowledge', 'ai-guardwall' ); } $purpose = isset( self::purposes()[ $purpose ] ) ? $purpose : 'malware'; $slug = sanitize_title( $name ) . '-' . wp_generate_password( 6, false, false ); $ok = $wpdb->insert( Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ), array( 'name' => mb_substr( $name, 0, 190 ), 'slug' => mb_substr( $slug, 0, 190 ), 'source' => 'imported', 'purpose' => $purpose, 'content' => $content, 'active' => 1, 'created_at' => gmdate( 'Y-m-d H:i:s' ), ) ); if ( ! $ok ) { return new WP_Error( 'svabg_knowledge_db', __( 'The file could not be saved.', 'ai-guardwall' ) ); } Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'settings', sprintf( /* translators: %s: name */ __( 'Knowledge file imported: %s', 'ai-guardwall' ), $name ) ); return (int) $wpdb->insert_id; } /** * Import an uploaded .md/.txt file ($_FILES entry). * * @since 1.0.0 * @param array $file One $_FILES entry. * @param string $purpose Purpose. * @return int|WP_Error */ public static function import_upload( $file, $purpose = 'malware' ) { if ( empty( $file['tmp_name'] ) || ! is_uploaded_file( $file['tmp_name'] ) ) { return new WP_Error( 'svabg_upload', __( 'No file was uploaded.', 'ai-guardwall' ) ); } $name = isset( $file['name'] ) ? (string) $file['name'] : ''; if ( ! preg_match( '/\.(md|markdown|txt)$/i', $name ) ) { return new WP_Error( 'svabg_upload_type', __( 'Only .md, .markdown and .txt files can be imported.', 'ai-guardwall' ) ); } if ( (int) $file['size'] > self::MAX_FILE_BYTES ) { return new WP_Error( 'svabg_knowledge_size', __( 'Knowledge files can be up to 512 KB.', 'ai-guardwall' ) ); } $content = file_get_contents( $file['tmp_name'] ); if ( false === $content ) { return new WP_Error( 'svabg_upload_read', __( 'The uploaded file could not be read.', 'ai-guardwall' ) ); } return self::add( preg_replace( '/\.(md|markdown|txt)$/i', '', sanitize_file_name( $name ) ), $content, $purpose ); } public static function set_active( $id, $active ) { global $wpdb; return false !== $wpdb->update( Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ), array( 'active' => $active ? 1 : 0 ), array( 'id' => (int) $id ) ); } public static function set_purpose( $id, $purpose ) { global $wpdb; $purpose = isset( self::purposes()[ $purpose ] ) ? $purpose : 'malware'; return false !== $wpdb->update( Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ), array( 'purpose' => $purpose ), array( 'id' => (int) $id ) ); } public static function delete( $id ) { global $wpdb; $row = self::get( $id ); if ( ! $row || 'bundled' === $row['source'] ) { return new WP_Error( 'svabg_knowledge_bundled', __( 'Bundled files cannot be deleted; switch them off instead.', 'ai-guardwall' ) ); } $wpdb->delete( Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ), array( 'id' => (int) $id ) ); return true; } /** * The active knowledge for a purpose as one text (bundled files first). * * @since 1.0.0 * @param string $purpose malware|hardening. * @return string */ public static function text_for( $purpose ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'knowledge' ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT name, content FROM %i WHERE active = 1 AND (purpose = %s OR purpose = 'general') ORDER BY source = 'bundled' DESC, id ASC", $table, $purpose ), ARRAY_A ); $out = ''; foreach ( (array) $rows as $row ) { $chunk = "\n\n\n" . trim( (string) $row['content'] ) . "\n"; if ( strlen( $out ) + strlen( $chunk ) > self::MAX_PROMPT_CHARS ) { $chunk = substr( $chunk, 0, max( 0, self::MAX_PROMPT_CHARS - strlen( $out ) ) ) . "\n[truncated]\n"; $out .= $chunk; break; } $out .= $chunk; } return trim( $out ); } }