Files
plesk-mon/plesk-api-subscriptions.php
T
2026-03-22 18:07:01 +00:00

209 lines
6.3 KiB
PHP

<?php
/**
* Plesk Subscription Retriever (XML-RPC API Version)
*
* Connects to multiple Plesk servers via the Plesk XML-RPC API
* and retrieves subscription information.
*/
// Server configuration
$servers = [
'Blue' => [
'host' => 'https://blue.example.com:8443',
'username' => 'admin',
'password' => 'your_password_here',
],
'Red' => [
'host' => 'https://red.example.com:8443',
'username' => 'admin',
'password' => 'your_password_here',
],
'Purple' => [
'host' => 'https://purple.example.com:8443',
'username' => 'admin',
'password' => 'your_password_here',
],
'Orange' => [
'host' => 'https://orange.example.com:8443',
'username' => 'admin',
'password' => 'your_password_here',
],
];
// Output file
$outputFile = __DIR__ . '/subscriptions-api.json';
/**
* Make XML-RPC request to Plesk API
*/
function pleskRequest(string $host, string $username, string $password, string $packet): ?SimpleXMLElement
{
$url = rtrim($host, '/') . '/enterprise/control/agent.php';
$headers = [
'Content-Type: text/xml',
'HTTP_PRETTY_PRINT: true',
'HTTP_AUTH_LOGIN: ' . $username,
'HTTP_AUTH_PASSWD: ' . $password,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $packet);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Set to true in production with valid certs
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error || $httpCode !== 200) {
return null;
}
return simplexml_load_string($response);
}
/**
* Get subscriptions from a Plesk server via API
*/
function getSubscriptionsFromServer(string $name, array $config): array
{
$result = [
'server_name' => $name,
'host' => $config['host'],
'status' => 'unknown',
'error' => null,
'subscriptions' => [],
];
// Build XML packet to get all domains
$packet = '<?xml version="1.0" encoding="UTF-8"?>
<packet version="1.6.8.0">
<domain>
<get_info>
<filter>
<name/>
</filter>
<dataset>
<gen_info/>
<hosting/>
</dataset>
</get_info>
</domain>
</packet>';
$xml = pleskRequest($config['host'], $config['username'], $config['password'], $packet);
if (!$xml) {
$result['status'] = 'error';
$result['error'] = 'Failed to connect to Plesk API';
return $result;
}
// Check for errors in response
if (isset($xml->system->status) && (string)$xml->system->status !== 'ok') {
$result['status'] = 'error';
$result['error'] = isset($xml->system->errtext)
? (string)$xml->system->errtext
: 'API error';
return $result;
}
$result['status'] = 'success';
// Parse domain information
if (isset($xml->domain)) {
foreach ($xml->domain as $domain) {
if (isset($domain->get_info->result->data)) {
$data = $domain->get_info->result->data;
$subscription = [
'primary_domain' => isset($data->name) ? (string)$data->name : '',
'status' => isset($data->gen_info->status) ? (string)$data->gen_info->status : '',
'additional_domains' => [],
'aliases' => [],
];
// Get domain aliases
$aliasPacket = '<?xml version="1.0" encoding="UTF-8"?>
<packet version="1.6.8.0">
<domain>
<get_info>
<filter>
<parent_name>' . htmlspecialchars($subscription['primary_domain']) . '</parent_name>
</filter>
<dataset>
<hosting/>
</dataset>
</get_info>
</domain>
</packet>';
$aliasXml = pleskRequest($config['host'], $config['username'], $config['password'], $aliasPacket);
if ($aliasXml && isset($aliasXml->domain->get_info->result->data->hosting->vhost_name)) {
foreach ($aliasXml->domain->get_info->result->data->hosting->vhost_name as $vhost) {
if ((string)$vhost !== $subscription['primary_domain']) {
$subscription['aliases'][] = (string)$vhost;
}
}
}
$result['subscriptions'][] = $subscription;
}
}
}
return $result;
}
/**
* Main execution
*/
echo "Plesk Subscription Retriever (API Version)\n";
echo "==========================================\n\n";
$allResults = [
'generated_at' => date('c'),
'servers' => [],
'summary' => [
'total_servers' => count($servers),
'successful' => 0,
'failed' => 0,
'total_subscriptions' => 0,
],
];
foreach ($servers as $serverName => $config) {
echo "Connecting to {$serverName} ({$config['host']})... ";
$serverData = getSubscriptionsFromServer($serverName, $config);
$allResults['servers'][] = $serverData;
if ($serverData['status'] === 'success') {
$count = count($serverData['subscriptions']);
echo "OK ({$count} subscriptions)\n";
$allResults['summary']['successful']++;
$allResults['summary']['total_subscriptions'] += $count;
} else {
echo "FAILED: {$serverData['error']}\n";
$allResults['summary']['failed']++;
}
}
// Save to JSON file
$jsonOutput = json_encode($allResults, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
file_put_contents($outputFile, $jsonOutput);
echo "\n";
echo "Summary:\n";
echo " Successful: {$allResults['summary']['successful']}\n";
echo " Failed: {$allResults['summary']['failed']}\n";
echo " Total Subscriptions: {$allResults['summary']['total_subscriptions']}\n";
echo "\nResults saved to: {$outputFile}\n";