<?php
require '../../../zb_system/function/c_system_base.php';
require '../../../zb_system/function/c_system_admin.php';
$zbp->Load();

if (!$zbp->CheckPlugin('CiyunPublisher')) {
    $zbp->ShowError(48);
    die();
}

$action = 'root';
if (!$zbp->CheckRights($action)) {
    $zbp->ShowError(6);
    die();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (function_exists('CheckIsRefererValid')) {
        CheckIsRefererValid();
    }
    $token = isset($_POST['generate_token']) ? bin2hex(random_bytes(24)) : trim($_POST['token'] ?? '');
    if (strlen($token) >= 32) {
        $zbp->Config('CiyunPublisher')->token = $token;
    }
    $zbp->Config('CiyunPublisher')->whitelist_ips = trim($_POST['whitelist_ips'] ?? '');
    $zbp->Config('CiyunPublisher')->force_draft = isset($_POST['force_draft']) ? '1' : '0';
    $zbp->SaveConfig('CiyunPublisher');
    $zbp->SetHint('good', '设置已保存');
    Redirect('./main.php');
}

$selfCheck = null;
if (isset($_GET['self_check'])) {
    $selfCheck = CiyunPublisher_SelfCheck();
}

require $blogpath . 'zb_system/admin/admin_header.php';
require $blogpath . 'zb_system/admin/admin_top.php';
?>
<div id="divMain">
  <div class="divHeader">CiyunPublisher 设置</div>
  <div class="SubMenu"></div>
  <div id="divMain2">
    <form method="post">
      <input type="hidden" name="csrfToken" value="<?php echo $zbp->GetCSRFToken(); ?>">
      <p>API Token（至少 32 位）</p>
      <p><input name="token" style="width:520px" value="<?php echo htmlspecialchars($zbp->Config('CiyunPublisher')->token); ?>"></p>
      <p><button type="submit" name="generate_token" value="1" class="button">一键生成 Token</button></p>
      <p>白名单 IP（可选，一行一个或英文逗号分隔）</p>
      <p><textarea name="whitelist_ips" style="width:520px;height:100px"><?php echo htmlspecialchars($zbp->Config('CiyunPublisher')->whitelist_ips); ?></textarea></p>
      <p><label><input type="checkbox" name="force_draft" value="1" <?php echo $zbp->Config('CiyunPublisher')->force_draft === '1' ? 'checked' : ''; ?>> 默认强制保存为草稿</label></p>
      <p>接口地址：<?php echo $zbp->host; ?>zb_users/plugin/CiyunPublisher/api.php</p>
      <div class="note">
        <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><input type="submit" class="button" value="保存"></p>
    </form>
    <p><a class="button" href="./main.php?self_check=1">插件自检</a></p>
    <?php if ($selfCheck !== null) { ?>
      <div class="note">
        <p>Token：<?php echo $selfCheck['token'] ? '正常' : '异常（至少 32 位）'; ?></p>
        <p>日志写入：<?php echo $selfCheck['log_writable'] ? '正常' : '异常（插件目录不可写）'; ?></p>
        <p>分类数量：<?php echo intval($selfCheck['categories']); ?></p>
        <p>作者数量：<?php echo intval($selfCheck['authors']); ?></p>
      </div>
    <?php } ?>
  </div>
</div>
<?php
require $blogpath . 'zb_system/admin/admin_footer.php';
RunTime();

function CiyunPublisher_SelfCheck() {
    global $zbp;
    return [
        'token' => strlen($zbp->Config('CiyunPublisher')->token) >= 32,
        'log_writable' => CiyunPublisher_LogWritable(),
        'categories' => isset($zbp->categorys) && is_array($zbp->categorys) ? count($zbp->categorys) : 0,
        'authors' => isset($zbp->members) && is_array($zbp->members) ? count($zbp->members) : 0,
    ];
}

function CiyunPublisher_LogWritable() {
    $file = __DIR__ . '/publish.log';
    if (file_exists($file)) {
        return is_writable($file);
    }
    return is_writable(__DIR__);
}
?>
