3a9b7a5f63b71c53f71e126485c5c0513c7ebe53
[Packages/TYPO3.CMS.git] / typo3 / contrib / pear / data / HTTP_Request2 / generate-list.php
1 <?php
2 /**
3 * Helper file for downloading Public Suffix List and converting it to PHP array
4 *
5 * You can run this script to update PSL to the current version instead of
6 * waiting for a new release of HTTP_Request2.
7 *
8 * @version SVN: $Id: generate-list.php 308480 2011-02-19 11:27:13Z avb $
9 */
10
11 /** URL to download Public Suffix List from */
12 define('LIST_URL', 'http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');
13 /** Name of PHP file to write */
14 define('OUTPUT_FILE', dirname(__FILE__) . '/public-suffix-list.php');
15
16 require_once 'HTTP/Request2.php';
17
18 function buildSubdomain(&$node, $tldParts)
19 {
20 $part = trim(array_pop($tldParts));
21
22 if (!array_key_exists($part, $node)) {
23 $node[$part] = array();
24 }
25
26 if (0 < count($tldParts)) {
27 buildSubdomain($node[$part], $tldParts);
28 }
29 }
30
31 function writeNode($fp, $valueTree, $key = null, $indent = 0)
32 {
33 if (is_null($key)) {
34 fwrite($fp, "return ");
35
36 } else {
37 fwrite($fp, str_repeat(' ', $indent) . "'$key' => ");
38 }
39
40 if (0 == ($count = count($valueTree))) {
41 fwrite($fp, 'true');
42 } else {
43 fwrite($fp, "array(\n");
44 for ($keys = array_keys($valueTree), $i = 0; $i < $count; $i++) {
45 writeNode($fp, $valueTree[$keys[$i]], $keys[$i], $indent + 1);
46 if ($i + 1 != $count) {
47 fwrite($fp, ",\n");
48 } else {
49 fwrite($fp, "\n");
50 }
51 }
52 fwrite($fp, str_repeat(' ', $indent) . ")");
53 }
54 }
55
56
57 try {
58 $request = new HTTP_Request2(LIST_URL);
59 $response = $request->send();
60 if (200 != $response->getStatus()) {
61 throw new Exception("List download URL returned status: " .
62 $response->getStatus() . ' ' . $response->getReasonPhrase());
63 }
64 $list = $response->getBody();
65 if (false === strpos($list, 'The Original Code is the Public Suffix List.')) {
66 throw new Exception("List download URL does not contain expected phrase");
67 }
68 if (!($fp = @fopen(OUTPUT_FILE, 'wt'))) {
69 throw new Exception("Unable to open " . OUTPUT_FILE);
70 }
71
72 } catch (Exception $e) {
73 die($e->getMessage());
74 }
75
76 $tldTree = array();
77 $license = true;
78
79 fwrite($fp, "<?php\n");
80
81 foreach (array_filter(array_map('trim', explode("\n", $list))) as $line) {
82 if ('//' != substr($line, 0, 2)) {
83 buildSubdomain($tldTree, explode('.', $line));
84
85 } elseif ($license) {
86 fwrite($fp, $line . "\n");
87
88 if (0 === strpos($line, "// ***** END LICENSE BLOCK")) {
89 $license = false;
90 fwrite($fp, "\n");
91 }
92 }
93 }
94
95 writeNode($fp, $tldTree);
96 fwrite($fp, ";\n?>");
97 fclose($fp);
98 ?>