mirror of
https://github.com/myronblair/jarvis
synced 2026-06-30 17:50:23 -05:00
160 lines
6.8 KiB
PHP
160 lines
6.8 KiB
PHP
<?php
|
|
/**
|
|
* JARVIS Sites Manager — read/write email settings across all hosted sites
|
|
*/
|
|
|
|
// ── Site definitions ────────────────────────────────────────────────
|
|
$SITES = [
|
|
'tomsjavajive' => [
|
|
'name' => "Tom's Java Jive",
|
|
'url' => 'https://tomsjavajive.com',
|
|
'type' => 'db',
|
|
'db' => ['host'=>'localhost','name'=>'toms_tjj_db','user'=>'toms_tjj_user','pass'=>'+60wlPc+55e@gFq4'],
|
|
'keys' => ['api_key'=>'cybermail_api_key','from_email'=>'cybermail_from_email','from_name'=>'cybermail_from_name','admin_email'=>'smtp_admin_email'],
|
|
],
|
|
'tomtomgames' => [
|
|
'name' => 'TomTomGames',
|
|
'url' => 'https://tomtomgames.com',
|
|
'type' => 'file',
|
|
'file' => '/home/tomtomgames.com/includes/config.php',
|
|
'keys' => ['api_key'=>'CYBERMAIL_API_KEY','from_email'=>'SMTP_FROM','from_name'=>'SMTP_FROM_NAME','admin_email'=>'ADMIN_EMAIL'],
|
|
],
|
|
'epictravelexpeditions' => [
|
|
'name' => 'Epic Travel Expeditions',
|
|
'url' => 'https://epictravelexpeditions.com',
|
|
'type' => 'file',
|
|
'file' => '/home/epictravelexpeditions.com/public_html/api/config.php',
|
|
'keys' => ['api_key'=>'CYBERMAIL_API_KEY','from_email'=>'MAIL_FROM','from_name'=>'MAIL_FROM_NAME','admin_email'=>'ADMIN_EMAIL'],
|
|
],
|
|
'parkerslingshot' => [
|
|
'name' => 'Parker Slingshot',
|
|
'url' => 'https://parkerslingshot.epictravelexpeditions.com',
|
|
'type' => 'file',
|
|
'file' => '/home/epictravelexpeditions.com/parkerslingshot/db.php',
|
|
'keys' => ['api_key'=>'CYBERMAIL_API_KEY','from_email'=>'MAIL_FROM','from_name'=>'MAIL_FROM_NAME','admin_email'=>'ADMIN_EMAIL'],
|
|
],
|
|
'parkerslingshotrentals' => [
|
|
'name' => 'Parker Slingshot Rentals',
|
|
'url' => 'https://parkerslingshotrentals.com',
|
|
'type' => 'file',
|
|
'file' => '/home/parkerslingshotrentals.com/public_html/db.php',
|
|
'keys' => ['api_key'=>'CYBERMAIL_API_KEY','from_email'=>'MAIL_FROM','from_name'=>'MAIL_FROM_NAME','admin_email'=>'ADMIN_EMAIL'],
|
|
],
|
|
];
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────────────
|
|
function fileGet(string $file, string $constant): string {
|
|
if (!file_exists($file)) return '';
|
|
$content = file_get_contents($file);
|
|
if (preg_match("/define\s*\(\s*['\"]" . preg_quote($constant, '/') . "['\"],\s*['\"]([^'\"]*)['\"].*?\)/s", $content, $m))
|
|
return $m[1];
|
|
return '';
|
|
}
|
|
|
|
function fileSet(string $file, string $constant, string $value): bool {
|
|
if (!file_exists($file)) return false;
|
|
$content = file_get_contents($file);
|
|
$safe = str_replace("'", "\\'", $value);
|
|
$new = preg_replace(
|
|
"/define\s*\(\s*['\"]" . preg_quote($constant, '/') . "['\"],\s*['\"][^'\"]*['\"](\s*)\)/",
|
|
"define('" . $constant . "', '" . $safe . "'$1)",
|
|
$content
|
|
);
|
|
if ($new === null) return false; // regex error
|
|
if ($new === $content) return true; // already correct value
|
|
return file_put_contents($file, $new) !== false;
|
|
}
|
|
|
|
function dbGet(array $db, string $key): string {
|
|
try {
|
|
$pdo = new PDO("mysql:host={$db['host']};dbname={$db['name']};charset=utf8mb4",
|
|
$db['user'], $db['pass'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
$s = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key=?");
|
|
$s->execute([$key]);
|
|
$row = $s->fetch(PDO::FETCH_ASSOC);
|
|
if (!$row) return '';
|
|
$decoded = json_decode($row['setting_value'], true);
|
|
return $decoded ?? $row['setting_value'];
|
|
} catch (Exception $e) { return ''; }
|
|
}
|
|
|
|
function dbSet(array $db, string $key, string $value): bool {
|
|
try {
|
|
$pdo = new PDO("mysql:host={$db['host']};dbname={$db['name']};charset=utf8mb4",
|
|
$db['user'], $db['pass'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
$existing = $pdo->prepare("SELECT id FROM settings WHERE setting_key=?");
|
|
$existing->execute([$key]);
|
|
if ($existing->fetch()) {
|
|
$pdo->prepare("UPDATE settings SET setting_value=? WHERE setting_key=?")->execute([json_encode($value), $key]);
|
|
} else {
|
|
$pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?,?)")->execute([$key, json_encode($value)]);
|
|
}
|
|
return true;
|
|
} catch (Exception $e) { return false; }
|
|
}
|
|
|
|
function siteGet(array $site, string $field): string {
|
|
$constant = $site['keys'][$field] ?? '';
|
|
if (!$constant) return '';
|
|
if ($site['type'] === 'db') return dbGet($site['db'], $constant);
|
|
return fileGet($site['file'], $constant);
|
|
}
|
|
|
|
function siteSet(array $site, string $field, string $value): bool {
|
|
$constant = $site['keys'][$field] ?? '';
|
|
if (!$constant) return false;
|
|
if ($site['type'] === 'db') return dbSet($site['db'], $constant, $value);
|
|
return fileSet($site['file'], $constant, $value);
|
|
}
|
|
|
|
// ── Router ───────────────────────────────────────────────────────────
|
|
if ($method === 'GET') {
|
|
$result = [];
|
|
foreach ($SITES as $id => $site) {
|
|
$result[$id] = [
|
|
'name' => $site['name'],
|
|
'url' => $site['url'],
|
|
'api_key' => siteGet($site, 'api_key'),
|
|
'from_email' => siteGet($site, 'from_email'),
|
|
'from_name' => siteGet($site, 'from_name'),
|
|
'admin_email'=> siteGet($site, 'admin_email'),
|
|
];
|
|
}
|
|
echo json_encode(['success' => true, 'sites' => $result]);
|
|
exit;
|
|
}
|
|
|
|
if ($method === 'POST') {
|
|
$action = $data['action'] ?? '';
|
|
$siteId = $data['site'] ?? '';
|
|
$results = [];
|
|
|
|
if ($action === 'push_key') {
|
|
// Push API key to all sites at once
|
|
$apiKey = trim($data['api_key'] ?? '');
|
|
if (!$apiKey) { echo json_encode(['success'=>false,'error'=>'API key required']); exit; }
|
|
foreach ($SITES as $id => $site) {
|
|
$results[$id] = siteSet($site, 'api_key', $apiKey);
|
|
}
|
|
echo json_encode(['success'=>true,'results'=>$results]);
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'save' && $siteId && isset($SITES[$siteId])) {
|
|
$site = $SITES[$siteId];
|
|
$fields = ['api_key', 'from_email', 'from_name', 'admin_email'];
|
|
foreach ($fields as $field) {
|
|
if (isset($data[$field])) {
|
|
$results[$field] = siteSet($site, $field, trim($data[$field]));
|
|
}
|
|
}
|
|
echo json_encode(['success'=>true,'results'=>$results]);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['success'=>false,'error'=>'Unknown action']);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['success'=>false,'error'=>'Method not allowed']);
|