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
33 * @uses extension pdo_sqlite
35 * @pluginDesc Provides information for a top level domain.
37 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
40 * connection to the database
46 * Prepared statement for selecting a single tld
52 * Prepared statement for selecting all tlds
58 * Checks for dependencies, sets up database and hard coded values
62 public function onLoad()
64 if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
65 $this->fail('PDO and pdo_sqlite extensions must be installed');
68 $help = $this->getPluginHandler()->getPlugin('Help');
69 $help->register($this);
71 $dbFile = dirname(__FILE__) . '/Tld/tld.db';
73 $this->db = new PDO('sqlite:' . $dbFile);
75 $this->select = $this->db->prepare('
76 SELECT type, description
78 WHERE LOWER(tld) = LOWER(:tld)
81 $this->selectAll = $this->db->prepare('
82 SELECT tld, type, description
85 } catch (PDOException $e) {
86 $this->getPluginHandler()->removePlugin($this);
91 * takes a tld in the format '.tld' and returns its related data
93 * @param string $tld tld to process
97 * @pluginCmd .[tld] request details about the tld
99 public function onCommandTld($tld)
101 $tld = ltrim($tld, '.');
102 $description = $this->getTld($tld);
104 $this->event->getSource(),
105 "{$this->getEvent()->getNick()}: .{$tld} -> "
106 . ($description ? $description : 'Unknown TLD')
111 * Retrieves the definition for a given TLD if it exists
113 * @param string $tld TLD to search for
115 * @return mixed Definition of the given TLD as a string or false if unknown
117 public function getTld($tld)
119 $tld = trim(strtolower($tld));
120 if ($this->select->execute(array('tld' => $tld))) {
121 $tlds = $this->select->fetch();
122 if (is_array($tlds)) {
123 return '(' . $tlds['type'] . ') ' . $tlds['description'];
130 * Retrieves a list of all the TLDs and their definitions
132 * @return mixed Array of all the TLDs and their definitions or FALSE on
135 public function getTlds()
137 if ($this->selectAll->execute()) {
138 $tlds = $this->selectAll->fetchAll();
139 if (is_array($tlds)) {
141 foreach ($tlds as $key => $tld) {
142 if (!empty($tld['tld'])) {
143 $tldinfo[$tld['tld']] = "({$tld['type']}) "
144 . $tld['description'];