phergie /Phergie/Plugin/Tld/db.php

Language PHP Lines 88
MD5 Hash d295168f7b901195d45e862689a2d774 Estimated Cost $1,359 (why?)
Repository https://github.com/markizano/phergie.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
 * Phergie
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

$dbFile = 'tld.db';

if (file_exists($dbFile)) {
    exit;
}

$db = new PDO('sqlite:' . dirname(__FILE__) . '/' . $dbFile);

$query = '
    CREATE TABLE tld (
        tld VARCHAR(20),
        type VARCHAR(20),
        description VARCHAR(255)
    )
';
$db->exec($query);

$insert = $db->prepare(
    'INSERT INTO tld (tld, type, description)
    VALUES (:tld, :type, :description)'
);

$contents = file_get_contents(
    'http://www.iana.org/domains/root/db/'
);

libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->loadHTML($contents);
libxml_clear_errors();

$descriptions = array(
    'com' => 'Commercial',
    'info' => 'Information',
    'net' => 'Network',
    'org' => 'Organization',
    'edu' => 'Educational',
    'name' => 'Individuals, by name'
);

$xpath = new DOMXPath($doc);
$rows = $xpath->query('//tr[contains(@class, "iana-group")]');
foreach (range(0, $rows->length - 1) as $index) {
    $row = $rows->item($index);
    $tld = strtolower(ltrim($row->childNodes->item(0)->textContent, '.'));
    $type = $row->childNodes->item(1)->nodeValue;
    if (isset($descriptions[$tld])) {
        $description = $descriptions[$tld];
    } else {
        $description = $row->childNodes->item(2)->textContent;
        $regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
         . '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
         . '\s*|\s*as advised.*$)}i';
        $description = preg_replace($regex, '', $description);
        $description = ucfirst(trim($description));
    }
    $data = array_map(
        'html_entity_decode',
        array(
            'tld' => $tld,
            'type' => $type,
            'description' => $description
        )
    );
    $insert->execute($data);
}
Back to Top