159 lines
6.1 KiB
PHP
159 lines
6.1 KiB
PHP
<?php
|
|
// WEVIA API Bridge — Interface directe avec nos APIs externes
|
|
// CF DNS + Office 365 + GitHub + Gitea
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { header("Access-Control-Allow-Origin: *"); exit; }
|
|
|
|
$secrets = [];
|
|
foreach (file('/etc/weval/secrets.env', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $l) {
|
|
if (empty(trim($l)) || $l[0] === '#') continue;
|
|
$p = strpos($l, '='); if ($p) $secrets[trim(substr($l, 0, $p))] = trim(substr($l, $p+1), " \t\"'");
|
|
}
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
$param = $_GET['param'] ?? '';
|
|
|
|
function api_call($url, $headers = [], $method = 'GET', $body = null) {
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 15,
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
]);
|
|
if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); }
|
|
if ($method === 'PUT') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); }
|
|
if ($method === 'DELETE') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); }
|
|
$r = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
|
|
return ['code' => $code, 'data' => json_decode($r, true) ?: $r];
|
|
}
|
|
|
|
// ============ CLOUDFLARE DNS ============
|
|
if ($action === 'cf_dns_list') {
|
|
$zone = $secrets['CF_ZONE_ID'];
|
|
$r = api_call("https://api.cloudflare.com/client/v4/zones/$zone/dns_records?per_page=100", [
|
|
"X-Auth-Email: " . $secrets['CF_EMAIL'],
|
|
"X-Auth-Key: " . $secrets['CF_API_TOKEN'],
|
|
"Content-Type: application/json"
|
|
]);
|
|
$records = $r['data']['result'] ?? [];
|
|
$out = array_map(fn($rec) => [
|
|
'name' => $rec['name'], 'type' => $rec['type'],
|
|
'content' => $rec['content'], 'proxied' => $rec['proxied'],
|
|
'id' => $rec['id']
|
|
], $records);
|
|
echo json_encode(['zone' => $zone, 'count' => count($out), 'records' => $out], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'cf_dns_add') {
|
|
$input = json_decode(file_get_contents("php://input"), true);
|
|
$zone = $secrets['CF_ZONE_ID'];
|
|
$r = api_call("https://api.cloudflare.com/client/v4/zones/$zone/dns_records", [
|
|
"X-Auth-Email: " . $secrets['CF_EMAIL'],
|
|
"X-Auth-Key: " . $secrets['CF_API_TOKEN'],
|
|
"Content-Type: application/json"
|
|
], 'POST', json_encode([
|
|
'type' => $input['type'] ?? 'A',
|
|
'name' => $input['name'] ?? '',
|
|
'content' => $input['content'] ?? '',
|
|
'proxied' => $input['proxied'] ?? true,
|
|
]));
|
|
echo json_encode($r['data'], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'cf_purge') {
|
|
$zone = $secrets['CF_ZONE_ID'];
|
|
$r = api_call("https://api.cloudflare.com/client/v4/zones/$zone/purge_cache", [
|
|
"X-Auth-Email: " . $secrets['CF_EMAIL'],
|
|
"X-Auth-Key: " . $secrets['CF_API_TOKEN'],
|
|
"Content-Type: application/json"
|
|
], 'POST', json_encode(['purge_everything' => true]));
|
|
echo json_encode(['purged' => true, 'result' => $r['data']], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'cf_analytics') {
|
|
$zone = $secrets['CF_ZONE_ID'];
|
|
$r = api_call("https://api.cloudflare.com/client/v4/zones/$zone/analytics/dashboard?since=-1440", [
|
|
"X-Auth-Email: " . $secrets['CF_EMAIL'],
|
|
"X-Auth-Key: " . $secrets['CF_API_TOKEN'],
|
|
]);
|
|
echo json_encode($r['data'], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
// ============ OFFICE 365 ============
|
|
if ($action === 'office_accounts') {
|
|
$r = api_call("http://127.0.0.1/api/office-checker.php?action=list", ["Host: weval-consulting.com"]);
|
|
echo json_encode($r['data'], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'office_check') {
|
|
$email = $param ?: 'all';
|
|
$r = api_call("http://127.0.0.1/api/office-checker.php?action=check&email=$email", ["Host: weval-consulting.com"]);
|
|
echo json_encode($r['data'], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
// ============ GITHUB ============
|
|
if ($action === 'github_repos') {
|
|
$token = $secrets['GITHUB_TOKEN'] ?? '';
|
|
$r = api_call("https://api.github.com/user/repos?per_page=30&sort=updated", [
|
|
"Authorization: token $token",
|
|
"Accept: application/vnd.github.v3+json",
|
|
"User-Agent: WEVIA-Master"
|
|
]);
|
|
if (is_array($r['data'])) {
|
|
$repos = array_map(fn($repo) => [
|
|
'name' => $repo['name'], 'private' => $repo['private'],
|
|
'updated' => $repo['updated_at'], 'stars' => $repo['stargazers_count'],
|
|
'url' => $repo['html_url']
|
|
], $r['data']);
|
|
echo json_encode(['count' => count($repos), 'repos' => $repos], JSON_PRETTY_PRINT);
|
|
} else {
|
|
echo json_encode(['error' => 'GitHub API failed', 'detail' => $r], JSON_PRETTY_PRINT);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// ============ GITEA ============
|
|
if ($action === 'gitea_repos') {
|
|
$token = $secrets['GITEA_SOVEREIGN_TOKEN'] ?? '';
|
|
$r = api_call("http://127.0.0.1:3300/api/v1/repos/search?limit=50", [
|
|
"Authorization: token $token",
|
|
"Accept: application/json"
|
|
]);
|
|
if (isset($r['data']['data'])) {
|
|
$repos = array_map(fn($repo) => [
|
|
'name' => $repo['name'], 'updated' => $repo['updated_at'],
|
|
], $r['data']['data']);
|
|
echo json_encode(['count' => count($repos), 'repos' => $repos], JSON_PRETTY_PRINT);
|
|
} else {
|
|
echo json_encode($r, JSON_PRETTY_PRINT);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// ============ STATUS ============
|
|
echo json_encode([
|
|
'service' => 'WEVIA API Bridge',
|
|
'actions' => [
|
|
'cf_dns_list' => 'List all Cloudflare DNS records',
|
|
'cf_dns_add' => 'Add DNS record (POST: type,name,content)',
|
|
'cf_purge' => 'Purge Cloudflare cache',
|
|
'cf_analytics' => 'Cloudflare analytics last 24h',
|
|
'office_accounts' => 'List Office 365 accounts',
|
|
'office_check' => 'Check Office email (?param=email)',
|
|
'github_repos' => 'List GitHub repos',
|
|
'gitea_repos' => 'List Gitea repos',
|
|
],
|
|
'credentials' => [
|
|
'cloudflare' => !empty($secrets['CF_API_TOKEN']),
|
|
'github' => !empty($secrets['GITHUB_TOKEN']),
|
|
'gitea' => !empty($secrets['GITEA_SOVEREIGN_TOKEN']),
|
|
]
|
|
], JSON_PRETTY_PRINT);
|