TYPO3  7.6
generate-list.php
Go to the documentation of this file.
1 <?php
10 define('LIST_URL', 'http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');
12 define('OUTPUT_FILE', dirname(__FILE__) . '/public-suffix-list.php');
13 
14 require_once 'HTTP/Request2.php';
15 
16 function buildSubdomain(&$node, $tldParts)
17 {
18  $part = trim(array_pop($tldParts));
19 
20  if (!array_key_exists($part, $node)) {
21  $node[$part] = array();
22  }
23 
24  if (0 < count($tldParts)) {
25  buildSubdomain($node[$part], $tldParts);
26  }
27 }
28 
29 function writeNode($fp, $valueTree, $key = null, $indent = 0)
30 {
31  if (is_null($key)) {
32  fwrite($fp, "return ");
33 
34  } else {
35  fwrite($fp, str_repeat(' ', $indent) . "'$key' => ");
36  }
37 
38  if (0 == ($count = count($valueTree))) {
39  fwrite($fp, 'true');
40  } else {
41  fwrite($fp, "array(\n");
42  for ($keys = array_keys($valueTree), $i = 0; $i < $count; $i++) {
43  writeNode($fp, $valueTree[$keys[$i]], $keys[$i], $indent + 1);
44  if ($i + 1 != $count) {
45  fwrite($fp, ",\n");
46  } else {
47  fwrite($fp, "\n");
48  }
49  }
50  fwrite($fp, str_repeat(' ', $indent) . ")");
51  }
52 }
53 
54 
55 try {
56  $request = new HTTP_Request2(LIST_URL);
57  $response = $request->send();
58  if (200 != $response->getStatus()) {
59  throw new Exception("List download URL returned status: " .
60  $response->getStatus() . ' ' . $response->getReasonPhrase());
61  }
62  $list = $response->getBody();
63  if (false === strpos($list, '// ===BEGIN ICANN DOMAINS===')) {
64  throw new Exception("List download URL does not contain expected phrase");
65  }
66  if (!($fp = @fopen(OUTPUT_FILE, 'wt'))) {
67  throw new Exception("Unable to open " . OUTPUT_FILE);
68  }
69 
70 } catch (Exception $e) {
71  die($e->getMessage());
72 }
73 
74 $tldTree = array();
75 $license = true;
76 
77 fwrite($fp, "<?php\n");
78 
79 foreach (array_filter(array_map('trim', explode("\n", $list))) as $line) {
80  if ('//' != substr($line, 0, 2)) {
81  buildSubdomain($tldTree, explode('.', $line));
82 
83  } elseif ($license) {
84  if (0 === strpos($line, "// ===BEGIN ICANN DOMAINS===")) {
85  fwrite($fp, "\n");
86  $license = false;
87  } else {
88  fwrite($fp, $line . "\n");
89  }
90  }
91 }
92 
93 writeNode($fp, $tldTree);
94 fwrite($fp, ";\n?>");
95 fclose($fp);
96 ?>