Repository: plesk/api-php-lib
Branch: master
Commit: 678d09a3ff43
Files: 129
Total size: 194.0 KB
Directory structure:
gitextract_ygetwf0b/
├── .github/
│ ├── dependabot.yml
│ └── workflows/
│ └── test.yml
├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── composer.json
├── docker-compose.yml
├── phpcs.xml.dist
├── phpunit-watcher.yml
├── phpunit.xml.dist
├── psalm.xml
├── src/
│ └── Api/
│ ├── AbstractStruct.php
│ ├── Client/
│ │ └── Exception.php
│ ├── Client.php
│ ├── Exception.php
│ ├── InternalClient.php
│ ├── Operator/
│ │ ├── Aps.php
│ │ ├── Certificate.php
│ │ ├── Customer.php
│ │ ├── Database.php
│ │ ├── DatabaseServer.php
│ │ ├── Dns.php
│ │ ├── DnsTemplate.php
│ │ ├── EventLog.php
│ │ ├── Ip.php
│ │ ├── Locale.php
│ │ ├── LogRotation.php
│ │ ├── Mail.php
│ │ ├── PhpHandler.php
│ │ ├── ProtectedDirectory.php
│ │ ├── Reseller.php
│ │ ├── ResellerPlan.php
│ │ ├── SecretKey.php
│ │ ├── Server.php
│ │ ├── ServicePlan.php
│ │ ├── ServicePlanAddon.php
│ │ ├── Session.php
│ │ ├── Site.php
│ │ ├── SiteAlias.php
│ │ ├── Subdomain.php
│ │ ├── Ui.php
│ │ ├── VirtualDirectory.php
│ │ └── Webspace.php
│ ├── Operator.php
│ ├── Struct/
│ │ ├── Certificate/
│ │ │ └── Info.php
│ │ ├── Customer/
│ │ │ ├── GeneralInfo.php
│ │ │ └── Info.php
│ │ ├── Database/
│ │ │ ├── Info.php
│ │ │ └── UserInfo.php
│ │ ├── DatabaseServer/
│ │ │ └── Info.php
│ │ ├── Dns/
│ │ │ └── Info.php
│ │ ├── EventLog/
│ │ │ ├── DetailedEvent.php
│ │ │ └── Event.php
│ │ ├── Ip/
│ │ │ └── Info.php
│ │ ├── Locale/
│ │ │ └── Info.php
│ │ ├── Mail/
│ │ │ ├── GeneralInfo.php
│ │ │ └── Info.php
│ │ ├── PhpHandler/
│ │ │ └── Info.php
│ │ ├── ProtectedDirectory/
│ │ │ ├── DataInfo.php
│ │ │ ├── Info.php
│ │ │ └── UserInfo.php
│ │ ├── Reseller/
│ │ │ ├── GeneralInfo.php
│ │ │ └── Info.php
│ │ ├── SecretKey/
│ │ │ └── Info.php
│ │ ├── Server/
│ │ │ ├── Admin.php
│ │ │ ├── GeneralInfo.php
│ │ │ ├── Preferences.php
│ │ │ ├── SessionPreferences.php
│ │ │ ├── Statistics/
│ │ │ │ ├── DiskSpace.php
│ │ │ │ ├── LoadAverage.php
│ │ │ │ ├── Memory.php
│ │ │ │ ├── Objects.php
│ │ │ │ ├── Other.php
│ │ │ │ ├── Swap.php
│ │ │ │ └── Version.php
│ │ │ ├── Statistics.php
│ │ │ └── UpdatesInfo.php
│ │ ├── ServicePlan/
│ │ │ └── Info.php
│ │ ├── ServicePlanAddon/
│ │ │ └── Info.php
│ │ ├── Session/
│ │ │ └── Info.php
│ │ ├── Site/
│ │ │ ├── GeneralInfo.php
│ │ │ ├── HostingInfo.php
│ │ │ └── Info.php
│ │ ├── SiteAlias/
│ │ │ ├── GeneralInfo.php
│ │ │ └── Info.php
│ │ ├── Subdomain/
│ │ │ └── Info.php
│ │ ├── Ui/
│ │ │ └── CustomButton.php
│ │ └── Webspace/
│ │ ├── DiskUsage.php
│ │ ├── GeneralInfo.php
│ │ ├── HostingPropertyInfo.php
│ │ ├── Info.php
│ │ ├── Limit.php
│ │ ├── LimitDescriptor.php
│ │ ├── LimitInfo.php
│ │ ├── Limits.php
│ │ ├── PermissionDescriptor.php
│ │ ├── PermissionInfo.php
│ │ ├── PhpSettings.php
│ │ └── PhysicalHostingDescriptor.php
│ └── XmlResponse.php
├── tests/
│ ├── AbstractTestCase.php
│ ├── ApiClientTest.php
│ ├── CertificateTest.php
│ ├── CustomerTest.php
│ ├── DatabaseServerTest.php
│ ├── DatabaseTest.php
│ ├── DnsTemplateTest.php
│ ├── DnsTest.php
│ ├── EventLogTest.php
│ ├── IpTest.php
│ ├── LocaleTest.php
│ ├── MailTest.php
│ ├── PhpHandlerTest.php
│ ├── ProtectedDirectoryTest.php
│ ├── ResellerTest.php
│ ├── SecretKeyTest.php
│ ├── ServerTest.php
│ ├── ServicePlanAddonTest.php
│ ├── ServicePlanTest.php
│ ├── SessionTest.php
│ ├── SiteAliasTest.php
│ ├── SiteTest.php
│ ├── SubdomainTest.php
│ ├── UiTest.php
│ ├── Utility/
│ │ ├── KeyLimitChecker.php
│ │ └── PasswordProvider.php
│ └── WebspaceTest.php
└── wait-for-plesk.sh
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/dependabot.yml
================================================
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
groups:
main:
patterns:
- "*"
commit-message:
prefix: "TECH Dependabot [github-actions] "
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "weekly"
commit-message:
prefix: "TECH Dependabot [composer] "
================================================
FILE: .github/workflows/test.yml
================================================
name: Tests
on:
push:
branches:
- master
pull_request:
jobs:
test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- run: docker compose run tests
- uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
================================================
FILE: .gitignore
================================================
/.env
/vendor/
/phpunit.xml
/.phpunit.result.cache
/coverage.xml
================================================
FILE: Dockerfile
================================================
FROM php:8.2-cli
RUN apt-get update \
&& apt-get install -y unzip \
&& docker-php-ext-install pcntl \
&& pecl install xdebug \
&& echo "zend_extension=xdebug.so" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
================================================
FILE: LICENSE
================================================
Copyright 1999-2025. WebPros International GmbH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: README.md
================================================
## PHP library for Plesk XML-RPC API
[](https://github.com/plesk/api-php-lib/actions/workflows/test.yml)
[](https://scrutinizer-ci.com/g/plesk/api-php-lib/?branch=master)
[](https://codecov.io/gh/plesk/api-php-lib)
PHP object-oriented library for Plesk XML-RPC API.
## Install Via Composer
[Composer](https://getcomposer.org/) is a preferable way to install the library:
`composer require plesk/api-php-lib`
## Usage Examples
Here is an example on how to use the library and create a customer with desired properties:
```php
$client = new \PleskX\Api\Client($host);
$client->setCredentials($login, $password);
$client->customer()->create([
'cname' => 'Plesk',
'pname' => 'John Smith',
'login' => 'john',
'passwd' => 'secret',
'email' => 'john@smith.com',
]);
```
It is possible to use a secret key instead of password for authentication.
```php
$client = new \PleskX\Api\Client($host);
$client->setSecretKey($secretKey)
```
In case of Plesk extension creation one can use an internal mechanism to access XML-RPC API. It does not require to pass authentication because the extension works in the context of Plesk.
```php
$client = new \PleskX\Api\InternalClient();
$protocols = $client->server()->getProtos();
```
For additional examples see tests/ directory.
## How to Run Unit Tests
One the possible ways to become familiar with the library is to check the unit tests.
To run the unit tests use the following command:
`REMOTE_HOST=your-plesk-host.dom REMOTE_PASSWORD=password composer test`
To use custom port one can provide a URL (e.g. for Docker container):
`REMOTE_URL=https://your-plesk-host.dom:port REMOTE_PASSWORD=password composer test`
One more way to run tests is to use Docker:
`docker-compose run tests`
## Continuous Testing
During active development it could be more convenient to run tests in continuous manner. Here is the way how to achieve it:
`REMOTE_URL=https://your-plesk-host.dom:port REMOTE_PASSWORD=password composer test:watch`
================================================
FILE: composer.json
================================================
{
"name": "plesk/api-php-lib",
"type": "library",
"description": "PHP object-oriented library for Plesk XML-RPC API",
"license": "Apache-2.0",
"authors": [
{
"name": "Alexei Yuzhakov",
"email": "sibprogrammer@gmail.com"
},
{
"name": "WebPros International GmbH.",
"email": "plesk-dev-leads@plesk.com"
}
],
"require": {
"php": "^7.4 || ^8.0",
"ext-curl": "*",
"ext-xml": "*",
"ext-simplexml": "*",
"ext-dom": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"spatie/phpunit-watcher": "^1.22",
"vimeo/psalm": "^4.10 || ^5.0",
"squizlabs/php_codesniffer": "^3.6 || ^4.0"
},
"config": {
"process-timeout": 0,
"platform": {
"php": "7.4.27"
}
},
"scripts": {
"test": "phpunit",
"test:watch": "phpunit-watcher watch",
"lint": [
"psalm",
"phpcs"
]
},
"autoload": {
"psr-4": {
"PleskX\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PleskXTest\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
================================================
FILE: docker-compose.yml
================================================
# Copyright 1999-2025. WebPros International GmbH.
version: '3'
services:
plesk:
image: plesk/plesk:latest
logging:
driver: none
ports:
["8443:8443"]
tmpfs:
- /tmp
- /run
- /run/lock
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup
cgroup: host
tests:
build: .
environment:
REMOTE_URL: https://plesk:8443
REMOTE_PASSWORD: changeme1Q**
command: >
bash -c "cd /opt/api-php-lib
&& composer install
&& ./wait-for-plesk.sh
&& composer lint
&& composer test -- --testdox"
depends_on:
- plesk
links:
- plesk
volumes:
- .:/opt/api-php-lib
================================================
FILE: phpcs.xml.dist
================================================
src
tests
================================================
FILE: phpunit-watcher.yml
================================================
# Copyright 1999-2025. WebPros International GmbH.
phpunit:
arguments: '--stop-on-failure'
timeout: 0
================================================
FILE: phpunit.xml.dist
================================================
./src
./tests
================================================
FILE: psalm.xml
================================================
================================================
FILE: src/Api/AbstractStruct.php
================================================
{key($property)};
} else {
/** @psalm-suppress PossiblyInvalidArgument */
$classPropertyName = $this->underToCamel(str_replace('-', '_', $property));
$value = $apiResponse->$property;
}
$reflectionProperty = new \ReflectionProperty($this, $classPropertyName);
$propertyType = $reflectionProperty->getType();
if (is_null($propertyType)) {
$docBlock = $reflectionProperty->getDocComment();
$propertyType = preg_replace('/^.+ @var ([a-z]+) .+$/', '\1', $docBlock);
} else {
/** @psalm-suppress UndefinedMethod */
$propertyType = $propertyType->getName();
}
if ('string' == $propertyType) {
$value = (string) $value;
} elseif ('int' == $propertyType) {
$value = (int) $value;
} elseif ('bool' == $propertyType) {
$value = in_array((string) $value, ['true', 'on', 'enabled']);
} else {
throw new \Exception("Unknown property type '$propertyType'.");
}
$this->$classPropertyName = $value;
}
}
/**
* Convert underscore separated words into camel case.
*
* @param string $under
*
* @return string
*/
private function underToCamel(string $under): string
{
$under = '_' . str_replace('_', ' ', strtolower($under));
return ltrim(str_replace(' ', '', ucwords($under)), '_');
}
}
================================================
FILE: src/Api/Client/Exception.php
================================================
host = $host;
$this->port = $port;
$this->protocol = $protocol;
}
/**
* Setup credentials for authentication.
*
* @param string $login
* @param string $password
*/
public function setCredentials(string $login, string $password): void
{
$this->login = $login;
$this->password = $password;
}
/**
* Define secret key for alternative authentication.
*
* @param string $secretKey
*/
public function setSecretKey(string $secretKey): void
{
$this->secretKey = $secretKey;
}
/**
* Set proxy server for requests.
*
* @param string $proxy
*/
public function setProxy(string $proxy): void
{
$this->proxy = $proxy;
}
/**
* Set default version for requests.
*
* @param string $version
*/
public function setVersion(string $version): void
{
$this->version = $version;
}
/**
* Set custom function to verify response of API call according your own needs.
* Default verifying will be used if it is not specified.
*
* @param callable|null $function
*/
public function setVerifyResponse(?callable $function = null): void
{
$this->verifyResponseCallback = $function;
}
/**
* Retrieve host used for communication.
*
* @return string
*/
public function getHost(): string
{
return $this->host;
}
/**
* Retrieve port used for communication.
*
* @return int
*/
public function getPort(): int
{
return $this->port;
}
/**
* Retrieve name of the protocol (http or https) used for communication.
*
* @return string
*/
public function getProtocol(): string
{
return $this->protocol;
}
/**
* Retrieve XML template for packet.
*
* @param string|null $version
*
* @return SimpleXMLElement
*/
public function getPacket($version = null): SimpleXMLElement
{
$protocolVersion = !is_null($version) ? $version : $this->version;
$content = "";
$content .= '';
return new SimpleXMLElement($content);
}
/**
* Perform API request.
*
* @param string|array|SimpleXMLElement $request
* @param int $mode
*
* @return XmlResponse
* @throws \Exception
*/
public function request($request, int $mode = self::RESPONSE_SHORT): XmlResponse
{
if ($request instanceof SimpleXMLElement) {
$request = $request->asXml();
} else {
$xml = $this->getPacket();
if (is_array($request)) {
$request = $this->arrayToXml($request, $xml)->asXML();
} elseif (preg_match('/^[a-z]/', $request)) {
$request = $this->expandRequestShortSyntax($request, $xml);
}
}
if ('sdk' == $this->protocol) {
$xml = $this->performSdkCall((string) $request);
} else {
$xml = $this->performHttpRequest((string) $request);
}
$this->verifyResponseCallback
? call_user_func($this->verifyResponseCallback, $xml)
: $this->verifyResponse($xml);
$result = (self::RESPONSE_FULL === $mode)
? $xml
: ($xml->xpath('//result') ?: [null])[0];
return new XmlResponse($result ? (string) $result->asXML() : '');
}
private function performSdkCall(string $request): XmlResponse
{
$version = ('' == $this->version) ? null : $this->version;
$requestXml = new SimpleXMLElement($request);
$innerNodes = $requestXml->children();
$innerXml = $innerNodes && count($innerNodes) > 0 && $innerNodes[0] ? $innerNodes[0]->asXml() : '';
/** @psalm-suppress UndefinedClass */
$result = \pm_ApiRpc::getService($version)->call($innerXml, $this->login);
return new XmlResponse($result ? (string) $result->asXML() : '');
}
/**
* Perform HTTP request to end-point.
*
* @param string $request
*
* @throws Client\Exception
*
* @return XmlResponse
*/
private function performHttpRequest($request)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$this->protocol://$this->host:$this->port/enterprise/control/agent.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->getHeaders());
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
if ('' !== $this->proxy) {
curl_setopt($curl, CURLOPT_PROXY, $this->proxy);
}
$result = curl_exec($curl);
if (false === $result) {
throw new Client\Exception(curl_error($curl), curl_errno($curl));
}
curl_close($curl);
return new XmlResponse((string) $result);
}
/**
* Perform multiple API requests using single HTTP request.
*
* @param array $requests
* @param int $mode
*
* @throws Client\Exception
*
* @return array
*/
public function multiRequest(array $requests, int $mode = self::RESPONSE_SHORT): array
{
$requestXml = $this->getPacket();
foreach ($requests as $request) {
if ($request instanceof SimpleXMLElement) {
throw new Client\Exception('SimpleXML type of request is not supported for multi requests.');
} else {
if (is_array($request)) {
$request = $this->arrayToXml($request, $requestXml)->asXML();
if (!$request) {
throw new Client\Exception('Failed to create an XML string for request');
}
} elseif (preg_match('/^[a-z]/', $request)) {
$this->expandRequestShortSyntax($request, $requestXml);
}
}
}
if ('sdk' == $this->protocol) {
throw new Client\Exception('Multi requests are not supported via SDK.');
} else {
$xmlString = $requestXml->asXML();
if (!$xmlString) {
throw new Client\Exception('Failed to create an XML string for request');
}
$responseXml = $this->performHttpRequest($xmlString);
}
return $this->splitResponseToArray($responseXml, $mode);
}
private function splitResponseToArray(XmlResponse $responseXml, $mode = self::RESPONSE_SHORT): array
{
$responses = [];
$nodes = $responseXml->children();
if (!$nodes) {
return [];
}
foreach ($nodes as $childNode) {
$dom = $this->getDomDocument($this->getPacket());
if (!$dom) {
continue;
}
$childDomNode = dom_import_simplexml($childNode);
if (!is_null($childDomNode)) {
$childDomNode = $dom->importNode($childDomNode, true);
$dom->documentElement->appendChild($childDomNode);
}
$response = simplexml_load_string($dom->saveXML());
if (!$response) {
return [];
}
$responses[] = (self::RESPONSE_FULL == $mode)
? $response
: ($response->xpath('//result') ?: [null])[0];
}
return $responses;
}
private function getDomDocument(SimpleXMLElement $xml): ?DOMDocument
{
$dom = dom_import_simplexml($xml);
if (is_null($dom)) {
return null;
}
return $dom->ownerDocument;
}
/**
* Retrieve list of headers needed for request.
*
* @return array
*/
private function getHeaders()
{
$headers = [
'Content-Type: text/xml',
'HTTP_PRETTY_PRINT: TRUE',
];
if ($this->secretKey) {
$headers[] = "KEY: $this->secretKey";
} else {
$headers[] = "HTTP_AUTH_LOGIN: $this->login";
$headers[] = "HTTP_AUTH_PASSWD: $this->password";
}
return $headers;
}
/**
* Verify that response does not contain errors.
*
* @param XmlResponse $xml
*
* @throws Exception
*/
private function verifyResponse($xml): void
{
if ($xml->system && $xml->system->status && 'error' == (string) $xml->system->status) {
throw new Exception((string) $xml->system->errtext, (int) $xml->system->errcode);
}
if ($xml->xpath('//status[text()="error"]') && $xml->xpath('//errcode') && $xml->xpath('//errtext')) {
$errorCode = (int) ($xml->xpath('//errcode') ?: [null])[0];
$errorMessage = (string) ($xml->xpath('//errtext') ?: [null])[0];
throw new Exception($errorMessage, $errorCode);
}
}
/**
* Expand short syntax (some.method.call) into full XML representation.
*
* @param string $request
* @param SimpleXMLElement $xml
*
* @return false|string
*/
private function expandRequestShortSyntax($request, SimpleXMLElement $xml)
{
$parts = explode('.', $request);
$node = $xml;
$lastParts = end($parts);
foreach ($parts as $part) {
// phpcs:ignore
@list($name, $value) = explode('=', $part);
if ($part !== $lastParts) {
$node = $node->addChild($name);
} else {
$node->{$name} = (string) $value;
}
}
return $xml->asXML();
}
/**
* Convert array to XML representation.
*
* @param array $array
* @param SimpleXMLElement $xml
* @param string $parentEl
*
* @return SimpleXMLElement
*/
private function arrayToXml(array $array, SimpleXMLElement $xml, $parentEl = null)
{
foreach ($array as $key => $value) {
$el = is_int($key) && $parentEl ? $parentEl : $key;
if (is_array($value)) {
$this->arrayToXml($value, $this->isAssocArray($value) ? $xml->addChild($el) : $xml, $el);
} elseif (!isset($xml->{$el})) {
$xml->{$el} = (string) $value;
} else {
$xml->{$el}[] = (string) $value;
}
}
return $xml;
}
/**
* @param array $array
*
* @return bool
*/
private function isAssocArray(array $array)
{
return $array && array_keys($array) !== range(0, count($array) - 1);
}
/**
* @param string $name
*
* @return mixed
*/
private function getOperator(string $name)
{
if (!isset($this->operatorsCache[$name])) {
$className = '\\PleskX\\Api\\Operator\\' . $name;
/** @psalm-suppress InvalidStringClass */
$this->operatorsCache[$name] = new $className($this);
}
return $this->operatorsCache[$name];
}
public function server(): Operator\Server
{
return $this->getOperator('Server');
}
public function customer(): Operator\Customer
{
return $this->getOperator('Customer');
}
public function webspace(): Operator\Webspace
{
return $this->getOperator('Webspace');
}
public function subdomain(): Operator\Subdomain
{
return $this->getOperator('Subdomain');
}
public function dns(): Operator\Dns
{
return $this->getOperator('Dns');
}
public function dnsTemplate(): Operator\DnsTemplate
{
return $this->getOperator('DnsTemplate');
}
public function databaseServer(): Operator\DatabaseServer
{
return $this->getOperator('DatabaseServer');
}
public function mail(): Operator\Mail
{
return $this->getOperator('Mail');
}
public function certificate(): Operator\Certificate
{
return $this->getOperator('Certificate');
}
public function siteAlias(): Operator\SiteAlias
{
return $this->getOperator('SiteAlias');
}
public function ip(): Operator\Ip
{
return $this->getOperator('Ip');
}
public function eventLog(): Operator\EventLog
{
return $this->getOperator('EventLog');
}
public function secretKey(): Operator\SecretKey
{
return $this->getOperator('SecretKey');
}
public function ui(): Operator\Ui
{
return $this->getOperator('Ui');
}
public function servicePlan(): Operator\ServicePlan
{
return $this->getOperator('ServicePlan');
}
public function virtualDirectory(): Operator\VirtualDirectory
{
return $this->getOperator('VirtualDirectory');
}
public function database(): Operator\Database
{
return $this->getOperator('Database');
}
public function session(): Operator\Session
{
return $this->getOperator('Session');
}
public function locale(): Operator\Locale
{
return $this->getOperator('Locale');
}
public function logRotation(): Operator\LogRotation
{
return $this->getOperator('LogRotation');
}
public function protectedDirectory(): Operator\ProtectedDirectory
{
return $this->getOperator('ProtectedDirectory');
}
public function reseller(): Operator\Reseller
{
return $this->getOperator('Reseller');
}
public function resellerPlan(): Operator\ResellerPlan
{
return $this->getOperator('ResellerPlan');
}
public function aps(): Operator\Aps
{
return $this->getOperator('Aps');
}
public function servicePlanAddon(): Operator\ServicePlanAddon
{
return $this->getOperator('ServicePlanAddon');
}
public function site(): Operator\Site
{
return $this->getOperator('Site');
}
public function phpHandler(): Operator\PhpHandler
{
return $this->getOperator('PhpHandler');
}
}
================================================
FILE: src/Api/Exception.php
================================================
login = $login;
}
}
================================================
FILE: src/Api/Operator/Aps.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('generate')->addChild('info');
foreach ($properties as $name => $value) {
$info->{$name} = $value;
}
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param array $properties
* @param string|Struct\Info $certificate
* @param string|null $privateKey
*/
public function install(array $properties, $certificate, ?string $privateKey = null): bool
{
return $this->callApi('install', $properties, $certificate, $privateKey);
}
/**
* @param array $properties
* @param Struct\Info $certificate
*/
public function update(array $properties, Struct\Info $certificate): bool
{
return $this->callApi('update', $properties, $certificate);
}
/**
* @param string $method
* @param array $properties
* @param string|Struct\Info $certificate
* @param string|null $privateKey
*/
private function callApi(string $method, array $properties, $certificate, ?string $privateKey = null): bool
{
$packet = $this->client->getPacket();
$installTag = $packet->addChild($this->wrapperTag)->addChild($method);
foreach ($properties as $name => $value) {
$installTag->{$name} = $value;
}
$contentTag = $installTag->addChild('content');
if (is_string($certificate)) {
$contentTag->addChild('csr', $certificate);
$contentTag->addChild('pvt', $privateKey);
} elseif ($certificate instanceof \PleskX\Api\Struct\Certificate\Info) {
foreach ($certificate->getMapping() as $name => $value) {
$contentTag->{$name} = $value;
}
}
$result = $this->client->request($packet);
return 'ok' == (string) $result->status;
}
public function delete(string $name, array $properties): bool
{
$packet = $this->client->getPacket();
$removeTag = $packet->addChild($this->wrapperTag)->addChild('remove');
$removeTag->addChild('filter')->addChild('name', $name);
foreach ($properties as $name => $value) {
$removeTag->{$name} = $value;
}
$result = $this->client->request($packet);
return 'ok' == (string) $result->status;
}
}
================================================
FILE: src/Api/Operator/Customer.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add')->addChild('gen_info');
foreach ($properties as $name => $value) {
$info->{$name} = $value;
}
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\GeneralInfo
*/
public function get(string $field, $value): Struct\GeneralInfo
{
$items = $this->getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
return reset($items);
}
/**
* @return Struct\GeneralInfo[]
*/
public function getAll(): array
{
return $this->getItems(Struct\GeneralInfo::class, 'gen_info');
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function enable(string $field, $value): bool
{
return $this->setProperties($field, $value, ['status' => 0]);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function disable(string $field, $value): bool
{
return $this->setProperties($field, $value, ['status' => 16]);
}
/**
* @param string $field
* @param int|string $value
* @param array $properties
*
* @return bool
*/
public function setProperties(string $field, $value, array $properties): bool
{
$packet = $this->client->getPacket();
$setTag = $packet->addChild($this->wrapperTag)->addChild('set');
$setTag->addChild('filter')->addChild($field, (string) $value);
$genInfoTag = $setTag->addChild('values')->addChild('gen_info');
foreach ($properties as $property => $propertyValue) {
$genInfoTag->addChild($property, (string) $propertyValue);
}
$response = $this->client->request($packet);
return 'ok' === (string) $response->status;
}
}
================================================
FILE: src/Api/Operator/Database.php
================================================
process('add-db', $properties));
}
public function createUser(array $properties): Struct\UserInfo
{
return new Struct\UserInfo($this->process('add-db-user', $properties));
}
private function process(string $command, array $properties): XmlResponse
{
$packet = $this->client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild($command);
foreach ($properties as $name => $value) {
if (false !== strpos($value, '&')) {
$info->$name = $value;
continue;
}
$info->{$name} = $value;
}
return $this->client->request($packet);
}
public function updateUser(array $properties): bool
{
$response = $this->process('set-db-user', $properties);
return 'ok' === (string) $response->status;
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\UserInfo
*/
public function getUser(string $field, $value): Struct\UserInfo
{
$items = $this->getAllUsers($field, $value);
return reset($items);
}
/**
* @param string|null $field
* @param int|string $value
*
* @return Struct\Info[]
*/
public function getAll(?string $field, $value): array
{
$response = $this->getBy('get-db', $field, $value);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if ($xmlResult && isset($xmlResult->id) && (int) $xmlResult->id > 0) {
$items[] = new Struct\Info($xmlResult);
}
}
return $items;
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\UserInfo[]
*/
public function getAllUsers(string $field, $value): array
{
$response = $this->getBy('get-db-users', $field, $value);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if ($xmlResult) {
$items[] = new Struct\UserInfo($xmlResult);
}
}
return $items;
}
/**
* @param string $command
* @param string|null $field
* @param int|string $value
*
* @return XmlResponse
*/
private function getBy(string $command, ?string $field, $value): XmlResponse
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild($command);
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = (string) $value;
}
return $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value, 'del-db');
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function deleteUser(string $field, $value): bool
{
return $this->deleteBy($field, $value, 'del-db-user');
}
}
================================================
FILE: src/Api/Operator/DatabaseServer.php
================================================
request('get-supported-types');
return (array) $response->type;
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getBy($field, $value);
return reset($items);
}
/**
* @return Struct\Info[]
*/
public function getAll(): array
{
return $this->getBy();
}
public function getDefault(string $type): Struct\Info
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get-default');
$filterTag = $getTag->addChild('filter');
/** @psalm-suppress UndefinedPropertyAssignment */
$filterTag->type = $type;
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param string|null $field
* @param int|string|null $value
*
* @return Struct\Info[]
*/
private function getBy($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = (string) $value;
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
if (!is_null($xmlResult->data)) {
$item = new Struct\Info($xmlResult->data);
$item->id = (int) $xmlResult->id;
$items[] = $item;
}
}
return $items;
}
}
================================================
FILE: src/Api/Operator/Dns.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add_rec');
foreach ($properties as $name => $value) {
$info->{$name} = $value;
}
return new Struct\Info($this->client->request($packet));
}
/**
* Send multiply records by one request.
*
* @param array $records
*
* @return \SimpleXMLElement[]
*/
public function bulkCreate(array $records): array
{
$packet = $this->client->getPacket();
foreach ($records as $properties) {
$info = $packet->addChild($this->wrapperTag)->addChild('add_rec');
foreach ($properties as $name => $value) {
$info->{$name} = $value;
}
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if ($xmlResult) {
$items[] = $xmlResult;
}
}
return $items;
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info[]
*/
public function getAll(string $field, $value): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get_rec');
$filterTag = $getTag->addChild('filter');
$filterTag->addChild($field, (string) $value);
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
if (!is_null($xmlResult->data)) {
$item = new Struct\Info($xmlResult->data);
$item->id = (int) $xmlResult->id;
$items[] = $item;
}
}
return $items;
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value, 'del_rec');
}
/**
* Delete multiply records by one request.
*
* @param array $recordIds
*
* @return \SimpleXMLElement[]
*/
public function bulkDelete(array $recordIds): array
{
$packet = $this->client->getPacket();
foreach ($recordIds as $recordId) {
$packet->addChild($this->wrapperTag)->addChild('del_rec')
->addChild('filter')->addChild('id', $recordId);
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if ($xmlResult) {
$items[] = $xmlResult;
}
}
return $items;
}
}
================================================
FILE: src/Api/Operator/DnsTemplate.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add_rec');
unset($properties['site-id'], $properties['site-alias-id']);
foreach ($properties as $name => $value) {
$info->{$name} = $value;
}
return new Struct\Info($this->client->request($packet));
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info[]
*/
public function getAll($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get_rec');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = (string) $value;
}
$getTag->addChild('template');
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
if (!is_null($xmlResult->data)) {
$item = new Struct\Info($xmlResult->data);
$item->id = (int) $xmlResult->id;
$items[] = $item;
}
}
return $items;
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
$packet = $this->client->getPacket();
$delTag = $packet->addChild($this->wrapperTag)->addChild('del_rec');
$delTag->addChild('filter')->addChild($field, (string) $value);
$delTag->addChild('template');
$response = $this->client->request($packet);
return 'ok' === (string) $response->status;
}
}
================================================
FILE: src/Api/Operator/EventLog.php
================================================
request('get');
foreach ($response->event ?? [] as $eventInfo) {
$records[] = new Struct\Event($eventInfo);
}
return $records;
}
/**
* @return Struct\DetailedEvent[]
*/
public function getDetailedLog(): array
{
$records = [];
$response = $this->request('get_events');
foreach ($response->event ?? [] as $eventInfo) {
$records[] = new Struct\DetailedEvent($eventInfo);
}
return $records;
}
public function getLastId(): int
{
return (int) $this->request('get-last-id')->getValue('id');
}
}
================================================
FILE: src/Api/Operator/Ip.php
================================================
client->getPacket();
$packet->addChild($this->wrapperTag)->addChild('get');
$response = $this->client->request($packet);
foreach ($response->addresses->ip_info ?? [] as $ipInfo) {
$ips[] = new Struct\Info($ipInfo);
}
return $ips;
}
}
================================================
FILE: src/Api/Operator/Locale.php
================================================
client->getPacket();
$filter = $packet->addChild($this->wrapperTag)->addChild('get')->addChild('filter');
if (!is_null($id)) {
$filter->addChild('id', $id);
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
foreach ($response->locale->get->result ?? [] as $localeInfo) {
if (!is_null($localeInfo->info)) {
$locales[(string) $localeInfo->info->id] = new Struct\Info($localeInfo->info);
}
}
return !is_null($id) ? reset($locales) : $locales;
}
}
================================================
FILE: src/Api/Operator/LogRotation.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('create');
$filter = $info->addChild('filter');
$filter->addChild('site-id', (string) $siteId);
$mailname = $filter->addChild('mailname');
$mailname->addChild('name', $name);
if ($mailbox) {
$mailname->addChild('mailbox')->addChild('enabled', 'true');
}
if (!empty($password)) {
/** @psalm-suppress UndefinedPropertyAssignment */
$mailname->addChild('password')->value = $password;
}
$response = $this->client->request($packet);
/** @psalm-suppress PossiblyNullArgument */
return new Struct\Info($response->mailname);
}
/**
* @param string $field
* @param int|string $value
* @param int $siteId
*
* @return bool
*/
public function delete(string $field, $value, int $siteId): bool
{
$packet = $this->client->getPacket();
$filter = $packet->addChild($this->wrapperTag)->addChild('remove')->addChild('filter');
$filter->addChild('site-id', (string) $siteId);
$filter->{$field} = (string) $value;
$response = $this->client->request($packet);
return 'ok' === (string) $response->status;
}
public function get(string $name, int $siteId): Struct\GeneralInfo
{
$items = $this->getAll($siteId, $name);
return reset($items);
}
/**
* @param int $siteId
* @param string|null $name
*
* @return Struct\GeneralInfo[]
*/
public function getAll(int $siteId, $name = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get_info');
$filterTag = $getTag->addChild('filter');
$filterTag->addChild('site-id', (string) $siteId);
if (!is_null($name)) {
$filterTag->addChild('name', $name);
}
$response = $this->client->request($packet, Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult || !isset($xmlResult->mailname)) {
continue;
}
$item = new Struct\GeneralInfo($xmlResult->mailname);
$items[] = $item;
}
return $items;
}
}
================================================
FILE: src/Api/Operator/PhpHandler.php
================================================
client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, (string) $value);
}
$response = $this->client->request($packet, Client::RESPONSE_FULL);
$xmlResult = ($response->xpath('//result') ?: [null])[0];
return $xmlResult ? new Info($xmlResult) : null;
}
/**
* @param string|null $field
* @param int|string $value
*
* @return Info[]
*/
public function getAll($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, (string) $value);
}
$response = $this->client->request($packet, Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
$item = new Info($xmlResult);
$items[] = $item;
}
return $items;
}
}
================================================
FILE: src/Api/Operator/ProtectedDirectory.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add');
$info->addChild('site-id', (string) $siteId);
$info->addChild('name', $name);
$info->addChild('header', $header);
return new Struct\Info($this->client->request($packet));
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value, 'delete');
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\DataInfo
*/
public function get(string $field, $value): Struct\DataInfo
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\DataInfo[]
*/
public function getAll(string $field, $value): array
{
$response = $this->getBy('get', $field, $value);
$items = [];
foreach ((array) $response->xpath('//result/data') as $xmlResult) {
if (!$xmlResult) {
continue;
}
$items[] = new Struct\DataInfo($xmlResult);
}
return $items;
}
/**
* @param Struct\Info $protectedDirectory
* @param string $login
* @param string $password
*
* @return Struct\UserInfo
* @psalm-suppress UndefinedPropertyAssignment
*/
public function addUser($protectedDirectory, $login, $password)
{
$packet = $this->client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add-user');
$info->{'pd-id'} = (string) $protectedDirectory->id;
$info->login = $login;
$info->password = $password;
return new Struct\UserInfo($this->client->request($packet));
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function deleteUser($field, $value)
{
return $this->deleteBy($field, $value, 'delete-user');
}
/**
* @param string $command
* @param string $field
* @param int|string $value
*
* @return \PleskX\Api\XmlResponse
*/
private function getBy(string $command, string $field, $value)
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild($command);
$filterTag = $getTag->addChild('filter');
$filterTag->{$field} = (string) $value;
return $this->client->request($packet, Client::RESPONSE_FULL);
}
}
================================================
FILE: src/Api/Operator/Reseller.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add')->addChild('gen-info');
foreach ($properties as $name => $value) {
$info->{$name} = $value;
}
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\GeneralInfo
*/
public function get(string $field, $value): Struct\GeneralInfo
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\GeneralInfo[]
*/
public function getAll($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, (string) $value);
}
$datasetTag = $getTag->addChild('dataset');
$datasetTag->addChild('gen-info');
$datasetTag->addChild('permissions');
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult || !$xmlResult->data) {
continue;
}
$item = new Struct\GeneralInfo($xmlResult->data);
$item->id = (int) $xmlResult->id;
$items[] = $item;
}
return $items;
}
}
================================================
FILE: src/Api/Operator/ResellerPlan.php
================================================
client->getPacket();
$createTag = $packet->addChild($this->wrapperTag)->addChild('create');
if ('' !== $ipAddress) {
$createTag->addChild('ip_address', $ipAddress);
}
if ('' !== $description) {
$createTag->addChild('description', $description);
}
$response = $this->client->request($packet);
return (string) $response->key;
}
public function delete(string $keyId): bool
{
return $this->deleteBy('key', $keyId, 'delete');
}
public function get(string $keyId): Struct\Info
{
$items = $this->getBy($keyId);
return reset($items);
}
/**
* @return Struct\Info[]
*/
public function getAll(): array
{
return $this->getBy();
}
/**
* @param string|null $keyId
*
* @return Struct\Info[]
*/
public function getBy($keyId = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get_info');
$filterTag = $getTag->addChild('filter');
if (!is_null($keyId)) {
$filterTag->addChild('key', $keyId);
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result/key_info') as $keyInfo) {
if (!$keyInfo) {
continue;
}
$items[] = new Struct\Info($keyInfo);
}
return $items;
}
}
================================================
FILE: src/Api/Operator/Server.php
================================================
client->getPacket();
$packet->addChild($this->wrapperTag)->addChild('get_protos');
$response = $this->client->request($packet);
/** @psalm-suppress PossiblyNullPropertyFetch */
return (array) $response->protos->proto;
}
public function getGeneralInfo(): Struct\GeneralInfo
{
return new Struct\GeneralInfo($this->getInfo('gen_info'));
}
public function getPreferences(): Struct\Preferences
{
return new Struct\Preferences($this->getInfo('prefs'));
}
public function getAdmin(): Struct\Admin
{
return new Struct\Admin($this->getInfo('admin'));
}
public function getKeyInfo(): array
{
$keyInfo = [];
$keyInfoXml = $this->getInfo('key');
foreach ($keyInfoXml->property ?? [] as $property) {
$keyInfo[(string) $property->name] = (string) $property->value;
}
return $keyInfo;
}
public function getComponents(): array
{
$components = [];
$componentsXml = $this->getInfo('components');
foreach ($componentsXml->component ?? [] as $component) {
$components[(string) $component->name] = (string) $component->version;
}
return $components;
}
public function getServiceStates(): array
{
$states = [];
$statesXml = $this->getInfo('services_state');
foreach ($statesXml->srv ?? [] as $service) {
$states[(string) $service->id] = [
'id' => (string) $service->id,
'title' => (string) $service->title,
'state' => (string) $service->state,
];
}
return $states;
}
public function getSessionPreferences(): Struct\SessionPreferences
{
return new Struct\SessionPreferences($this->getInfo('session_setup'));
}
public function getShells(): array
{
$shells = [];
$shellsXml = $this->getInfo('shells');
foreach ($shellsXml->shell ?? [] as $shell) {
$shells[(string) $shell->name] = (string) $shell->path;
}
return $shells;
}
public function getNetworkInterfaces(): array
{
$interfacesXml = $this->getInfo('interfaces');
return (array) $interfacesXml->interface;
}
public function getStatistics(): Struct\Statistics
{
return new Struct\Statistics($this->getInfo('stat'));
}
public function getSiteIsolationConfig(): array
{
$config = [];
$configXml = $this->getInfo('site-isolation-config');
foreach ($configXml->property ?? [] as $property) {
$config[(string) $property->name] = (string) $property->value;
}
return $config;
}
public function getUpdatesInfo(): Struct\UpdatesInfo
{
return new Struct\UpdatesInfo($this->getInfo('updates'));
}
public function createSession(string $login, string $clientIp): string
{
$packet = $this->client->getPacket();
$sessionNode = $packet->addChild($this->wrapperTag)->addChild('create_session');
$sessionNode->addChild('login', $login);
$dataNode = $sessionNode->addChild('data');
$dataNode->addChild('user_ip', base64_encode($clientIp));
$dataNode->addChild('source_server');
$response = $this->client->request($packet);
return (string) $response->id;
}
private function getInfo(string $operation): XmlResponse
{
$packet = $this->client->getPacket();
$packet->addChild($this->wrapperTag)->addChild('get')->addChild($operation);
$response = $this->client->request($packet);
return $response->$operation;
}
}
================================================
FILE: src/Api/Operator/ServicePlan.php
================================================
request(['add' => $properties]);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getBy($field, $value);
return reset($items);
}
/**
* @return Struct\Info[]
*/
public function getAll(): array
{
return $this->getBy();
}
/**
* @param string|null $field
* @param int|string|null $value
*
* @return Struct\Info[]
*/
private function getBy($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, (string) $value);
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
$items[] = new Struct\Info($xmlResult);
}
return $items;
}
}
================================================
FILE: src/Api/Operator/ServicePlanAddon.php
================================================
request(['add' => $properties]);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getBy($field, $value);
return reset($items);
}
/**
* @return Struct\Info[]
*/
public function getAll(): array
{
return $this->getBy();
}
/**
* @param string|null $field
* @param int|string|null $value
*
* @return Struct\Info[]
*/
private function getBy($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, (string) $value);
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
$items[] = new Struct\Info($xmlResult);
}
return $items;
}
}
================================================
FILE: src/Api/Operator/Session.php
================================================
client->getPacket();
$creator = $packet->addChild('server')->addChild('create_session');
$creator->addChild('login', $username);
$loginData = $creator->addChild('data');
$loginData->addChild('user_ip', base64_encode($userIp));
$loginData->addChild('source_server', '');
$response = $this->client->request($packet);
return (string) $response->id;
}
/**
* @return Struct\Info[]
*/
public function get(): array
{
$sessions = [];
$response = $this->request('get');
foreach ($response->session ?? [] as $sessionInfo) {
$sessions[(string) $sessionInfo->id] = new Struct\Info($sessionInfo);
}
return $sessions;
}
public function terminate(string $sessionId): bool
{
$response = $this->request("terminate.session-id=$sessionId");
return 'ok' === (string) $response->status;
}
}
================================================
FILE: src/Api/Operator/Site.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add');
$infoGeneral = $info->addChild('gen_setup');
foreach ($properties as $name => $value) {
if (!is_scalar($value)) {
continue;
}
$infoGeneral->{$name} = (string) $value;
}
// set hosting properties
if (isset($properties[static::PROPERTIES_HOSTING]) && is_array($properties[static::PROPERTIES_HOSTING])) {
$hostingNode = $info->addChild('hosting')->addChild('vrt_hst');
foreach ($properties[static::PROPERTIES_HOSTING] as $name => $value) {
$propertyNode = $hostingNode->addChild('property');
/** @psalm-suppress UndefinedPropertyAssignment */
$propertyNode->name = $name;
/** @psalm-suppress UndefinedPropertyAssignment */
$propertyNode->value = $value;
}
}
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return ?Struct\GeneralInfo
*/
public function get(string $field, $value): ?Struct\GeneralInfo
{
$items = $this->getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
return reset($items) ?: null;
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\HostingInfo|null
*/
public function getHosting(string $field, $value): ?Struct\HostingInfo
{
$items = $this->getItems(
Struct\HostingInfo::class,
'hosting',
$field,
$value,
function (\SimpleXMLElement $node) {
return isset($node->vrt_hst);
}
);
return empty($items) ? null : reset($items);
}
/**
* @return Struct\GeneralInfo[]
*/
public function getAll(): array
{
return $this->getItems(Struct\GeneralInfo::class, 'gen_info');
}
}
================================================
FILE: src/Api/Operator/SiteAlias.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('create');
if (count($preferences) > 0) {
$prefs = $info->addChild('pref');
foreach ($preferences as $key => $value) {
$prefs->addChild($key, is_bool($value) ? ($value ? '1' : '0') : $value);
}
}
$info->addChild('site-id', $properties['site-id']);
$info->addChild('name', $properties['name']);
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value, 'delete');
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\GeneralInfo
*/
public function get(string $field, $value): Struct\GeneralInfo
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\GeneralInfo[]
*/
public function getAll($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = (string) $value;
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult) {
continue;
}
if (!is_null($xmlResult->info)) {
$item = new Struct\GeneralInfo($xmlResult->info);
$items[] = $item;
}
}
return $items;
}
}
================================================
FILE: src/Api/Operator/Subdomain.php
================================================
client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add');
foreach ($properties as $name => $value) {
if (is_array($value)) {
foreach ($value as $propertyName => $propertyValue) {
$property = $info->addChild($name);
/** @psalm-suppress UndefinedPropertyAssignment */
$property->name = $propertyName;
/** @psalm-suppress UndefinedPropertyAssignment */
$property->value = $propertyValue;
}
continue;
}
$info->{$name} = $value;
}
$response = $this->client->request($packet);
return new Struct\Info($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get(string $field, $value): Struct\Info
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info[]
*/
public function getAll($field = null, $value = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, (string) $value);
}
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult || empty($xmlResult->data)) {
continue;
}
$item = new Struct\Info($xmlResult->data);
$item->id = (int) $xmlResult->id;
$items[] = $item;
}
return $items;
}
}
================================================
FILE: src/Api/Operator/Ui.php
================================================
request('get-navigation');
/** @psalm-suppress ImplicitToStringCast, PossiblyNullArgument */
return unserialize(base64_decode($response->navigation));
}
public function createCustomButton(string $owner, array $properties): int
{
$packet = $this->client->getPacket();
$buttonNode = $packet->addChild($this->wrapperTag)->addChild('create-custombutton');
$buttonNode->addChild('owner')->addChild($owner);
$propertiesNode = $buttonNode->addChild('properties');
foreach ($properties as $name => $value) {
$propertiesNode->{$name} = $value;
}
$response = $this->client->request($packet);
return (int) $response->id;
}
public function getCustomButton(int $id): Struct\CustomButton
{
$response = $this->request("get-custombutton.filter.custombutton-id=$id");
return new Struct\CustomButton($response);
}
public function deleteCustomButton(int $id): bool
{
return $this->deleteBy('custombutton-id', $id, 'delete-custombutton');
}
}
================================================
FILE: src/Api/Operator/VirtualDirectory.php
================================================
request('get-permission-descriptor.filter');
return new Struct\PermissionDescriptor($response);
}
public function getLimitDescriptor(): Struct\LimitDescriptor
{
$response = $this->request('get-limit-descriptor.filter');
return new Struct\LimitDescriptor($response);
}
public function getPhysicalHostingDescriptor(): Struct\PhysicalHostingDescriptor
{
$response = $this->request('get-physical-hosting-descriptor.filter');
return new Struct\PhysicalHostingDescriptor($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\PhpSettings
*/
public function getPhpSettings(string $field, $value): Struct\PhpSettings
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$getTag->addChild('filter')->addChild($field, (string) $value);
$getTag->addChild('dataset')->addChild('php-settings');
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
return new Struct\PhpSettings($response);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Limits
*/
public function getLimits(string $field, $value): Struct\Limits
{
$items = $this->getItems(Struct\Limits::class, 'limits', $field, $value);
return reset($items);
}
/**
* @param array $properties
* @param array|null $hostingProperties
* @param string $planName
*
* @return Struct\Info
*/
public function create(array $properties, ?array $hostingProperties = null, string $planName = ''): Struct\Info
{
$packet = $this->client->getPacket();
$info = $packet->addChild($this->wrapperTag)->addChild('add');
$infoGeneral = $info->addChild('gen_setup');
foreach ($properties as $name => $value) {
if (is_array($value)) {
continue;
} else {
$infoGeneral->addChild($name, (string) $value);
}
}
if ($hostingProperties) {
$infoHosting = $info->addChild('hosting')->addChild('vrt_hst');
foreach ($hostingProperties as $name => $value) {
$property = $infoHosting->addChild('property');
/** @psalm-suppress UndefinedPropertyAssignment */
$property->name = $name;
/** @psalm-suppress UndefinedPropertyAssignment */
$property->value = $value;
}
if (isset($properties['ip_address'])) {
foreach ((array) $properties['ip_address'] as $ipAddress) {
$infoHosting->addChild('ip_address', $ipAddress);
}
}
}
if ('' !== $planName) {
$info->addChild('plan-name', $planName);
}
$response = $this->client->request($packet);
return new Struct\Info($response, $properties['name'] ?? '');
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function delete(string $field, $value): bool
{
return $this->deleteBy($field, $value);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\GeneralInfo
*/
public function get(string $field, $value): Struct\GeneralInfo
{
$items = $this->getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
return reset($items);
}
/**
* @return Struct\GeneralInfo[]
*/
public function getAll(): array
{
return $this->getItems(Struct\GeneralInfo::class, 'gen_info');
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\DiskUsage
*/
public function getDiskUsage(string $field, $value): Struct\DiskUsage
{
$items = $this->getItems(Struct\DiskUsage::class, 'disk_usage', $field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function enable(string $field, $value): bool
{
return $this->setProperties($field, $value, ['status' => 0]);
}
/**
* @param string $field
* @param int|string $value
*
* @return bool
*/
public function disable(string $field, $value): bool
{
return $this->setProperties($field, $value, ['status' => 16]);
}
/**
* @param string $field
* @param int|string $value
* @param array $properties
*
* @return bool
*/
public function setProperties(string $field, $value, array $properties): bool
{
$packet = $this->client->getPacket();
$setTag = $packet->addChild($this->wrapperTag)->addChild('set');
$setTag->addChild('filter')->addChild($field, (string) $value);
$genInfoTag = $setTag->addChild('values')->addChild('gen_setup');
foreach ($properties as $property => $propertyValue) {
$genInfoTag->addChild($property, (string) $propertyValue);
}
$response = $this->client->request($packet);
return 'ok' === (string) $response->status;
}
}
================================================
FILE: src/Api/Operator.php
================================================
client = $client;
if ('' === $this->wrapperTag) {
$classNameParts = explode('\\', get_class($this));
$this->wrapperTag = end($classNameParts);
$this->wrapperTag = strtolower(preg_replace('/([a-z])([A-Z])/', '\1-\2', $this->wrapperTag));
}
}
/**
* Perform plain API request.
*
* @param string|array $request
* @param int $mode
*
* @return XmlResponse
*/
public function request($request, $mode = Client::RESPONSE_SHORT): XmlResponse
{
$wrapperTag = $this->wrapperTag;
if (is_array($request)) {
$request = [$wrapperTag => $request];
} elseif (preg_match('/^[a-z]/', $request)) {
$request = "$wrapperTag.$request";
} else {
$request = "<$wrapperTag>$request$wrapperTag>";
}
return $this->client->request($request, $mode);
}
/**
* @param string $field
* @param int|string $value
* @param string $deleteMethodName
*
* @return bool
*/
protected function deleteBy(string $field, $value, string $deleteMethodName = 'del'): bool
{
$response = $this->request([
$deleteMethodName => [
'filter' => [
$field => $value,
],
],
]);
return 'ok' === (string) $response->status;
}
/**
* @param string $structClass
* @param string $infoTag
* @param string|null $field
* @param int|string|null $value
* @param callable|null $filter
*
* @return array
*/
protected function getItems($structClass, $infoTag, $field = null, $value = null, ?callable $filter = null): array
{
$packet = $this->client->getPacket();
$getTag = $packet->addChild($this->wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->{$field} = (string) $value;
}
$getTag->addChild('dataset')->addChild($infoTag);
$response = $this->client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ((array) $response->xpath('//result') as $xmlResult) {
if (!$xmlResult || !isset($xmlResult->data) || !isset($xmlResult->data->$infoTag)) {
continue;
}
if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) {
continue;
}
/** @psalm-suppress InvalidStringClass */
$item = new $structClass($xmlResult->data->$infoTag);
if (isset($xmlResult->id) && property_exists($item, 'id')) {
$item->id = (int) $xmlResult->id;
}
$items[] = $item;
}
return $items;
}
}
================================================
FILE: src/Api/Struct/Certificate/Info.php
================================================
initScalarProperties($input, [
['csr' => 'request'],
['pvt' => 'privateKey'],
]);
} else {
foreach ($input as $name => $value) {
$this->$name = $value;
}
}
}
public function getMapping(): array
{
return array_filter([
'csr' => $this->request,
'pvt' => $this->privateKey,
'cert' => $this->publicKey,
'ca' => $this->publicKeyCA,
]);
}
}
================================================
FILE: src/Api/Struct/Customer/GeneralInfo.php
================================================
initScalarProperties($apiResponse, [
['cname' => 'company'],
['pname' => 'personalName'],
'login',
'guid',
'email',
'phone',
'fax',
'address',
['pcode' => 'postalCode'],
'city',
'state',
'country',
'external-id',
'description',
]);
$this->enabled = '0' === (string) $apiResponse->status;
}
}
================================================
FILE: src/Api/Struct/Customer/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'guid',
]);
}
}
================================================
FILE: src/Api/Struct/Database/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'name',
'type',
'webspace-id',
'db-server-id',
'default-user-id',
]);
}
}
================================================
FILE: src/Api/Struct/Database/UserInfo.php
================================================
initScalarProperties($apiResponse, [
'id',
'login',
'db-id',
]);
}
}
================================================
FILE: src/Api/Struct/DatabaseServer/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'host',
'port',
'type',
]);
}
}
================================================
FILE: src/Api/Struct/Dns/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'site-id',
'site-alias-id',
'type',
'host',
'value',
'opt',
]);
}
}
================================================
FILE: src/Api/Struct/EventLog/DetailedEvent.php
================================================
initScalarProperties($apiResponse, [
'id',
'type',
'time',
'class',
['obj_id' => 'objectId'],
'user',
'host',
]);
}
}
================================================
FILE: src/Api/Struct/EventLog/Event.php
================================================
initScalarProperties($apiResponse, [
'type',
'time',
'class',
'id',
]);
}
}
================================================
FILE: src/Api/Struct/Ip/Info.php
================================================
initScalarProperties($apiResponse, [
'ip_address',
'netmask',
'type',
'interface',
]);
}
}
================================================
FILE: src/Api/Struct/Locale/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
['lang' => 'language'],
'country',
]);
}
}
================================================
FILE: src/Api/Struct/Mail/GeneralInfo.php
================================================
initScalarProperties($apiResponse, [
'id',
'name',
'description',
]);
}
}
================================================
FILE: src/Api/Struct/Mail/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'name',
]);
}
}
================================================
FILE: src/Api/Struct/PhpHandler/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'display-name',
'full-version',
'version',
'type',
'path',
'clipath',
'phpini',
'custom',
'handler-status',
]);
}
}
================================================
FILE: src/Api/Struct/ProtectedDirectory/DataInfo.php
================================================
initScalarProperties($apiResponse, [
'name',
'header',
]);
}
}
================================================
FILE: src/Api/Struct/ProtectedDirectory/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
]);
}
}
================================================
FILE: src/Api/Struct/ProtectedDirectory/UserInfo.php
================================================
initScalarProperties($apiResponse, [
'id',
]);
}
}
================================================
FILE: src/Api/Struct/Reseller/GeneralInfo.php
================================================
{'gen-info'})) {
$this->initScalarProperties($apiResponse->{'gen-info'}, [
['pname' => 'personalName'],
'login',
]);
}
$this->permissions = [];
foreach ($apiResponse->permissions->permission ?? [] as $permissionInfo) {
$this->permissions[(string) $permissionInfo->name] = (string) $permissionInfo->value;
}
}
}
================================================
FILE: src/Api/Struct/Reseller/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'guid',
]);
}
}
================================================
FILE: src/Api/Struct/SecretKey/Info.php
================================================
initScalarProperties($apiResponse, [
'key',
'ip_address',
'description',
'login',
]);
}
}
================================================
FILE: src/Api/Struct/Server/Admin.php
================================================
initScalarProperties($apiResponse, [
['admin_cname' => 'companyName'],
['admin_pname' => 'name'],
['admin_email' => 'email'],
]);
}
}
================================================
FILE: src/Api/Struct/Server/GeneralInfo.php
================================================
initScalarProperties($apiResponse, [
'server_name',
'server_guid',
'mode',
]);
}
}
================================================
FILE: src/Api/Struct/Server/Preferences.php
================================================
initScalarProperties($apiResponse, [
'stat_ttl',
'traffic_accounting',
'restart_apache_interval',
]);
}
}
================================================
FILE: src/Api/Struct/Server/SessionPreferences.php
================================================
initScalarProperties($apiResponse, [
'login_timeout',
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/DiskSpace.php
================================================
initScalarProperties($apiResponse, [
'total',
'used',
'free',
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/LoadAverage.php
================================================
load1min = (float) $apiResponse->l1 / 100.0;
$this->load5min = (float) $apiResponse->l5 / 100.0;
$this->load15min = (float) $apiResponse->l15 / 100.0;
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/Memory.php
================================================
initScalarProperties($apiResponse, [
'total',
'used',
'free',
'shared',
'buffer',
'cached',
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/Objects.php
================================================
initScalarProperties($apiResponse, [
'clients',
'domains',
'databases',
['active_domains' => 'activeDomains'],
['mail_boxes' => 'mailBoxes'],
['mail_redirects' => 'mailRedirects'],
['mail_groups' => 'mailGroups'],
['mail_responders' => 'mailResponders'],
['database_users' => 'databaseUsers'],
['problem_clients' => 'problemClients'],
['problem_domains' => 'problemDomains'],
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/Other.php
================================================
initScalarProperties($apiResponse, [
'cpu',
'uptime',
['inside_vz' => 'insideVz'],
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/Swap.php
================================================
initScalarProperties($apiResponse, [
'total',
'used',
'free',
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics/Version.php
================================================
initScalarProperties($apiResponse, [
['plesk_name' => 'internalName'],
['plesk_version' => 'version'],
['plesk_build' => 'build'],
['plesk_os' => 'osName'],
['plesk_os_version' => 'osVersion'],
['os_release' => 'osRelease'],
]);
}
}
================================================
FILE: src/Api/Struct/Server/Statistics.php
================================================
objects = new Statistics\Objects($apiResponse->objects);
$this->version = new Statistics\Version($apiResponse->version);
$this->other = new Statistics\Other($apiResponse->other);
$this->loadAverage = new Statistics\LoadAverage($apiResponse->load_avg);
$this->memory = new Statistics\Memory($apiResponse->mem);
$this->swap = new Statistics\Swap($apiResponse->swap);
$this->diskSpace = [];
foreach ($apiResponse->diskspace ?? [] as $disk) {
$this->diskSpace[(string) $disk->device->name] = new Statistics\DiskSpace($disk->device);
}
}
}
================================================
FILE: src/Api/Struct/Server/UpdatesInfo.php
================================================
initScalarProperties($apiResponse, [
'last_installed_update',
'install_updates_automatically',
]);
}
}
================================================
FILE: src/Api/Struct/ServicePlan/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'name',
'guid',
'external-id',
]);
}
}
================================================
FILE: src/Api/Struct/ServicePlanAddon/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'name',
'guid',
'external-id',
]);
}
}
================================================
FILE: src/Api/Struct/Session/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'type',
'ip-address',
'login',
'login-time',
'idle',
]);
}
}
================================================
FILE: src/Api/Struct/Site/GeneralInfo.php
================================================
initScalarProperties($apiResponse, [
['cr_date' => 'creationDate'],
'name',
'ascii-name',
'status',
'real_size',
'guid',
'description',
'webspace-guid',
'webspace-id',
]);
foreach ($apiResponse->dns_ip_address ?? [] as $ip) {
$this->ipAddresses[] = (string) $ip;
}
}
}
================================================
FILE: src/Api/Struct/Site/HostingInfo.php
================================================
vrt_hst->property ?? [] as $property) {
$this->properties[(string) $property->name] = (string) $property->value;
}
if (!is_null($apiResponse->vrt_hst)) {
$this->initScalarProperties($apiResponse->vrt_hst, [
'ip_address',
]);
}
}
}
================================================
FILE: src/Api/Struct/Site/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'guid',
]);
}
}
================================================
FILE: src/Api/Struct/SiteAlias/GeneralInfo.php
================================================
initScalarProperties($apiResponse, [
'name',
'ascii-name',
'status',
]);
}
}
================================================
FILE: src/Api/Struct/SiteAlias/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'status',
]);
}
}
================================================
FILE: src/Api/Struct/Subdomain/Info.php
================================================
properties = [];
$this->initScalarProperties($apiResponse, [
'id',
'parent',
'name',
]);
foreach ($apiResponse->property ?? [] as $propertyInfo) {
$this->properties[(string) $propertyInfo->name] = (string) $propertyInfo->value;
}
}
}
================================================
FILE: src/Api/Struct/Ui/CustomButton.php
================================================
initScalarProperties($apiResponse, ['id']);
if (!is_null($apiResponse->properties)) {
$this->initScalarProperties($apiResponse->properties, [
'sort_key',
'public',
'internal',
['noframe' => 'noFrame'],
'place',
'url',
'text',
]);
}
}
}
================================================
FILE: src/Api/Struct/Webspace/DiskUsage.php
================================================
initScalarProperties($apiResponse, [
'httpdocs',
'httpsdocs',
'subdomains',
'anonftp',
'logs',
'dbases',
'mailboxes',
'maillists',
'domaindumps',
'configs',
'chroot',
]);
}
}
================================================
FILE: src/Api/Struct/Webspace/GeneralInfo.php
================================================
initScalarProperties($apiResponse, [
['cr_date' => 'creationDate'],
'name',
'ascii-name',
'status',
'real_size',
'owner-id',
'guid',
'vendor-guid',
'description',
'admin-description',
]);
foreach ($apiResponse->dns_ip_address ?? [] as $ip) {
$this->ipAddresses[] = (string) $ip;
}
$this->enabled = '0' === (string) $apiResponse->status;
}
}
================================================
FILE: src/Api/Struct/Webspace/HostingPropertyInfo.php
================================================
initScalarProperties($apiResponse, [
'name',
'type',
'label',
]);
}
}
================================================
FILE: src/Api/Struct/Webspace/Info.php
================================================
initScalarProperties($apiResponse, [
'id',
'guid',
]);
$this->name = $name;
}
}
================================================
FILE: src/Api/Struct/Webspace/Limit.php
================================================
initScalarProperties($apiResponse, [
'name',
'value',
]);
}
}
================================================
FILE: src/Api/Struct/Webspace/LimitDescriptor.php
================================================
limits = [];
foreach ($apiResponse->descriptor->property ?? [] as $propertyInfo) {
$this->limits[(string) $propertyInfo->name] = new LimitInfo($propertyInfo);
}
}
}
================================================
FILE: src/Api/Struct/Webspace/LimitInfo.php
================================================
initScalarProperties($apiResponse, [
'name',
'type',
'label',
]);
}
}
================================================
FILE: src/Api/Struct/Webspace/Limits.php
================================================
initScalarProperties($apiResponse, ['overuse']);
$this->limits = [];
foreach ($apiResponse->limit ?? [] as $limit) {
$this->limits[(string) $limit->name] = new Limit($limit);
}
}
}
================================================
FILE: src/Api/Struct/Webspace/PermissionDescriptor.php
================================================
permissions = [];
foreach ($apiResponse->descriptor->property ?? [] as $propertyInfo) {
$this->permissions[(string) $propertyInfo->name] = new PermissionInfo($propertyInfo);
}
}
}
================================================
FILE: src/Api/Struct/Webspace/PermissionInfo.php
================================================
initScalarProperties($apiResponse, [
'name',
'type',
'label',
]);
}
}
================================================
FILE: src/Api/Struct/Webspace/PhpSettings.php
================================================
properties = [];
foreach ($apiResponse->webspace->get->result->data->{'php-settings'}->setting ?? [] as $setting) {
$this->properties[(string) $setting->name] = (string) $setting->value;
}
}
}
================================================
FILE: src/Api/Struct/Webspace/PhysicalHostingDescriptor.php
================================================
properties = [];
foreach ($apiResponse->descriptor->property ?? [] as $propertyInfo) {
$this->properties[(string) $propertyInfo->name] = new HostingPropertyInfo($propertyInfo);
}
}
}
================================================
FILE: src/Api/XmlResponse.php
================================================
xpath('//' . $node);
if (is_array($result) && isset($result[0])) {
return (string) $result[0];
}
return '';
}
}
================================================
FILE: tests/AbstractTestCase.php
================================================
setCredentials($login, $password);
$proxy = getenv('REMOTE_PROXY');
if ($proxy) {
static::$client->setProxy($proxy);
}
}
public static function tearDownAfterClass(): void
{
foreach (self::$webspaces as $webspace) {
try {
static::$client->webspace()->delete('id', $webspace->id);
// phpcs:ignore
} catch (\Exception $e) {
}
}
foreach (self::$servicePlans as $servicePlan) {
try {
static::$client->servicePlan()->delete('id', $servicePlan->id);
// phpcs:ignore
} catch (\Exception $e) {
}
}
foreach (self::$servicePlanAddons as $servicePlanAddon) {
try {
static::$client->servicePlanAddon()->delete('id', $servicePlanAddon->id);
// phpcs:ignore
} catch (\Exception $e) {
}
}
}
protected static function getIpAddress(): string
{
$ips = static::$client->ip()->get();
$ipInfo = reset($ips);
return $ipInfo->ipAddress;
}
protected static function createWebspace(): \PleskX\Api\Struct\Webspace\Info
{
$id = uniqid();
$webspace = static::$client->webspace()->create(
[
'name' => "test{$id}.test",
'ip_address' => static::getIpAddress(),
],
[
'ftp_login' => "u{$id}",
'ftp_password' => PasswordProvider::STRONG_PASSWORD,
]
);
self::$webspaces[] = $webspace;
return $webspace;
}
protected static function createServicePlan(): \PleskX\Api\Struct\ServicePlan\Info
{
$id = uniqid();
$servicePlan = static::$client->servicePlan()->create(['name' => "test{$id}plan"]);
self::$servicePlans[] = $servicePlan;
return $servicePlan;
}
protected static function createServicePlanAddon(): \PleskX\Api\Struct\ServicePlanAddon\Info
{
$id = uniqid();
$servicePlanAddon = static::$client->servicePlanAddon()->create(['name' => "test{$id}planaddon"]);
self::$servicePlanAddons[] = $servicePlanAddon;
return $servicePlanAddon;
}
}
================================================
FILE: tests/ApiClientTest.php
================================================
expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1005);
$packet = static::$client->getPacket('100.0.0');
$packet->addChild('server')->addChild('get_protos');
static::$client->request($packet);
}
public function testUnknownOperator()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1014);
$packet = static::$client->getPacket();
$packet->addChild('unknown');
static::$client->request($packet);
}
public function testInvalidXmlRequest()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1014);
static::$client->request('');
}
public function testInvalidCredentials()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1001);
$host = static::$client->getHost();
$port = static::$client->getPort();
$protocol = static::$client->getProtocol();
$client = new \PleskX\Api\Client($host, $port, $protocol);
$client->setCredentials('bad-login', 'bad-password');
$packet = static::$client->getPacket();
$packet->addChild('server')->addChild('get_protos');
$client->request($packet);
}
public function testInvalidSecretKey()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(11003);
$host = static::$client->getHost();
$port = static::$client->getPort();
$protocol = static::$client->getProtocol();
$client = new \PleskX\Api\Client($host, $port, $protocol);
$client->setSecretKey('bad-key');
$packet = static::$client->getPacket();
$packet->addChild('server')->addChild('get_protos');
$client->request($packet);
}
public function testLatestMajorProtocol()
{
$packet = static::$client->getPacket('1.6');
$packet->addChild('server')->addChild('get_protos');
$result = static::$client->request($packet);
$this->assertEquals('ok', $result->status);
}
public function testLatestMinorProtocol()
{
$packet = static::$client->getPacket('1.6.5');
$packet->addChild('server')->addChild('get_protos');
$result = static::$client->request($packet);
$this->assertEquals('ok', $result->status);
}
public function testRequestShortSyntax()
{
$response = static::$client->request('server.get.gen_info');
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
}
public function testOperatorPlainRequest()
{
$response = static::$client->server()->request('get.gen_info');
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
$this->assertEquals(36, strlen($response->getValue('server_guid')));
}
public function testRequestArraySyntax()
{
$response = static::$client->request([
'server' => [
'get' => [
'gen_info' => '',
],
],
]);
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
}
public function testOperatorArraySyntax()
{
$response = static::$client->server()->request(['get' => ['gen_info' => '']]);
$this->assertGreaterThan(0, strlen($response->gen_info->server_name));
}
public function testMultiRequest()
{
$responses = static::$client->multiRequest([
'server.get_protos',
'server.get.gen_info',
]);
$this->assertCount(2, $responses);
$protos = (array) $responses[0]->protos->proto;
$generalInfo = $responses[1];
$this->assertContains('1.6.6.0', $protos);
$this->assertGreaterThan(0, strlen($generalInfo->gen_info->server_name));
}
public function testConnectionError()
{
$this->expectException(\PleskX\Api\Client\Exception::class);
$client = new \PleskX\Api\Client('invalid-host.dom');
$client->server()->getProtos();
}
public function testGetHost()
{
$client = new \PleskX\Api\Client('example.dom');
$this->assertEquals('example.dom', $client->getHost());
}
public function testGetPort()
{
$client = new \PleskX\Api\Client('example.dom', 12345);
$this->assertEquals(12345, $client->getPort());
}
public function testGetProtocol()
{
$client = new \PleskX\Api\Client('example.dom', 8880, 'http');
$this->assertEquals('http', $client->getProtocol());
}
public function testSetVerifyResponse()
{
static::$client->setVerifyResponse(function ($xml) {
if ($xml->xpath('//proto')) {
throw new Exception('proto');
}
});
try {
static::$client->server()->getProtos();
} catch (Exception $e) {
$this->assertEquals('proto', $e->getMessage());
} finally {
static::$client->setVerifyResponse();
}
}
}
================================================
FILE: tests/CertificateTest.php
================================================
2048,
'country' => 'CH',
'state' => 'Schaffhausen',
'location' => 'Schaffhausen',
'company' => 'Plesk',
'email' => 'info@plesk.com',
'name' => 'plesk.com',
];
private array $distinguishedNames = [
"countryName" => "CH",
"stateOrProvinceName" => "Schaffhausen",
"localityName" => "Schaffhausen",
"organizationName" => "Plesk",
"emailAddress" => "info@plesk.com"
];
public function testGenerate()
{
$certificate = static::$client->certificate()->generate($this->certificateProperties);
$this->assertGreaterThan(0, strlen($certificate->request));
$this->assertStringStartsWith('-----BEGIN CERTIFICATE REQUEST-----', $certificate->request);
$this->assertGreaterThan(0, strlen($certificate->privateKey));
$this->assertStringStartsWith('-----BEGIN PRIVATE KEY-----', $certificate->privateKey);
}
public function testInstall()
{
$certificate = static::$client->certificate()->generate($this->certificateProperties);
$result = static::$client->certificate()->install([
'name' => 'test',
'admin' => true,
], $certificate->request, $certificate->privateKey);
$this->assertTrue($result);
static::$client->certificate()->delete('test', ['admin' => true]);
}
public function testUpdate()
{
$payLoad = [
'name' => 'test',
'admin' => true,
];
$certificate = static::$client->certificate()->generate($this->certificateProperties);
static::$client->certificate()->install($payLoad, $certificate);
$certificate = $this->generateCertificateOpenSsl($this->distinguishedNames);
$result = static::$client->certificate()->update($payLoad, $certificate);
$this->assertTrue($result);
static::$client->certificate()->delete('test', ['admin' => true]);
}
public function testDelete()
{
$certificate = static::$client->certificate()->generate($this->certificateProperties);
static::$client->certificate()->install([
'name' => 'test',
'admin' => true,
], $certificate);
$result = static::$client->certificate()->delete('test', ['admin' => true]);
$this->assertTrue($result);
}
private function generateCertificateOpenSsl(array $dn): Struct\Info
{
$privkey = openssl_pkey_new([
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
]);
$csr = openssl_csr_new($dn, $privkey, ['digest_alg' => 'sha256']);
$x509 = openssl_csr_sign($csr, null, $privkey, $days = 365, ['digest_alg' => 'sha256']);
openssl_csr_export($csr, $csrout);
openssl_x509_export($x509, $certout);
openssl_pkey_export($privkey, $pkeyout);
return new Struct\Info([
'publicKey' => $certout,
'request' => $csrout,
'privateKey' => $pkeyout
]);
}
}
================================================
FILE: tests/CustomerTest.php
================================================
customerProperties = [
'cname' => 'Plesk',
'pname' => 'John Smith',
'login' => 'john-unit-test',
'passwd' => PasswordProvider::STRONG_PASSWORD,
'email' => 'john@smith.com',
'external-id' => 'link:12345',
'description' => 'Good guy',
];
}
public function testCreate()
{
$customer = static::$client->customer()->create($this->customerProperties);
$this->assertIsInt($customer->id);
$this->assertGreaterThan(0, $customer->id);
static::$client->customer()->delete('id', $customer->id);
}
public function testDelete()
{
$customer = static::$client->customer()->create($this->customerProperties);
$result = static::$client->customer()->delete('id', $customer->id);
$this->assertTrue($result);
}
public function testGet()
{
$customer = static::$client->customer()->create($this->customerProperties);
$customerInfo = static::$client->customer()->get('id', $customer->id);
$this->assertEquals('Plesk', $customerInfo->company);
$this->assertEquals('John Smith', $customerInfo->personalName);
$this->assertEquals('john-unit-test', $customerInfo->login);
$this->assertEquals('john@smith.com', $customerInfo->email);
$this->assertEquals('Good guy', $customerInfo->description);
$this->assertEquals('link:12345', $customerInfo->externalId);
$this->assertEquals($customer->id, $customerInfo->id);
static::$client->customer()->delete('id', $customer->id);
}
public function testGetAll()
{
$keyInfo = static::$client->server()->getKeyInfo();
if (!KeyLimitChecker::checkByType($keyInfo, KeyLimitChecker::LIMIT_CLIENTS, 2)) {
$this->markTestSkipped('License does not allow to create more than 1 customer.');
}
static::$client->customer()->create([
'pname' => 'John Smith',
'login' => 'customer-a',
'passwd' => PasswordProvider::STRONG_PASSWORD,
]);
static::$client->customer()->create([
'pname' => 'Mike Black',
'login' => 'customer-b',
'passwd' => PasswordProvider::STRONG_PASSWORD,
]);
$customersInfo = static::$client->customer()->getAll();
$this->assertIsArray($customersInfo);
$customersCheck = array_filter($customersInfo, function ($value) {
return $value->personalName === 'John Smith' || $value->personalName === 'Mike Black';
});
$this->assertCount(2, $customersCheck);
static::$client->customer()->delete('login', 'customer-a');
static::$client->customer()->delete('login', 'customer-b');
}
public function testGetAllEmpty()
{
$customersInfo = static::$client->customer()->getAll();
$this->assertCount(0, $customersInfo);
}
public function testEnable()
{
$customer = static::$client->customer()->create($this->customerProperties);
static::$client->customer()->disable('id', $customer->id);
static::$client->customer()->enable('id', $customer->id);
$customerInfo = static::$client->customer()->get('id', $customer->id);
$this->assertTrue($customerInfo->enabled);
static::$client->customer()->delete('id', $customer->id);
}
public function testDisable()
{
$customer = static::$client->customer()->create($this->customerProperties);
static::$client->customer()->disable('id', $customer->id);
$customerInfo = static::$client->customer()->get('id', $customer->id);
$this->assertFalse($customerInfo->enabled);
static::$client->customer()->delete('id', $customer->id);
}
public function testSetProperties()
{
$customer = static::$client->customer()->create($this->customerProperties);
static::$client->customer()->setProperties('id', $customer->id, [
'pname' => 'Mike Black',
'email' => 'mike@black.com',
]);
$customerInfo = static::$client->customer()->get('id', $customer->id);
$this->assertEquals('Mike Black', $customerInfo->personalName);
$this->assertEquals('mike@black.com', $customerInfo->email);
static::$client->customer()->delete('id', $customer->id);
}
}
================================================
FILE: tests/DatabaseServerTest.php
================================================
databaseServer()->getSupportedTypes();
$this->assertGreaterThan(0, count($types));
$this->assertContains('mysql', $types);
}
public function testGet()
{
$dbServer = static::$client->databaseServer()->get('id', 1);
$this->assertEquals('localhost', $dbServer->host);
$this->assertGreaterThan(0, $dbServer->port);
}
public function testGetAll()
{
$dbServers = static::$client->databaseServer()->getAll();
$this->assertIsArray($dbServers);
$this->assertGreaterThan(0, count($dbServers));
$this->assertEquals('localhost', $dbServers[0]->host);
}
public function testGetDefault()
{
$dbServer = static::$client->databaseServer()->getDefault('mysql');
$this->assertEquals('mysql', $dbServer->type);
$this->assertGreaterThan(0, $dbServer->id);
}
}
================================================
FILE: tests/DatabaseTest.php
================================================
databaseServer()->getDefault('mysql');
}
public function testCreate()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
static::$client->database()->delete('id', $database->id);
}
public function testCreateUser()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
'login' => 'test_user1',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
static::$client->database()->deleteUser('id', $user->id);
static::$client->database()->delete('id', $database->id);
}
public function testUpdateUser()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
'login' => 'test_user1',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$updatedUser = static::$client->database()->updateUser([
'id' => $user->id,
'login' => 'test_user2',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$this->assertEquals(true, $updatedUser);
static::$client->database()->deleteUser('id', $user->id);
static::$client->database()->delete('id', $database->id);
}
public function testGetById()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$db = static::$client->database()->get('id', $database->id);
$this->assertEquals('test1', $db->name);
$this->assertEquals(static::$databaseServer->type, $db->type);
$this->assertEquals(static::$webspace->id, $db->webspaceId);
$this->assertEquals(static::$databaseServer->id, $db->dbServerId);
static::$client->database()->delete('id', $database->id);
}
public function testGetAllByWebspaceId()
{
$db1 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$db2 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test2',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$databases = static::$client->database()->getAll('webspace-id', static::$webspace->id);
$this->assertEquals('test1', $databases[0]->name);
$this->assertEquals('test2', $databases[1]->name);
$this->assertEquals(static::$webspace->id, $databases[0]->webspaceId);
$this->assertEquals(static::$databaseServer->id, $databases[1]->dbServerId);
static::$client->database()->delete('id', $db1->id);
static::$client->database()->delete('id', $db2->id);
}
public function testGetUserById()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
'login' => 'test_user1',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$dbUser = static::$client->database()->getUser('id', $user->id);
$this->assertEquals('test_user1', $dbUser->login);
$this->assertEquals($database->id, $dbUser->dbId);
static::$client->database()->deleteUser('id', $user->id);
static::$client->database()->delete('id', $database->id);
}
public function testGetAllUsersByDbId()
{
$db1 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$db2 = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test2',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user1 = $this->createUser([
'db-id' => $db1->id,
'login' => 'test_user1',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$user2 = $this->createUser([
'db-id' => $db1->id,
'login' => 'test_user2',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$user3 = $this->createUser([
'db-id' => $db2->id,
'login' => 'test_user3',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$dbUsers = static::$client->database()->getAllUsers('db-id', $db1->id);
$this->assertEquals(2, count($dbUsers));
$this->assertEquals('test_user1', $dbUsers[0]->login);
$this->assertEquals('test_user2', $dbUsers[1]->login);
static::$client->database()->deleteUser('id', $user1->id);
static::$client->database()->deleteUser('id', $user2->id);
static::$client->database()->deleteUser('id', $user3->id);
static::$client->database()->delete('id', $db1->id);
static::$client->database()->delete('id', $db2->id);
}
public function testDelete()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$result = static::$client->database()->delete('id', $database->id);
$this->assertTrue($result);
}
public function testDeleteUser()
{
$database = $this->createDatabase([
'webspace-id' => static::$webspace->id,
'name' => 'test1',
'type' => static::$databaseServer->type,
'db-server-id' => static::$databaseServer->id,
]);
$user = $this->createUser([
'db-id' => $database->id,
'login' => 'test_user1',
'password' => PasswordProvider::STRONG_PASSWORD,
]);
$result = static::$client->database()->deleteUser('id', $user->id);
$this->assertTrue($result);
static::$client->database()->delete('id', $database->id);
}
private function createDatabase(array $params): \PleskX\Api\Struct\Database\Info
{
$database = static::$client->database()->create($params);
$this->assertIsInt($database->id);
$this->assertGreaterThan(0, $database->id);
return $database;
}
private function createUser(array $params): \PleskX\Api\Struct\Database\UserInfo
{
$user = static::$client->database()->createUser($params);
$this->assertIsInt($user->id);
$this->assertGreaterThan(0, $user->id);
return $user;
}
public function testGetAllForWebspaceWithNoDatabases()
{
$databases = static::$client->database()->getAll('webspace-id', static::$webspace->id);
$this->assertIsArray($databases);
$this->assertEmpty($databases);
}
}
================================================
FILE: tests/DnsTemplateTest.php
================================================
server()->getServiceStates();
static::$isDnsSupported = $serviceStates['dns'] && ('running' == $serviceStates['dns']['state']);
}
protected function setUp(): void
{
parent::setUp();
if (!static::$isDnsSupported) {
$this->markTestSkipped('DNS system is not supported.');
}
}
public function testCreate()
{
$dns = static::$client->dnsTemplate()->create([
'type' => 'TXT',
'host' => 'test.create',
'value' => 'value',
]);
$this->assertIsInt($dns->id);
$this->assertGreaterThan(0, $dns->id);
$this->assertEquals(0, $dns->siteId);
$this->assertEquals(0, $dns->siteAliasId);
static::$client->dnsTemplate()->delete('id', $dns->id);
}
public function testGetById()
{
$dns = static::$client->dnsTemplate()->create([
'type' => 'TXT',
'host' => 'test.get.by.id',
'value' => 'value',
]);
$dnsInfo = static::$client->dnsTemplate()->get('id', $dns->id);
$this->assertEquals('TXT', $dnsInfo->type);
$this->assertEquals('value', $dnsInfo->value);
static::$client->dnsTemplate()->delete('id', $dns->id);
}
public function testGetAll()
{
$dns = static::$client->dnsTemplate()->create([
'type' => 'TXT',
'host' => 'test.get.all',
'value' => 'value',
]);
$dns2 = static::$client->dnsTemplate()->create([
'type' => 'TXT',
'host' => 'test.get.all',
'value' => 'value2',
]);
$dnsInfo = static::$client->dnsTemplate()->getAll();
$dsRecords = [];
foreach ($dnsInfo as $dnsRec) {
if ('TXT' === $dnsRec->type && 0 === strpos($dnsRec->host, 'test.get.all')) {
$dsRecords[] = $dnsRec;
}
}
$this->assertCount(2, $dsRecords);
static::$client->dnsTemplate()->delete('id', $dns->id);
static::$client->dnsTemplate()->delete('id', $dns2->id);
}
public function testDelete()
{
$dns = static::$client->dnsTemplate()->create([
'type' => 'TXT',
'host' => 'test.delete',
'value' => 'value',
]);
$result = static::$client->dnsTemplate()->delete('id', $dns->id);
$this->assertTrue($result);
}
}
================================================
FILE: tests/DnsTest.php
================================================
server()->getServiceStates();
static::$isDnsSupported = isset($serviceStates['dns']) && ('running' == $serviceStates['dns']['state']);
if (static::$isDnsSupported) {
static::$webspace = static::createWebspace();
}
}
protected function setUp(): void
{
parent::setUp();
if (!static::$isDnsSupported) {
$this->markTestSkipped('DNS system is not supported.');
}
}
public function testCreate()
{
$dns = static::$client->dns()->create([
'site-id' => static::$webspace->id,
'type' => 'TXT',
'host' => 'host',
'value' => 'value',
]);
$this->assertIsInt($dns->id);
$this->assertGreaterThan(0, $dns->id);
static::$client->dns()->delete('id', $dns->id);
}
/**
* @return \SimpleXMLElement[]
*/
public function testBulkCreate(): array
{
$response = static::$client->dns()->bulkCreate([
[
'site-id' => static::$webspace->id,
'type' => 'TXT',
'host' => 'host',
'value' => 'value',
],
[
'site-id' => static::$webspace->id,
'type' => 'A',
'host' => 'host',
'value' => '1.1.1.1',
],
[
'site-id' => static::$webspace->id,
'type' => 'MX',
'host' => 'custom-mail',
'value' => '1.1.1.1',
'opt' => '10',
],
]);
$this->assertCount(3, $response);
foreach ($response as $xml) {
$this->assertEquals('ok', (string) $xml->status);
$this->assertGreaterThan(0, (int) $xml->id);
}
return $response;
}
/**
* @depends testBulkCreate
*
* @param \SimpleXMLElement[] $createdRecords
*/
public function testBulkDelete(array $createdRecords)
{
$createdRecordIds = array_map(function ($record) {
return (int) $record->id;
}, $createdRecords);
$response = static::$client->dns()->bulkDelete($createdRecordIds);
$this->assertCount(3, $response);
foreach ($response as $xml) {
$this->assertEquals('ok', (string) $xml->status);
$this->assertGreaterThan(0, (int) $xml->id);
}
}
public function testGetById()
{
$dns = static::$client->dns()->create([
'site-id' => static::$webspace->id,
'type' => 'TXT',
'host' => '',
'value' => 'value',
]);
$dnsInfo = static::$client->dns()->get('id', $dns->id);
$this->assertEquals('TXT', $dnsInfo->type);
$this->assertEquals(static::$webspace->id, $dnsInfo->siteId);
$this->assertEquals('value', $dnsInfo->value);
static::$client->dns()->delete('id', $dns->id);
}
public function testGetAllByWebspaceId()
{
$dns = static::$client->dns()->create([
'site-id' => static::$webspace->id,
'type' => 'DS',
'host' => 'host',
'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292118',
]);
$dns2 = static::$client->dns()->create([
'site-id' => static::$webspace->id,
'type' => 'DS',
'host' => 'host',
'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292119',
]);
$dnsInfo = static::$client->dns()->getAll('site-id', static::$webspace->id);
$dsRecords = [];
foreach ($dnsInfo as $dnsRec) {
if ('DS' == $dnsRec->type) {
$dsRecords[] = $dnsRec;
}
}
$this->assertEquals(2, count($dsRecords));
foreach ($dsRecords as $dsRecord) {
$this->assertEquals(static::$webspace->id, $dsRecord->siteId);
}
static::$client->dns()->delete('id', $dns->id);
static::$client->dns()->delete('id', $dns2->id);
}
public function testDelete()
{
$dns = static::$client->dns()->create([
'site-id' => static::$webspace->id,
'type' => 'TXT',
'host' => 'host',
'value' => 'value',
]);
$result = static::$client->dns()->delete('id', $dns->id);
$this->assertTrue($result);
}
}
================================================
FILE: tests/EventLogTest.php
================================================
eventLog()->get();
$this->assertGreaterThan(0, $events);
$event = reset($events);
$this->assertGreaterThan(0, $event->time);
}
public function testGetDetailedLog()
{
$events = static::$client->eventLog()->getDetailedLog();
$this->assertGreaterThan(0, $events);
$event = reset($events);
$this->assertGreaterThan(0, $event->time);
}
public function testGetLastId()
{
$lastId = static::$client->eventLog()->getLastId();
$this->assertGreaterThan(0, $lastId);
}
}
================================================
FILE: tests/IpTest.php
================================================
ip()->get();
$this->assertGreaterThan(0, count($ips));
$ip = reset($ips);
$this->assertMatchesRegularExpression('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip->ipAddress);
}
}
================================================
FILE: tests/LocaleTest.php
================================================
locale()->get();
$this->assertGreaterThan(0, count($locales));
$locale = $locales['en-US'];
$this->assertEquals('en-US', $locale->id);
}
public function testGetById()
{
$locale = static::$client->locale()->get('en-US');
$this->assertEquals('en-US', $locale->id);
}
}
================================================
FILE: tests/MailTest.php
================================================
server()->getServiceStates();
static::$isMailSupported = isset($serviceStates['smtp']) && ('running' == $serviceStates['smtp']['state']);
if (static::$isMailSupported) {
static::$webspace = static::createWebspace();
}
}
protected function setUp(): void
{
parent::setUp();
if (!static::$isMailSupported) {
$this->markTestSkipped('Mail system is not supported.');
}
}
public function testCreate()
{
$mailname = static::$client->mail()->create(
'test',
static::$webspace->id,
true,
PasswordProvider::STRONG_PASSWORD
);
$this->assertIsInt($mailname->id);
$this->assertGreaterThan(0, $mailname->id);
$this->assertEquals('test', $mailname->name);
static::$client->mail()->delete('name', $mailname->name, static::$webspace->id);
}
public function testCreateMultiForwarding()
{
$mailname = static::$client->request([
'mail' => [
'create' => [
'filter' => [
'site-id' => static::$webspace->id,
'mailname' => [
'name' => 'test',
'mailbox' => [
'enabled' => true,
],
'forwarding' => [
'enabled' => true,
'address' => [
'user1@example.com',
'user2@example.com',
],
],
'alias' => [
'test1',
'test2',
],
'password' => [
'value' => PasswordProvider::STRONG_PASSWORD,
],
],
],
],
],
]);
$mailnameInfo = static::$client->request([
'mail' => [
'get_info' => [
'filter' => [
'site-id' => static::$webspace->id,
'name' => 'test',
],
'forwarding' => null,
'aliases' => null,
],
],
]);
$this->assertSame(2, count($mailnameInfo->mailname->forwarding->address));
$this->assertSame(2, count($mailnameInfo->mailname->alias));
static::$client->mail()->delete('name', 'test', static::$webspace->id);
}
public function testDelete()
{
$mailname = static::$client->mail()->create('test', static::$webspace->id);
$result = static::$client->mail()->delete('name', $mailname->name, static::$webspace->id);
$this->assertTrue($result);
}
public function testGet()
{
$mailname = static::$client->mail()->create('test', static::$webspace->id);
$mailnameInfo = static::$client->mail()->get('test', static::$webspace->id);
$this->assertEquals('test', $mailnameInfo->name);
$this->assertEquals($mailname->id, $mailnameInfo->id);
static::$client->mail()->delete('name', $mailname->name, static::$webspace->id);
}
public function testGetAll()
{
$mailname = static::$client->mail()->create('test', static::$webspace->id);
$mailnamesInfo = static::$client->mail()->getAll(static::$webspace->id);
$this->assertCount(1, $mailnamesInfo);
$this->assertEquals('test', $mailnamesInfo[0]->name);
static::$client->mail()->delete('name', $mailname->name, static::$webspace->id);
}
public function testGetAllWithoutMailnames()
{
$mailnamesInfo = static::$client->mail()->getAll(static::$webspace->id);
$this->assertCount(0, $mailnamesInfo);
}
}
================================================
FILE: tests/PhpHandlerTest.php
================================================
phpHandler()->get();
$this->assertTrue(property_exists($handler, 'type'));
}
public function testGetAll()
{
$handlers = static::$client->phpHandler()->getAll();
$this->assertIsArray($handlers);
$this->assertNotEmpty($handlers);
$handler = current($handlers);
$this->assertIsObject($handler);
$this->assertTrue(property_exists($handler, 'type'));
}
public function testGetUnknownHandlerThrowsException()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionMessage('Php handler does not exists');
static::$client->phpHandler()->get('id', 'this-handler-does-not-exist');
}
}
================================================
FILE: tests/ProtectedDirectoryTest.php
================================================
protectedDirectory()->add('/', static::$webspace->id);
$this->assertIsObject($protectedDirectory);
$this->assertGreaterThan(0, $protectedDirectory->id);
static::$client->protectedDirectory()->delete('id', $protectedDirectory->id);
}
public function testAddInvalidDirectory()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1019);
static::$client->protectedDirectory()->add('', static::$webspace->id);
}
public function testDelete()
{
$protectedDirectory = static::$client->protectedDirectory()->add('/', static::$webspace->id);
$result = static::$client->protectedDirectory()->delete('id', $protectedDirectory->id);
$this->assertTrue($result);
}
public function testGetById()
{
$protectedDirectory = static::$client->protectedDirectory()->add('test', static::$webspace->id);
$foundDirectory = static::$client->protectedDirectory()->get('id', $protectedDirectory->id);
$this->assertEquals('test', $foundDirectory->name);
static::$client->protectedDirectory()->delete('id', $protectedDirectory->id);
}
public function testGetUnknownDirectory()
{
$this->expectException(\PleskX\Api\Exception::class);
$this->expectExceptionCode(1013);
$nonExistentDirectoryId = 99999999;
static::$client->protectedDirectory()->get('id', $nonExistentDirectoryId);
}
public function testAddUser()
{
$protectedDirectory = static::$client->protectedDirectory()->add('/', static::$webspace->id);
$user = static::$client->protectedDirectory()->addUser(
$protectedDirectory,
'john',
PasswordProvider::STRONG_PASSWORD
);
$this->assertGreaterThan(0, $user->id);
static::$client->protectedDirectory()->delete('id', $protectedDirectory->id);
}
public function testDeleteUser()
{
$protectedDirectory = static::$client->protectedDirectory()->add('/', static::$webspace->id);
$user = static::$client->protectedDirectory()->addUser(
$protectedDirectory,
'john',
PasswordProvider::STRONG_PASSWORD
);
$result = static::$client->protectedDirectory()->deleteUser('id', $user->id);
$this->assertTrue($result);
static::$client->protectedDirectory()->delete('id', $protectedDirectory->id);
}
}
================================================
FILE: tests/ResellerTest.php
================================================
resellerProperties = [
'pname' => 'John Reseller',
'login' => 'reseller-unit-test',
'passwd' => PasswordProvider::STRONG_PASSWORD,
];
}
public function testCreate()
{
$reseller = static::$client->reseller()->create($this->resellerProperties);
$this->assertIsInt($reseller->id);
$this->assertGreaterThan(0, $reseller->id);
static::$client->reseller()->delete('id', $reseller->id);
}
public function testDelete()
{
$reseller = static::$client->reseller()->create($this->resellerProperties);
$result = static::$client->reseller()->delete('id', $reseller->id);
$this->assertTrue($result);
}
public function testGet()
{
$reseller = static::$client->reseller()->create($this->resellerProperties);
$resellerInfo = static::$client->reseller()->get('id', $reseller->id);
$this->assertEquals('John Reseller', $resellerInfo->personalName);
$this->assertEquals('reseller-unit-test', $resellerInfo->login);
$this->assertGreaterThan(0, count($resellerInfo->permissions));
$this->assertEquals($reseller->id, $resellerInfo->id);
static::$client->reseller()->delete('id', $reseller->id);
}
public function testGetAll()
{
$keyInfo = static::$client->server()->getKeyInfo();
if (!KeyLimitChecker::checkByType($keyInfo, KeyLimitChecker::LIMIT_RESELLERS, 2)) {
$this->markTestSkipped('License does not allow to create more than 1 reseller.');
}
static::$client->reseller()->create([
'pname' => 'John Reseller',
'login' => 'reseller-a',
'passwd' => PasswordProvider::STRONG_PASSWORD,
]);
static::$client->reseller()->create([
'pname' => 'Mike Reseller',
'login' => 'reseller-b',
'passwd' => PasswordProvider::STRONG_PASSWORD,
]);
$resellersInfo = static::$client->reseller()->getAll();
$this->assertCount(2, $resellersInfo);
$this->assertEquals('John Reseller', $resellersInfo[0]->personalName);
$this->assertEquals('reseller-a', $resellersInfo[0]->login);
static::$client->reseller()->delete('login', 'reseller-a');
static::$client->reseller()->delete('login', 'reseller-b');
}
public function testGetAllEmpty()
{
$resellersInfo = static::$client->reseller()->getAll();
$this->assertCount(0, $resellersInfo);
}
}
================================================
FILE: tests/SecretKeyTest.php
================================================
secretKey()->create('192.168.0.1');
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/',
$keyId
);
static::$client->secretKey()->delete($keyId);
}
public function testCreateAutoIp()
{
$keyId = static::$client->secretKey()->create();
$this->assertNotEmpty($keyId);
static::$client->secretKey()->delete($keyId);
}
public function testCreateMultiIps()
{
$keyId = static::$client->secretKey()->create(join(',', ['192.168.0.1', '192.168.0.2']));
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/',
$keyId
);
static::$client->secretKey()->delete($keyId);
}
public function testCreateWithDescription()
{
$keyId = static::$client->secretKey()->create('192.168.0.1', 'test key');
$keyInfo = static::$client->secretKey()->get($keyId);
$this->assertEquals('test key', $keyInfo->description);
static::$client->secretKey()->delete($keyId);
}
public function testGet()
{
$keyId = static::$client->secretKey()->create('192.168.0.1');
$keyInfo = static::$client->secretKey()->get($keyId);
$this->assertNotEmpty($keyInfo->key);
$this->assertEquals('192.168.0.1', $keyInfo->ipAddress);
$this->assertEquals('admin', $keyInfo->login);
static::$client->secretKey()->delete($keyId);
}
public function testGetAll()
{
$keyIds = [];
$keyIds[] = static::$client->secretKey()->create('192.168.0.1');
$keyIds[] = static::$client->secretKey()->create('192.168.0.2');
$keys = static::$client->secretKey()->getAll();
$this->assertGreaterThanOrEqual(2, count($keys));
$keyIpAddresses = array_map(function ($key) {
return $key->ipAddress;
}, $keys);
$this->assertContains('192.168.0.1', $keyIpAddresses);
$this->assertContains('192.168.0.2', $keyIpAddresses);
foreach ($keyIds as $keyId) {
static::$client->secretKey()->delete($keyId);
}
}
public function testDelete()
{
$keyId = static::$client->secretKey()->create('192.168.0.1');
static::$client->secretKey()->delete($keyId);
try {
static::$client->secretKey()->get($keyId);
$this->fail("Secret key $keyId was not deleted.");
} catch (Exception $exception) {
$this->assertEquals(1013, $exception->getCode());
}
}
public function testListEmpty()
{
$keys = static::$client->secretKey()->getAll();
foreach ($keys as $key) {
static::$client->secretKey()->delete($key->key);
}
$keys = static::$client->secretKey()->getAll();
$this->assertEquals(0, count($keys));
}
}
================================================
FILE: tests/ServerTest.php
================================================
server()->getProtos();
$this->assertIsArray($protos);
$this->assertContains('1.6.3.0', $protos);
}
public function testGetGenInfo()
{
$generalInfo = static::$client->server()->getGeneralInfo();
$this->assertGreaterThan(0, strlen($generalInfo->serverName));
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/',
strtolower($generalInfo->serverGuid)
);
$this->assertEquals('standard', $generalInfo->mode);
}
public function testGetPreferences()
{
$preferences = static::$client->server()->getPreferences();
$this->assertIsNumeric($preferences->statTtl);
$this->assertGreaterThan(0, $preferences->statTtl);
$this->assertEquals(0, $preferences->restartApacheInterval);
}
public function testGetAdmin()
{
$admin = static::$client->server()->getAdmin();
$this->assertGreaterThan(0, strlen($admin->name));
$this->assertStringContainsString('@', $admin->email);
}
public function testGetKeyInfo()
{
$keyInfo = static::$client->server()->getKeyInfo();
$this->assertIsArray($keyInfo);
$this->assertGreaterThan(0, count($keyInfo));
$this->assertArrayHasKey('plesk_key_id', $keyInfo);
$this->assertArrayHasKey('lim_date', $keyInfo);
}
public function testGetComponents()
{
$components = static::$client->server()->getComponents();
$this->assertIsArray($components);
$this->assertGreaterThan(0, count($components));
$this->assertArrayHasKey('psa', $components);
}
public function testGetServiceStates()
{
$serviceStates = static::$client->server()->getServiceStates();
$this->assertIsArray($serviceStates);
$this->assertGreaterThan(0, count($serviceStates));
$service = current($serviceStates);
$this->assertIsArray($service);
$this->assertArrayHasKey('id', $service);
$this->assertArrayHasKey('title', $service);
$this->assertArrayHasKey('state', $service);
}
public function testGetSessionPreferences()
{
$preferences = static::$client->server()->getSessionPreferences();
$this->assertIsNumeric($preferences->loginTimeout);
$this->assertGreaterThan(0, $preferences->loginTimeout);
}
public function testGetShells()
{
$shells = static::$client->server()->getShells();
$this->assertIsArray($shells);
$this->assertGreaterThan(0, count($shells));
}
public function testGetNetworkInterfaces()
{
$netInterfaces = static::$client->server()->getNetworkInterfaces();
$this->assertIsArray($netInterfaces);
$this->assertGreaterThan(0, count($netInterfaces));
}
public function testGetStatistics()
{
$stats = static::$client->server()->getStatistics();
$this->assertIsNumeric($stats->objects->clients);
$this->assertIsNumeric($stats->objects->domains);
$this->assertIsNumeric($stats->objects->databases);
$this->assertEquals('psa', $stats->version->internalName);
$this->assertNotEmpty($stats->version->osName);
$this->assertNotEmpty($stats->version->osVersion);
$this->assertGreaterThan(0, $stats->other->uptime);
$this->assertGreaterThan(0, strlen($stats->other->cpu));
$this->assertIsFloat($stats->loadAverage->load1min);
$this->assertGreaterThan(0, $stats->memory->total);
$this->assertGreaterThan($stats->memory->free, $stats->memory->total);
$this->assertIsNumeric($stats->swap->total);
$this->assertIsArray($stats->diskSpace);
}
public function testGetSiteIsolationConfig()
{
$config = static::$client->server()->getSiteIsolationConfig();
$this->assertIsArray($config);
$this->assertGreaterThan(0, count($config));
$this->assertArrayHasKey('php', $config);
}
public function testGetUpdatesInfo()
{
$updatesInfo = static::$client->server()->getUpdatesInfo();
$this->assertIsBool($updatesInfo->installUpdatesAutomatically);
}
}
================================================
FILE: tests/ServicePlanAddonTest.php
================================================
servicePlanAddon()->get('id', $servicePlanAddon->id);
$this->assertNotEmpty($servicePlanAddonInfo->name);
$this->assertSame($servicePlanAddon->id, $servicePlanAddonInfo->id);
}
public function testGetAll()
{
static::createServicePlanAddon();
static::createServicePlanAddon();
static::createServicePlanAddon();
$servicePlanAddons = static::$client->servicePlanAddon()->getAll();
$this->assertIsArray($servicePlanAddons);
$this->assertGreaterThan(2, count($servicePlanAddons));
$this->assertNotEmpty($servicePlanAddons[0]->name);
}
public function testCreate()
{
$servicePlanAddon = static::createServicePlanAddon();
$this->assertGreaterThan(0, $servicePlanAddon->id);
static::$client->servicePlanAddon()->delete('id', $servicePlanAddon->id);
}
public function testDelete()
{
$servicePlanAddon = static::createServicePlanAddon();
$result = static::$client->servicePlanAddon()->delete('id', $servicePlanAddon->id);
$this->assertTrue($result);
}
public function testCreateComplex()
{
$properties = [
'name' => 'Complex Service Plan Addon',
'limits' => [
'limit' => [
'name' => 'disk_space',
'value' => 1024 * 1024 * 1024, // 1 GB
],
],
];
$servicePlanAddon = static::$client->servicePlanAddon()->create($properties);
$this->assertGreaterThan(0, $servicePlanAddon->id);
static::$client->servicePlanAddon()->delete('id', $servicePlanAddon->id);
}
}
================================================
FILE: tests/ServicePlanTest.php
================================================
servicePlan()->get('id', $servicePlan->id);
$this->assertNotEmpty($servicePlanInfo->name);
$this->assertSame($servicePlan->id, $servicePlanInfo->id);
static::$client->servicePlan()->delete('id', $servicePlan->id);
}
public function testGetAll()
{
static::createServicePlan();
static::createServicePlan();
static::createServicePlan();
$servicePlans = static::$client->servicePlan()->getAll();
$this->assertIsArray($servicePlans);
$this->assertGreaterThan(2, count($servicePlans));
$this->assertNotEmpty($servicePlans[0]->name);
}
public function testCreateServicePlan()
{
$servicePlan = static::createServicePlan();
$this->assertGreaterThan(0, $servicePlan->id);
static::$client->servicePlan()->delete('id', $servicePlan->id);
}
public function testDelete()
{
$servicePlan = static::createServicePlan();
$result = static::$client->servicePlan()->delete('id', $servicePlan->id);
$this->assertTrue($result);
}
public function testCreateComplexServicePlan()
{
$properties = [
'name' => 'Complex Service Plan',
'limits' => [
'overuse' => 'block',
'limit' => [
'name' => 'disk_space',
'value' => 1024 * 1024 * 1024, // 1 GB
],
],
'preferences' => [
'stat' => 6,
'maillists' => 'true',
],
'hosting' => [
'property' => [
[
'name' => 'ftp_quota',
'value' => '-1',
],
[
'name' => 'ssl',
'value' => 'true',
],
],
],
];
$servicePlan = static::$client->servicePlan()->create($properties);
$this->assertGreaterThan(0, $servicePlan->id);
static::$client->servicePlan()->delete('id', $servicePlan->id);
}
}
================================================
FILE: tests/SessionTest.php
================================================
session()->create('admin', '127.0.0.1');
$this->assertIsString($sessionToken);
$this->assertGreaterThan(10, strlen($sessionToken));
}
public function testGet()
{
$sessionId = static::$client->server()->createSession('admin', '127.0.0.1');
$sessions = static::$client->session()->get();
$this->assertArrayHasKey($sessionId, $sessions);
$sessionInfo = $sessions[$sessionId];
$this->assertEquals('admin', $sessionInfo->login);
$this->assertEquals('127.0.0.1', $sessionInfo->ipAddress);
$this->assertEquals($sessionId, $sessionInfo->id);
}
public function testTerminate()
{
$sessionId = static::$client->server()->createSession('admin', '127.0.0.1');
static::$client->session()->terminate($sessionId);
$sessions = static::$client->session()->get();
$this->assertArrayNotHasKey($sessionId, $sessions);
}
}
================================================
FILE: tests/SiteAliasTest.php
================================================
$name,
'site-id' => static::$webspace->id,
], $properties);
return static::$client->siteAlias()->create($properties);
}
public function testCreate()
{
$siteAlias = $this->createSiteAlias('alias.dom');
$this->assertIsNumeric($siteAlias->id);
$this->assertGreaterThan(0, $siteAlias->id);
static::$client->siteAlias()->delete('id', $siteAlias->id);
}
public function testDelete()
{
$siteAlias = $this->createSiteAlias('alias.dom');
$result = static::$client->siteAlias()->delete('id', $siteAlias->id);
$this->assertTrue($result);
}
public function testGet()
{
$siteAlias = $this->createSiteAlias('alias.dom');
$siteAliasInfo = static::$client->siteAlias()->get('id', $siteAlias->id);
$this->assertEquals('alias.dom', $siteAliasInfo->name);
static::$client->siteAlias()->delete('id', $siteAlias->id);
}
public function testGetAll()
{
$siteAlias = $this->createSiteAlias('alias.dom');
$siteAlias2 = $this->createSiteAlias('alias2.dom');
$siteAliasInfo = static::$client->siteAlias()->get('id', $siteAlias->id);
$this->assertEquals('alias.dom', $siteAliasInfo->name);
$siteAliasesInfo = static::$client->siteAlias()->getAll('site-id', self::$webspace->id);
$this->assertCount(2, $siteAliasesInfo);
$this->assertEquals('alias.dom', $siteAliasesInfo[0]->name);
$this->assertEquals('alias.dom', $siteAliasesInfo[0]->asciiName);
static::$client->siteAlias()->delete('id', $siteAlias->id);
static::$client->siteAlias()->delete('id', $siteAlias2->id);
}
}
================================================
FILE: tests/SiteTest.php
================================================
server()->getKeyInfo();
if (!KeyLimitChecker::checkByType($keyInfo, KeyLimitChecker::LIMIT_DOMAINS, 2)) {
$this->markTestSkipped('License does not allow to create more than 1 domain.');
}
}
private function createSite($name, array $properties = []): \PleskX\Api\Struct\Site\Info
{
$properties = array_merge([
'name' => $name,
'webspace-id' => static::$webspace->id,
], $properties);
return static::$client->site()->create($properties);
}
public function testCreate()
{
$site = $this->createSite('addon.dom');
$this->assertIsNumeric($site->id);
$this->assertGreaterThan(0, $site->id);
static::$client->site()->delete('id', $site->id);
}
public function testDelete()
{
$site = $this->createSite('addon.dom');
$result = static::$client->site()->delete('id', $site->id);
$this->assertTrue($result);
}
public function testGet()
{
$site = $this->createSite('addon.dom');
$siteInfo = static::$client->site()->get('id', $site->id);
$this->assertEquals('addon.dom', $siteInfo->name);
$this->assertMatchesRegularExpression("/^\d{4}-\d{2}-\d{2}$/", $siteInfo->creationDate);
$this->assertEquals(36, strlen($siteInfo->guid));
$siteGuid = $siteInfo->guid;
$siteInfo = static::$client->site()->get('guid', $siteGuid);
$this->assertEquals($site->id, $siteInfo->id);
static::$client->site()->delete('id', $site->id);
$siteInfo = static::$client->site()->get('parent-id', static::$webspace->id);
$this->assertNull($siteInfo);
}
public function testGetHostingWoHosting()
{
$site = $this->createSite('addon.dom');
$siteHosting = static::$client->site()->getHosting('id', $site->id);
$this->assertNull($siteHosting);
static::$client->site()->delete('id', $site->id);
}
public function testGetHostingWithHosting()
{
$properties = [
'hosting' => [
'www_root' => 'addon.dom',
],
];
$site = $this->createSite('addon.dom', $properties);
$siteHosting = static::$client->site()->getHosting('id', $site->id);
$this->assertArrayHasKey('www_root', $siteHosting->properties);
$this->assertStringEndsWith('addon.dom', $siteHosting->properties['www_root']);
static::$client->site()->delete('id', $site->id);
}
public function testGetAll()
{
$site = $this->createSite('addon.dom');
$site2 = $this->createSite('addon2.dom');
$sitesInfo = static::$client->site()->getAll();
$this->assertCount(2, $sitesInfo);
$this->assertEquals('addon.dom', $sitesInfo[0]->name);
$this->assertEquals('addon.dom', $sitesInfo[0]->asciiName);
$this->assertEquals($site->id, $sitesInfo[0]->id);
static::$client->site()->delete('id', $site->id);
static::$client->site()->delete('id', $site2->id);
}
public function testGetAllWithoutSites()
{
$sitesInfo = static::$client->site()->getAll();
$this->assertEmpty($sitesInfo);
}
}
================================================
FILE: tests/SubdomainTest.php
================================================
webspace()->get('id', static::$webspace->id);
static::$webspaceName = $webspaceInfo->name;
}
private function createSubdomain(string $name): \PleskX\Api\Struct\Subdomain\Info
{
return static::$client->subdomain()->create([
'parent' => static::$webspaceName,
'name' => $name,
'property' => [
'www_root' => $name,
],
]);
}
public function testCreate()
{
$subdomain = $this->createSubdomain('sub');
$this->assertIsInt($subdomain->id);
$this->assertGreaterThan(0, $subdomain->id);
static::$client->subdomain()->delete('id', $subdomain->id);
}
public function testDelete()
{
$subdomain = $this->createSubdomain('sub');
$result = static::$client->subdomain()->delete('id', $subdomain->id);
$this->assertTrue($result);
}
public function testGet()
{
$name = 'sub';
$subdomain = $this->createSubdomain($name);
$subdomainInfo = static::$client->subdomain()->get('id', $subdomain->id);
$this->assertEquals($name . '.' . $subdomainInfo->parent, $subdomainInfo->name);
$this->assertTrue(false !== strpos($subdomainInfo->properties['www_root'], $name));
$this->assertEquals($subdomain->id, $subdomainInfo->id);
static::$client->subdomain()->delete('id', $subdomain->id);
}
public function testGetAll()
{
$name = 'sub';
$name2 = 'sub2';
$subdomain = $this->createSubdomain($name);
$subdomain2 = $this->createSubdomain($name2);
$subdomainsInfo = static::$client->subdomain()->getAll();
$this->assertCount(2, $subdomainsInfo);
$this->assertEquals($name . '.' . $subdomainsInfo[0]->parent, $subdomainsInfo[0]->name);
$this->assertTrue(false !== strpos($subdomainsInfo[0]->properties['www_root'], $name));
$this->assertEquals($name2 . '.' . $subdomainsInfo[1]->parent, $subdomainsInfo[1]->name);
$this->assertTrue(false !== strpos($subdomainsInfo[1]->properties['www_root'], $name2));
static::$client->subdomain()->delete('id', $subdomain->id);
static::$client->subdomain()->delete('id', $subdomain2->id);
$subdomainsInfo = static::$client->subdomain()->getAll();
$this->assertEmpty($subdomainsInfo);
}
}
================================================
FILE: tests/UiTest.php
================================================
'admin',
'url' => 'http://example.com',
'text' => 'Example site',
];
public function testGetNavigation()
{
$navigation = static::$client->ui()->getNavigation();
$this->assertIsArray($navigation);
$this->assertGreaterThan(0, count($navigation));
$this->assertArrayHasKey('general', $navigation);
$this->assertArrayHasKey('hosting', $navigation);
$hostingSection = $navigation['hosting'];
$this->assertArrayHasKey('name', $hostingSection);
$this->assertArrayHasKey('nodes', $hostingSection);
$this->assertGreaterThan(0, count($hostingSection['nodes']));
}
public function testCreateCustomButton()
{
$buttonId = static::$client->ui()->createCustomButton('admin', $this->customButtonProperties);
$this->assertGreaterThan(0, $buttonId);
static::$client->ui()->deleteCustomButton($buttonId);
}
public function testGetCustomButton()
{
$buttonId = static::$client->ui()->createCustomButton('admin', $this->customButtonProperties);
$customButtonInfo = static::$client->ui()->getCustomButton($buttonId);
$this->assertEquals('http://example.com', $customButtonInfo->url);
$this->assertEquals('Example site', $customButtonInfo->text);
static::$client->ui()->deleteCustomButton($buttonId);
}
public function testDeleteCustomButton()
{
$buttonId = static::$client->ui()->createCustomButton('admin', $this->customButtonProperties);
$result = static::$client->ui()->deleteCustomButton($buttonId);
$this->assertTrue($result);
}
}
================================================
FILE: tests/Utility/KeyLimitChecker.php
================================================
$minimalRequirement;
}
}
================================================
FILE: tests/Utility/PasswordProvider.php
================================================
webspace()->getPermissionDescriptor();
$this->assertIsArray($descriptor->permissions);
$this->assertNotEmpty($descriptor->permissions);
}
public function testGetLimitDescriptor()
{
$descriptor = static::$client->webspace()->getLimitDescriptor();
$this->assertIsArray($descriptor->limits);
$this->assertNotEmpty($descriptor->limits);
}
public function testGetDiskUsage()
{
$webspace = static::createWebspace();
$diskusage = static::$client->webspace()->getDiskUsage('id', $webspace->id);
$this->assertTrue(property_exists($diskusage, 'httpdocs'));
static::$client->webspace()->delete('id', $webspace->id);
}
public function testGetPhysicalHostingDescriptor()
{
$descriptor = static::$client->webspace()->getPhysicalHostingDescriptor();
$this->assertIsArray($descriptor->properties);
$this->assertNotEmpty($descriptor->properties);
$ftpLoginProperty = $descriptor->properties['ftp_login'];
$this->assertEquals('ftp_login', $ftpLoginProperty->name);
$this->assertEquals('string', $ftpLoginProperty->type);
}
public function testGetPhpSettings()
{
$webspace = static::createWebspace();
$info = static::$client->webspace()->getPhpSettings('id', $webspace->id);
$this->assertArrayHasKey('open_basedir', $info->properties);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testGetLimits()
{
$webspace = static::createWebspace();
$limits = static::$client->webspace()->getLimits('id', $webspace->id);
$this->assertIsArray($limits->limits);
$this->assertNotEmpty($limits->limits);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testCreateWebspace()
{
$webspace = static::createWebspace();
$this->assertGreaterThan(0, $webspace->id);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testDelete()
{
$webspace = static::createWebspace();
$result = static::$client->webspace()->delete('id', $webspace->id);
$this->assertTrue($result);
}
public function testDeleteByName()
{
$webspace = static::createWebspace();
$result = static::$client->webspace()->delete('name', $webspace->name);
$this->assertTrue($result);
}
public function testRequestCreateWebspace()
{
$handlers = static::$client->phpHandler()->getAll();
$enabledHandlers = array_filter($handlers, function ($handler) {
return $handler->handlerStatus !== 'disabled';
});
$this->assertGreaterThan(0, count($enabledHandlers));
$handler = current($enabledHandlers);
$request = [
'add' => [
'gen_setup' => [
'name' => 'webspace-test-full.test',
'htype' => 'vrt_hst',
'status' => '0',
'ip_address' => [static::getIpAddress()],
],
'hosting' => [
'vrt_hst' => [
'property' => [
[
'name' => 'php_handler_id',
'value' => $handler->id,
],
[
'name' => 'ftp_login',
'value' => 'testuser',
],
[
'name' => 'ftp_password',
'value' => PasswordProvider::STRONG_PASSWORD,
],
],
'ip_address' => static::getIpAddress(),
],
],
'limits' => [
'overuse' => 'block',
'limit' => [
[
'name' => 'mbox_quota',
'value' => 100,
],
],
],
'prefs' => [
'www' => 'false',
'stat_ttl' => 6,
],
'performance' => [
'bandwidth' => 120,
'max_connections' => 10000,
],
'permissions' => [
'permission' => [
[
'name' => 'manage_sh_access',
'value' => 'true',
],
],
],
'php-settings' => [
'setting' => [
[
'name' => 'memory_limit',
'value' => '128M',
],
[
'name' => 'safe_mode',
'value' => 'false',
],
],
],
'plan-name' => 'Unlimited',
],
];
$webspace = static::$client->webspace()->request($request);
$this->assertGreaterThan(0, $webspace->id);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testGet()
{
$webspace = static::createWebspace();
$webspaceInfo = static::$client->webspace()->get('id', $webspace->id);
$this->assertNotEmpty($webspaceInfo->name);
$this->assertEquals(0, $webspaceInfo->realSize);
$this->assertEquals($webspaceInfo->name, $webspaceInfo->asciiName);
$this->assertIsArray($webspaceInfo->ipAddresses);
$this->assertEquals(36, strlen($webspaceInfo->guid));
$this->assertMatchesRegularExpression("/^\d{4}-\d{2}-\d{2}$/", $webspaceInfo->creationDate);
$this->assertEquals($webspace->id, $webspaceInfo->id);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testEnable()
{
$webspace = static::createWebspace();
static::$client->webspace()->disable('id', $webspace->id);
static::$client->webspace()->enable('id', $webspace->id);
$webspaceInfo = static::$client->webspace()->get('id', $webspace->id);
$this->assertTrue($webspaceInfo->enabled);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testDisable()
{
$webspace = static::createWebspace();
static::$client->webspace()->disable('id', $webspace->id);
$webspaceInfo = static::$client->webspace()->get('id', $webspace->id);
$this->assertFalse($webspaceInfo->enabled);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testSetProperties()
{
$webspace = static::createWebspace();
static::$client->webspace()->setProperties('id', $webspace->id, [
'description' => 'Test Description',
]);
$webspaceInfo = static::$client->webspace()->get('id', $webspace->id);
$this->assertEquals('Test Description', $webspaceInfo->description);
static::$client->webspace()->delete('id', $webspace->id);
}
public function testIpsAsArray()
{
$webspace = static::$client->webspace()->create(
[
'name' => "test-ips.test",
'ip_address' => [static::getIpAddress()],
],
[
'ftp_login' => "u-ips",
'ftp_password' => PasswordProvider::STRONG_PASSWORD,
]
);
$this->assertGreaterThan(0, $webspace->id);
static::$client->webspace()->delete('id', $webspace->id);
}
}
================================================
FILE: wait-for-plesk.sh
================================================
#!/bin/bash
### Copyright 1999-2025. WebPros International GmbH.
COUNTER=1
while : ; do
curl -ksL https://plesk:8443/ | grep "Plesk" > /dev/null
[ $? -eq 0 ] && break
echo "($COUNTER) Waiting for the Plesk initialization..."
sleep 5
COUNTER=$((COUNTER + 1))
if [ $COUNTER -eq 60 ]; then
echo "Too long, interrupting..."
break
fi
done