]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Tld.php
Merged in changes to Phergie
[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     Phergie_Plugin_Http pear.phergie.org
33  * @uses     extension PDO
34  * @uses     extension pdo_sqlite
35  *
36  * @pluginDesc Provides information for a top level domain.
37  */
38 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
39 {
40     /**
41      * connection to the database
42      * @var PDO
43      */
44     protected $db;
45     /**
46      * Some fixed TLD values, keys must be lowercase
47      * @var array
48      */
49     protected static $fixedTlds;
50
51     /**
52      * Prepared statement for selecting a single tld
53      * @var PDOStatement
54      */
55     protected $select;
56
57     /**
58      * Prepared statement for selecting all tlds
59      * @var PDOStatement
60      */
61     protected $selectAll;
62
63     /**
64      * Checks for dependencies, sets up database and hard coded values
65      *
66      * @return void
67      */
68     public function onLoad()
69     {
70         $help = $this->getPluginHandler()->getPlugin('Help');
71         $help->register($this);
72
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.'
80             );
81         }
82
83         $dbFile = dirname(__FILE__) . '/Tld/tld.db';
84         try {
85             $this->db = new PDO('sqlite:' . $dbFile);
86
87             $this->select = $this->db->prepare('
88                 SELECT type, description
89                 FROM tld
90                 WHERE LOWER(tld) = LOWER(:tld)
91             ');
92
93             $this->selectAll = $this->db->prepare('
94                 SELECT tld, type, description
95                 FROM btld
96             ');
97         } catch (PDOException $e) {
98             $this->getPluginHandler()->removePlugin($this);
99         }
100     }
101
102     /**
103      * takes a tld in the format '.tld' and returns its related data
104      *
105      * @param string $tld tld to process
106      *
107      * @return null
108      *
109      * @pluginCmd .[tld] request details about the tld
110      */
111     public function onCommandTld($tld)
112     {
113         $tld = ltrim($tld, '.');
114         $description = $this->getTld($tld);
115         $this->doPrivmsg(
116             $this->event->getSource(),
117             "{$this->getEvent()->getNick()}: .{$tld} -> "
118             . ($description ? $description : 'Unknown TLD')
119         );
120     }
121
122     /**
123      * Retrieves the definition for a given TLD if it exists
124      *
125      * @param string $tld TLD to search for
126      *
127      * @return string Definition of the given TLD
128      */
129     public function getTld($tld)
130     {
131         $tld = trim(strtolower($tld));
132         if (isset(self::$fixedTlds[$tld])) {
133             return self::$fixedTlds[$tld];
134         } else {
135             if ($this->select->execute(array('tld' => $tld))) {
136                 $tlds = $this->select->fetch();
137                 if (is_array($tlds)) {
138                     return '(' . $tlds['type'] . ') ' . $tlds['description'];
139                 }
140             }
141         }
142         return false;
143     }
144
145     /**
146      * Retrieves a list of all the TLDs and their definitions
147      *
148      * @return array Array of all the TLDs and their definitions
149      */
150     public function getTlds()
151     {
152         if ($this->selectAll->execute()) {
153             $tlds = $this->selectAll->fetchAll();
154             if (is_array($tlds)) {
155                 $tldinfo = array();
156                 foreach ($tlds as $key => $tld) {
157                     if (!empty($tld['tld'])) {
158                         $tldinfo[$tld['tld']] = "({$tld['type']}) "
159                         . $tld['description'];
160                     }
161                 }
162                 unset($tlds);
163                 return $tldinfo;
164             }
165         }
166         return false;
167     }
168 }