Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions apis/commands/opensrs_domains_dnssec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* OpenSRS DNSSEC Management
*
* OpenSRS handles DNSSEC via the provisioning modify command with data => 'dnssec'.
*
* @copyright Copyright (c) 2021, Phillips Data, Inc.
* @license http://opensource.org/licenses/mit-license.php MIT License
* @package opensrs.commands
*/
class OpensrsDomainsDnssec
{
/**
* @var OpensrsApi
*/
private $api;

/**
* Sets the API to use for communication
*
* @param OpensrsApi $api The API to use for communication
*/
public function __construct(OpensrsApi $api)
{
$this->api = $api;
}

/**
* Gets the DNSSEC DS records for a domain.
*
* @param array $vars An array of input params including:
* - domain The domain name
* @return OpensrsResponse The response object
*/
public function getDnssecRecords(array $vars) : OpensrsResponse
{
return $this->api->submit('get', array_merge($vars, [
'type' => 'dnssec'
]));
}

/**
* Adds a DNSSEC DS record to a domain.
*
* @param array $vars An array of input params including:
* - domain The domain name
* - dnssec Array of DS records, each containing:
* - key_tag The key tag
* - algorithm The algorithm number
* - digest_type The digest type
* - digest The digest value
* @return OpensrsResponse The response object
*/
public function addDnssecRecord(array $vars) : OpensrsResponse
{
return $this->api->submit('modify', array_merge($vars, [
'data' => 'dnssec',
'affect_domains' => '0'
]));
}

/**
* Removes DNSSEC DS records from a domain.
*
* @param array $vars An array of input params including:
* - domain The domain name
* - dnssec Empty array to clear all DS records
* @return OpensrsResponse The response object
*/
public function removeDnssecRecord(array $vars) : OpensrsResponse
{
return $this->api->submit('modify', array_merge($vars, [
'data' => 'dnssec',
'affect_domains' => '0'
]));
}
}
80 changes: 80 additions & 0 deletions apis/commands/opensrs_domains_forwarding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* OpenSRS Domain Forwarding Management
*
* @copyright Copyright (c) 2021, Phillips Data, Inc.
* @license http://opensource.org/licenses/mit-license.php MIT License
* @package opensrs.commands
*/
class OpensrsDomainsForwarding
{
/**
* @var OpensrsApi
*/
private $api;

/**
* Sets the API to use for communication
*
* @param OpensrsApi $api The API to use for communication
*/
public function __construct(OpensrsApi $api)
{
$this->api = $api;
}

/**
* Gets domain forwarding settings for the specified domain.
*
* @param array $vars An array of input params including:
* - domain The domain name
* @return OpensrsResponse The response object
*/
public function getDomainForwarding(array $vars) : OpensrsResponse
{
return $this->api->submit('get', array_merge($vars, ['type' => 'forwarding_email']));
}

/**
* Sets URL forwarding for the specified domain.
*
* @param array $vars An array of input params including:
* - domain The domain name
* - forwarding_uri The destination URL
* @return OpensrsResponse The response object
*/
public function setDomainForwarding(array $vars) : OpensrsResponse
{
return $this->api->submit('modify', array_merge($vars, [
'data' => 'forwarding_email',
'affect_domains' => '0'
]));
}

/**
* Creates URL forwarding for the specified domain via DNS zone.
*
* @param array $vars An array of input params including:
* - source The source subdomain or @ for root
* - destination The destination URL
* - type The redirect type (301, 302, or frame)
* - domain The domain name
* @return OpensrsResponse The response object
*/
public function createDomainForwarding(array $vars) : OpensrsResponse
{
return $this->api->submit('set_dns_zone', $vars);
}

/**
* Deletes URL forwarding for the specified domain.
*
* @param array $vars An array of input params including:
* - domain The domain name
* @return OpensrsResponse The response object
*/
public function deleteDomainForwarding(array $vars) : OpensrsResponse
{
return $this->api->submit('set_dns_zone', $vars);
}
}
24 changes: 19 additions & 5 deletions apis/opensrs_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
. DIRECTORY_SEPARATOR . 'opensrs_domains_provisioning.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'commands'
. DIRECTORY_SEPARATOR . 'opensrs_domains_transfer.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'commands'
. DIRECTORY_SEPARATOR . 'opensrs_domains_forwarding.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'commands'
. DIRECTORY_SEPARATOR . 'opensrs_domains_dnssec.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'commands' . DIRECTORY_SEPARATOR . 'opensrs_ssl.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'commands' . DIRECTORY_SEPARATOR . 'opensrs_users.php';

Expand Down Expand Up @@ -44,6 +48,11 @@ class OpensrsApi
*/
private $sandbox;

/**
* @var mixed The logger instance
*/
private $logger;

/**
* @var array An array representing the last request made
*/
Expand Down Expand Up @@ -87,11 +96,11 @@ public function submit(string $command, array $args = [], string $object = 'doma
}

// Build signature
$siganture = md5($xml_request . $this->key);
$signature = md5($xml_request . $this->key);
$headers = [
'Content-Type: text/xml',
'X-Username: ' . trim($this->username),
'X-Signature: ' . md5($siganture . $this->key),
'X-Signature: ' . md5($signature . $this->key),
'Content-Length: ' . strlen($xml_request)
];

Expand All @@ -109,6 +118,8 @@ public function submit(string $command, array $args = [], string $object = 'doma
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

if (Configure::get('Blesta.curl_verify_ssl')) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
Expand All @@ -120,8 +131,11 @@ public function submit(string $command, array $args = [], string $object = 'doma

$response = curl_exec($ch);

if ($response == false) {
if ($response === false) {
$this->logger->error(curl_error($ch));
curl_close($ch);

return new OpensrsResponse('');
}

curl_close($ch);
Expand Down Expand Up @@ -182,11 +196,11 @@ private function buildRecursiveAttributes(SimpleXMLElement &$dt_assoc, array $ar
if (is_array($value)) {
$assoc = $dt_assoc->addChild('item');
$assoc->addAttribute('key', $key);
$assoc = $assoc->addChild(isset($value[0]) ? 'dt_array' : 'dt_assoc');
$assoc = $assoc->addChild((empty($value) || isset($value[0])) ? 'dt_array' : 'dt_assoc');

$this->buildRecursiveAttributes($assoc, $value);
} else {
$dt_assoc->addChild('item', $value)
$dt_assoc->addChild('item', htmlspecialchars((string) $value, ENT_XML1, 'UTF-8'))
->addAttribute('key', $key);
}
}
Expand Down
32 changes: 23 additions & 9 deletions apis/opensrs_response.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public function response() : ?object
*
* @return string The status (OK = success, ERROR = error, null = invalid responses)
*/
public function status() : ?string
public function status() : string
{
if ($this->xml && $this->xml instanceof SimpleXMLElement) {
return ($this->formatResponse($this->xml->body->data_block)->is_success ?? false) ? 'OK' : 'ERROR';
}

return null;
return 'ERROR';
}

/**
Expand All @@ -70,18 +70,32 @@ public function status() : ?string
public function errors() : ?object
{
if ($this->xml && $this->xml instanceof SimpleXMLElement) {
$error = 'Internal Server Error';
$data = $this->formatResponse($this->xml->body->data_block);

$error_msg = $data->attributes['error']
?? $data->response_text
?? 'Internal Server Error';

// Extract detailed per-domain error messages from attributes.details
$details = [];
if (isset($data->attributes['details']) && is_array($data->attributes['details'])) {
foreach ($data->attributes['details'] as $domain => $info) {
if (is_array($info) && !empty($info['response_text'])) {
$details[] = trim($info['response_text']);
}
}
}

$error_msg = $this->formatResponse($this->xml->body->data_block)->attributes['error']
?? $this->formatResponse($this->xml->body->data_block)->response_text
?? $error;
if (!empty($details)) {
$error_msg = implode('; ', $details);
}

return (object)[
'response_text' => $error_msg,
'response_code' => $this->formatResponse($this->xml->body->data_block)->response_code ?? 500
'response_code' => $data->response_code ?? 500
];
}

return null;
}

Expand All @@ -92,7 +106,7 @@ public function errors() : ?object
*/
public function raw() : string
{
return $this->raw;
return $this->raw ?? '';
}

/**
Expand Down
Loading