<?php
ob_start();
require_once __DIR__ . '/../config.php';

// 从数据库获取配置
$client_id = getSiteConfig('oauth_gitee_client_id');
$client_secret = getSiteConfig('oauth_gitee_client_secret');
$callback = SITE_URL . 'api/gitee_callback.php';
if (empty($client_id) || empty($client_secret)) {
    die('Gitee登录未配置，请联系管理员');
}

// 生成 state
session_start();
$state = md5(uniqid(rand(), true));
$_SESSION['gitee_state'] = $state;

// 构建授权 URL
$redirect_uri = urlencode($callback);
$auth_url = "https://gitee.com/oauth/authorize?client_id={$client_id}&redirect_uri={$redirect_uri}&response_type=code&state={$state}";

header('Location: ' . $auth_url);
exit;