code format

This commit is contained in:
gently 2023-03-29 20:42:35 +08:00
parent 6c323fa5da
commit 6cf739ad41

View File

@ -1,6 +1,7 @@
<?php <?php
/** /**
* url地址相关的操作类 * url地址相关的操作类
* Formatter: https://www.duplichecker.com/php-formatter
* *
* @file addressMaker.class.php * @file addressMaker.class.php
* @author gently * @author gently
@ -8,11 +9,11 @@
* *
* *
*/ */
!defined('ROOT_DIR') && die('Access Denied.'); !defined("ROOT_DIR") && die("Access Denied.");
class addressMaker{ class addressMaker
{
const LINK_URL = 'https://github.com/privacy-protection-tools/anti-AD'; const LINK_URL = "https://github.com/privacy-protection-tools/anti-AD";
/** /**
* 分离域名 * 分离域名
@ -20,41 +21,64 @@ class addressMaker{
* @param $str_domain * @param $str_domain
* @return string * @return string
*/ */
public static function extract_main_domain($str_domain){ public static function extract_main_domain($str_domain)
{
if (empty($str_domain)) { if (empty($str_domain)) {
return ""; return "";
} }
$str_reg = '/^(?:(?:[a-z0-9\-]*[a-z0-9]\.)*?|\.)?([a-z0-9\-]*[a-z0-9]('; $str_reg = "/^(?:(?:[a-z0-9\-]*[a-z0-9]\.)*?|\.)?([a-z0-9\-]*[a-z0-9](";
/************start CN域名的特殊处理规则其中包括了各行政区特别后缀的cn域名*****************************/ /************start CN域名的特殊处理规则其中包括了各行政区特别后缀的cn域名*****************************/
$str_reg .= '\.ac\.cn|\.ah\.cn|\.bj\.cn|\.com\.cn|\.cq\.cn|\.fj\.cn|\.gd\.cn|\.gov\.cn|\.gs\.cn'; $str_reg .=
$str_reg .= '|\.gx\.cn|\.gz\.cn|\.ha\.cn|\.hb\.cn|\.he\.cn|\.hi\.cn|\.hk\.cn|\.hl\.cn|\.hn\.cn'; "\.ac\.cn|\.ah\.cn|\.bj\.cn|\.com\.cn|\.cq\.cn|\.fj\.cn|\.gd\.cn|\.gov\.cn|\.gs\.cn";
$str_reg .= '|\.jl\.cn|\.js\.cn|\.jx\.cn|\.ln\.cn|\.mo\.cn|\.net\.cn|\.nm\.cn|\.nx\.cn|\.org\.cn'; $str_reg .=
$str_reg .= '|\.qh\.cn|\.sc\.cn|\.sd\.cn|\.sh\.cn|\.sn\.cn|\.sx\.cn|\.tj\.cn|\.tw\.cn|\.xj\.cn'; "|\.gx\.cn|\.gz\.cn|\.ha\.cn|\.hb\.cn|\.he\.cn|\.hi\.cn|\.hk\.cn|\.hl\.cn|\.hn\.cn";
$str_reg .= '|\.xz\.cn|\.yn\.cn|\.zj\.cn|\.edu.cn'; $str_reg .=
"|\.jl\.cn|\.js\.cn|\.jx\.cn|\.ln\.cn|\.mo\.cn|\.net\.cn|\.nm\.cn|\.nx\.cn|\.org\.cn";
$str_reg .=
"|\.qh\.cn|\.sc\.cn|\.sd\.cn|\.sh\.cn|\.sn\.cn|\.sx\.cn|\.tj\.cn|\.tw\.cn|\.xj\.cn";
$str_reg .= "|\.xz\.cn|\.yn\.cn|\.zj\.cn|\.edu.cn";
/************end CN域名的特殊处理规则其中包括了各行政区特别后缀的cn域名******************************/ /************end CN域名的特殊处理规则其中包括了各行政区特别后缀的cn域名******************************/
$str_reg .= '|\.cn|\.com|\.net|\.org|\.me|\.co|\.info|\.cc|\.tv'; $str_reg .= "|\.cn|\.com|\.net|\.org|\.me|\.co|\.info|\.cc|\.tv";
$str_reg .= '|\.pw|\.biz|\.top|\.win|\.bid|\.cf|\.club|\.ne|\.de|\.la|\.us|\.mobi|\.hn|\.asia'; $str_reg .=
$str_reg .= '|\.jp|\.tw|\.am|\.hk|\.site|\.live|\.xyz|\.space|\.fr|\.es|\.nl|\.au|\.in|\.ru'; "|\.pw|\.biz|\.top|\.win|\.bid|\.cf|\.club|\.ne|\.de|\.la|\.us|\.mobi|\.hn|\.asia";
$str_reg .= '|\.su|\.world|\.io|\.trade|\.bet|\.im|\.fm|\.today|\.wang|\.rocks|\.vip|\.eu|\.run'; $str_reg .=
$str_reg .= '|\.online|\.website|\.cricket|\.date|\.men|\.ca|\.xxx|\.name|\.pl|\.be|\.il|\.gov|\.it'; "|\.jp|\.tw|\.am|\.hk|\.site|\.live|\.xyz|\.space|\.fr|\.es|\.nl|\.au|\.in|\.ru";
$str_reg .= '|\.cl|\.tk|\.cz|\.hu|\.ro|\.vg|\.ws|\.nu|\.vn|\.lt|\.edu|\.lv|\.mx|\.by|\.gr|\.br|\.fi'; $str_reg .=
$str_reg .= '|\.pt|\.dk|\.se|\.at|\.id|\.ve|\.ir|\.ma|\.ch|\.nf|\.bg|\.ua|\.is|\.hr|\.shop|\.xin|\.si|\.or'; "|\.su|\.world|\.io|\.trade|\.bet|\.im|\.fm|\.today|\.wang|\.rocks|\.vip|\.eu|\.run";
$str_reg .= '|\.sk|\.kz|\.tt|\.so|\.gg|\.ms|\.ink|\.pro|\.work|\.click|\.link|\.ly|\.ai|\.tech|\.kr|\.to'; $str_reg .=
$str_reg .= '|\.uk|\.ad|\.ac|\.md|\.ml|\.cm|\.re|\.ph|\.my|\.lu|\.network|\.sh|\.fun|\.az|\.cx|\.ga'; "|\.online|\.website|\.cricket|\.date|\.men|\.ca|\.xxx|\.name|\.pl|\.be|\.il|\.gov|\.it";
$str_reg .= '|\.ae|\.bz|\.gq|\.gs|\.pk|\.sex|\.stream|\.support|\.pub|\.nz|\.ng|\.zw|\.sx|\.studio|\.media|\.zone'; $str_reg .=
$str_reg .= '|\.icu|\.ie|\.li|\.bar|\.video|\.wiki|\.ltd|\.cash|\.pink|\.loan|\.gdn|\.app|\.ovh|\.land|\.st|\.how'; "|\.cl|\.tk|\.cz|\.hu|\.ro|\.vg|\.ws|\.nu|\.vn|\.lt|\.edu|\.lv|\.mx|\.by|\.gr|\.br|\.fi";
$str_reg .= '|\.kim|\.download|\.ag|\.technology|\.company|\.guru|\.gt|\.sg|\.photo|\.digital|\.one|\.tr'; $str_reg .=
$str_reg .= '|\.show|\.sncf|\.uz|\.as|\.ee|\.fyi|\.cloud|\.group|\.promo|\.party|\.services|\.life|\.no'; "|\.pt|\.dk|\.se|\.at|\.id|\.ve|\.ir|\.ma|\.ch|\.nf|\.bg|\.ua|\.is|\.hr|\.shop|\.xin|\.si|\.or";
$str_reg .= '|\.watch|\.works|\.buzz|\.best|\.center|\.host|\.style|\.press|\.solutions|\.exchange|\.wtf'; $str_reg .=
$str_reg .= '|\.delivery|\.page|\.webcam|\.cam|\.supply|\.accountant|\.systems|\.agency|\.science|\.awe'; "|\.sk|\.kz|\.tt|\.so|\.gg|\.ms|\.ink|\.pro|\.work|\.click|\.link|\.ly|\.ai|\.tech|\.kr|\.to";
$str_reg .= '|\.gd|\.review|\.tc|\.mn|\.cool|\.monster|\.do|\.bi|\.news|\.boom|\.lol|\.events|\.jobs'; $str_reg .=
$str_reg .= '|\.ooo|\.social|\.ninja|\.blue|\.plus|\.racing|\.ht|\.tl|\.cat|\.tf|\.al|\.vc|\.cr'; "|\.uk|\.ad|\.ac|\.md|\.ml|\.cm|\.re|\.ph|\.my|\.lu|\.network|\.sh|\.fun|\.az|\.cx|\.ga";
$str_reg .= ')'; $str_reg .=
"|\.ae|\.bz|\.gq|\.gs|\.pk|\.sex|\.stream|\.support|\.pub|\.nz|\.ng|\.zw|\.sx|\.studio|\.media|\.zone";
$str_reg .=
"|\.icu|\.ie|\.li|\.bar|\.video|\.wiki|\.ltd|\.cash|\.pink|\.loan|\.gdn|\.app|\.ovh|\.land|\.st|\.how";
$str_reg .=
"|\.kim|\.download|\.ag|\.technology|\.company|\.guru|\.gt|\.sg|\.photo|\.digital|\.one|\.tr";
$str_reg .=
"|\.show|\.sncf|\.uz|\.as|\.ee|\.fyi|\.cloud|\.group|\.promo|\.party|\.services|\.life|\.no";
$str_reg .=
"|\.watch|\.works|\.buzz|\.best|\.center|\.host|\.style|\.press|\.solutions|\.exchange|\.wtf";
$str_reg .=
"|\.delivery|\.page|\.webcam|\.cam|\.supply|\.accountant|\.systems|\.agency|\.science|\.awe";
$str_reg .=
"|\.gd|\.review|\.tc|\.mn|\.cool|\.monster|\.do|\.bi|\.news|\.boom|\.lol|\.events|\.jobs";
$str_reg .=
"|\.ooo|\.social|\.ninja|\.blue|\.plus|\.racing|\.ht|\.tl|\.cat|\.tf|\.al|\.vc|\.cr";
$str_reg .= ")";
$str_reg .= '(\.hk|\.tw|\.uk|\.jp|\.kr|\.th|\.au|\.ua|\.so|\.br|\.sg|\.pt|\.ec|\.ar|\.my'; $str_reg .=
$str_reg .= '|\.tr|\.bd|\.mk|\.za|\.mt|\.sm|\.ge|\.kg|\.ke|\.de|\.ve|\.es|\.ru|\.pk|\.mx'; "(\.hk|\.tw|\.uk|\.jp|\.kr|\.th|\.au|\.ua|\.so|\.br|\.sg|\.pt|\.ec|\.ar|\.my";
$str_reg .= '|\.nz|\.py|\.pe|\.ph|\.pl|\.ng|\.pa|\.fj'; $str_reg .=
"|\.tr|\.bd|\.mk|\.za|\.mt|\.sm|\.ge|\.kg|\.ke|\.de|\.ve|\.es|\.ru|\.pk|\.mx";
$str_reg .= "|\.nz|\.py|\.pe|\.ph|\.pl|\.ng|\.pa|\.fj";
$str_reg .= ')?)$/'; $str_reg .= ')?)$/';
if (preg_match($str_reg, $str_domain, $matches)) { if (preg_match($str_reg, $str_domain, $matches)) {
@ -62,7 +86,6 @@ class addressMaker{
} }
return ""; return "";
} }
/** /**
@ -73,16 +96,20 @@ class addressMaker{
* @param Array $arr_whitelist 白名单列表 * @param Array $arr_whitelist 白名单列表
* @return array * @return array
*/ */
public static function get_domain_from_easylist($str_easylist, $strict_mode = false, $arr_whitelist = array()){ public static function get_domain_from_easylist(
$str_easylist,
$strict_mode = false,
$arr_whitelist = []
) {
$strlen = strlen($str_easylist); $strlen = strlen($str_easylist);
if ($strlen < 10) { if ($strlen < 10) {
return array(); return [];
} }
$str_easylist = $str_easylist . "\n"; //防止最后一行没有换行符 $str_easylist = $str_easylist . "\n"; //防止最后一行没有换行符
$i = 0; $i = 0;
$arr_domains = array(); $arr_domains = [];
while ($i < $strlen) { while ($i < $strlen) {
$end_pos = strpos($str_easylist, "\n", $i); $end_pos = strpos($str_easylist, "\n", $i);
$line = trim(substr($str_easylist, $i, $end_pos - $i)); $line = trim(substr($str_easylist, $i, $end_pos - $i));
@ -91,20 +118,29 @@ class addressMaker{
continue; continue;
} }
if($line[0] != '|' || $line[1] != '|'){ if ($line[0] != "|" || $line[1] != "|") {
continue; continue;
} }
if(preg_match('/^\|\|([0-9a-z\-\.]+[a-z]+)\^(\$([^=~]+?,)?(image|third-party|script)(,[^=~]+)?)?$/', $line, $matches)){ if (
preg_match(
if(substr($matches[1], 0, 4) == 'www.'){ '/^\|\|([0-9a-z\-\.]+[a-z]+)\^(\$([^=~]+?,)?(image|third-party|script)(,[^=~]+)?)?$/',
$line,
$matches
)
) {
if (substr($matches[1], 0, 4) == "www.") {
$row = substr($matches[1], 4); $row = substr($matches[1], 4);
} else { } else {
$row = $matches[1]; $row = $matches[1];
} }
$main_domain = self::extract_main_domain($matches[1]); $main_domain = self::extract_main_domain($matches[1]);
if($strict_mode && (!array_key_exists($main_domain, $arr_whitelist) || ($arr_whitelist[$main_domain] >= 1))){ if (
$arr_domains[$main_domain] = array($main_domain); $strict_mode &&
(!array_key_exists($main_domain, $arr_whitelist) ||
$arr_whitelist[$main_domain] >= 1)
) {
$arr_domains[$main_domain] = [$main_domain];
} else { } else {
$arr_domains[$main_domain][] = $row; $arr_domains[$main_domain][] = $row;
} }
@ -122,38 +158,49 @@ class addressMaker{
* @param Array $arr_whitelist 白名单 * @param Array $arr_whitelist 白名单
* @return array * @return array
*/ */
public static function get_domain_list($str_hosts, $strict_mode = false, $arr_whitelist = array()){ public static function get_domain_list(
$str_hosts,
$strict_mode = false,
$arr_whitelist = []
) {
$strlen = strlen($str_hosts); $strlen = strlen($str_hosts);
if ($strlen < 3) { if ($strlen < 3) {
return array(); return [];
} }
$str_hosts = $str_hosts . "\n"; //防止最后一行没有换行符 $str_hosts = $str_hosts . "\n"; //防止最后一行没有换行符
$i = 0; $i = 0;
$arr_domains = array(); $arr_domains = [];
while ($i < $strlen) { while ($i < $strlen) {
$end_pos = strpos($str_hosts, "\n", $i); $end_pos = strpos($str_hosts, "\n", $i);
$line = trim(substr($str_hosts, $i, $end_pos - $i)); $line = trim(substr($str_hosts, $i, $end_pos - $i));
$i = $end_pos + 1; $i = $end_pos + 1;
if(empty($line) || ($line[0] == '#')){//注释行忽略 if (empty($line) || $line[0] == "#") {
//注释行忽略
continue; continue;
} }
$line = strtolower(preg_replace('/[\s\t]+/', "/", $line)); $line = strtolower(preg_replace('/[\s\t]+/', "/", $line));
if((strpos($line, '127.0.0.1') === false) && if (
(strpos($line, '::') === false) && strpos($line, "127.0.0.1") === false &&
(strpos($line, '0.0.0.0') === false)){ strpos($line, "::") === false &&
strpos($line, "0.0.0.0") === false
) {
continue; continue;
} }
$row = explode('/', $line); $row = explode("/", $line);
if(strpos($row[1], '.') === false){ if (strpos($row[1], ".") === false) {
continue; continue;
} }
$main_domain = self::extract_main_domain($row[1]); $main_domain = self::extract_main_domain($row[1]);
if($strict_mode && (!array_key_exists($main_domain, $arr_whitelist) || ($arr_whitelist[$main_domain] >= 1))){ if (
$arr_domains[$main_domain] = array($main_domain); $strict_mode &&
(!array_key_exists($main_domain, $arr_whitelist) ||
$arr_whitelist[$main_domain] >= 1)
) {
$arr_domains[$main_domain] = [$main_domain];
} else { } else {
$arr_domains[$main_domain][] = $row[1]; $arr_domains[$main_domain][] = $row[1];
} }
@ -162,12 +209,13 @@ class addressMaker{
return $arr_domains; return $arr_domains;
} }
private static function write_conf_header($fp, $header, $arr_params = array()){ private static function write_conf_header($fp, $header, $arr_params = [])
$header = str_replace('{DATE}', date('YmdHis'), $header); {
$header = str_replace('{URL}', self::LINK_URL, $header); $header = str_replace("{DATE}", date("YmdHis"), $header);
$header = str_replace("{URL}", self::LINK_URL, $header);
foreach ($arr_params as $keyword => $val) { foreach ($arr_params as $keyword => $val) {
$header = str_replace('{' . $keyword . '}', $val, $header); $header = str_replace("{" . $keyword . "}", $val, $header);
} }
return fwrite($fp, $header); return fwrite($fp, $header);
} }
@ -180,8 +228,11 @@ class addressMaker{
* @param array $arr_whitelist * @param array $arr_whitelist
* @return false|int * @return false|int
*/ */
public static function write_to_file(array $arr_src, array $arr_format, array $arr_whitelist = array()){ public static function write_to_file(
array $arr_src,
array $arr_format,
array $arr_whitelist = []
) {
if (count($arr_src) < 1) { if (count($arr_src) < 1) {
return false; return false;
} }
@ -192,30 +243,36 @@ class addressMaker{
} }
} }
$str_result = ''; $str_result = "";
$line_count = 0; $line_count = 0;
$arr_written = []; $arr_written = [];
foreach ($arr_src as $main_domain => $arr_subdomains) { foreach ($arr_src as $main_domain => $arr_subdomains) {
if (
if(array_key_exists($main_domain, $arr_whitelist) && ($arr_whitelist[$main_domain] > 0)){ array_key_exists($main_domain, $arr_whitelist) &&
$arr_whitelist[$main_domain] > 0
) {
continue; continue;
} }
if(empty($main_domain)){//不匹配记录(一般是不合法域名或者未收录的后缀) if (empty($main_domain)) {
//不匹配记录(一般是不合法域名或者未收录的后缀)
continue; continue;
} }
if ( if (
(1 !== $arr_format['full_domain']) 1 !== $arr_format["full_domain"] &&
&& (!array_key_exists($main_domain, $arr_whitelist)) !array_key_exists($main_domain, $arr_whitelist) &&
&& (in_array($main_domain, $arr_subdomains) (in_array($main_domain, $arr_subdomains) ||
|| in_array('www.' . $main_domain, $arr_subdomains) in_array("www." . $main_domain, $arr_subdomains) ||
|| in_array('.' . $main_domain, $arr_subdomains) in_array("." . $main_domain, $arr_subdomains))
)
) { ) {
$str_result .= str_replace('{DOMAIN}', $main_domain, $arr_format['format']) . "\n"; $str_result .=
str_replace(
"{DOMAIN}",
$main_domain,
$arr_format["format"]
) . "\n";
$line_count++; $line_count++;
continue; continue;
} }
@ -227,10 +284,15 @@ class addressMaker{
continue; continue;
} }
$arr_tmp_domain = explode('.', $subdomain); $arr_tmp_domain = explode(".", $subdomain);
$tmp_domain_len = count($arr_tmp_domain); $tmp_domain_len = count($arr_tmp_domain);
if ($tmp_domain_len < 3) { if ($tmp_domain_len < 3) {
$str_result .= str_replace('{DOMAIN}', $subdomain, $arr_format['format']) . "\n"; $str_result .=
str_replace(
"{DOMAIN}",
$subdomain,
$arr_format["format"]
) . "\n";
$line_count++; $line_count++;
$arr_written[$subdomain] = 2; $arr_written[$subdomain] = 2;
continue; continue;
@ -239,20 +301,28 @@ class addressMaker{
$matched_flag = false; $matched_flag = false;
for ($pos = 3; $pos <= $tmp_domain_len; $pos++) { for ($pos = 3; $pos <= $tmp_domain_len; $pos++) {
$arr_tmp = array_slice($arr_tmp_domain, -1 * $pos); $arr_tmp = array_slice($arr_tmp_domain, -1 * $pos);
$tmp = implode('.', $arr_tmp); $tmp = implode(".", $arr_tmp);
if (array_key_exists($tmp, $arr_whitelist)) { if (array_key_exists($tmp, $arr_whitelist)) {
$matched_flag = $arr_whitelist[$tmp] === 1; $matched_flag = $arr_whitelist[$tmp] === 1;
break; break;
} }
if(($tmp === $subdomain) || array_key_exists($tmp, $arr_subdomains)){ if (
$tmp === $subdomain ||
array_key_exists($tmp, $arr_subdomains)
) {
if (!array_key_exists($tmp, $arr_written)) { if (!array_key_exists($tmp, $arr_written)) {
$str_result .= str_replace('{DOMAIN}', $tmp, $arr_format['format']) . "\n"; $str_result .=
str_replace(
"{DOMAIN}",
$tmp,
$arr_format["format"]
) . "\n";
$line_count++; $line_count++;
$arr_written[$tmp] = 2; $arr_written[$tmp] = 2;
} }
$matched_flag = 1 !== $arr_format['full_domain']; $matched_flag = 1 !== $arr_format["full_domain"];
break; break;
} }
} }
@ -262,7 +332,12 @@ class addressMaker{
} }
if (!array_key_exists($subdomain, $arr_written)) { if (!array_key_exists($subdomain, $arr_written)) {
$str_result .= str_replace('{DOMAIN}', $subdomain, $arr_format['format']) . "\n"; $str_result .=
str_replace(
"{DOMAIN}",
$subdomain,
$arr_format["format"]
) . "\n";
$line_count++; $line_count++;
$arr_written[$subdomain] = 3; $arr_written[$subdomain] = 3;
} }
@ -270,8 +345,10 @@ class addressMaker{
} }
unset($arr_written); unset($arr_written);
$fp = fopen(ROOT_DIR . $arr_format['filename'], 'w'); $fp = fopen(ROOT_DIR . $arr_format["filename"], "w");
$write_len = self::write_conf_header($fp, $arr_format['header'], array('COUNT' => $line_count)); $write_len = self::write_conf_header($fp, $arr_format["header"], [
"COUNT" => $line_count,
]);
$write_len += fwrite($fp, $str_result); $write_len += fwrite($fp, $str_result);
return $write_len; return $write_len;
} }