<?php
!defined('EMLOG_ROOT') && exit('access denied!');

$configFile = __DIR__ . '/config.php';
$config = file_exists($configFile) ? include $configFile : ['token' => bin2hex(random_bytes(24)), 'whitelist_ips' => '', 'force_draft' => '1'];
if (!file_exists($configFile)) {
    file_put_contents($configFile, "<?php\nreturn " . var_export($config, true) . ";\n");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $token = isset($_POST['generate_token']) ? bin2hex(random_bytes(24)) : trim($_POST['token'] ?? '');
    if (strlen($token) >= 32) {
        $config['token'] = $token;
    }
    $config['whitelist_ips'] = trim($_POST['whitelist_ips'] ?? '');
    $config['force_draft'] = isset($_POST['force_draft']) ? '1' : '0';
    file_put_contents($configFile, "<?php\nreturn " . var_export($config, true) . ";\n");
    echo '<div class="alert alert-success">设置已保存</div>';
}
?>
<h3>CiyunPublisher 设置</h3>
<form method="post">
  <p>API Token（至少 32 位）</p>
  <p><input name="token" style="width:520px" value="<?php echo htmlspecialchars($config['token']); ?>"></p>
  <p><button type="submit" name="generate_token" value="1">一键生成 Token</button></p>
  <p>白名单 IP（可选，一行一个或英文逗号分隔）</p>
  <p><textarea name="whitelist_ips" style="width:520px;height:100px"><?php echo htmlspecialchars($config['whitelist_ips']); ?></textarea></p>
  <p><label><input type="checkbox" name="force_draft" value="1" <?php echo $config['force_draft'] === '1' ? 'checked' : ''; ?>> 默认强制保存为草稿</label></p>
  <p>接口地址：/content/plugins/ciyun_publisher/api.php</p>
  <div class="alert alert-info">
    <p><strong>慈云数据</strong> 高性能云服务器，稳定 · 高速 · 安全</p>
    <p>官网：https://www.ciyundata.com/</p>
    <p>工单：https://www.ciyundata.com/plugin/61/ticket.htm</p>
    <p>QQ群：499997757</p>
  </div>
  <p><button type="submit">保存</button></p>
</form>
