Files
html/api/wevia-mcp-hub.php
2026-04-14 03:05:01 +02:00

134 lines
6.9 KiB
PHP

<?php
/**
* WEVIA MCP HUB SOUVERAIN — Clone MCP Claude en interne
* Centralise Gmail, Calendar, Drive, Slack, GitHub, Ethica, WEVADS
* Usage: POST /api/wevia-mcp-hub.php {service:"gmail", action:"search", params:{q:"from:hetzner"}}
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$secrets = [];
if (file_exists('/etc/weval/secrets.env')) {
foreach (file('/etc/weval/secrets.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
if (strpos($line, '#') === 0) continue;
list($k, $v) = array_pad(explode('=', $line, 2), 2, '');
$secrets[trim($k)] = trim($v);
}
}
$input = json_decode(file_get_contents('php://input'), true) ?: $_GET;
$service = $input['service'] ?? '';
$action = $input['action'] ?? '';
$params = $input['params'] ?? [];
if (!$service) {
echo json_encode([
'hub' => 'WEVIA MCP Hub Souverain v1.0',
'services' => [
'sovereign' => ['status'=>'active','actions'=>['chat','models','health'],'endpoint'=>'http://127.0.0.1:4000'],
'ethica' => ['status'=>'active','actions'=>['stats','search','hcp'],'endpoint'=>'http://127.0.0.1:8443/api/ethica-api.php'],
'github' => ['status'=>'active','actions'=>['repos','issues','commits'],'token'=>!empty($secrets['GITHUB_TOKEN'])],
'gitea' => ['status'=>'active','actions'=>['repos','issues'],'token'=>!empty($secrets['GITEA_SOVEREIGN_TOKEN'])],
'cloudflare' => ['status'=>'active','actions'=>['dns','purge','workers'],'token'=>!empty($secrets['CF_API_TOKEN'])],
'wevads' => ['status'=>'active','actions'=>['configs','stats','pipeline'],'endpoint'=>'http://10.1.0.3:5890'],
'sentinel' => ['status'=>'active','actions'=>['exec','deploy','backup']],
'meta' => ['status'=>'configured','actions'=>['ads','pages'],'token'=>!empty($secrets['META_APP_ID'])],
'stripe' => ['status'=>'configured','actions'=>['payments','invoices'],'token'=>!empty($secrets['STRIPE_SK_LIVE'])],
'kaggle' => ['status'=>'configured','actions'=>['datasets','notebooks'],'token'=>!empty($secrets['KAGGLE_API_TOKEN'])],
'huggingface' => ['status'=>'active','actions'=>['models','spaces','inference'],'token'=>!empty($secrets['HF_TOKEN'])],
],
'providers_ia' => 12,
'tools' => 316
]);
exit;
}
$result = null;
switch ($service) {
case 'sovereign':
if ($action === 'chat') {
$ch = curl_init('http://127.0.0.1:4000/v1/chat/completions');
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>1,CURLOPT_TIMEOUT=>10,CURLOPT_POST=>1,
CURLOPT_HTTPHEADER=>['Content-Type: application/json'],
CURLOPT_POSTFIELDS=>json_encode(['model'=>$params['model']??'auto','messages'=>$params['messages']??[['role'=>'user','content'=>$params['prompt']??'hello']],'max_tokens'=>$params['max_tokens']??200])]);
$result = json_decode(curl_exec($ch), true); curl_close($ch);
} elseif ($action === 'health') {
$result = json_decode(file_get_contents('http://127.0.0.1:4000/health'), true);
} elseif ($action === 'models') {
$result = json_decode(file_get_contents('http://127.0.0.1:4000/v1/models'), true);
}
break;
case 'ethica':
if ($action === 'stats') {
$result = json_decode(@file_get_contents('http://127.0.0.1:8443/api/ethica-stats-api.php'), true);
} elseif ($action === 'search') {
$q = urlencode($params['q'] ?? '');
$result = json_decode(@file_get_contents("http://127.0.0.1:8443/api/ethica-api.php?action=search&q=$q"), true);
}
break;
case 'github':
$token = $secrets['GITHUB_TOKEN'] ?? $secrets['WEVAL_GITHUB_PAT'] ?? '';
$headers = "Authorization: token $token\r\nUser-Agent: WEVIA-Hub\r\n";
$ctx = stream_context_create(['http'=>['header'=>$headers,'timeout'=>5]]);
if ($action === 'repos') {
$result = json_decode(@file_get_contents('https://api.github.com/user/repos?per_page=10&sort=updated', false, $ctx), true);
if (is_array($result)) $result = array_map(fn($r) => ['name'=>$r['full_name'],'updated'=>$r['updated_at'],'stars'=>$r['stargazers_count']], $result);
} elseif ($action === 'commits') {
$repo = $params['repo'] ?? 'Yacineutt/weval-consulting';
$result = json_decode(@file_get_contents("https://api.github.com/repos/$repo/commits?per_page=5", false, $ctx), true);
if (is_array($result)) $result = array_map(fn($c) => ['sha'=>substr($c['sha'],0,7),'msg'=>$c['commit']['message'],'date'=>$c['commit']['author']['date']], $result);
}
break;
case 'gitea':
$token = $secrets['GITEA_SOVEREIGN_TOKEN'] ?? '';
$result = json_decode(@file_get_contents("http://127.0.0.1:3000/api/v1/repos/search?token=$token&limit=10"), true);
break;
case 'cloudflare':
$token = $secrets['CF_API_TOKEN'] ?? '';
$zone = $secrets['CF_ZONE_ID'] ?? '';
$headers = "Authorization: Bearer $token\r\nContent-Type: application/json\r\n";
$ctx = stream_context_create(['http'=>['header'=>$headers,'timeout'=>5]]);
if ($action === 'dns') {
$result = json_decode(@file_get_contents("https://api.cloudflare.com/client/v4/zones/$zone/dns_records?per_page=20", false, $ctx), true);
} elseif ($action === 'purge') {
$result = ['info'=>'Use cf_purge action in Action Engine'];
}
break;
case 'wevads':
if ($action === 'stats') {
$result = json_decode(@file_get_contents('http://10.1.0.3:5890/api/wevads-stats.php'), true);
} elseif ($action === 'pipeline') {
$result = json_decode(@file_get_contents('http://10.1.0.3:5890/api/pipeline-status.php'), true);
}
break;
case 'sentinel':
if ($action === 'exec') {
$cmd = $params['cmd'] ?? 'echo OK';
$result = trim(shell_exec('sudo timeout 4 bash -c ' . escapeshellarg($cmd) . ' 2>&1'));
}
break;
case 'huggingface':
$token = $secrets['HF_TOKEN'] ?? '';
$headers = "Authorization: Bearer $token\r\n";
$ctx = stream_context_create(['http'=>['header'=>$headers,'timeout'=>5]]);
if ($action === 'models') {
$result = json_decode(@file_get_contents('https://huggingface.co/api/models?author=yace222&limit=5', false, $ctx), true);
} elseif ($action === 'spaces') {
$result = json_decode(@file_get_contents('https://huggingface.co/api/spaces?author=yace222&limit=5', false, $ctx), true);
}
break;
default:
$result = ['error'=>"Service '$service' non reconnu",'available'=>['sovereign','ethica','github','gitea','cloudflare','wevads','sentinel','huggingface','meta','stripe','kaggle']];
}
echo json_encode($result ?: ['error'=>'no result','service'=>$service,'action'=>$action], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);