Skip to content
wiki.fftac.org

AI Engine - Source Excerpt 03

Back to AI Engine

Summary

This source excerpt preserves a bounded section of Anti-Christ.org/wp-content/plugins/fftac-ai-engine/inc/ai-engine.php so readers can inspect the evidence without opening the full source file.

**Source path:** Anti-Christ.org/wp-content/plugins/fftac-ai-engine/inc/ai-engine.php

update_option($option_keys['encrypted'], $encrypted, false);
    update_option($option_keys['hint'], antichrist_engine_foundation_ai_get_api_key_hint($api_key), false);
    update_option($option_keys['saved_at'], gmdate('Y-m-d H:i:s'), false);

    return true;
}

/**
 * Deletes the encrypted Foundation API key from WordPress options.
 *
 * @return void
 */
function antichrist_engine_foundation_ai_delete_site_api_key()
{
    $option_keys = antichrist_engine_foundation_ai_get_site_key_option_keys();

    update_option($option_keys['encrypted'], '', false);
    update_option($option_keys['hint'], '', false);
    update_option($option_keys['saved_at'], '', false);
}

/**
 * Gets the Foundation API key saved in WordPress options.
 *
 * @return string
 */
function antichrist_engine_foundation_ai_get_stored_site_api_key()
{
    $option_keys = antichrist_engine_foundation_ai_get_site_key_option_keys();
    $stored = (string) get_option($option_keys['encrypted'], '');

    if ('' === $stored) {
        return '';
    }

    return antichrist_engine_foundation_ai_decrypt_secret($stored);
}

/**
 * Gets the active Foundation API key source.
 *
 * @return string
 */
function antichrist_engine_foundation_ai_get_site_api_key_source()
{
    if (defined('OPENAI_API_KEY') && OPENAI_API_KEY) {
        return 'constant';
    }

    $environment_key = getenv('OPENAI_API_KEY');

    if ($environment_key) {
        return 'environment';
    }

    if ('' !== antichrist_engine_foundation_ai_get_stored_site_api_key()) {
        return 'stored';
    }

    $filtered = (string) apply_filters('antichrist_engine_ai_api_key', '');

    return '' !== $filtered ? 'filter' : '';
}

/**
 * Gets a readable label for a Foundation API key source.
 *
 * @param string $source Key source.
 * @return string
 */
function antichrist_engine_foundation_ai_get_site_api_key_source_label($source)
{
    $labels = array(
        'constant'    => __('wp-config constant', 'antichrist-engine'),
        'environment' => __('hosting environment', 'antichrist-engine'),
        'stored'      => __('AI Engine settings', 'antichrist-engine'),
        'filter'      => __('theme filter', 'antichrist-engine'),
    );

    return $labels[$source] ?? __('none', 'antichrist-engine');
}

/**
 * Gets non-sensitive status for the Foundation API key.
 *
 * @return array<string, mixed>
 */
function antichrist_engine_foundation_ai_get_site_api_key_status()
{
    $option_keys = antichrist_engine_foundation_ai_get_site_key_option_keys();
    $stored_key = antichrist_engine_foundation_ai_get_stored_site_api_key();
    $source = antichrist_engine_foundation_ai_get_site_api_key_source();
    $api_key = antichrist_engine_foundation_ai_get_api_key();

    return array(
        'has_key'        => '' !== $api_key,
        'has_stored_key' => '' !== $stored_key,
        'source'         => $source,
        'hint'           => '' !== $api_key ? antichrist_engine_foundation_ai_get_api_key_hint($api_key) : '',
        'saved_at'       => (string) get_option($option_keys['saved_at'], ''),
    );
}

/**
 * Sanitizes the Foundation API key option submitted from AI Engine settings.
 *
 * Blank submissions preserve the current encrypted key. The clear checkbox
 * removes it. New nonblank values are encrypted before storage.
 *
 * @param mixed $submitted_key Submitted key.
 * @return string
 */
function antichrist_engine_foundation_ai_sanitize_site_api_key_option($submitted_key)
{
    $option_keys = antichrist_engine_foundation_ai_get_site_key_option_keys();
    $should_clear = !empty($_POST['antichrist_engine_ai_site_api_key_clear']);
    $submitted_key = antichrist_engine_foundation_ai_sanitize_api_key_value($submitted_key);

    if ($should_clear) {
        update_option($option_keys['hint'], '', false);
        update_option($option_keys['saved_at'], '', false);
        return '';
    }

    if ('' === $submitted_key) {
        return (string) get_option($option_keys['encrypted'], '');
    }

    $encrypted = antichrist_engine_foundation_ai_encrypt_secret($submitted_key);

    if ('' === $encrypted) {
        if (function_exists('add_settings_error')) {
            add_settings_error(
                'antichrist_engine_ai_site_api_key',
                'antichrist_engine_ai_site_api_key_encrypt_failed',
                __('The Foundation OpenAI API key could not be encrypted, so the previous saved key was kept.', 'antichrist-engine'),
                'error'
            );
        }

        return (string) get_option($option_keys['encrypted'], '');
    }

    update_option($option_keys['hint'], antichrist_engine_foundation_ai_get_api_key_hint($submitted_key), false);
    update_option($option_keys['saved_at'], gmdate('Y-m-d H:i:s'), false);

    return $encrypted;
}

/**
 * Sanitizes the saved AI model option.
 *
 * @param mixed $model Submitted model.
 * @return string
 */
function antichrist_engine_foundation_ai_sanitize_model_option($model)
{
    $model = is_scalar($model) ? wp_unslash((string) $model) : '';
    $model = preg_replace('/[^A-Za-z0-9._:-]/', '', trim((string) $model));
    $model = substr((string) $model, 0, 120);

    return '' !== $model ? $model : 'gpt-5.4';
}

/**
 * Sanitizes the saved default AI persona option.
 *
 * @param mixed $persona_key Submitted persona key.
 * @return string
 */
function antichrist_engine_foundation_ai_sanitize_default_persona_option($persona_key)
{
    $persona_key = sanitize_key(is_scalar($persona_key) ? (string) $persona_key : '');
    $personas = antichrist_engine_foundation_ai_default_personas();

    return isset($personas[$persona_key]) ? $persona_key : 'adversary';
}

/**
 * Sanitizes the saved default AI protocol option.
 *
 * @param mixed $protocol_key Submitted protocol key.
 * @return string
 */
function antichrist_engine_foundation_ai_sanitize_default_protocol_option($protocol_key)
{
    $protocol_key = sanitize_key(is_scalar($protocol_key) ? (string) $protocol_key : '');
    $protocols = antichrist_engine_foundation_ai_protocols();

    return isset($protocols[$protocol_key]) ? $protocol_key : 'authority_audit';
}

/**
 * Sanitizes saved AI persona field overrides.
 *
 * @param mixed $submitted Submitted overrides.
 * @return array<string, array<string, string>>
 */
function antichrist_engine_foundation_ai_sanitize_persona_overrides_option($submitted)
{
    if (!empty($_POST['antichrist_engine_ai_persona_overrides_reset'])) {
        return array();
    }

    $submitted = is_array($submitted) ? wp_unslash($submitted) : array();
    $defaults = antichrist_engine_foundation_ai_default_personas();
    $fields = antichrist_engine_foundation_ai_get_persona_editable_fields();
    $overrides = array();

    foreach ($defaults as $persona_key => $default_persona) {
        if (empty($submitted[$persona_key]) || !is_array($submitted[$persona_key])) {
            continue;
        }

        foreach ($fields as $field_key => $max_length) {
            if (!array_key_exists($field_key, $submitted[$persona_key])) {
                continue;
            }

            $value = antichrist_engine_foundation_ai_sanitize_plain_text($submitted[$persona_key][$field_key], $max_length);
            $default_value = (string) ($default_persona[$field_key] ?? '');

            if ('' !== $value && $value !== $default_value) {
                $overrides[$persona_key][$field_key] = $value;
            }
        }
    }

    return $overrides;
}

/**
 * Gets the model used by the AI engine.
 *
 * @return string
 */
function antichrist_engine_foundation_ai_get_model()
{
    $model = (string) get_option('antichrist_engine_ai_model', 'gpt-5.4');
    $model = antichrist_engine_foundation_ai_sanitize_model_option($model);

    return (string) apply_filters('antichrist_engine_ai_model', $model);
}

/**
 * Gets the default AI persona key.
 *
 * @return string
 */
function antichrist_engine_foundation_ai_get_default_persona_key()
{
    $persona_key = sanitize_key((string) get_option('antichrist_engine_ai_default_persona', 'adversary'));
    $personas = antichrist_engine_foundation_ai_default_personas();

    return isset($personas[$persona_key]) ? $persona_key : 'adversary';
}

/**
 * Gets the default AI inquiry protocol.
 *
 * @return string
 */
function antichrist_engine_foundation_ai_get_default_protocol_key()
{
    $protocol_key = sanitize_key((string) get_option('antichrist_engine_ai_default_protocol', 'authority_audit'));
    $protocols = antichrist_engine_foundation_ai_protocols();

    return isset($protocols[$protocol_key]) ? $protocol_key : 'authority_audit';
}

/**
 * Gets a known AI persona.
 *
 * @param string $persona_key Requested persona key.
 * @return array<string, string>
 */