} if (!is_writable(ZF2_HOSTNAME_VALIDATOR_FILE)) { printf("Error: Cannot update file '%s'%s", ZF2_HOSTNAME_VALIDATOR_FILE, PHP_EOL); exit(1); } // get current list of official TLDs $client = new Client(); $client->setOptions(['adapter' => 'Zend\\Http\\Client\\Adapter\\Curl']); $client->setUri(IANA_URL); $client->setMethod('GET'); $response = $client->send(); if (!$response->isSuccess()) { printf("Error: cannot get '%s'%s", IANA_URL, PHP_EOL); exit(1); } $decodePunycode = getPunycodeDecoder(); // Get new TLDs from the list previously fetched $newValidTlds = []; foreach (preg_grep('/^[^#]/', preg_split("#\r?\n#", $response->getBody())) as $line) { $newValidTlds[] = sprintf("%s'%s',\n", str_repeat(' ', 8), $decodePunycode(strtolower($line))); } $newFileContent = []; // new file content $insertDone = false; // becomes 'true' when we find start of $validTlds declaration $insertFinish = false; // becomes 'true' when we find end of $validTlds declaration foreach (file(ZF2_HOSTNAME_VALIDATOR_FILE) as $line) { if ($insertDone === $insertFinish) { // Outside of $validTlds definition; keep line as-is $newFileContent[] = $line;
/** * Extract new Valid TLDs from a string containing one per line. * * @param string $string * @return array */ function getNewValidTlds($string) { $decodePunycode = getPunycodeDecoder(); // Get new TLDs from the list previously fetched $newValidTlds = []; foreach (preg_grep('/^[^#]/', preg_split("#\r?\n#", $string)) as $line) { $newValidTlds[] = sprintf("%s'%s',\n", str_repeat(' ', 8), $decodePunycode(strtolower($line))); } return $newValidTlds; }