Skip to content

Commit

Permalink
Merge pull request #15 from apeschar/safe-import
Browse files Browse the repository at this point in the history
Safe import
  • Loading branch information
eldadfux authored Jul 17, 2022
2 parents fa3a692 + 1b348f8 commit 5647020
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions data/import.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
<?php

$list = explode("\n", file_get_contents('https://publicsuffix.org/list/public_suffix_list.dat'));
$data = file_get_contents('https://publicsuffix.org/list/public_suffix_list.dat');
if ($data === false) {
throw new RuntimeException("Could not download public suffix list");
}

$list = explode("\n", $data);

function arrayToCode(array $data, $level = 0):string {
$output = '['."\n";

$level++;
$tabs = str_repeat ("\t", $level);

$tabs = str_repeat("\t", $level);

foreach($data as $key => $node) {
$key = is_integer($key) ? '' : '\''.$key.'\' => ';
$value = (is_array($node)) ? arrayToCode($node, $level) : str_replace("'", "\'", $node);
$output .= $tabs.$key.((is_string($node)) ? '\''.$value.'\'' : $value).(($key !== array_key_last($data)) ? ', ' : '')."\n";
$key = is_integer($key) ? '' : var_export($key, true) . ' => ';
$value = is_array($node) ? arrayToCode($node, $level) : var_export($node, true);
$output .= $tabs.$key.$value.",\n";
}

$level--;

$tabs = str_repeat ("\t", $level);
$tabs = str_repeat("\t", $level);

$output .= $tabs.']';

Expand All @@ -34,18 +39,18 @@ function arrayToCode(array $data, $level = 0):string {
$comments = [];
continue;
}

if(mb_strpos($line, '===END ICANN DOMAINS===')) {
$type = null;
continue;
}

if(mb_strpos($line, '===BEGIN PRIVATE DOMAINS===')) {
$type = 'PRIVATE';
$comments = [];
continue;
}

if(mb_strpos($line, '===END PRIVATE DOMAINS===')) {
$type = null;
continue;
Expand All @@ -69,4 +74,8 @@ function arrayToCode(array $data, $level = 0):string {
$comments = [];
}

if (!isset($domains['com'])) {
throw new RuntimeException(".com is missing from public suffix list; it must be corrupted");
}

file_put_contents(__DIR__.'/data.php', "<?php\n\nreturn ".arrayToCode($domains).';');

0 comments on commit 5647020

Please sign in to comment.