Skip to content

Commit e041fbd

Browse files
author
Rafael Grigorian
committed
Fixed #72
1 parent aa2bc5c commit e041fbd

File tree

4 files changed

+93
-12
lines changed

4 files changed

+93
-12
lines changed

Console/Command/SetAuth.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JetRails\Cloudflare\Console\Command;
44

55
use stdClass;
6+
use JetRails\Cloudflare\Helper\Adminhtml\PublicSuffixList;
67
use Magento\Framework\App\Cache\Type\Config as ConfigType;
78
use Magento\Framework\App\Cache\TypeListInterface;
89
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -29,13 +30,16 @@ class SetAuth extends Command {
2930
protected $_configWriter;
3031
protected $_encryptor;
3132
protected $_storeManager;
33+
protected $_publicSuffixList;
34+
protected $_psl;
3235

3336
public function __construct (
3437
StoreManagerInterface $storeManager,
3538
TypeListInterface $cacheTypeList,
3639
EncryptorInterface $encryptor,
3740
ScopeConfigInterface $configReader,
38-
WriterInterface $configWriter
41+
WriterInterface $configWriter,
42+
PublicSuffixList $publicSuffixList
3943
) {
4044
// Call the parent constructor
4145
parent::__construct ();
@@ -45,6 +49,7 @@ public function __construct (
4549
$this->_configReader = $configReader;
4650
$this->_configWriter = $configWriter;
4751
$this->_encryptor = $encryptor;
52+
$this->_publicSuffixList = $publicSuffixList;
4853
}
4954

5055
/**
@@ -86,10 +91,13 @@ protected function configure () {
8691
* @return array All domains for all stores
8792
*/
8893
private function _getDomainNames () {
94+
if ( $this->_psl == null ) {
95+
$this->_psl = $this->_publicSuffixList->getPSL ();
96+
}
8997
$domains = array ();
9098
$stores = $this->_storeManager->getStores ();
9199
foreach ( $stores as $store ) {
92-
$domain = parse_url ( $store->getBaseUrl () ) ["host"];
100+
$domain = $this->_publicSuffixList->extract ( $store->getBaseUrl (), $this->_psl ) ["root_domain"];
93101
array_push ( $domains, $domain );
94102
}
95103
$domains = array_unique ( $domains );

Helper/Adminhtml/Data.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JetRails\Cloudflare\Helper\Adminhtml;
44

55
use stdClass;
6+
use JetRails\Cloudflare\Helper\Adminhtml\PublicSuffixList;
67
use Magento\Framework\App\Cache\TypeListInterface;
78
use Magento\Framework\App\Cache\Type\Config as ConfigType;
89
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -29,10 +30,6 @@
2930
*/
3031
class Data extends AbstractHelper {
3132

32-
/**
33-
* @var string XPATH_AUTH_ZONE Path to auth zone setting
34-
* @var string XPATH_AUTH_TOKEN Path to auth token setting
35-
*/
3633
const XPATH_AUTH_ZONE = "cloudflare/configuration/auth_zone";
3734
const XPATH_AUTH_TOKEN = "cloudflare/configuration/auth_token";
3835

@@ -43,6 +40,8 @@ class Data extends AbstractHelper {
4340
protected $_coreSession;
4441
protected $_storeManager;
4542
protected $_urlBuilder;
43+
protected $_publicSuffixList;
44+
protected $_psl;
4645

4746
public function __construct (
4847
Context $context,
@@ -53,6 +52,7 @@ public function __construct (
5352
StoreManagerInterface $storeManager,
5453
WriterInterface $configWriter,
5554
UrlInterface $urlBuilder,
55+
PublicSuffixList $publicSuffixList,
5656
array $data = []
5757
) {
5858
parent::__construct ( $context, $data );
@@ -63,6 +63,18 @@ public function __construct (
6363
$this->_coreSession = $coreSession;
6464
$this->_storeManager = $storeManager;
6565
$this->_urlBuilder = $urlBuilder;
66+
$this->_publicSuffixList = $publicSuffixList;
67+
$this->refreshPSL ();
68+
}
69+
70+
public function refreshPSL () {
71+
$data = $this->_coreSession->getPSL ();
72+
if ( $data == null ) {
73+
$data = array_values ( $this->_publicSuffixList->getPSL () );
74+
$this->_coreSession->setPSL ( $data );
75+
}
76+
$this->_psl = $data;
77+
return $this->_psl;
6678
}
6779

6880
/**
@@ -160,9 +172,7 @@ public function getDomainName () {
160172
return $session->getCloudflareSelectedDomain ();
161173
}
162174
$domain = $this->_storeManager->getStore ()->getBaseUrl ();
163-
$domain = parse_url ( $domain ) ["host"];
164-
preg_match ( "/([^.\s]+\.([^.\s]{3,}|[^.\s]{2}\.[^.\s]{2}|[^.\s]{2}))\b$/im", $domain, $matches );
165-
return $matches [ 1 ];
175+
return $this->_publicSuffixList->extract ( $domain, $this->_psl ) ["root_domain"];
166176
}
167177

168178
/**
@@ -178,9 +188,7 @@ public function getDomainNames () {
178188
$domains = array ();
179189
$stores = $this->_storeManager->getStores ();
180190
foreach ( $stores as $store ) {
181-
$domain = parse_url ( $store->getBaseUrl () ) ["host"];
182-
preg_match ( "/([^.\s]+\.([^.\s]{3,}|[^.\s]{2}\.[^.\s]{2}|[^.\s]{2}))\b$/im", $domain, $matches );
183-
$domain = $matches [ 1 ];
191+
$domain = $this->_publicSuffixList->extract ( $store->getBaseUrl (), $this->_psl ) ["root_domain"];
184192
array_push ( $domains, $domain );
185193
}
186194
$domains = array_unique ( $domains );

Helper/Adminhtml/PublicSuffixList.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Helper\Adminhtml;
4+
5+
use Magento\Framework\App\Helper\AbstractHelper;
6+
7+
/**
8+
* This helper exists to download a list of domain suffixes that are used to then determine the
9+
* effective TLD and the root domain of a hostname.
10+
* @version 1.3.4
11+
* @package JetRails® Cloudflare
12+
* @author Rafael Grigorian <development@jetrails.com>
13+
* @copyright © 2018 JETRAILS, All rights reserved
14+
* @license MIT https://opensource.org/licenses/MIT
15+
*/
16+
class PublicSuffixList extends AbstractHelper {
17+
18+
protected function _endsWith ( $haystack, $needle ) {
19+
$length = strlen ( $needle );
20+
return $length > 0 ? substr ( $haystack, - $length ) === $needle : true;
21+
}
22+
23+
public function getPSL () {
24+
$handle = curl_init ();
25+
curl_setopt ( $handle, CURLOPT_URL, "https://publicsuffix.org/list/public_suffix_list.dat" );
26+
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, 1 );
27+
curl_setopt ( $handle, CURLOPT_TIMEOUT, 3 );
28+
$output = curl_exec ( $handle );
29+
$code = curl_getinfo ( $handle, CURLINFO_HTTP_CODE );
30+
curl_close ( $handle );
31+
if ( $code == 200 ) {
32+
$lines = explode ( "\n", $output );
33+
$lines = array_filter ( $lines, function ( $line ) { return $line != "" && !in_array ( $line [0], [ "/", " ", "\t" ] ); } );
34+
$lines = array_map ( function ( $line ) { return trim ( $line, ".!* \t" ); }, $lines );
35+
return $lines;
36+
}
37+
return [];
38+
}
39+
40+
public function extract ( $url, $suffixes = null ) {
41+
$host_name = parse_url ( $url ) ["host"];
42+
$max_segments = null;
43+
$best_match = null;
44+
if ( $suffixes == null ) {
45+
$suffixes = $this->getPSL ();
46+
}
47+
foreach ( $suffixes as $suffix ) {
48+
if ( $this->_endsWith ( $host_name, ".$suffix" ) ) {
49+
$count = substr_count ( $suffix, "." );
50+
if ( $max_segments == null || $count > $max_segments ) {
51+
$max_segments = $count;
52+
$best_match = $suffix;
53+
}
54+
}
55+
}
56+
$root_domain = implode ( ".", array_slice ( explode ( ".", $host_name ), -1 * ( $max_segments + 2 ) ) );
57+
return [
58+
"host_name" => $host_name,
59+
"root_domain" => $root_domain,
60+
"effective_tld" => $best_match ? $best_match : "",
61+
];
62+
}
63+
64+
}

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<argument name="encryptor" xsi:type="object" >Magento\Framework\Encryption\EncryptorInterface\Proxy</argument>
2525
<argument name="configReader" xsi:type="object" >Magento\Framework\App\Config\ScopeConfigInterface\Proxy</argument>
2626
<argument name="configWriter" xsi:type="object" >Magento\Framework\App\Config\Storage\WriterInterface\Proxy</argument>
27+
<argument name="publicSuffixList" xsi:type="object" >JetRails\Cloudflare\Helper\Adminhtml\PublicSuffixList\Proxy</argument>
2728
</arguments>
2829
</type>
2930
<type name="JetRails\Cloudflare\Console\Command\Caching\DevelopmentMode\Get" >

0 commit comments

Comments
 (0)