Files
html/api/wevia-office-senders-intent.php
2026-04-17 17:15:01 +02:00

107 lines
6.3 KiB
PHP

<?php
// V2 - intent office_senders - diagnostic + activer sender via draft URL (pas auto UPDATE)
if (!function_exists('wevia_office_senders_diag')) {
function wevia_office_senders_diag($msg) {
if (!$msg) return false;
if (!preg_match('/\b(office\s+senders?|graph\s+accounts?|office\s+workflow|senders?\s+office|active[rz]?\s+senders?|can[-_\s]send|oauth\s+office|azure\s+tenants?)\b/i', $msg)) return false;
$out = ['intent' => 'office_senders', 'tool' => 'office_senders_real_diagnostic'];
// Exec RÉEL via sentinel S95
$sr = function($sql) {
$ch = curl_init('http://10.1.0.3:5890/api/sentinel-brain.php?action=exec&cmd=' . urlencode("PGPASSWORD=admin123 psql -U admin -d adx_system -h 127.0.0.1 -t -A -c \"{$sql}\""));
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 6]);
$r = curl_exec($ch);
curl_close($ch);
$j = @json_decode($r, true);
return trim($j['output'] ?? '');
};
$s = [];
$s['office_total'] = (int)$sr("SELECT count(*) FROM admin.office_accounts");
$s['office_active'] = (int)$sr("SELECT count(*) FROM admin.office_accounts WHERE LOWER(status)='active'");
$s['graph_total'] = (int)$sr("SELECT count(*) FROM admin.graph_accounts");
$s['graph_can_send'] = (int)$sr("SELECT count(*) FROM admin.graph_accounts WHERE can_send=true");
$s['graph_has_mailbox'] = (int)$sr("SELECT count(*) FROM admin.graph_accounts WHERE has_mailbox=true");
$s['graph_disabled'] = (int)$sr("SELECT count(*) FROM admin.graph_accounts WHERE status='disabled'");
$s['tenants_active'] = (int)$sr("SELECT count(*) FROM admin.graph_tenants WHERE status='active'");
$s['tenants_sends_today'] = (int)$sr("SELECT COALESCE(SUM(sends_today),0) FROM admin.graph_tenants");
// Tenants actifs + sends_today (preuve OAuth fonctionne)
$top_tenants_raw = $sr("SELECT tenant_domain || '|' || sends_today || '|' || daily_limit || '|' || users_count || '|' || COALESCE(last_send_at::text,'never') FROM admin.graph_tenants WHERE status='active' ORDER BY sends_today DESC LIMIT 5");
$top_tenants = [];
foreach (explode("\n", $top_tenants_raw) as $line) {
$parts = explode('|', trim($line));
if (count($parts) >= 5) {
$top_tenants[] = [
'tenant' => $parts[0],
'sends_today' => (int)$parts[1],
'daily_limit' => (int)$parts[2],
'users_count' => (int)$parts[3],
'last_send' => $parts[4],
'operational' => (int)$parts[1] > 0 ? 'YES - envoyant actuellement' : 'token existe, accounts disabled',
];
}
}
// Diagnostic
$working_tenants = count(array_filter($top_tenants, fn($t) => $t['sends_today'] > 0));
$ready_to_reactivate = $s['graph_has_mailbox'] - $s['graph_can_send'];
$out['stats'] = $s;
$out['top_tenants'] = $top_tenants;
$out['health'] = [
'office_active_rate' => $s['office_total'] > 0 ? round(100 * $s['office_active'] / $s['office_total']) . '%' : '0%',
'graph_tenants_operational' => "{$working_tenants}/9 tenants envoient aujourd'hui",
'accounts_ready_to_reactivate' => "{$ready_to_reactivate} graph_accounts has_mailbox=true mais status=disabled (safety)",
'pmta_alternative' => 'PMTA S95 port 25 - route de secours active',
];
$out['root_cause'] = $s['graph_can_send'] == 0 && $working_tenants > 0
? "Tenants OAuth OK ({$working_tenants} envoyent encore via send queues). graph_accounts status=disabled explicitement (safety/pause precedente). Re-activation = 1 SQL UPDATE pour Yacine."
: "Azure AD tenants client_secret potentiellement expire OU accounts jamais configures.";
$out['actions_drafts'] = [];
// Draft SQL pour Yacine (pas d'exec auto)
if ($ready_to_reactivate > 0 && $working_tenants > 0) {
$out['actions_drafts'][] = [
'label' => 'Option A: Re-activer 10 graph_accounts pour tests (Yacine UPDATE DB)',
'sql_preview' => "UPDATE admin.graph_accounts SET status='active', can_send=true WHERE has_mailbox=true AND status='disabled' AND id IN (SELECT id FROM admin.graph_accounts WHERE has_mailbox=true AND status='disabled' LIMIT 10)",
'who_runs' => 'Yacine (via psql S95 ou interface admin)',
'safety' => 'Limit 10 - micro dose test',
'url' => 'https://weval-consulting.com/office-admins.html',
];
$out['actions_drafts'][] = [
'label' => 'Option B: Test pipeline via PMTA direct (pas besoin Graph)',
'command_preview' => 'curl -X POST http://127.0.0.1/api/wevads-v2-engine.php?action=send_test&token=WEVADS2026 -d "to=yacineutt@gmail.com&method=pmta&..."',
'who_runs' => 'Yacine depuis /send-controller-test.html',
'safety' => 'Rate limit 100/h deja existant',
'url' => 'https://weval-consulting.com/send-controller.php?action=status&token=WEVADS2026',
];
}
$out['dashboard'] = 'https://weval-consulting.com/office-workflow.html';
$out['ui_pages'] = [
'office-hub' => 'https://weval-consulting.com/office-hub.html',
'office-admins' => 'https://weval-consulting.com/office-admins.html',
'office-workflow' => 'https://weval-consulting.com/office-workflow.html',
'azure-reregister' => 'https://weval-consulting.com/azure-reregister.html',
];
$out['doctrine'] = 'JAMAIS UPDATE DB auto sur senders (risque pipeline) - draft SQL pour Yacine';
header("Content-Type: application/json");
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
exit;
}
}
$_os_msg = '';
$_body = @file_get_contents('php://input');
if ($_body) {
$_j = @json_decode($_body, true);
if (is_array($_j) && !empty($_j['message'])) $_os_msg = $_j['message'];
}
if (!$_os_msg) $_os_msg = $_POST['message'] ?? $_GET['message'] ?? '';
if ($_os_msg) wevia_office_senders_diag($_os_msg);