<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php
class DialBunnyKeysClass
{
  function encrypt_data($data) {
    $encryption_key = wp_salt('secure_auth'); // Use WordPress Salt for security
    $iv = openssl_random_pseudo_bytes(16); // Generate a secure IV
    $encrypted = openssl_encrypt($data, 'AES-256-CBC', $encryption_key, 0, $iv);
    return base64_encode($iv . $encrypted); // Store IV + Encrypted data
  }

  function decrypt_data($encrypted_data) {
      $encryption_key = wp_salt('secure_auth');
      $data = base64_decode($encrypted_data);
      $iv = substr($data, 0, 16); // Extract IV
      $encrypted = substr($data, 16);
      return openssl_decrypt($encrypted, 'AES-256-CBC', $encryption_key, 0, $iv);
  }

  // Twilio API details.
  function TWILIO_SID()
  {
    global $wpdb;
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $keys = $wpdb->get_results($wpdb->prepare("SELECT twilio_sid FROM {$wpdb->prefix}dialbunny_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ($keys != null) { return $this->decrypt_data($keys[0]->twilio_sid); }
  }
  function TWILIO_TOKEN()
  {
    global $wpdb;
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $keys = $wpdb->get_results($wpdb->prepare("SELECT twilio_token FROM {$wpdb->prefix}dialbunny_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ($keys != null) { return $this->decrypt_data($keys[0]->twilio_token); }
  }

  function TwiML_App_SID()
  {
    global $wpdb;
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $keys = $wpdb->get_results($wpdb->prepare("SELECT TwiML_App_SID FROM {$wpdb->prefix}dialbunny_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ($keys != null) { return $this->decrypt_data($keys[0]->TwiML_App_SID); }
  }

  function API_KEY()
  {
    global $wpdb;
    $table = $wpdb->prefix . 'dialbunny_api_keys';
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $keys = $wpdb->get_results($wpdb->prepare("SELECT API_KEY FROM {$wpdb->prefix}dialbunny_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ($keys != null) { return $this->decrypt_data($keys[0]->API_KEY); }
  }

  function API_SECRET()
  {
    global $wpdb;
    $table = $wpdb->prefix . 'dialbunny_api_keys';
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $keys = $wpdb->get_results($wpdb->prepare("SELECT API_SECRET FROM {$wpdb->prefix}dialbunny_api_keys WHERE id = %s LIMIT 1", 'ACWJB59')); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    if ($keys != null) { return $this->decrypt_data($keys[0]->API_SECRET); }
  }

}
 ?>
