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