first commit

This commit is contained in:
2024-11-04 20:48:19 +01:00
commit 2fa33a3be9
7968 changed files with 2313063 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
preset: recommended
disabled:
- align_double_arrow
- phpdoc_align
- blank_line_after_opening_tag

View File

@@ -0,0 +1,4 @@
services: docker
script:
- docker-compose run tests

View File

@@ -0,0 +1,6 @@
FROM php:7.3-cli
RUN apt-get update \
&& apt-get install -y unzip \
&& docker-php-ext-install pcntl \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

View File

@@ -0,0 +1,13 @@
Copyright 1999-2020. Plesk 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.

View File

@@ -0,0 +1,66 @@
## PHP library for Plesk XML-RPC API
[![Build Status](https://travis-ci.com/plesk/api-php-lib.svg?branch=master)](https://travis-ci.com/plesk/api-php-lib) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/plesk/api-php-lib/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/plesk/api-php-lib/?branch=master)
[![StyleCI](https://styleci.io/repos/26514840/shield?branch=master)](https://styleci.io/repos/26514840)
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`

View File

@@ -0,0 +1,48 @@
{
"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": "Plesk International GmbH.",
"email": "plesk-dev-leads@plesk.com"
}
],
"require": {
"php": "^7.3",
"ext-curl": "*",
"ext-xml": "*",
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"spatie/phpunit-watcher": "^1.22"
},
"config": {
"process-timeout": 0
},
"scripts": {
"test": "phpunit",
"test:watch": "phpunit-watcher watch"
},
"autoload": {
"psr-4": {
"PleskX\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"PleskXTest\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}

View File

@@ -0,0 +1,21 @@
version: '2'
services:
plesk:
image: plesk/plesk
logging:
driver: none
ports:
["8443:8443"]
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 test -- --testdox"
depends_on:
- plesk
links:
- plesk
volumes:
- .:/opt/api-php-lib

View File

@@ -0,0 +1,3 @@
phpunit:
arguments: '--stop-on-failure'
timeout: 0

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 1999-2020. Plesk International GmbH. -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
verbose="true"
colors="true">
<testsuites>
<testsuite name="E2E">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<ini name="error_reporting" value="-1"/>
<env name="REMOTE_URL" value=""/>
<env name="REMOTE_PASSWORD" value=""/>
</php>
</phpunit>

View File

@@ -0,0 +1,580 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api;
defined('ABSPATH') or die();
use SimpleXMLElement;
/**
* Client for Plesk XML-RPC API.
*/
class Client
{
const RESPONSE_SHORT = 1;
const RESPONSE_FULL = 2;
protected $_host;
protected $_port;
protected $_protocol;
protected $_login;
protected $_password;
protected $_secretKey;
protected $_version = '';
protected $_operatorsCache = [];
/**
* @var callable
*/
protected $_verifyResponseCallback;
/**
* Create client.
*
* @param string $host
* @param int $port
* @param string $protocol
*/
public function __construct($host, $port = 8443, $protocol = 'https')
{
$this->_host = $host;
$this->_port = $port;
$this->_protocol = $protocol;
}
/**
* Setup credentials for authentication.
*
* @param string $login
* @param string $password
*/
public function setCredentials($login, $password)
{
$this->_login = $login;
$this->_password = $password;
}
/**
* Define secret key for alternative authentication.
*
* @param string $secretKey
*/
public function setSecretKey($secretKey)
{
$this->_secretKey = $secretKey;
}
/**
* Set default version for requests.
*
* @param string $version
*/
public function setVersion($version)
{
$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)
{
$this->_verifyResponseCallback = $function;
}
/**
* Retrieve host used for communication.
*
* @return string
*/
public function getHost()
{
return $this->_host;
}
/**
* Retrieve port used for communication.
*
* @return int
*/
public function getPort()
{
return $this->_port;
}
/**
* Retrieve name of the protocol (http or https) used for communication.
*
* @return string
*/
public function getProtocol()
{
return $this->_protocol;
}
/**
* Retrieve XML template for packet.
*
* @param string|null $version
*
* @return SimpleXMLElement
*/
public function getPacket($version = null)
{
$protocolVersion = !is_null($version) ? $version : $this->_version;
$content = "<?xml version='1.0' encoding='UTF-8' ?>";
$content .= '<packet'.('' === $protocolVersion ? '' : " version='$protocolVersion'").'/>';
return new SimpleXMLElement($content);
}
/**
* Perform API request.
*
* @param string|array|SimpleXMLElement $request
* @param int $mode
*
* @return XmlResponse
*/
public function request($request, $mode = self::RESPONSE_SHORT)
{
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) {
$version = ('' == $this->_version) ? null : $this->_version;
$requestXml = new SimpleXMLElement((string) $request);
$xml = \pm_ApiRpc::getService($version)->call($requestXml->children()[0]->asXml(), $this->_login);
} else {
$xml = $this->_performHttpRequest($request);
}
$this->_verifyResponseCallback
? call_user_func($this->_verifyResponseCallback, $xml)
: $this->_verifyResponse($xml);
return (self::RESPONSE_FULL == $mode) ? $xml : $xml->xpath('//result')[0];
}
/**
* 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);
$result = curl_exec($curl);
if (false === $result) {
throw new Client\Exception(curl_error($curl), curl_errno($curl));
}
curl_close($curl);
$xml = new XmlResponse($result);
return $xml;
}
/**
* Perform multiple API requests using single HTTP request.
*
* @param $requests
* @param int $mode
*
* @throws Client\Exception
*
* @return array
*/
public function multiRequest($requests, $mode = self::RESPONSE_SHORT)
{
$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();
} elseif (preg_match('/^[a-z]/', $request)) {
$this->_expandRequestShortSyntax($request, $requestXml);
}
}
$responses[] = $this->request($request);
}
if ('sdk' == $this->_protocol) {
throw new Client\Exception('Multi requests are not supported via SDK.');
} else {
$responseXml = $this->_performHttpRequest($requestXml->asXML());
}
$responses = [];
foreach ($responseXml->children() as $childNode) {
$xml = $this->getPacket();
$dom = dom_import_simplexml($xml)->ownerDocument;
$childDomNode = dom_import_simplexml($childNode);
$childDomNode = $dom->importNode($childDomNode, true);
$dom->documentElement->appendChild($childDomNode);
$response = simplexml_load_string($dom->saveXML());
$responses[] = (self::RESPONSE_FULL == $mode) ? $response : $response->xpath('//result')[0];
}
return $responses;
}
/**
* Retrieve list of headers needed for request.
*
* @return array
*/
protected 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
*/
protected function _verifyResponse($xml)
{
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')[0];
$errorMessage = (string) $xml->xpath('//errtext')[0];
throw new Exception($errorMessage, $errorCode);
}
}
/**
* Expand short syntax (some.method.call) into full XML representation.
*
* @param string $request
* @param SimpleXMLElement $xml
*
* @return string
*/
protected function _expandRequestShortSyntax($request, SimpleXMLElement $xml)
{
$parts = explode('.', $request);
$node = $xml;
foreach ($parts as $part) {
@list($name, $value) = explode('=', $part);
$node = $node->addChild($name, $value);
}
return $xml->asXML();
}
/**
* Convert array to XML representation.
*
* @param array $array
* @param SimpleXMLElement $xml
* @param string $parentEl
*
* @return SimpleXMLElement
*/
protected 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);
} else {
$xml->addChild($el, $value);
}
}
return $xml;
}
/**
* @param array $array
*
* @return bool
*/
protected function _isAssocArray(array $array)
{
return $array && array_keys($array) !== range(0, count($array) - 1);
}
/**
* @param string $name
*
* @return \PleskX\Api\Operator
*/
protected function _getOperator($name)
{
if (!isset($this->_operatorsCache[$name])) {
$className = '\\PleskX\\Api\\Operator\\'.$name;
$this->_operatorsCache[$name] = new $className($this);
}
return $this->_operatorsCache[$name];
}
/**
* @return Operator\Server
*/
public function server()
{
return $this->_getOperator('Server');
}
/**
* @return Operator\Customer
*/
public function customer()
{
return $this->_getOperator('Customer');
}
/**
* @return Operator\Webspace
*/
public function webspace()
{
return $this->_getOperator('Webspace');
}
/**
* @return Operator\Subdomain
*/
public function subdomain()
{
return $this->_getOperator('Subdomain');
}
/**
* @return Operator\Dns
*/
public function dns()
{
return $this->_getOperator('Dns');
}
/**
* @return Operator\DnsTemplate
*/
public function dnsTemplate()
{
return $this->_getOperator('DnsTemplate');
}
/**
* @return Operator\DatabaseServer
*/
public function databaseServer()
{
return $this->_getOperator('DatabaseServer');
}
/**
* @return Operator\Mail
*/
public function mail()
{
return $this->_getOperator('Mail');
}
/**
* @return Operator\Certificate
*/
public function certificate()
{
return $this->_getOperator('Certificate');
}
/**
* @return Operator\SiteAlias
*/
public function siteAlias()
{
return $this->_getOperator('SiteAlias');
}
/**
* @return Operator\Ip
*/
public function ip()
{
return $this->_getOperator('Ip');
}
/**
* @return Operator\EventLog
*/
public function eventLog()
{
return $this->_getOperator('EventLog');
}
/**
* @return Operator\SecretKey
*/
public function secretKey()
{
return $this->_getOperator('SecretKey');
}
/**
* @return Operator\Ui
*/
public function ui()
{
return $this->_getOperator('Ui');
}
/**
* @return Operator\ServicePlan
*/
public function servicePlan()
{
return $this->_getOperator('ServicePlan');
}
/**
* @return Operator\VirtualDirectory
*/
public function virtualDirectory()
{
return $this->_getOperator('VirtualDirectory');
}
/**
* @return Operator\Database
*/
public function database()
{
return $this->_getOperator('Database');
}
/**
* @return Operator\Session
*/
public function session()
{
return $this->_getOperator('Session');
}
/**
* @return Operator\Locale
*/
public function locale()
{
return $this->_getOperator('Locale');
}
/**
* @return Operator\LogRotation
*/
public function logRotation()
{
return $this->_getOperator('LogRotation');
}
/**
* @return Operator\ProtectedDirectory
*/
public function protectedDirectory()
{
return $this->_getOperator('ProtectedDirectory');
}
/**
* @return Operator\Reseller
*/
public function reseller()
{
return $this->_getOperator('Reseller');
}
/**
* @return Operator\ResellerPlan
*/
public function resellerPlan()
{
return $this->_getOperator('ResellerPlan');
}
/**
* @return Operator\Aps
*/
public function aps()
{
return $this->_getOperator('Aps');
}
/**
* @return Operator\ServicePlanAddon
*/
public function servicePlanAddon()
{
return $this->_getOperator('ServicePlanAddon');
}
/**
* @return Operator\Site
*/
public function site()
{
return $this->_getOperator('Site');
}
/**
* @return Operator\PhpHandler
*/
public function phpHandler()
{
return $this->_getOperator('PhpHandler');
}
}

View File

@@ -0,0 +1,12 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Client;
defined('ABSPATH') or die();
/**
* Transport layer exception.
*/
class Exception extends \Exception
{
}

View File

@@ -0,0 +1,11 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api;
defined('ABSPATH') or die();
/**
* Exceptions for XML-RPC API client.
*/
class Exception extends \Exception
{
}

View File

@@ -0,0 +1,25 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api;
defined('ABSPATH') or die();
/**
* Internal client for Plesk XML-RPC API (via SDK).
*/
class InternalClient extends Client
{
public function __construct()
{
parent::__construct('localhost', 0, 'sdk');
}
/**
* Setup login to execute requests under certain user.
*
* @param $login
*/
public function setLogin($login)
{
$this->_login = $login;
}
}

View File

@@ -0,0 +1,95 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api;
defined('ABSPATH') or die();
class Operator
{
/** @var string|null */
protected $_wrapperTag = null;
/** @var \PleskX\Api\Client */
protected $_client;
public function __construct($client)
{
$this->_client = $client;
if (is_null($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)
{
$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 _delete($field, $value, $deleteMethodName = 'del')
{
$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 mixed
*/
protected function _getItems($structClass, $infoTag, $field = null, $value = null, callable $filter = null)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}
$getTag->addChild('dataset')->addChild($infoTag);
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ($response->xpath('//result') as $xmlResult) {
if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) {
continue;
}
$items[] = new $structClass($xmlResult->data->$infoTag);
}
return $items;
}
}

View File

@@ -0,0 +1,65 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\Certificate as Struct;
class Certificate extends \PleskX\Api\Operator
{
/**
* @param array $properties
*
* @return Struct\Info
*/
public function generate($properties)
{
$packet = $this->_client->getPacket();
$info = $packet->addChild($this->_wrapperTag)->addChild('generate')->addChild('info');
foreach ($properties as $name => $value) {
$info->addChild($name, $value);
}
$response = $this->_client->request($packet);
return new Struct\Info($response);
}
public function create($properties)
{
$packet = $this->_client->getPacket();
$info = $packet->addChild($this->_wrapperTag)->addChild('add')->addChild('gen_info');
foreach ($properties as $name => $value) {
$info->addChild($name, $value);
}
$response = $this->_client->request($packet);
return new Struct\Info($response);
}
public function install($domains, $properties)
{
$packet = $this->_client->getPacket();
foreach ($domains as $domain) {
$install = $packet->addChild($this->_wrapperTag)->addChild('install');
$install->addChild('name', $domain);
$install->addChild('webspace', $domain);
$content = $install->addChild('content');
foreach ($properties as $name => $value) {
$content->addChild($name, $value);
}
}
$response = $this->_client->request($packet);
return new Struct\Info($response);
}
}

View File

@@ -0,0 +1,131 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\Dns as Struct;
class Dns extends \PleskX\Api\Operator
{
/**
* @param array $properties
*
* @return Struct\Info
*/
public function create($properties)
{
$packet = $this->_client->getPacket();
$info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
foreach ($properties as $name => $value) {
$info->addChild($name, $value);
}
return new Struct\Info($this->_client->request($packet));
}
/**
* Send multiply records by one request.
*
* @param array $records
*
* @return \PleskX\Api\XmlResponse[]
*/
public function bulkCreate(array $records)
{
$packet = $this->_client->getPacket();
foreach ($records as $properties) {
$info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
foreach ($properties as $name => $value) {
$info->addChild($name, $value);
}
}
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ($response->xpath('//result') as $xmlResult) {
$items[] = $xmlResult;
}
return $items;
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info
*/
public function get($field, $value)
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\Info[]
*/
public function getAll($field, $value)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ($response->xpath('//result') as $xmlResult) {
$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($field, $value)
{
return $this->_delete($field, $value, 'del_rec');
}
/**
* Delete multiply records by one request.
*
* @param array $recordIds
*
* @return \PleskX\Api\XmlResponse[]
*/
public function bulkDelete(array $recordIds)
{
$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 ($response->xpath('//result') as $xmlResult) {
$items[] = $xmlResult;
}
return $items;
}
}

View File

@@ -0,0 +1,49 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\EventLog as Struct;
class EventLog extends \PleskX\Api\Operator
{
protected $_wrapperTag = 'event_log';
/**
* @return Struct\Event[]
*/
public function get()
{
$records = [];
$response = $this->request('get');
foreach ($response->event as $eventInfo) {
$records[] = new Struct\Event($eventInfo);
}
return $records;
}
/**
* @return Struct\DetailedEvent[]
*/
public function getDetailedLog()
{
$records = [];
$response = $this->request('get_events');
foreach ($response->event as $eventInfo) {
$records[] = new Struct\DetailedEvent($eventInfo);
}
return $records;
}
/**
* @return int
*/
public function getLastId()
{
return (int) $this->request('get-last-id')->getValue('id');
}
}

View File

@@ -0,0 +1,26 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\Ip as Struct;
class Ip extends \PleskX\Api\Operator
{
/**
* @return Struct\Info[]
*/
public function get()
{
$ips = [];
$packet = $this->_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;
}
}

View File

@@ -0,0 +1,33 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\Locale as Struct;
class Locale extends \PleskX\Api\Operator
{
/**
* @param string|null $id
*
* @return Struct\Info|Struct\Info[]
*/
public function get($id = null)
{
$locales = [];
$packet = $this->_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) {
$locales[(string) $localeInfo->info->id] = new Struct\Info($localeInfo->info);
}
return !is_null($id) ? reset($locales) : $locales;
}
}

View File

@@ -0,0 +1,59 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Client;
use PleskX\Api\Operator;
use PleskX\Api\Struct\PhpHandler\Info;
class PhpHandler extends Operator
{
/**
* @param string $field
* @param int|string $value
*
* @return Info
*/
public function get($field, $value)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}
$response = $this->_client->request($packet, Client::RESPONSE_FULL);
$xmlResult = $response->xpath('//result')[0];
return new Info($xmlResult);
}
/**
* @param string|null $field
* @param int|string $value
*
* @return Info[]
*/
public function getAll($field = null, $value = null)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}
$response = $this->_client->request($packet, Client::RESPONSE_FULL);
$items = [];
foreach ($response->xpath('//result') as $xmlResult) {
$item = new Info($xmlResult);
$items[] = $item;
}
return $items;
}
}

View File

@@ -0,0 +1,123 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\ProtectedDirectory as Struct;
class ProtectedDirectory extends \PleskX\Api\Operator
{
protected $_wrapperTag = 'protected-dir';
/**
* @param string $name
* @param int $siteId
* @param string $header
*
* @return Struct\Info
*/
public function add($name, $siteId, $header = '')
{
$packet = $this->_client->getPacket();
$info = $packet->addChild($this->_wrapperTag)->addChild('add');
$info->addChild('site-id', $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($field, $value)
{
return $this->_delete($field, $value, 'delete');
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\DataInfo|false
*/
public function get($field, $value)
{
$items = $this->getAll($field, $value);
return reset($items);
}
/**
* @param string $field
* @param int|string $value
*
* @return Struct\DataInfo[]
*/
public function getAll($field, $value)
{
$response = $this->_get('get', $field, $value);
$items = [];
foreach ($response->xpath('//result/data') as $xmlResult) {
$items[] = new Struct\DataInfo($xmlResult);
}
return $items;
}
/**
* @param Struct\Info $protectedDirectory
* @param string $login
* @param string $password
*
* @return Struct\UserInfo
*/
public function addUser($protectedDirectory, $login, $password)
{
$packet = $this->_client->getPacket();
$info = $packet->addChild($this->_wrapperTag)->addChild('add-user');
$info->addChild('pd-id', $protectedDirectory->id);
$info->addChild('login', $login);
$info->addChild('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->_delete($field, $value, 'delete-user');
}
/**
* @param $command
* @param $field
* @param $value
*
* @return \PleskX\Api\XmlResponse
*/
private function _get($command, $field, $value)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild($command);
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
return $response;
}
}

View File

@@ -0,0 +1,80 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\SecretKey as Struct;
class SecretKey extends \PleskX\Api\Operator
{
protected $_wrapperTag = 'secret_key';
/**
* @param string $ipAddress
*
* @return string
*/
public function create($ipAddress)
{
$packet = $this->_client->getPacket();
$packet->addChild($this->_wrapperTag)->addChild('create')->addChild('ip_address', $ipAddress);
$response = $this->_client->request($packet);
return (string) $response->key;
}
/**
* @param string $keyId
*
* @return bool
*/
public function delete($keyId)
{
return $this->_delete('key', $keyId, 'delete');
}
/**
* @param string $keyId
*
* @return Struct\Info
*/
public function get($keyId)
{
$items = $this->_get($keyId);
return reset($items);
}
/**
* @return Struct\Info[]
*/
public function getAll()
{
return $this->_get();
}
/**
* @param string|null $keyId
*
* @return Struct\Info[]
*/
public function _get($keyId = null)
{
$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 ($response->xpath('//result/key_info') as $keyInfo) {
$items[] = new Struct\Info($keyInfo);
}
return $items;
}
}

View File

@@ -0,0 +1,173 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\Server as Struct;
class Server extends \PleskX\Api\Operator
{
/**
* @return array
*/
public function getProtos()
{
$packet = $this->_client->getPacket();
$packet->addChild($this->_wrapperTag)->addChild('get_protos');
$response = $this->_client->request($packet);
return (array) $response->protos->proto;
}
public function getGeneralInfo()
{
return new Struct\GeneralInfo($this->_getInfo('gen_info'));
}
public function getPreferences()
{
return new Struct\Preferences($this->_getInfo('prefs'));
}
public function getAdmin()
{
return new Struct\Admin($this->_getInfo('admin'));
}
/**
* @return array
*/
public function getKeyInfo()
{
$keyInfo = [];
$keyInfoXml = $this->_getInfo('key');
foreach ($keyInfoXml->property as $property) {
$keyInfo[(string) $property->name] = (string) $property->value;
}
return $keyInfo;
}
/**
* @return array
*/
public function getComponents()
{
$components = [];
$componentsXml = $this->_getInfo('components');
foreach ($componentsXml->component as $component) {
$components[(string) $component->name] = (string) $component->version;
}
return $components;
}
/**
* @return array
*/
public function getServiceStates()
{
$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()
{
return new Struct\SessionPreferences($this->_getInfo('session_setup'));
}
/**
* @return array
*/
public function getShells()
{
$shells = [];
$shellsXml = $this->_getInfo('shells');
foreach ($shellsXml->shell as $shell) {
$shells[(string) $shell->name] = (string) $shell->path;
}
return $shells;
}
/**
* @return array
*/
public function getNetworkInterfaces()
{
$interfacesXml = $this->_getInfo('interfaces');
return (array) $interfacesXml->interface;
}
public function getStatistics()
{
return new Struct\Statistics($this->_getInfo('stat'));
}
/**
* @return array
*/
public function getSiteIsolationConfig()
{
$config = [];
$configXml = $this->_getInfo('site-isolation-config');
foreach ($configXml->property as $property) {
$config[(string) $property->name] = (string) $property->value;
}
return $config;
}
public function getUpdatesInfo()
{
return new Struct\UpdatesInfo($this->_getInfo('updates'));
}
/**
* @param string $login
* @param string $clientIp
*
* @return string
*/
public function createSession($login, $clientIp)
{
$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;
}
/**
* @param string $operation
*
* @return \SimpleXMLElement
*/
private function _getInfo($operation)
{
$packet = $this->_client->getPacket();
$packet->addChild($this->_wrapperTag)->addChild('get')->addChild($operation);
$response = $this->_client->request($packet);
return $response->$operation;
}
}

View File

@@ -0,0 +1,36 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\Session as Struct;
class Session extends \PleskX\Api\Operator
{
/**
* @return Struct\Info[]
*/
public function get()
{
$sessions = [];
$response = $this->request('get');
foreach ($response->session as $sessionInfo) {
$sessions[(string) $sessionInfo->id] = new Struct\Info($sessionInfo);
}
return $sessions;
}
/**
* @param string $sessionId
*
* @return bool
*/
public function terminate($sessionId)
{
$response = $this->request("terminate.session-id=$sessionId");
return 'ok' === (string) $response->status;
}
}

View File

@@ -0,0 +1,75 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Operator;
defined('ABSPATH') or die();
use PleskX\Api\Struct\SiteAlias as Struct;
class SiteAlias extends \PleskX\Api\Operator
{
/**
* @param array $properties
* @param array $preferences
*
* @return Struct\Info
*/
public function create(array $properties, array $preferences = [])
{
$packet = $this->_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 Struct\Info
*/
public function get($field, $value)
{
$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)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ($response->xpath('//result') as $xmlResult) {
$item = new Struct\GeneralInfo($xmlResult->info);
$items[] = $item;
}
return $items;
}
}

View File

@@ -0,0 +1,63 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api;
defined('ABSPATH') or die();
abstract class Struct
{
public function __set($property, $value)
{
throw new \Exception("Try to set an undeclared property '$property'.");
}
/**
* Initialize list of scalar properties by response.
*
* @param \SimpleXMLElement $apiResponse
* @param array $properties
*
* @throws \Exception
*/
protected function _initScalarProperties($apiResponse, array $properties)
{
foreach ($properties as $property) {
if (is_array($property)) {
$classPropertyName = current($property);
$value = $apiResponse->{key($property)};
} else {
$classPropertyName = $this->_underToCamel(str_replace('-', '_', $property));
$value = $apiResponse->$property;
}
$reflectionProperty = new \ReflectionProperty($this, $classPropertyName);
$docBlock = $reflectionProperty->getDocComment();
$propertyType = preg_replace('/^.+ @var ([a-z]+) .+$/', '\1', $docBlock);
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($under)
{
$under = '_'.str_replace('_', ' ', strtolower($under));
return ltrim(str_replace(' ', '', ucwords($under)), '_');
}
}

View File

@@ -0,0 +1,21 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Certificate;
defined('ABSPATH') or die();
class Info extends \PleskX\Api\Struct
{
/** @var string */
public $request;
/** @var string */
public $privateKey;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
['csr' => 'request'],
['pvt' => 'privateKey'],
]);
}
}

View File

@@ -0,0 +1,41 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Dns;
defined('ABSPATH') or die();
class Info extends \PleskX\Api\Struct
{
/** @var int */
public $id;
/** @var int */
public $siteId;
/** @var int */
public $siteAliasId;
/** @var string */
public $type;
/** @var string */
public $host;
/** @var string */
public $value;
/** @var string */
public $opt;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'site-id',
'site-alias-id',
'type',
'host',
'value',
'opt',
]);
}
}

View File

@@ -0,0 +1,41 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\EventLog;
defined('ABSPATH') or die();
class DetailedEvent extends \PleskX\Api\Struct
{
/** @var int */
public $id;
/** @var string */
public $type;
/** @var int */
public $time;
/** @var string */
public $class;
/** @var string */
public $objectId;
/** @var string */
public $user;
/** @var string */
public $host;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'type',
'time',
'class',
['obj_id' => 'objectId'],
'user',
'host',
]);
}
}

View File

@@ -0,0 +1,29 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\EventLog;
defined('ABSPATH') or die();
class Event extends \PleskX\Api\Struct
{
/** @var string */
public $type;
/** @var int */
public $time;
/** @var string */
public $class;
/** @var string */
public $id;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'type',
'time',
'class',
'id',
]);
}
}

View File

@@ -0,0 +1,25 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Locale;
defined('ABSPATH') or die();
class Info extends \PleskX\Api\Struct
{
/** @var string */
public $id;
/** @var string */
public $language;
/** @var string */
public $country;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
['lang' => 'language'],
'country',
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\PhpHandler;
defined('ABSPATH') or die();
use PleskX\Api\Struct;
class Info extends Struct
{
/** @var string */
public $id;
/** @var string */
public $displayName;
/** @var string */
public $fullVersion;
/** @var string */
public $version;
/** @var string */
public $type;
/** @var string */
public $path;
/** @var string */
public $clipath;
/** @var string */
public $phpini;
/** @var string */
public $custom;
/** @var string */
public $handlerStatus;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'display-name',
'full-version',
'version',
'type',
'path',
'clipath',
'phpini',
'custom',
'handler-status',
]);
}
}

View File

@@ -0,0 +1,29 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\SecretKey;
class Info extends \PleskX\Api\Struct
{
/** @var string */
public $key;
/** @var string */
public $ipAddress;
/** @var string */
public $description;
/** @var string */
public $login;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'key',
'ip_address',
'description',
'login',
]);
}
}

View File

@@ -0,0 +1,37 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Session;
defined('ABSPATH') or die();
class Info extends \PleskX\Api\Struct
{
/** @var string */
public $id;
/** @var string */
public $type;
/** @var string */
public $ipAddress;
/** @var string */
public $login;
/** @var string */
public $loginTime;
/** @var string */
public $idle;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'type',
'ip-address',
'login',
'login-time',
'idle',
]);
}
}

View File

@@ -0,0 +1,33 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Site;
defined('ABSPATH') or die();
class GeneralInfo extends \PleskX\Api\Struct
{
/** @var string */
public $name;
/** @var string */
public $asciiName;
/** @var string */
public $guid;
/** @var string */
public $status;
/** @var string */
public $description;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'name',
'ascii-name',
'status',
'guid',
'description',
]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Site;
defined('ABSPATH') or die();
class HostingInfo extends \PleskX\Api\Struct
{
/** @var array */
public $properties = [];
/** @var string */
public $ipAddress;
public function __construct($apiResponse)
{
foreach ($apiResponse->vrt_hst->property as $property) {
$this->properties[(string) $property->name] = (string) $property->value;
}
$this->_initScalarProperties($apiResponse->vrt_hst, [
'ip_address',
]);
}
}

View File

@@ -0,0 +1,21 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\Site;
defined('ABSPATH') or die();
class Info extends \PleskX\Api\Struct
{
/** @var int */
public $id;
/** @var string */
public $guid;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'guid',
]);
}
}

View File

@@ -0,0 +1,25 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\SiteAlias;
defined('ABSPATH') or die();
class GeneralInfo extends \PleskX\Api\Struct
{
/** @var string */
public $name;
/** @var string */
public $asciiName;
/** @var string */
public $status;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'name',
'ascii-name',
'status',
]);
}
}

View File

@@ -0,0 +1,21 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api\Struct\SiteAlias;
defined('ABSPATH') or die();
class Info extends \PleskX\Api\Struct
{
/** @var string */
public $status;
/** @var int */
public $id;
public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'status',
]);
}
}

View File

@@ -0,0 +1,22 @@
<?php
// Copyright 1999-2020. Plesk International GmbH.
namespace PleskX\Api;
defined('ABSPATH') or die();
/**
* XML wrapper for responses.
*/
class XmlResponse extends \SimpleXMLElement
{
/**
* Retrieve value by node name.
*
* @param string $node
*
* @return string
*/
public function getValue($node)
{
return (string) $this->xpath('//'.$node)[0];
}
}

View File

@@ -0,0 +1,8 @@
#!/bin/bash
### Copyright 1999-2020. Plesk International GmbH.
while : ; do
curl -ksL https://plesk:8443/ | grep "<title>Plesk" > /dev/null
[ $? -eq 0 ] && break
sleep 5
done

View File

@@ -0,0 +1 @@
<?php // You don't belong here. ?>