Skip to content

Commit

Permalink
Update owndomain handling to accept FQDN
Browse files Browse the repository at this point in the history
Update owndomain handling to accept a FQDN as one field, rather than separate SLD and TLD, and to instead detect the TLD.
  • Loading branch information
admdly committed Nov 6, 2023
1 parent b35b2ef commit ef30e9e
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/modules/Cart/Api/Guest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ public function add_item($data)

return $this->getService()->addItem($cart, $product, $data);
}
}
}
7 changes: 1 addition & 6 deletions src/modules/Cart/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ public function addItem(\Model_Cart $cart, \Model_Product $product, array $data)

$productServiceFromList = $productFromList->getService();

// @deprecated logic
if (method_exists($productServiceFromList, 'prependOrderConfig')) {
$productFromListConfig = $productServiceFromList->prependOrderConfig($productFromList, $productFromListConfig);
}

if (method_exists($productServiceFromList, 'attachOrderConfig')) {
$model = $this->di['db']->load('Product', $productFromList->id);
$productFromListConfig = $productServiceFromList->attachOrderConfig($model, $productFromListConfig);
Expand Down Expand Up @@ -522,7 +517,7 @@ public function createFromCart(\Model_Client $client, $gateway_id = null)
$item['register_sld'] = (isset($item['register_sld'])) ? strtolower($item['register_sld']) : null;
$item['transfer_sld'] = (isset($item['transfer_sld'])) ? strtolower($item['transfer_sld']) : null;
$item['sld'] = (isset($item['sld'])) ? strtolower($item['sld']) : null;
$item['domain']['owndomain_sld'] = (isset($item['domain']['owndomain_sld'])) ? strtolower($item['domain']['owndomain_sld']) : null;
$item['domain']['owndomain'] = (isset($item['domain']['owndomain'])) ? strtolower($item['domain']['owndomain']) : null;
$item['domain']['register_sld'] = (isset($item['domain']['register_sld'])) ? strtolower($item['domain']['register_sld']) : null;
$item['domain']['transfer_sld'] = (isset($item['domain']['transfer_sld'])) ? strtolower($item['domain']['transfer_sld']) : null;

Expand Down
6 changes: 0 additions & 6 deletions src/modules/Order/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,7 @@ public function createOrder(\Model_Client $client, \Model_Product $product, arra
if ($period) {
$config['period'] = $period;
}
$se = $this->di['mod_service']('service' . $product->type);
// @deprecated logic
if (method_exists($se, 'prependOrderConfig')) {
$config = $se->prependOrderConfig($product, $config);
}

// @migration script
$se = $this->di['mod_service']('service' . $product->type);
if (method_exists($se, 'attachOrderConfig')) {
$config = $se->attachOrderConfig($product, $config);
Expand Down
25 changes: 16 additions & 9 deletions src/modules/Servicedomain/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,20 @@ public function validateOrderData(&$data)
}

if ($action == 'owndomain') {
if (!isset($data['owndomain_sld'])) {
throw new \FOSSBilling\Exception('Order data must contain :field configuration field', [':field' => 'owndomain_sld']);
if (!isset($data['owndomain'])) {
throw new \Box_Exception('Order data must contain :field configuration field', [':field' => 'owndomain']);
}

if (!$validator->isSldValid($data['owndomain_sld'])) {
$safe_dom = htmlspecialchars($data['owndomain_sld'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
[$sld, $tld] = $this->_getTuple($data['owndomain']);

throw new \FOSSBilling\InformationException('Domain name :domain is invalid', [':domain' => $safe_dom]);
if (!$validator->isSldValid($sld)) {
$safe_domain = htmlspecialchars($data['owndomain'], ENT_QUOTES | ENT_HTML5, 'UTF-8');

throw new \Box_Exception('Domain name :domain is not valid', [':domain' => $safe_domain]);
}

$required = [
'owndomain_tld' => 'Domain TLD is invalid.',
'owndomain_sld' => 'Domain name is required.',
'owndomain' => 'Domain name provided is not valid.',
];
$this->di['validator']->checkRequiredParamsForArray($required, $data);
}
Expand Down Expand Up @@ -616,11 +617,17 @@ public function toApiArray(\Model_ServiceDomain $model, $deep = false, $identity
private function _getTuple($data)
{
$action = $data['action'];

[$sld, $tld] = [null, null];

if ($action == 'owndomain') {
$sld = $data['owndomain_sld'];
$tld = str_contains($data['domain']['owndomain_tld'], '.') ? $data['domain']['owndomain_tld'] : '.' . $data['domain']['owndomain_tld'];
$domain = (string) $data['domain']['owndomain'];
$tldSepPos = strrpos($domain, '.');
if ($tldSepPos === false) {
throw new \Box_Exception('Domain name :domain is not valid', [':domain' => $domain]);
}
$tld = substr($domain, $tldSepPos); // TLD includes leading period (e.g. .com).
$sld = substr($domain, 0, strlen($domain) - strlen($tld)); // SLD is everything before TLD.
}

if ($action == 'transfer') {
Expand Down
20 changes: 15 additions & 5 deletions src/modules/Servicehosting/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function getCartProductTitle($product, array $data)
{
try {
[$sld, $tld] = $this->_getDomainTuple($data);

return __trans(':hosting for :domain', [':hosting' => $product->title, ':domain' => $sld . $tld]);
} catch (\Exception $e) {
// should never occur, but in case
Expand Down Expand Up @@ -526,9 +525,20 @@ private function _getDomainTuple($data)

[$sld, $tld] = [null, null];

// When handling own domains, accept FQDN and parse out TLD.
if ($data['domain']['action'] == 'owndomain') {
$sld = $data['domain']['owndomain_sld'];
$tld = str_contains($data['domain']['owndomain_tld'], '.') ? $data['domain']['owndomain_tld'] : '.' . $data['domain']['owndomain_tld'];
$required = [
'owndomain' => 'Hosting product must have a defined owndomain parameter.'
];
$this->di['validator']->checkRequiredParamsForArray($required, $data['domain']);

$domain = $data['domain']['owndomain'];
$tldSepPos = strrpos($domain, '.');
if ($tldSepPos === false) {
throw new \Box_Exception('Hosting product domain name, :domain, invalid', [':domain' => $domain]);
}
$tld = substr($domain, $tldSepPos); // TLD includes leading period (e.g. .com).
$sld = substr($domain, 0, strlen($domain) - strlen($tld)); // SLD is everything before TLD.
}

if ($data['domain']['action'] == 'register') {
Expand Down Expand Up @@ -931,7 +941,7 @@ public function getMangerUrls(\Model_ServiceHostingServer $model)
return [false, false];
}

public function prependOrderConfig(\Model_Product $product, array $data)
public function attachOrderConfig(\Model_Product $product, array $data)
{
[$sld, $tld] = $this->_getDomainTuple($data);
$data['sld'] = $sld;
Expand All @@ -943,7 +953,7 @@ public function prependOrderConfig(\Model_Product $product, array $data)

public function getDomainProductFromConfig(\Model_Product $product, array &$data)
{
$data = $this->prependOrderConfig($product, $data);
$data = $this->attachOrderConfig($product, $data);
$product->getService()->validateOrderData($data);
$c = $this->di['tools']->decodeJ($product->config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
<label class="form-label col-3 col-form-label">{{ 'Domain'|trans }}:</label>
<div class="col">
<div class="input-group">
<input class="form-control w-50" type="text" name="config[domain][owndomain_sld]" placeholder="{{ 'Domain name'|trans }}">
<input class="form-control" type="text" name="config[domain][owndomain_tld]" value=".com" placeholder="{{ 'Domain TLD'|trans }}">
<input class="form-control w-50" type="text" name="config[domain][owndomain]" placeholder="{{ 'Domain Name'|trans }}">
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
<div id="owndomain" class="domain_action" style="display: none;">
<fieldset>
<div class="row-fluid">
<div class="controls">
<input type="text" name="domain[owndomain_sld]" value="{{ request.owndomain_sld }}" placeholder="{{ 'mydomain'|trans }}" class="span4">
<input type="text" name="domain[owndomain_tld]" value="{{ request.owndomain_tld|default('.com') }}" class="span4">
</div>
<div class="controls">
<input type="text" name="domain[owndomain]" value="{{ request.owndomain }}" placeholder="mydomain.com" class="span4">
</div>
</div>
</fieldset>
</div>
Expand Down
4 changes: 0 additions & 4 deletions tests/integration/bb-modules/mod_cart/Api_ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ public function testAddonActivatedWhileOrderInvoiceUnpaid()
'period' => '6M',
'domain' => array(
'action' => "register",
'owndomain_sld' =>"",
'owndomain_tld' =>".com",
'register_sld' =>"newdomainregister",
'register_tld' =>".com",
'register_years' => "1",
Expand Down Expand Up @@ -156,8 +154,6 @@ public function testOrderWithAddonCoverFromAccountBalance()
'period' => '6M',
'domain' => array(
'action' => "register",
'owndomain_sld' =>"",
'owndomain_tld' =>".com",
'register_sld' =>"newdomainregister2",
'register_tld' =>".com",
'register_years' => "1",
Expand Down
22 changes: 10 additions & 12 deletions tests/integration/bb-modules/mod_cart/Api_GuestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,14 @@ public function testAddHostingProductWithOwnDomainWhenExists()
{
$this->api_guest->cart_reset();

$data = array(
$data = [
'id' => 8,
'period' => '3M',
'domain' => array(
'domain' => [
'action'=> 'owndomain',
'owndomain_tld'=> '.com',
'owndomain_sld'=> 'exist',
),
);
'owndomain'=> 'exist.com'
],
];
$bool = $this->api_guest->cart_add_item($data);
$this->assertTrue($bool);
}
Expand All @@ -341,15 +340,14 @@ public function testAddHostingProductWithOwnDomain()
{
$this->api_guest->cart_reset();

$data = array(
$data = [
'id' => 8,
'period' => '3M',
'domain' => array(
'domain' => [
'action'=> 'owndomain',
'owndomain_tld'=> '.com',
'owndomain_sld'=> 'example',
),
);
'owndomain'=> 'example.com'
],
];
$bool = $this->api_guest->cart_add_item($data);
$this->assertTrue($bool);

Expand Down
4 changes: 2 additions & 2 deletions tests/integration/bb-modules/mod_order/Api_AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ public static function products()
array(7, array()), //downloadable
array(10, array('action'=>'register', 'register_sld'=>'test', 'register_tld'=>".com", 'register_years'=>'3')), //domain
array(10, array('action'=>'transfer', 'transfer_sld'=>'test', 'transfer_tld'=>".com", 'transfer_code'=>'asdasd')), //domain
array(10, array('action'=>'owndomain', 'owndomain_sld'=>'test', 'owndomain_tld'=>".com", 'register_years'=>'3')), //domain
array(10, array('action'=>'owndomain', 'owndomain'=>'test.com', 'register_years'=>'3')), //domain
array(12, array('some'=>'var')), //membership
array(8, array('domain'=>array('action'=>'owndomain', 'owndomain_sld'=>'cololo', 'owndomain_tld'=>'.com'))), //hosting
array(8, array('domain'=>array('action'=>'owndomain', 'owndomain'=>'cololo.com'))), //hosting

array(3, array()), //addon
);
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/Cart/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ public function testaddItemm_TypeHosting()
->method('getDomainProductFromConfig')
->will($this->returnValue($domainProduct));
$serviceHostingServiceMock->expects($this->atLeastOnce())
->method('prependOrderConfig')
->method('attachOrderConfig')
->will($this->returnValue(array()));

$serviceMock = $this->getMockBuilder('\Box\Mod\Cart\Service')
Expand Down
10 changes: 4 additions & 6 deletions tests/modules/Servicedomain/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public function validateOrderDataProvider()
array(
array(
'action' => 'owndomain',
'owndomain_sld' => 'example',
'owndomain_tld' => '.com'
'owndomain' => 'example.com'
),
$this->never(),
$this->never(),
Expand Down Expand Up @@ -163,9 +162,9 @@ public function validateOrderDateOwndomainExceptionsProvider()
{
return array(
array(
array( //"owndomain_sld" is missing
array( //owndomain SLD is missing
'action' => 'owndomain',
'owndomain_tld' => '.com'
'owndomain' => '.com'
),
$this->never(),
true,
Expand All @@ -174,8 +173,7 @@ public function validateOrderDateOwndomainExceptionsProvider()
array(
array(
'action' => 'owndomain',
'owndomain_sld' => 'example',
'owndomain_tld' => '.com'
'owndomain' => 'example.com'
),
$this->atLeastOnce(),
false ////"isSldValid" returns false
Expand Down

0 comments on commit ef30e9e

Please sign in to comment.