Skip to content

Commit 256e4d1

Browse files
author
Rafael Grigorian
committed
Fixed #153
1 parent 3ebe3d0 commit 256e4d1

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

src/app/code/community/JetRails/Cloudflare/Helper/Data.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ class JetRails_Cloudflare_Helper_Data extends Mage_Core_Helper_Abstract {
2323
const XPATH_AUTH_ZONE = "cloudflare/configuration/auth_zone";
2424
const XPATH_AUTH_TOKEN = "cloudflare/configuration/auth_token";
2525

26+
/**
27+
* This method returns the public suffix list either from cache based on
28+
* the user session, or downloads it directly
29+
* @return array[string] Public Suffix List
30+
*/
31+
public function refreshPSL () {
32+
$session = Mage::getSingleton ("core/session");
33+
$data = $session->getPSL ();
34+
if ( $data == null ) {
35+
$psl = Mage::helper ("cloudflare/publicSuffixList");
36+
$data = array_values ( $psl->getPSL () );
37+
$session->setPSL ( $data );
38+
}
39+
return $data;
40+
}
41+
2642
/**
2743
* This method gets the value for the authorization zone and decrypts
2844
* it. It then returns that result to the caller.
@@ -73,13 +89,12 @@ public function getAuthToken () {
7389
*/
7490
public function getDomainName () {
7591
$session = Mage::getSingleton ("core/session");
92+
$psl = Mage::helper ("cloudflare/publicSuffixList");
7693
if ( !empty ( $session->getCloudflareSelectedDomain () ) ) {
7794
return $session->getCloudflareSelectedDomain ();
7895
}
7996
$domain = Mage::getBaseUrl ( Mage_Core_Model_Store::URL_TYPE_WEB );
80-
$domain = parse_url ( $domain ) ["host"];
81-
preg_match ( "/([^.\s]+\.([^.\s]{3,}|[^.\s]{2}\.[^.\s]{2}|[^.\s]{2}))\b$/im", $domain, $matches );
82-
return $matches [ 1 ];
97+
return $psl->extract ( $domain, $this->refreshPSL () ) ["root_domain"];
8398
}
8499

85100
/**
@@ -91,15 +106,15 @@ public function getDomainName () {
91106
* @return array All domains for all stores
92107
*/
93108
public function getDomainNames () {
109+
$psl = Mage::helper ("cloudflare/publicSuffixList");
94110
$selection = $this->getDomainName ();
95111
$domains = array ();
112+
$pslData = $this->refreshPSL ();
96113
foreach ( Mage::app ()->getWebsites () as $website ) {
97114
foreach ( $website->getGroups () as $group ) {
98115
$stores = $group->getStores ();
99116
foreach ( $stores as $store ) {
100-
$domain = parse_url ( $store->getBaseUrl () ) ["host"];
101-
preg_match ( "/([^.\s]+\.([^.\s]{3,}|[^.\s]{2}\.[^.\s]{2}|[^.\s]{2}))\b$/im", $domain, $matches );
102-
$domain = $matches [ 1 ];
117+
$domain = $psl->extract ( $store->getBaseUrl (), $pslData ) ["root_domain"];
103118
array_push ( $domains, $domain );
104119
}
105120
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* This helper exists to download a list of domain suffixes that are used to then determine the
5+
* effective TLD and the root domain of a hostname.
6+
* @version 1.2.4
7+
* @package JetRails® Cloudflare
8+
* @author Rafael Grigorian <development@jetrails.com>
9+
* @copyright © 2018 JETRAILS, All rights reserved
10+
* @license MIT https://opensource.org/licenses/MIT
11+
*/
12+
class JetRails_Cloudflare_Helper_PublicSuffixList extends Mage_Core_Helper_Abstract {
13+
14+
protected function _endsWith ( $haystack, $needle ) {
15+
$length = strlen ( $needle );
16+
return $length > 0 ? substr ( $haystack, - $length ) === $needle : true;
17+
}
18+
19+
public function getPSL () {
20+
error_log ("Downloading PSL...");
21+
$handle = curl_init ();
22+
curl_setopt ( $handle, CURLOPT_URL, "https://publicsuffix.org/list/public_suffix_list.dat" );
23+
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, 1 );
24+
curl_setopt ( $handle, CURLOPT_TIMEOUT, 3 );
25+
$output = curl_exec ( $handle );
26+
$code = curl_getinfo ( $handle, CURLINFO_HTTP_CODE );
27+
curl_close ( $handle );
28+
if ( $code == 200 ) {
29+
$lines = explode ( "\n", $output );
30+
$lines = array_filter ( $lines, function ( $line ) { return $line != "" && !in_array ( $line [0], [ "/", " ", "\t" ] ); } );
31+
$lines = array_map ( function ( $line ) { return trim ( $line, ".!* \t" ); }, $lines );
32+
return $lines;
33+
}
34+
return [];
35+
}
36+
37+
public function extract ( $url, $suffixes = null ) {
38+
$host_name = parse_url ( $url ) ["host"];
39+
$max_segments = null;
40+
$best_match = null;
41+
if ( $suffixes == null ) {
42+
$suffixes = $this->getPSL ();
43+
}
44+
foreach ( $suffixes as $suffix ) {
45+
if ( $this->_endsWith ( $host_name, ".$suffix" ) ) {
46+
$count = substr_count ( $suffix, "." );
47+
if ( $max_segments == null || $count > $max_segments ) {
48+
$max_segments = $count;
49+
$best_match = $suffix;
50+
}
51+
}
52+
}
53+
$root_domain = implode ( ".", array_slice ( explode ( ".", $host_name ), -1 * ( $max_segments + 2 ) ) );
54+
return [
55+
"host_name" => $host_name,
56+
"root_domain" => $root_domain,
57+
"effective_tld" => $best_match ? $best_match : "",
58+
];
59+
}
60+
61+
}

0 commit comments

Comments
 (0)