-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from plesk/atikhonov-bugfix-EXTPLESK-1653
BUGFIX EXTPLESK-1653 DomainConnect API query supported template shoul…
- Loading branch information
Showing
4 changed files
with
148 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |