10 * This source file is subject to the new BSD license that is bundled
11 * with this package in the file LICENSE.
12 * It is also available through the world-wide-web at this URL:
13 * http://phergie.org/license
16 * @package Phergie_Plugin_Url
17 * @author Phergie Development Team <team@phergie.org>
18 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
19 * @license http://phergie.org/license New BSD License
20 * @link http://pear.phergie.org/package/Phergie_Plugin_Url
24 * Responds to a request for a TLD (formatted as .tld where tld is the TLD to
25 * be looked up) with its corresponding description.
28 * @package Phergie_Plugin_Tld
29 * @author Phergie Development Team <team@phergie.org>
30 * @license http://phergie.org/license New BSD License
31 * @link http://pear.phergie.org/package/Phergie_Plugin_Tld
32 * @uses Phergie_Plugin_Http pear.phergie.org
34 * @pluginDesc Provides information for a top level domain.
36 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
39 * connection to the database
44 * Some fixed TLD values, keys must be lowercase
47 protected static $fixedTlds;
50 * Prepared statement for selecting a single tld
56 * Prepared statement for selecting all tlds
62 * Checks for dependencies, sets up database and hard coded values
66 public function onLoad()
68 $help = $this->getPluginHandler()->getPlugin('Help');
69 $help->register($this);
71 if (!is_array(self::$fixedTlds)) {
72 self::$fixedTlds = array(
73 'phergie' => 'You can find Phergie at http://www.phergie.org',
74 'spoon' => 'Don\'t you know? There is no spoon!',
75 'poo' => 'Do you really think that\'s funny?',
76 'root' => 'Diagnostic marker to indicate '
77 . 'a root zone load was not truncated.'
82 $dbFile = dirname(__FILE__) . '/Tld/tld.db';
83 $dbManager = new Phergie_Db_Sqlite($dbFile);
84 $this->db = $dbManager->getDb();
85 if (!$dbManager->hasTable('tld')) {
86 $query = 'CREATE TABLE tld ('
88 . 'type VARCHAR(20), '
89 . 'description VARCHAR(255))';
91 $this->db->exec($query);
93 // prepare a statement to populate the table with
95 $insert = $this->db->prepare(
97 (tld, type, description)
98 VALUES (:tld, :type, :description)'
101 // grab tld data from iana.org...
102 $contents = file_get_contents(
103 'http://www.iana.org/domains/root/db/'
106 // ...and then parse it out
107 $regex = '{<tr class="iana-group[^>]*><td><a[^>]*>\s*\.?([^<]+)\s*'
108 . '(?:<br/><span[^>]*>[^<]*</span>)?</a></td><td>\s*'
109 . '([^<]+)\s*</td><td>\s*([^<]+)\s*}i';
110 preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
112 foreach ($matches as $match) {
113 list(, $tld, $type, $description) = array_pad($match, 4, null);
114 $type = trim(strtolower($type));
115 if ($type != 'test') {
116 $tld = trim(strtolower($tld));
117 $description = trim($description);
122 $description = 'Commercial';
126 $description = 'Information';
130 $description = 'Network';
134 $description = 'Organization';
138 $description = 'Educational';
142 $description = 'Individuals, by name';
146 if (empty($tld) || empty($description)) {
150 $regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
151 . '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
152 . '\s*|\s*as advised.*$)}i';
153 $description = preg_replace($regex, '', $description);
154 $description = ucfirst(trim($description));
157 'html_entity_decode', array(
160 'description' => $description
164 $insert->execute($data);
181 // Create a prepared statements for retrieving TLDs
182 $this->select = $this->db->prepare(
183 'SELECT type, description '
184 . 'FROM tld WHERE LOWER(tld) = LOWER(:tld)'
187 $this->selectAll = $this->db->prepare(
188 'SELECT tld, type, description FROM tld'
190 } catch (PDOException $e) {
195 * takes a tld in the format '.tld' and returns its related data
197 * @param string $tld tld to process
201 * @pluginCmd .[tld] request details about the tld
203 public function onCommandTld($tld)
205 $tld = ltrim($tld, '.');
206 $description = $this->getTld($tld);
208 $this->event->getSource(),
209 "{$this->getEvent()->getNick()}: .{$tld} -> "
210 . ($description ? $description : 'Unknown TLD')
215 * Retrieves the definition for a given TLD if it exists
217 * @param string $tld TLD to search for
219 * @return string Definition of the given TLD
221 public function getTld($tld)
223 $tld = trim(strtolower($tld));
224 if (isset(self::$fixedTlds[$tld])) {
225 return self::$fixedTlds[$tld];
227 if ($this->select->execute(array('tld' => $tld))) {
228 $tlds = $this->select->fetch();
229 if (is_array($tlds)) {
230 return '(' . $tlds['type'] . ') ' . $tlds['description'];
238 * Retrieves a list of all the TLDs and their definitions
240 * @return array Array of all the TLDs and their definitions
242 public function getTlds()
244 if ($this->selectAll->execute()) {
245 $tlds = $this->selectAll->fetchAll();
246 if (is_array($tlds)) {
248 foreach ($tlds as $key => $tld) {
249 if (!empty($tld['tld'])) {
250 $tldinfo[$tld['tld']] = "({$tld['type']}) "
251 . $tld['description'];