/** * Check if the email is valid. * * @param string $email * @param bool $useExampleDomainCheck * @param bool $useTypoInDomainCheck * @param bool $useTemporaryDomainCheck * @param bool $useDnsCheck (do not use, if you don't need it) * * @return bool */ public static function isValid($email, $useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) { // must be a string if (!is_string($email)) { return false; } // make sure string length is limited to avoid DOS attacks $emailStringLength = strlen($email); if ($emailStringLength >= 320 || $emailStringLength <= 2) { return false; } unset($emailStringLength); $email = str_replace(array('.', '@'), array('.', '@'), $email); if (strpos($email, '@') === false || strpos($email, '.') === false && strpos($email, ':') === false) { return false; } if (!preg_match('/^(?<local>.*<?)(?:.*)@(?<domain>.*)(?:>?)$/', $email, $parts)) { return false; } else { $local = $parts['local']; $domain = $parts['domain']; // Escaped spaces are allowed in the "local"-part. $local = str_replace('\\ ', '', $local); // Spaces in quotes e.g. "firstname lastname"@foo.bar are also allowed in the "local"-part. $quoteHelperForIdn = false; if (preg_match('/^"(?<inner>[^"]*)"$/mU', $local, $parts)) { $quoteHelperForIdn = true; $local = trim(str_replace($parts['inner'], str_replace(' ', '', $parts['inner']), $local), '"'); } if (strpos($local, ' ') !== false || strpos($local, '".') !== false) { return false; } if (function_exists('idn_to_ascii') && UTF8::max_chr_width($email) <= 3) { $localTmp = idn_to_ascii($local); if ($localTmp) { $local = $localTmp; } unset($localTmp); $domainTmp = idn_to_ascii($domain); if ($domainTmp) { $domain = $domainTmp; } unset($domainTmp); } else { static $punycode = null; if ($punycode === null) { $punycode = new Punycode(); } $local = $punycode->encode($local); $domain = $punycode->encode($domain); } if ($quoteHelperForIdn === true) { $local = '"' . $local . '"'; } $email = $local . '@' . $domain; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { return false; } else { $valid = true; } if ($useExampleDomainCheck === true && self::isExampleDomain($domain) === true) { return false; } if ($useTypoInDomainCheck === true && self::isTypoInDomain($domain) === true) { return false; } if ($useTemporaryDomainCheck === true && self::isTemporaryDomain($domain) === true) { return false; } if ($useDnsCheck === true && self::isDnsError($domain) === true) { return false; } } return $valid; }
/** * Decode a Punycode domain name to its Unicode counterpart * * @param string $input Domain name in Punycode * * @return string Unicode domain name */ public function decode($input) { if ($this->idnSupport === true) { return idn_to_utf8($input); } return self::$punycode->decode($input); }
public function query($dmName, $force = false) { //'http://reports.internic.net/cgi/whois?whois_nic=baidu.com&type=domain'; $curl = new Curl(); $curl->setReferer($this->refererUrl); $Punycode = new Punycode(); $page = $curl->get($this->url, array('whois_nic' => $Punycode->encode($dmName), 'type' => 'domain')); return $this->parse($page); }
public function query($dmName, $force = false) { //'https://whois.cnnic.net.cn/WhoisServlet?queryType=Domain&domain=teqhost.cn'; $curl = new Curl(); $curl->setReferer($this->refererUrl); $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); $Punycode = new Punycode(); $page = $curl->get($this->url, array('domain' => $Punycode->encode($dmName), 'queryType' => 'Domain')); return $this->parse($page); }
/** * Extracts lower-case ASCII root domain from URL if it is available and valid, returns false otherwise * * @param string $url * @return string|bool */ public static function getRootDomain($url) { $parsed = self::parseUrl($url); if (isset($parsed['rootdomain']) && !empty($parsed['rootdomain'])) { $punycode = new Punycode(); $rootDomain = $punycode->encode($parsed['rootdomain']); return filter_var('http://' . $rootDomain, FILTER_VALIDATE_URL) ? strtolower($rootDomain) : false; } return false; }
public function punycode(Request $request, Texts $textsModel, functions $functions) { $Punycode = new Punycode(); $method = $request->input('method'); $source = $request->input('source'); if ($method == '') { $method = 'to_puny'; } if ($method == "to_puny") { $res = $Punycode->encode($source); } else { $res = $Punycode->decode($source); } $langLinks = $functions->giveLangLinks($request->url()); $params = $textsModel->getLocaleOf('punicode'); return view('indexuse', ['params' => $params, 'res' => $res, 'source' => $source, 'method' => $method, 'langLinks' => $langLinks]); }
/** * Checks an URL for validity, and punycode encode the returned component. * * @param string $url * @param int $component * * @return string|false */ private static function parseUrl($url, $component) { if (!isset(self::$cache[$url]['url'])) { // Strip protocol $scheme = parse_url($url, PHP_URL_SCHEME); $url = str_replace($scheme . '://', '', $url); $url = str_replace($scheme . ':', '', $url); // Punycode encode domain $host = parse_url('http://' . $url, PHP_URL_HOST); $punycode = new Punycode(); $url = str_replace($host, $punycode->encode($host), $url); // Add back normalized protocol $url = 'http://' . $url; // Remove all illegal characters from a url $url = filter_var($url, FILTER_SANITIZE_URL); // Sanity check if (($check = filter_var($url, FILTER_VALIDATE_URL)) === false) { $url = false; } self::$cache[$url]['url'] = $url; } return parse_url(strtolower(self::$cache[$url]['url']), $component); }
/** * Validate Host data before insertion into a URL host component * * @param mixed $data the data to insert * * @return array * * @throws RuntimeException If the added is invalid */ protected function validate($data) { $data = $this->validateSegment($data); if (!$data) { return $data; } $this->saveInternalEncoding(); if (!$this->isValidHostLength($data)) { $this->restoreInternalEncoding(); throw new RuntimeException('Invalid hostname, check its length'); } if (!$this->isValidHostPattern($data)) { $this->restoreInternalEncoding(); throw new RuntimeException('Invalid host label, check its content'); } if (!$this->isValidHostLabels($data)) { $this->restoreInternalEncoding(); throw new RuntimeException('Invalid host label counts, check its count'); } $data = $this->sanitizeValue($data); $data = explode($this->delimiter, $this->punycode->decode(implode($this->delimiter, $data))); $this->restoreInternalEncoding(); return $data; }
const METHOD_IANA = 'iana'; const METHOD_PUBLICSUFFIX = 'ps'; $method = METHOD_PUBLICSUFFIX; $sep = ','; use Pdp\PublicSuffixListManager; use TrueBV\Punycode; require_once './../vendor/autoload.php'; /** * @param object $obj * @return object */ function with($obj) { return $obj; } $punycode = new Punycode(); $domainParserFile = '../src/SemaltBlocker/Domainparser.php'; if ($method === METHOD_IANA) { $rootDBUrl = 'http://www.iana.org/domains/root/db'; $contents = file_get_contents($rootDBUrl); $regex = '/<span class="domain tld"><a .*>\\.(.*)<\\/a>/'; preg_match_all($regex, $contents, $matches); $tlds = explode($sep, '.' . implode($sep . '.', $matches[1])); } elseif ($method === METHOD_PUBLICSUFFIX) { $publicSuffixList = with(new PublicSuffixListManager())->getList(); $tlds = []; function glue($key, $value, $sep = ',') { if (is_array($value) && !empty($value)) { // Ignore exceptions and just return key if (key($value) === '*') {