Skip to content

Commit

Permalink
Merge pull request #121 from plesk/atikhonov-bugfix-EXTPLESK-1653
Browse files Browse the repository at this point in the history
BUGFIX EXTPLESK-1653 DomainConnect API query supported template shoul…
  • Loading branch information
alexsibtihon authored Oct 7, 2021
2 parents 17864e5 + bc45b8a commit e68eb03
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 61 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require": {
"php": "^5.6|^7.0|^7.1",
"plesk/api-php-lib": "^1.0"
"plesk/api-php-lib": "^1.0",
"ext-json": "*"
},
"require-dev": {
"plesk/pm-api-stubs": "^17.8",
Expand Down
42 changes: 9 additions & 33 deletions src/htdocs/public/index.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
<?php
// Copyright 1999-2018. Plesk International GmbH.
// Copyright 1999-2021. Plesk International GmbH.

require_once 'sdk.php';
pm_Context::init('domain-connect');

if (!pm_Config::get('dnsProvider')) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 The DNS provider is disabled by server configuration', true, 400);
exit;
}

if (!preg_match('|/v2/(.*)/settings|', $_SERVER['REQUEST_URI'], $matches)) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400);
exit;
}
$domain = $matches[1];

try {
$pmDomain = pm_Domain::getByName($domain);
} catch (pm_Exception $e) {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 The domain does not belong to the DNS provider', true, 404);
exit;
}

$domainDns = new \PleskExt\DomainConnect\DomainDns($pmDomain);

$data = array_filter([
'providerId' => pm_Config::get('providerId'),
'providerName' => pm_Config::get('providerName'),
'providerDisplayName' => pm_Config::get('providerDisplayName', empty($_GET['providerDisplayName']) ? null : $_GET['providerDisplayName']),
'urlSyncUX' => 'https://' . $_SERVER['HTTP_HOST'] . '/modules/domain-connect/index.php/',
'urlAPI' => 'https://' . $_SERVER['HTTP_HOST'] . '/modules/domain-connect/index.php/',
'width' => 750,
'height' => 750,
'nameServers' => $domainDns->getNameServers(),
]);
header("Content-Type: application/json");
echo json_encode($data);
$requestHandler = new \PleskExt\DomainConnect\PublicRequestHandler(
$_SERVER['HTTP_HOST'],
$_SERVER['SERVER_PROTOCOL'],
$_SERVER['REQUEST_URI']
);
$requestHandler->setGetParams($_GET)->handle();
exit;
34 changes: 7 additions & 27 deletions src/plib/controllers/V2Controller.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php
// Copyright 1999-2018. Plesk International GmbH.
// Copyright 1999-2021. Plesk International GmbH.

use PleskExt\DomainConnect\Template;
use PleskExt\DomainConnect\Exception\TemplateNotFound;

class V2Controller extends pm_Controller_Action
{
Expand All @@ -15,13 +14,15 @@ public function init()
}
}

/**
* @throws pm_Exception
*/
public function domaintemplatesAction()
{
if ($this->isAction('apply')) {
$this->forward('apply');
} else {
$this->forward('query');
if (!$this->isAction('apply')) {
throw new \pm_Exception('Bad request', 400);
}
$this->forward('apply');
}

private function isAction($action)
Expand All @@ -30,27 +31,6 @@ private function isAction($action)
return (bool)preg_match("#/{$action}(/\?|\?|/$|$)#", $this->getRequest()->getRequestUri());
}

public function queryAction()
{
$provider = $this->getParam('providers');
$service = $this->getParam('services');

try {
new Template($provider, $service);
} catch (TemplateNotFound $e) {
$this->getResponse()
->setBody($this->view->escape($e->getMessage()))
->setHttpResponseCode(404);
} catch (\pm_Exception $e) {
$this->getResponse()
->setBody($this->view->escape($e->getMessage()))
->setHttpResponseCode(500);
}

$this->getHelper('viewRenderer')->setNoRender();
$this->getHelper('layout')->disableLayout();
}

public function applyAction()
{
$provider = $this->getParam('providers');
Expand Down
130 changes: 130 additions & 0 deletions src/plib/library/PublicRequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
// Copyright 1999-2021. Plesk International GmbH.

namespace PleskExt\DomainConnect;

use PleskExt\DomainConnect\Exception\TemplateNotFound;

class PublicRequestHandler
{
/**
* @var string
*/
private $host;

/**
* @var string
*/
private $protocol;

/**
* @var string
*/
private $requestUri;

/**
* @var array
*/
private $getParams = [];

/**
* @param $host
* @param $protocol
* @param $requestUri
*/
public function __construct($host, $protocol, $requestUri)
{
$this->host = $host;
$this->protocol = $protocol;
$this->requestUri = $requestUri;
}

/**
* @param array $params
* @return $this
*/
public function setGetParams(array $params)
{
$this->getParams = $params;
return $this;
}

public function handle()
{
if (!\pm_Config::get('dnsProvider')) {
$this->setHeader("{$this->protocol} 403 The DNS provider is disabled by server configuration", 400);
return;
}

if (preg_match('|/v2/(.*)/settings|', $this->requestUri, $matches)) {
$this->handleSettingsUri($matches[1]);
} elseif (preg_match('|/v2/domainTemplates/providers/(.+)/services/(.+)|', $this->requestUri, $matches)) {
$this->handleQueryTemplateUri($matches[1], $matches[2]);
} else {
$this->setHeader("{$this->protocol} 400 Bad Request", 400);
}
}

/**
* @param string $provider
* @param string $service
*/
private function handleQueryTemplateUri($provider, $service)
{
try {
new Template($provider, $service);
} catch (TemplateNotFound $e) {
$this->setHeader("{$this->protocol} 404 Not found", 404);
echo $e->getMessage();
} catch (\pm_Exception $e) {
$this->setHeader("{$this->protocol} 500 Internal server error", 500);
echo $e->getMessage();
}
}

/**
* @param string $domain
*/
private function handleSettingsUri($domain)
{
try {
$pmDomain = \pm_Domain::getByName($domain);
} catch (\pm_Exception $e) {
$this->setHeader("{$this->protocol} 404 The domain does not belong to the DNS provider", 404);
return;
}

$domainDns = new DomainDns($pmDomain);
$data = array_filter([
'providerId' => \pm_Config::get('providerId'),
'providerName' => \pm_Config::get('providerName'),
'providerDisplayName' => \pm_Config::get('providerDisplayName', $this->getGetParam('providerDisplayName')),
'urlSyncUX' => "https://{$this->host}/modules/domain-connect/index.php/",
'urlAPI' => "https://{{$this->host}}/modules/domain-connect/public/index.php/",
'width' => 750,
'height' => 750,
'nameServers' => $domainDns->getNameServers(),
]);
$this->setHeader('Content-Type: application/json');
echo json_encode($data);
}

/**
* @param string $header
* @param int $code
*/
private function setHeader($header, $code = 0)
{
header($header, true, $code);
}

/**
* @param string $name
* @param mixed $default
* @return mixed|null
*/
private function getGetParam($name, $default = null)
{
return isset($this->getParams[$name]) ? $this->getParams[$name] : $default;
}
}

0 comments on commit e68eb03

Please sign in to comment.