| Server IP : 213.255.246.8 / Your IP : 216.73.216.87 Web Server : Apache System : Linux dublin.stapolin.com 5.14.0-362.18.1.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Jan 29 07:05:48 EST 2024 x86_64 User : stapolin ( 1019) PHP Version : 8.4.24 Disable Function : exec,passthru,shell_exec,system MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/stapolin/public_html/dash.stapolin.com/api/ |
Upload File : |
<?php
/**
* Websites API Endpoint
*/
require_once __DIR__ . '/../includes/functions.php';
// Base path for redirects (go up one directory from api/)
$basePath = dirname($_SERVER['SCRIPT_NAME']);
$basePath = dirname($basePath); // Go up from /api to parent
$action = $_POST['action'] ?? $_GET['action'] ?? '';
$redirectParam = $_POST['redirect'] ?? '';
// Build proper redirect URL
function buildRedirect($basePath, $redirectParam, $defaultPage, $params) {
$page = $redirectParam ?: $defaultPage;
// If redirect param doesn't start with /, prepend basePath
if (strpos($page, '/') !== 0) {
$page = $basePath . '/' . $page;
}
$separator = (strpos($page, '?') !== false) ? '&' : '?';
return $page . $separator . $params;
}
try {
$db = getDb();
switch ($action) {
case 'create':
$websiteUrl = trim($_POST['website_url'] ?? '');
$clientId = !empty($_POST['client_id']) ? (int)$_POST['client_id'] : null;
if (empty($websiteUrl)) {
redirect(buildRedirect($basePath, $redirectParam, 'websites.php', 'error=' . urlencode('Website URL is required')));
}
// Normalize URL - remove trailing slash
$websiteUrl = rtrim($websiteUrl, '/');
$db->execute(
"INSERT INTO st_websites (website_url, website_name, client_id) VALUES (?, ?, ?)",
[
$websiteUrl,
trim($_POST['website_name'] ?? '') ?: null,
$clientId
]
);
redirect(buildRedirect($basePath, $redirectParam, 'websites.php', 'success=' . urlencode('Website "' . $websiteUrl . '" created successfully')));
break;
case 'update':
$websiteId = (int)($_POST['website_id'] ?? 0);
$websiteUrl = trim($_POST['website_url'] ?? '');
if (!$websiteId || empty($websiteUrl)) {
redirect(buildRedirect($basePath, $redirectParam, 'websites.php', 'error=' . urlencode('Invalid request')));
}
// Normalize URL - remove trailing slash
$websiteUrl = rtrim($websiteUrl, '/');
$clientId = !empty($_POST['client_id']) ? (int)$_POST['client_id'] : null;
$db->execute(
"UPDATE st_websites SET website_url = ?, website_name = ?, client_id = ? WHERE website_id = ?",
[
$websiteUrl,
trim($_POST['website_name'] ?? '') ?: null,
$clientId,
$websiteId
]
);
redirect(buildRedirect($basePath, $redirectParam, 'websites.php', 'success=' . urlencode('Website updated successfully')));
break;
case 'delete':
$websiteId = (int)($_POST['website_id'] ?? 0);
if (!$websiteId) {
jsonResponse(['error' => 'Invalid website ID'], 400);
}
// Delete related records first
$db->execute("DELETE FROM st_plugin WHERE website_id = ?", [$websiteId]);
$db->execute("DELETE FROM st_themes WHERE website_id = ?", [$websiteId]);
$db->execute("DELETE FROM st_core WHERE website_id = ?", [$websiteId]);
$db->execute("DELETE FROM st_extra_work WHERE website_id = ?", [$websiteId]);
// Then delete the website
$db->execute("DELETE FROM st_websites WHERE website_id = ?", [$websiteId]);
redirect($basePath . '/websites.php?success=' . urlencode('Website deleted successfully'));
break;
case 'list':
$clientId = isset($_GET['client_id']) ? (int)$_GET['client_id'] : null;
if ($clientId) {
$websites = $db->fetchAll(
"SELECT w.*, c.client_name FROM st_websites w
LEFT JOIN st_clients c ON w.client_id = c.client_id
WHERE w.client_id = ?
ORDER BY w.website_url",
[$clientId]
);
} else {
$websites = $db->fetchAll(
"SELECT w.*, c.client_name FROM st_websites w
LEFT JOIN st_clients c ON w.client_id = c.client_id
ORDER BY c.client_name, w.website_url"
);
}
jsonResponse(['websites' => $websites]);
break;
default:
redirect($basePath . '/websites.php?error=' . urlencode('Invalid action'));
}
} catch (Exception $e) {
if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) {
jsonResponse(['error' => $e->getMessage()], 500);
} else {
redirect(buildRedirect($basePath, $redirectParam, 'websites.php', 'error=' . urlencode('An error occurred: ' . $e->getMessage())));
}
}