<?php
/**
 * Security Functions
 *
 * @package ReplyIt_Chat_Widget
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

class ReplyIt_Security {

    /**
     * Sanitize widget ID
     */
    public static function sanitize_widget_id($widget_id) {
        // Widget ID format: widget_XXXXXXXXXXXX
        if (!preg_match('/^widget_[a-zA-Z0-9]{24}$/', $widget_id)) {
            return '';
        }
        return sanitize_text_field($widget_id);
    }

    /**
     * Sanitize API secret
     */
    public static function sanitize_api_secret($api_secret) {
        // API secret is SHA-256 hash (64 characters hex)
        if (!preg_match('/^[a-f0-9]{64}$/i', $api_secret)) {
            return '';
        }
        return sanitize_text_field($api_secret);
    }

    /**
     * Mask API secret for display
     */
    public static function mask_api_secret($api_secret) {
        if (empty($api_secret)) {
            return '';
        }
        return substr($api_secret, 0, 8) . '...' . substr($api_secret, -8);
    }
}
