]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/data/generate-list.php
Merge branch 'master' of git.gnu.io:gnu/gnu-social into mmn_fixes
[quix0rs-gnu-social.git] / extlib / data / generate-list.php
1 <?php\r
2 /**\r
3  * Helper file for downloading Public Suffix List and converting it to PHP array\r
4  *\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
7  *\r
8  * @version SVN: $Id: generate-list.php 308480 2011-02-19 11:27:13Z avb $\r
9  */\r
10 \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
15 \r
16 require_once 'HTTP/Request2.php';\r
17 \r
18 function buildSubdomain(&$node, $tldParts)\r
19 {\r
20     $part = trim(array_pop($tldParts));\r
21 \r
22     if (!array_key_exists($part, $node)) {\r
23         $node[$part] = array();\r
24     }\r
25 \r
26     if (0 < count($tldParts)) {\r
27         buildSubdomain($node[$part], $tldParts);\r
28     }\r
29 }\r
30 \r
31 function writeNode($fp, $valueTree, $key = null, $indent = 0)\r
32 {\r
33     if (is_null($key)) {\r
34         fwrite($fp, "return ");\r
35 \r
36     } else {\r
37         fwrite($fp, str_repeat(' ', $indent) . "'$key' => ");\r
38     }\r
39 \r
40     if (0 == ($count = count($valueTree))) {\r
41         fwrite($fp, 'true');\r
42     } else {\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
47                 fwrite($fp, ",\n");\r
48             } else {\r
49                 fwrite($fp, "\n");\r
50             }\r
51         }\r
52         fwrite($fp, str_repeat(' ', $indent) . ")");\r
53     }\r
54 }\r
55 \r
56 \r
57 try {\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
63     }\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
67     }\r
68     if (!($fp = @fopen(OUTPUT_FILE, 'wt'))) {\r
69         throw new Exception("Unable to open " . OUTPUT_FILE);\r
70     }\r
71 \r
72 } catch (Exception $e) {\r
73     die($e->getMessage());\r
74 }\r
75 \r
76 $tldTree = array();\r
77 $license = true;\r
78 \r
79 fwrite($fp, "<?php\n");\r
80 \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
84 \r
85     } elseif ($license) {\r
86         fwrite($fp, $line . "\n");\r
87 \r
88         if (0 === strpos($line, "// ***** END LICENSE BLOCK")) {\r
89             $license = false;\r
90             fwrite($fp, "\n");\r
91         }\r
92     }\r
93 }\r
94 \r
95 writeNode($fp, $tldTree);\r
96 fwrite($fp, ";\n?>");\r
97 fclose($fp);\r
98 ?>