3 * Helper file for downloading Public Suffix List and converting it to PHP array
\r
5 * You can run this script to update PSL to the current version instead of
\r
6 * waiting for a new release of HTTP_Request2.
\r
8 * @version SVN: $Id: generate-list.php 308480 2011-02-19 11:27:13Z avb $
\r
11 /** URL to download Public Suffix List from */
\r
12 define('LIST_URL', 'http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');
\r
13 /** Name of PHP file to write */
\r
14 define('OUTPUT_FILE', dirname(__FILE__) . '/public-suffix-list.php');
\r
16 require_once 'HTTP/Request2.php';
\r
18 function buildSubdomain(&$node, $tldParts)
\r
20 $part = trim(array_pop($tldParts));
\r
22 if (!array_key_exists($part, $node)) {
\r
23 $node[$part] = array();
\r
26 if (0 < count($tldParts)) {
\r
27 buildSubdomain($node[$part], $tldParts);
\r
31 function writeNode($fp, $valueTree, $key = null, $indent = 0)
\r
33 if (is_null($key)) {
\r
34 fwrite($fp, "return ");
\r
37 fwrite($fp, str_repeat(' ', $indent) . "'$key' => ");
\r
40 if (0 == ($count = count($valueTree))) {
\r
41 fwrite($fp, 'true');
\r
43 fwrite($fp, "array(\n");
\r
44 for ($keys = array_keys($valueTree), $i = 0; $i < $count; $i++) {
\r
45 writeNode($fp, $valueTree[$keys[$i]], $keys[$i], $indent + 1);
\r
46 if ($i + 1 != $count) {
\r
52 fwrite($fp, str_repeat(' ', $indent) . ")");
\r
58 $request = new HTTP_Request2(LIST_URL);
\r
59 $response = $request->send();
\r
60 if (200 != $response->getStatus()) {
\r
61 throw new Exception("List download URL returned status: " .
\r
62 $response->getStatus() . ' ' . $response->getReasonPhrase());
\r
64 $list = $response->getBody();
\r
65 if (false === strpos($list, 'The Original Code is the Public Suffix List.')) {
\r
66 throw new Exception("List download URL does not contain expected phrase");
\r
68 if (!($fp = @fopen(OUTPUT_FILE, 'wt'))) {
\r
69 throw new Exception("Unable to open " . OUTPUT_FILE);
\r
72 } catch (Exception $e) {
\r
73 die($e->getMessage());
\r
79 fwrite($fp, "<?php\n");
\r
81 foreach (array_filter(array_map('trim', explode("\n", $list))) as $line) {
\r
82 if ('//' != substr($line, 0, 2)) {
\r
83 buildSubdomain($tldTree, explode('.', $line));
\r
85 } elseif ($license) {
\r
86 fwrite($fp, $line . "\n");
\r
88 if (0 === strpos($line, "// ***** END LICENSE BLOCK")) {
\r
95 writeNode($fp, $tldTree);
\r
96 fwrite($fp, ";\n?>");
\r