]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Tld.php
Merge remote branch 'statusnet/1.0.x' into irc-plugin
[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  *
34  * @pluginDesc Provides information for a top level domain.
35  */
36 class Phergie_Plugin_Tld extends Phergie_Plugin_Abstract
37 {
38     /**
39      * connection to the database
40      * @var PDO
41      */
42     protected $db;
43     /**
44      * Some fixed TLD values, keys must be lowercase
45      * @var array
46      */
47     protected static $fixedTlds;
48
49     /**
50      * Prepared statement for selecting a single tld
51      * @var PDOStatement
52      */
53     protected $select;
54
55     /**
56      * Prepared statement for selecting all tlds
57      * @var PDOStatement
58      */
59     protected $selectAll;
60
61     /**
62      * Checks for dependencies, sets up database and hard coded values
63      *
64      * @return void
65      */
66     public function onLoad()
67     {
68         $help = $this->getPluginHandler()->getPlugin('Help');
69         $help->register($this);
70
71         if (!is_array(self::$fixedTlds)) {
72             self::$fixedTlds = array(
73                 'phergie' => 'You can find Phergie at http://www.phergie.org',
74                 'spoon'   => 'Don\'t you know? There is no spoon!',
75                 'poo'     => 'Do you really think that\'s funny?',
76                 'root'    => 'Diagnostic marker to indicate '
77                 . 'a root zone load was not truncated.'
78             );
79         }
80
81         try {
82             $dbFile = dirname(__FILE__) . '/Tld/tld.db';
83             $dbManager = new Phergie_Db_Sqlite($dbFile);
84             $this->db = $dbManager->getDb();
85             if (!$dbManager->hasTable('tld')) {
86                 $query = 'CREATE TABLE tld ('
87                         . 'tld VARCHAR(20), '
88                         . 'type VARCHAR(20), '
89                         . 'description VARCHAR(255))';
90
91                 $this->db->exec($query);
92
93                 // prepare a statement to populate the table with
94                 // tld information
95                 $insert = $this->db->prepare(
96                     'INSERT INTO tld
97                     (tld, type, description)
98                     VALUES (:tld, :type, :description)'
99                 );
100
101                 // grab tld data from iana.org...
102                 $contents = file_get_contents(
103                     'http://www.iana.org/domains/root/db/'
104                 );
105
106                 // ...and then parse it out
107                 $regex = '{<tr class="iana-group[^>]*><td><a[^>]*>\s*\.?([^<]+)\s*'
108                         . '(?:<br/><span[^>]*>[^<]*</span>)?</a></td><td>\s*'
109                         . '([^<]+)\s*</td><td>\s*([^<]+)\s*}i';
110                 preg_match_all($regex, $contents, $matches, PREG_SET_ORDER);
111
112                 foreach ($matches as $match) {
113                     list(, $tld, $type, $description) = array_pad($match, 4, null);
114                     $type = trim(strtolower($type));
115                     if ($type != 'test') {
116                         $tld = trim(strtolower($tld));
117                         $description = trim($description);
118
119                         switch ($tld) {
120
121                         case 'com':
122                             $description = 'Commercial';
123                             break;
124
125                         case 'info':
126                             $description = 'Information';
127                             break;
128
129                         case 'net':
130                             $description = 'Network';
131                             break;
132
133                         case 'org':
134                             $description = 'Organization';
135                             break;
136
137                         case 'edu':
138                             $description = 'Educational';
139                             break;
140
141                         case 'name':
142                             $description = 'Individuals, by name';
143                             break;
144                         }
145
146                         if (empty($tld) || empty($description)) {
147                             continue;
148                         }
149
150                         $regex = '{(^(?:Reserved|Restricted)\s*(?:exclusively\s*)?'
151                                  . '(?:for|to)\s*(?:members of\s*)?(?:the|support)?'
152                                  . '\s*|\s*as advised.*$)}i';
153                         $description = preg_replace($regex, '', $description);
154                         $description = ucfirst(trim($description));
155
156                         $data = array_map(
157                             'html_entity_decode', array(
158                                 'tld' => $tld,
159                                 'type' => $type,
160                                 'description' => $description
161                             )
162                         );
163
164                         $insert->execute($data);
165                     }
166                 }
167
168                 unset(
169                     $insert,
170                     $matches,
171                     $match,
172                     $contents,
173                     $tld,
174                     $type,
175                     $description,
176                     $data,
177                     $regex
178                 );
179             }
180
181             // Create a prepared statements for retrieving TLDs
182             $this->select = $this->db->prepare(
183                 'SELECT type, description '
184                 . 'FROM tld WHERE LOWER(tld) = LOWER(:tld)'
185             );
186
187             $this->selectAll = $this->db->prepare(
188                 'SELECT tld, type, description FROM tld'
189             );
190         } catch (PDOException $e) {
191         }
192     }
193
194     /**
195      * takes a tld in the format '.tld' and returns its related data
196      *
197      * @param string $tld tld to process
198      *
199      * @return null
200      *
201      * @pluginCmd .[tld] request details about the tld
202      */
203     public function onCommandTld($tld)
204     {
205         $tld = ltrim($tld, '.');
206         $description = $this->getTld($tld);
207         $this->doPrivmsg(
208             $this->event->getSource(),
209             "{$this->getEvent()->getNick()}: .{$tld} -> "
210             . ($description ? $description : 'Unknown TLD')
211         );
212     }
213
214     /**
215      * Retrieves the definition for a given TLD if it exists
216      *
217      * @param string $tld TLD to search for
218      *
219      * @return string Definition of the given TLD
220      */
221     public function getTld($tld)
222     {
223         $tld = trim(strtolower($tld));
224         if (isset(self::$fixedTlds[$tld])) {
225             return self::$fixedTlds[$tld];
226         } else {
227             if ($this->select->execute(array('tld' => $tld))) {
228                 $tlds = $this->select->fetch();
229                 if (is_array($tlds)) {
230                     return '(' . $tlds['type'] . ') ' . $tlds['description'];
231                 }
232             }
233         }
234         return false;
235     }
236
237     /**
238      * Retrieves a list of all the TLDs and their definitions
239      *
240      * @return array Array of all the TLDs and their definitions
241      */
242     public function getTlds()
243     {
244         if ($this->selectAll->execute()) {
245             $tlds = $this->selectAll->fetchAll();
246             if (is_array($tlds)) {
247                 $tldinfo = array();
248                 foreach ($tlds as $key => $tld) {
249                     if (!empty($tld['tld'])) {
250                         $tldinfo[$tld['tld']] = "({$tld['type']}) "
251                         . $tld['description'];
252                     }
253                 }
254                 unset($tlds);
255                 return $tldinfo;
256             }
257         }
258         return false;
259     }
260 }
261