Dict.php
13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<?php
// {{{ license
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Chandrashekhar Bhosle <cnb@php.net> |
// +----------------------------------------------------------------------+
//
// $Id$
// }}}
// {{{ includes
require_once 'PEAR.php';
require_once 'Net/Socket.php';
// }}}
// {{{ defines
define('NET_DICT_SERVER', 'dict.org');
define('NET_DICT_PORT', '2628');
// }}}
/**
*
* @author Chandrashekhar Bhosle <cnb@freedomink.org>
* @package Net
*/
class Net_Dict {
// {{{ properties
/**
* Default DICT server name
*
* @var string
*/
var $server = NET_DICT_SERVER;
/**
* Default DICT Port
*
* @var int
*/
var $port = NET_DICT_PORT;
/**
* Socket object
*
* @var object
*/
var $_socket;
/**
* Server Information
*
* @var string
*/
var $servinfo;
/**
* if caching is on or off
*
* @var boolean
*/
var $caching = false;
/**
* PEAR Cache
*
* @var object
*/
var $cache;
// }}}
// {{{ Constructor
/**
* Constructor
*/
function Net_Dict()
{
}
// }}}
// {{{ define()
/**
* Gets definitions for the specified word in the specified database.
*
* @param string $word
* @param string $database
*
* @return mixed Array of definitions if sucessful, else PEAR_Error
*/
function define($word, $database='*')
{
if ($this->caching) {
if ($defines = $this->cache->get($word, 'Net_Dict_Defs')) {
return $defines;
}
}
if (!is_object($_socket)) {
$this->connect();
}
$resp = $this->_sendCmd("DEFINE $database '$word'");
if (PEAR::isError($resp)) {
return $resp;
}
list($num) = explode(' ', $resp['text'], 2);
for ($i = 0; $i < $num; $i++) {
$resp = $this->_socket->readLine();
preg_match("/(\d{3})\s+?\"(.+)?\"\s+?(\S+)\s+?\"(.+)?\"/",
$resp, $matches);
$defines[$i]['response'] = $resp;
$defines[$i]['word'] = $matches[2];
$defines[$i]['database'] = $matches[3];
$defines[$i]['description'] = $matches[4];
$resp = $this->_getMultiline();
$defines[$i]['definition'] = $resp['text'];
}
$this->readLine(); /* discard status */
if ($this->caching) {
$this->cache->save($word, $defines, 0, 'Net_Dict_Defs');
}
return $defines;
}
// }}}
// {{{ match()
/**
* Searches an index for the dictionary, and reports words
* which were found using a particular strategy.
*
* @param string $word
* @param string $strategy
* @param string $database
*
* @return mixed Array of matches if successful, else PEAR_Error
*/
function match($word, $strategy='substring', $database='*')
{
$resp = $this->_sendCmd("MATCH $database $strategy '$word'");
if (PEAR::isError($resp)) {
return $resp;
}
$resp = $this->_getMultiLine();
$this->readLine(); /* discard status */
preg_match_all("/(\S+)?\s\"(.+?)\"/", $resp['text'], $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$matched[$i]['database'] = $matches[1][$i];
$matched[$i]['word'] = $matches[2][$i];
}
return $matched;
}
// }}}
// {{{ showDatabases()
/**
* Gets list of available databases
*
* @return mixed Array of databases if successful, else PEAR_Error
*/
function showDatabases()
{
$resp = $this->_sendCmd('SHOW DB');
if (PEAR::isError($resp)) {
return $resp;
}
$resp = $this->_getMultiLine();
$this->readLine(); /* discard status */
preg_match_all("/(\S+)?\s+?\"(.+?)\"/", $resp['text'], $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$databases[$i]['database'] = $matches[1][$i];
$databases[$i]['description'] = $matches[2][$i];
}
return $databases;
}
// }}}
// {{{ showStrategies()
/**
* Gets a list of available strategies
*
* @return mixed Array of strategies if successful, else PEAR_Error
*/
function showStrategies()
{
$resp = $this->_sendCmd('SHOW STRAT');
if (PEAR::isError($resp)) {
return $resp;
}
$resp = $this->_getMultiLine();
$this->readLine(); /* discard status */
preg_match_all("/(\S+)?\s+?\"(.+?)\"/", $resp['text'], $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$strategies[$i]['strategy'] = $matches[1][$i];
$strategies[$i]['description'] = $matches[2][$i];
}
return $strategies;
}
// }}}
// {{{ showInfo()
/**
* Gets source, copyright, and licensing information about the
* specified database.
*
* @param string $database
*
* @return mixed string if successful, else PEAR_Error
*/
function showInfo($database)
{
return $this->simpleQuery('SHOW INFO ' . $database);
}
// }}}
// {{{ showServer()
/**
* Gets local server information written by the local administrator.
* This could include information about local databases or strategies,
* or administrative information such as who to contact for access to
* databases requiring authentication.
*
* @return mixed string if sucessful, else PEAR_Error
*/
function showServer()
{
return $this->simpleQuery('SHOW SERVER');
}
// }}}
// {{{ client()
/**
* Allows the client to provide information about itself
* for possible logging and statistical purposes. All clients SHOULD
* send this command after connecting to the server. All DICT servers
* MUST implement this command (note, though, that the server doesn't
* have to do anything with the information provided by the client).
*
* @param string $text
*
* @return mixed string if successful, else PEAR_Error
*/
function client($text='cnb')
{
$this->_sendCmd('CLIENT ' . $text);
}
// }}}
// {{{ status()
/**
* Display some server-specific timing or debugging information. This
* information may be useful in debugging or tuning a DICT server. All
* DICT servers MUST implement this command (note, though, that the text
* part of the response is not specified and may be omitted).
*
* @return mixed string if successful, else PEAR_Error
*/
function status()
{
$resp = $this->_sendCmd('STATUS');
return $resp['text'];
}
// }}}
// {{{ help()
/**
* Provides a short summary of commands that are understood by this
* implementation of the DICT server. The help text will be presented
* as a textual response, terminated by a single period on a line by
* itself. All DICT servers MUST implement this command.
*
* @return mixed string on success, else PEAR_Error
*/
function help()
{
return $this->simpleQuery('HELP');
}
// }}}
// {{{ quit()
/**
* This command is used by the client to cleanly exit the server.
* All DICT servers MUST implement this command.
*
* @return mixed string on success, else PEAR_Error
*/
function quit()
{
return $this->_sendCmd('QUIT');
}
// }}}
// {{{ optionMIME()
/**
* Requests that all text responses be prefaced by a MIME header
* [RFC2045] followed by a single blank line (CRLF).
*
* @return mixed
*/
function optionMIME()
{
}
// }}}
// {{{ auth()
/**
* The client can authenticate itself to the server using a username and
* password. The authentication-string will be computed as in the APOP
* protocol discussed in [RFC1939].
*
* @param string $user
* @param string $auth
*
* @return mixed
*/
function auth($user, $auth)
{
}
// }}}
// {{{ SASLAuth()
/**
* The Simple Authentication and Security Layer (SASL) is currently
* being developed [RFC2222]. The DICT protocol reserves the SASLAUTH
* and SASLRESP commands for this method of authentication.
*
* @param string $mechanism
* @param string $initial_response
*
* @return mixed
*/
function SASLAuth($mechanism, $initial_response)
{
}
// }}}
// {{{ SASLResp()
/**
* The client will send all responses using the SASLRESP command and a
* BASE64-encoded parameter.
*
* @param string $response
*
* @return mixed
*/
function SASLResp($response)
{
}
// }}}
// {{{ connect()
/**
* Connects to a dict server and sets up a socket
*
* @param string $server
* @param integer $port
*
* @return mixed true on success, else PEAR_Error
*/
function connect($server='', $port = 0)
{
$s = new Net_Socket;
if (empty($server))
$server = $this->server;
if (0 == $port)
$port = $this->port;
$err = $s->connect($server, $port);
if (PEAR::isError($err)) {
return $err;
}
$banner = $s->readLine();
preg_match("/\d{3} (.*) <(.*)> <(.*)>/", $banner, &$reg);
$this->servinfo["signature"] = $reg[1];
$this->servinfo["capabilities"] = explode(".", $reg[2]);
$this->servinfo["msg-id"] = $reg[3];
$this->_socket = $s;
return true;
}
// }}}
// {{{ setServer()
/**
* Sets the server and port of dict server
*
* @param string $server
* @param int $port
*/
function setServer($server, $port = 0)
{
$this->server = $server;
if (0 < $port) {
$this->port = $port;
}
}
// }}}
// {{{ setCache()
/**
* Sets caching on or off and provides the cache type and parameters
*
* @param boolean $cache
* @param string $container
* @param array $container_options
*/
function setCache($flag = false, $container = '', $container_options = '')
{
$this->caching = $flag;
if ($this->caching) {
require_once 'Cache.php';
if (is_object($this->cache))
unset($this->cache);
$this->cache = new Cache($container, $container_options);
}
}
// }}}
// {{{ _sendCmd()
/**
* Sends a command, checks the reponse, and
* if good returns the reponse, other wise
* returns false.
*
* @param $cmd Command to send (\r\n will be appended)
*
* @return mixed First line of response if successful, otherwise false
*/
function _sendCmd($cmd)
{
$result = $this->_socket->writeLine($cmd);
if (PEAR::isError($result) AND $result) {
return $result;
}
$data = $this->_socket->readLine();
if (PEAR::isError($data)) {
return $data;
}
$resp['code'] = substr($data, 0, 3);
$resp['text'] = ltrim(substr($data, 3));
if (!Net_Dict::isOK($resp)) {
return new PEAR_Error( $resp['text'],
$resp['code']);
}
return $resp;
}
// }}}
// {{{ _getMultiline()
/**
* Reads a multiline reponse and returns the data
*
* @return mixed string on success or PEAR_Error
*/
function _getMultiline()
{
$data = '';
while(($tmp = $this->readLine()) != '.') {
if (substr($tmp, 0, 2) == '..') {
$tmp = substr($tmp, 1);
}
$data .= $tmp . "\r\n";
}
$resp['text'] = substr($data, 0, -2);
return $resp;
}
// }}}
// {{{ _readLine()
/**
* Alias to Net_Socket::readLine();
*/
function readLine()
{
return $this->_socket->readLine();
}
// }}}
// {{{ simpleQuery()
/**
* Runs a generic dict query
*
* @param string $query
*
* @return mixed string on success, else PEAR_Error
*/
function simpleQuery($query)
{
$resp = $this->_sendCmd($query);
if (PEAR::isError($resp)) {
return $resp;
}
$resp = $this->_getMultiLine();
$this->readLine(); /* discard status */
return $resp['text'];
}
// }}}
// {{{ isOK()
/**
* Checks if a response code is positive
*
* @param array $resp
*
* @return boolean
*/
function isOK($resp)
{
$positives = array(1, 2, 3);
return in_array(substr($resp['code'], 0, 1), $positives);
}
// }}}
} // end class Dict