generate-list.php 2.7 KB

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