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 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
38 * Connection to the database
45 * Prepared statement for selecting a single TLD
52 * Prepared statement for selecting all TLDs
59 * Checks for dependencies and sets up the database and hard-coded values.
63 public function onLoad()
65 if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
66 $this->fail('PDO and pdo_sqlite extensions must be installed');
69 $dbFile = dirname(__FILE__) . '/Tld/tld.db';
71 $this->db = new PDO('sqlite:' . $dbFile);
73 $this->select = $this->db->prepare('
74 SELECT type, description
76 WHERE LOWER(tld) = LOWER(:tld)
79 $this->selectAll = $this->db->prepare('
80 SELECT tld, type, description
83 } catch (PDOException $e) {
84 $this->getPluginHandler()->removePlugin($this);
89 * takes a tld in the format '.tld' and returns its related data
91 * @param string $tld tld to process
95 public function onCommandTld($tld)
97 $tld = ltrim($tld, '.');
98 $description = $this->getTld($tld);
100 $this->event->getSource(),
101 "{$this->getEvent()->getNick()}: .{$tld} -> "
102 . ($description ? $description : 'Unknown TLD')
107 * Retrieves the definition for a given TLD if it exists
109 * @param string $tld TLD to search for
111 * @return mixed Definition of the given TLD as a string or false if unknown
113 public function getTld($tld)
115 $tld = trim(strtolower($tld));
116 if ($this->select->execute(array('tld' => $tld))) {
117 $tlds = $this->select->fetch();
118 if (is_array($tlds)) {
119 return '(' . $tlds['type'] . ') ' . $tlds['description'];
126 * Retrieves a list of all the TLDs and their definitions
128 * @return mixed Array of all the TLDs and their definitions or FALSE on
131 public function getTlds()
133 if ($this->selectAll->execute()) {
134 $tlds = $this->selectAll->fetchAll();
135 if (is_array($tlds)) {
137 foreach ($tlds as $key => $tld) {
138 if (!empty($tld['tld'])) {
139 $tldinfo[$tld['tld']] = "({$tld['type']}) "
140 . $tld['description'];