From fbd3e1dd361e95c6c9603613db3a35eb50eb11ed Mon Sep 17 00:00:00 2001 From: pezcurrel Date: Thu, 24 Oct 2024 07:31:40 +0200 Subject: [PATCH] Refactored --- web/lib/gettlds.php | 1537 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 1504 insertions(+), 33 deletions(-) diff --git a/web/lib/gettlds.php b/web/lib/gettlds.php index c8106f3..8543b9e 100644 --- a/web/lib/gettlds.php +++ b/web/lib/gettlds.php @@ -1,44 +1,1515 @@ =3 && $tlds[0][0]=='#' && preg_match('#^\d+$#',$tlds[1])===1) { + array_shift($tlds); + $ts=array_shift($tlds); + if (preg_match('#^\d+$#',$ts)===1) { + foreach ($tlds as $val) { + if (preg_match('#^[a-z\-\d]+$#',$val)!==1) { + $warnings[]="TLDs cache file «{$cachefp}» had unexpected format (3)"; + $tlds=false; + break; + } + } + } else { + $warnings[]="TLDs cache file «{$cachefp}» had unexpected format (2)"; + $tlds=false; + } } else { - $ldlt=rtrim($ldlt); - if (preg_match('#^\d+$#',$ldlt)!==1) - $ldlt=0; - else - $ldlt+=0; + $warnings[]="TLDs cache file «{$cachefp}» had unexpected format (1)"; + $tlds=false; } - if ($now-$ldlt>86400 || !file_exists($tldsfp)) {// if more than 1 day has passed since last list dl or list file can't be found + $now=time(); +// if cache file doesn't exist, is not readable or has wrong format, or if its timestamp is more than 1 day old, try to load tlds from the web + if ($tlds===false || $now-$ts>86400) { $url='https://data.iana.org/TLD/tlds-alpha-by-domain.txt'; - $buf=@file_get_contents($url); - if ($buf===false) - echo "gettlds: could not download «{$url}»\n"; - elseif (@file_put_contents($tldsfp,$buf)===false) - echo "gettlds: could not save «{$tldsfp}»\n"; - elseif (@file_put_contents($ldlfp,$now."\n")===false) - echo "gettlds: could not save «{$ldlfp}»\n"; + $tlds=@file($url,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); + if ($tlds===false) { + $warnings[]="could not load TLDs from «{$url}»"; + } else { + if ($tlds[0][0]=='#') array_shift($tlds);// remove first line if it's comment + foreach ($tlds as $key=>$val) { + if (preg_match('#^[A-Z\-\d]+$#',$val)!==1) { + echo "{$val}\n"; + $tlds=false; + break; + } else { + $tlds[$key]=strtolower($val); + } + } + if ($tlds===false) $warnings[]="TLDs from «{$url}» had unexpected format"; + } } - if (!isset($buf)) - $buf=@file_get_contents($tldsfp); - if ($buf!==false) { - $tlds=[]; - $buf=explode("\n",$buf); - foreach ($buf as $val) - if (trim($val)!=='' && $val[0]!='#') - $tlds[]=$val; + if ($tlds===false) { + $tlds=$deftlds; + $warnings[]='resorted to embedded TLDs (1)'; + } else { + if (@file_put_contents($cachefp,"# This file is automatically generated, please do not modify it\n{$now}\n".implode("\n",$tlds)."\n")===false) { + $warnings[]="could not write TLDs into cache file «{$cachefp}»"; + $tlds=$deftlds; + $warnings[]='resorted to embedded TLDs (2)'; + } +// uncomment next line to output tlds for $deftlds +// echo "'".implode("',\n'",$tlds)."'\n"; + } + if ($regexformat) { rsort($tlds); - foreach ($tlds as $key=>$val) - $tlds[$key]=strtolower($val); + $tlds=implode('|',$tlds); } - return $tlds; + (count($warnings)==0) ? $warnings=null : $warnings=implode('; ',$warnings); + return ['tlds'=>$tlds, 'warnings'=>$warnings]; } ?>