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 * @uses extension pdo_sqlite
36 * @pluginDesc Provides information for a top level domain.
38 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
41 * connection to the database
46 * Some fixed TLD values, keys must be lowercase
49 protected static $fixedTlds;
52 * Prepared statement for selecting a single tld
58 * Prepared statement for selecting all tlds
64 * Checks for dependencies, sets up database and hard coded values
68 public function onLoad()
70 $help = $this->getPluginHandler()->getPlugin('Help');
71 $help->register($this);
73 if (!is_array(self::$fixedTlds)) {
74 self::$fixedTlds = array(
75 'phergie' => 'You can find Phergie at http://www.phergie.org',
76 'spoon' => 'Don\'t you know? There is no spoon!',
77 'poo' => 'Do you really think that\'s funny?',
78 'root' => 'Diagnostic marker to indicate '
79 . 'a root zone load was not truncated.'
83 $dbFile = dirname(__FILE__) . '/Tld/tld.db';
85 $this->db = new PDO('sqlite:' . $dbFile);
87 $this->select = $this->db->prepare('
88 SELECT type, description
90 WHERE LOWER(tld) = LOWER(:tld)
93 $this->selectAll = $this->db->prepare('
94 SELECT tld, type, description
97 } catch (PDOException $e) {
98 $this->getPluginHandler()->removePlugin($this);
103 * takes a tld in the format '.tld' and returns its related data
105 * @param string $tld tld to process
109 * @pluginCmd .[tld] request details about the tld
111 public function onCommandTld($tld)
113 $tld = ltrim($tld, '.');
114 $description = $this->getTld($tld);
116 $this->event->getSource(),
117 "{$this->getEvent()->getNick()}: .{$tld} -> "
118 . ($description ? $description : 'Unknown TLD')
123 * Retrieves the definition for a given TLD if it exists
125 * @param string $tld TLD to search for
127 * @return string Definition of the given TLD
129 public function getTld($tld)
131 $tld = trim(strtolower($tld));
132 if (isset(self::$fixedTlds[$tld])) {
133 return self::$fixedTlds[$tld];
135 if ($this->select->execute(array('tld' => $tld))) {
136 $tlds = $this->select->fetch();
137 if (is_array($tlds)) {
138 return '(' . $tlds['type'] . ') ' . $tlds['description'];
146 * Retrieves a list of all the TLDs and their definitions
148 * @return array Array of all the TLDs and their definitions
150 public function getTlds()
152 if ($this->selectAll->execute()) {
153 $tlds = $this->selectAll->fetchAll();
154 if (is_array($tlds)) {
156 foreach ($tlds as $key => $tld) {
157 if (!empty($tld['tld'])) {
158 $tldinfo[$tld['tld']] = "({$tld['type']}) "
159 . $tld['description'];