<?php
declare(strict_types=1);

/**
 * OnePanel B3: action handler group: tools
 * Generated from legacy action.php blocks for maintainability.
 */

function op_handle_tools(string $actionKey): void {
  switch ($actionKey) {
    case 'encryptltq':
      $_SESSION['ltq_from'] = $_POST['ltq_from'];
      	$_SESSION['ltq_key'] = $_POST['ltq_key'];
      	$_SESSION['ltq_to'] = ascii2hex(openssl_encrypt($_POST['ltq_from'], "AES-128-ECB", $_POST['ltq_key'], OPENSSL_RAW_DATA));
      	header("Location: dashboard.php?ltq-settings");
      	exit;
      return;
    case 'decryptltq':
      $_SESSION['ltq_from'] = $_POST['ltq_from'];
      	$_SESSION['ltq_key'] = $_POST['ltq_key'];
      	$_SESSION['ltq_to'] = openssl_decrypt(hex2bin($_POST['ltq_from']), "AES-128-ECB", $_POST['ltq_key'], OPENSSL_RAW_DATA);
      	header("Location: dashboard.php?ltq-settings");
      	exit;
      return;
    case 'checkrewrites':
      $_SESSION['rewrite_results'] = checkRewrites();
      	header("Location: dashboard.php?profile");
      	exit;
      return;
    default:
      http_response_code(400);
      echo 'Unknown action';
      return;
  }
}

// ---- B3.2 normalized wrappers ----

function handle_tools_encryptltq(array $post): void {
  _op_auth_required();
  $input = op_post_string($post, 'input', 1, 100000);
  op_try(function() use ($input) {
    // legacy function handles output/redirect; keep it if exists
    if (function_exists('encryptLTQ')) { encryptLTQ($input); }
  }, 'dashboard.php?tools', null);
}

function handle_tools_decryptltq(array $post): void {
  _op_auth_required();
  $input = op_post_string($post, 'input', 1, 100000);
  op_try(function() use ($input) {
    if (function_exists('decryptLTQ')) { decryptLTQ($input); }
  }, 'dashboard.php?tools', null);
}

function handle_tools_checkrewrites(array $post): void {
  _op_auth_required();
  op_try(function() {
    if (function_exists('checkRewrites')) { checkRewrites(); }
  }, 'dashboard.php?tools', null);
}
