]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Tld.php
Revert "Merged in Phergie changes"
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Tld.php
1 <?php
2
3 /**
4  * Phergie
5  *
6  * PHP version 5
7  *
8  * LICENSE
9  *
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
14  *
15  * @category  Phergie
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
21  */
22
23 /**
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.
26  *
27  * @category Phergie
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     extension PDO
33  * @uses     extension pdo_sqlite
34  *
35  * @pluginDesc Provides information for a top level domain.
36  */
37 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
38 {
39     /**
40      * connection to the database
41      * @var PDO
42      */
43     protected $db;
44
45     /**
46      * Prepared statement for selecting a single tld
47      * @var PDOStatement
48      */
49     protected $select;
50
51     /**
52      * Prepared statement for selecting all tlds
53      * @var PDOStatement
54      */
55     protected $selectAll;
56
57     /**
58      * Checks for dependencies, sets up database and hard coded values
59      *
60      * @return void
61      */
62     public function onLoad()
63     {
64         if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
65             $this->fail('PDO and pdo_sqlite extensions must be installed');
66         }
67
68         $help = $this->getPluginHandler()->getPlugin('Help');
69         $help->register($this);
70
71         $dbFile = dirname(__FILE__) . '/Tld/tld.db';
72         try {
73             $this->db = new PDO('sqlite:' . $dbFile);
74
75             $this->select = $this->db->prepare('
76                 SELECT type, description
77                 FROM tld
78                 WHERE LOWER(tld) = LOWER(:tld)
79             ');
80
81             $this->selectAll = $this->db->prepare('
82                 SELECT tld, type, description
83                 FROM btld
84             ');
85         } catch (PDOException $e) {
86             $this->getPluginHandler()->removePlugin($this);
87         }
88     }
89
90     /**
91      * takes a tld in the format '.tld' and returns its related data
92      *
93      * @param string $tld tld to process
94      *
95      * @return null
96      *
97      * @pluginCmd .[tld] request details about the tld
98      */
99     public function onCommandTld($tld)
100     {
101         $tld = ltrim($tld, '.');
102         $description = $this->getTld($tld);
103         $this->doPrivmsg(
104             $this->event->getSource(),
105             "{$this->getEvent()->getNick()}: .{$tld} -> "
106             . ($description ? $description : 'Unknown TLD')
107         );
108     }
109
110     /**
111      * Retrieves the definition for a given TLD if it exists
112      *
113      * @param string $tld TLD to search for
114      *
115      * @return mixed Definition of the given TLD as a string or false if unknown
116      */
117     public function getTld($tld)
118     {
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'];
124             }
125         }
126         return false;
127     }
128
129     /**
130      * Retrieves a list of all the TLDs and their definitions
131      *
132      * @return mixed Array of all the TLDs and their definitions or FALSE on
133       *        failure
134      */
135     public function getTlds()
136     {
137         if ($this->selectAll->execute()) {
138             $tlds = $this->selectAll->fetchAll();
139             if (is_array($tlds)) {
140                 $tldinfo = array();
141                 foreach ($tlds as $key => $tld) {
142                     if (!empty($tld['tld'])) {
143                         $tldinfo[$tld['tld']] = "({$tld['type']}) "
144                         . $tld['description'];
145                     }
146                 }
147                 return $tldinfo;
148             }
149         }
150         return false;
151     }
152 }