~ubuntu-branches/ubuntu/saucy/php-net-nntp/saucy

« back to all changes in this revision

Viewing changes to Net_NNTP-1.5.0/docs/examples/demo/article.php

  • Committer: Package Import Robot
  • Author(s): Jan-Pascal van Best
  • Date: 2013-05-31 23:38:17 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130531233817-4yi3exxgq17g6c4h
Tags: 1.5.0-1
New upstream release 1.5.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
 
3
 
 
4
/**
 
5
 * 
 
6
 * 
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * <pre>
 
10
 * +-----------------------------------------------------------------------+
 
11
 * |                                                                       |
 
12
 * | W3C� SOFTWARE NOTICE AND LICENSE                                      |
 
13
 * | http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231   |
 
14
 * |                                                                       |
 
15
 * | This work (and included software, documentation such as READMEs,      |
 
16
 * | or other related items) is being provided by the copyright holders    |
 
17
 * | under the following license. By obtaining, using and/or copying       |
 
18
 * | this work, you (the licensee) agree that you have read, understood,   |
 
19
 * | and will comply with the following terms and conditions.              |
 
20
 * |                                                                       |
 
21
 * | Permission to copy, modify, and distribute this software and its      |
 
22
 * | documentation, with or without modification, for any purpose and      |
 
23
 * | without fee or royalty is hereby granted, provided that you include   |
 
24
 * | the following on ALL copies of the software and documentation or      |
 
25
 * | portions thereof, including modifications:                            |
 
26
 * |                                                                       |
 
27
 * | 1. The full text of this NOTICE in a location viewable to users       |
 
28
 * |    of the redistributed or derivative work.                           |
 
29
 * |                                                                       |
 
30
 * | 2. Any pre-existing intellectual property disclaimers, notices,       |
 
31
 * |    or terms and conditions. If none exist, the W3C Software Short     |
 
32
 * |    Notice should be included (hypertext is preferred, text is         |
 
33
 * |    permitted) within the body of any redistributed or derivative      |
 
34
 * |    code.                                                              |
 
35
 * |                                                                       |
 
36
 * | 3. Notice of any changes or modifications to the files, including     |
 
37
 * |    the date changes were made. (We recommend you provide URIs to      |
 
38
 * |    the location from which the code is derived.)                      |
 
39
 * |                                                                       |
 
40
 * | THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT    |
 
41
 * | HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED,    |
 
42
 * | INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR        |
 
43
 * | FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE    |
 
44
 * | OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS,           |
 
45
 * | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.                               |
 
46
 * |                                                                       |
 
47
 * | COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT,        |
 
48
 * | SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE        |
 
49
 * | SOFTWARE OR DOCUMENTATION.                                            |
 
50
 * |                                                                       |
 
51
 * | The name and trademarks of copyright holders may NOT be used in       |
 
52
 * | advertising or publicity pertaining to the software without           |
 
53
 * | specific, written prior permission. Title to copyright in this        |
 
54
 * | software and any associated documentation will at all times           |
 
55
 * | remain with copyright holders.                                        |
 
56
 * |                                                                       |
 
57
 * +-----------------------------------------------------------------------+
 
58
 * </pre>
 
59
 *
 
60
 * @category   Net
 
61
 * @package    Net_NNTP
 
62
 * @author     Heino H. Gehlsen <heino@gehlsen.dk>
 
63
 * @copyright  2002-2011 Heino H. Gehlsen <heino@gehlsen.dk>. All Rights Reserved.
 
64
 * @license    http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 W3C� SOFTWARE NOTICE AND LICENSE
 
65
 * @version    SVN: $Id: article.php 306619 2010-12-24 12:16:07Z heino $
 
66
 * @link       http://pear.php.net/package/Net_NNTP
 
67
 * @see        
 
68
 * @since      File available since release 1.3.0
 
69
 */
 
70
 
 
71
 
 
72
/**
 
73
 *
 
74
 */
 
75
require_once 'config.inc.php';
 
76
 
 
77
/**
 
78
 *
 
79
 */
 
80
require_once 'common.inc.php';
 
81
 
 
82
 
 
83
/* Validate input */
 
84
/*
 
85
 
 
86
// Must have either $messageNum or $messageID
 
87
if (is_null($messageNum) and is_null($messageID)) {
 
88
    error('Error: Nither message number nor message-id provided!');
 
89
}
 
90
 
 
91
// Only $messageNum OR $messageID
 
92
if (!is_null($messageNum) and !is_null($messageId)) {
 
93
    error('Error: Both message-id _AND_ message number provided!');
 
94
}
 
95
 
 
96
// $messageNum requires $group
 
97
if (is_null($messageNum) and !is_null($group)) {
 
98
    error('Error: Message number requires group!');
 
99
}
 
100
*/
 
101
 
 
102
//
 
103
$messageID  = $group === null ? $article : null;
 
104
#$article    = $group !== null ? $article : null;
 
105
 
 
106
/* Prepare breadcrumbs */
 
107
 
 
108
$breadcrumbs = array();
 
109
$breadcrumbs['Frontpage'] =  './index.php?' . query();
 
110
$breadcrumbs['groups @ '  . ($host == null ? 'localhost' : $host)] = './groups.php?' . query();
 
111
if ($messageID !== null) {
 
112
    $breadcrumbs['Article: '.htmlentities($messageID)]   = null;
 
113
} else {
 
114
    $breadcrumbs['group: '.$group] =  './group.php?' . query("group=$group&from=$from&next=$next");
 
115
    $breadcrumbs['Article: #'.$article] = null;
 
116
}
 
117
 
 
118
 
 
119
// Connect
 
120
$posting = $nntp->connect($host, $encryption, $port);
 
121
if (PEAR::isError($posting)) {
 
122
    error('Unable to connect to NNTP server: ' . $posting->getMessage());
 
123
}
 
124
 
 
125
 
 
126
// Start TLS encryption
 
127
if ($starttls) {
 
128
    $R = $nntp->cmdStartTLS();
 
129
    if (PEAR::isError($R)) {
 
130
        error('Unable to connect to NNTP server: ' . $R->getMessage());
 
131
    }
 
132
}
 
133
 
 
134
// Authenticate
 
135
if (!is_null($user) && !is_null($pass)) {
 
136
    $authenticated = $nntp->authenticate($user, $pass);
 
137
    if (PEAR::isError($authenticated)) {
 
138
        error('Unable to authenticate: ' . $authenticated->getMessage());
 
139
    }
 
140
}
 
141
 
 
142
// If asked for a article in a group, select group then article
 
143
if ($messageID === null) {
 
144
 
 
145
    // Select group
 
146
    $summary = $nntp->selectGroup($group);
 
147
    if (PEAR::isError($summary)) {
 
148
        error($summary->getMessage());
 
149
    }
 
150
 
 
151
    // Select article
 
152
    $article = $nntp->selectArticle($article);
 
153
    if (PEAR::isError($article)) {
 
154
        error($article->getMessage());
 
155
    }
 
156
 
 
157
    if ($article === false) {
 
158
        error('The article is not avalible on the server!');
 
159
    }
 
160
 
 
161
    // Fetch overview
 
162
    $overview = $nntp->getOverview();
 
163
    if (PEAR::isError($overview)) {
 
164
        $logger->warning('Error fetching overview (Server response: ' . $overview->getMessage() . ')');
 
165
 
 
166
        // 
 
167
        $overview = false;
 
168
    }
 
169
 
 
170
    // Fetch 'Newsgroups' header field
 
171
    $groups = $nntp->getHeaderField('Newsgroups');
 
172
    if (PEAR::isError($groups)) {
 
173
        $logger->warning('Error fetching \'Newsgroups\' header field (Server response: ' . $groups->getMessage() . ')');
 
174
 
 
175
        // 
 
176
        $groups = false;
 
177
    }
 
178
}
 
179
 
 
180
 
 
181
// Fetch header
 
182
$header = $nntp->getHeader($messageID);
 
183
if (PEAR::isError($header)) {
 
184
    error('Error fetching header (Server response: ', $header->getMessage(), ')');
 
185
}
 
186
if ($header === false) {
 
187
    error('The article is not avalible on the server!');
 
188
}
 
189
 
 
190
 
 
191
 
 
192
// Fetch body
 
193
$body = $nntp->getBody($messageID);
 
194
if (PEAR::isError($body)) {
 
195
    error('Error fetching body (Server response: ', $body->getMessage(), ')');
 
196
}
 
197
 
 
198
 
 
199
// Close connection
 
200
$nntp->disconnect();
 
201
 
 
202
 
 
203
 
 
204
/* ... */
 
205
 
 
206
/**
 
207
 *
 
208
 */
 
209
function x($header, $fieldname, $index = 0)
 
210
{
 
211
    //
 
212
    $fieldname = strtolower($fieldname);
 
213
 
 
214
    //
 
215
    for ($i = 0, $j = 0 ; $i < count($header) ; $i++) {
 
216
 
 
217
        //
 
218
        $line = $header[$i];
 
219
        
 
220
        //
 
221
        @list($tag, $value) = explode(": ", $line, 2);
 
222
        if (strtolower($tag) != $fieldname) {
 
223
            continue;
 
224
        }
 
225
 
 
226
        // Skip if $index not reached
 
227
        if ($j++ < $index) {
 
228
            continue;
 
229
        }
 
230
 
 
231
        // Append folded lines...
 
232
        while (($next = $header[++$i]) && ($next[0] == ' ' || $next[0] == "\t")) {
 
233
            $value .= ' ' . ltrim($next, " \t");
 
234
        }
 
235
 
 
236
        // Set $group
 
237
        return $value;
 
238
    }
 
239
}
 
240
 
 
241
 
 
242
//
 
243
if (!empty($overview)) {
 
244
    $subject    = $overview['Subject'];
 
245
    $from       = $overview['From'];
 
246
    $date       = $overview['Date'];
 
247
    $references = $overview['References'];
 
248
 
 
249
} else {
 
250
    $subject    = x($header, 'Subject');
 
251
    $from       = x($header, 'From');
 
252
    $date       = x($header, 'Date');
 
253
    $references = x($header, 'References');
 
254
 
 
255
    if (empty($references)) {
 
256
        $references = x($header, 'In-reply-to');
 
257
    }
 
258
}
 
259
 
 
260
 
 
261
//
 
262
if (empty($groups)) {
 
263
    $logger->info('Received an empty \'Newsgroups\' header field - parsing header as backup...');
 
264
    $groups = x($header, 'Newsgroups');
 
265
}
 
266
 
 
267
 
 
268
//
 
269
$references = empty($references) ? null : preg_split("/[ \t]/", $references);
 
270
 
 
271
 
 
272
 
 
273
 
 
274
/**
 
275
 *
 
276
 */
 
277
function outputHead()
 
278
{
 
279
    //
 
280
    extract($GLOBALS);
 
281
 
 
282
    echo '<table id="article-head" border="0" cellpadding="2" cellspacing="2" width="100%">' . "\r\n";
 
283
 
 
284
    // Subject
 
285
    echo ' <tr>' . "\r\n";
 
286
    echo '  <td class="label">Subject:</td>' . "\r\n";
 
287
    echo '  <td class="value" colspan="3"><b>' . htmlspecialchars($subject) . '</b></td>', "\r\n";
 
288
    echo ' </tr>', "\r\n";
 
289
    // From
 
290
    echo ' <tr>', "\r\n";
 
291
    echo '  <td class="label">From:</td>' . "\r\n";
 
292
    echo '  <td class="value">' . htmlspecialchars($from) . '</td>', "\r\n";
 
293
    // Date
 
294
    echo '  <td class="label">Date:</td>' . "\r\n";
 
295
    echo '  <td class="value">' . htmlspecialchars($date) . '</td>', "\r\n";
 
296
    echo ' </tr>', "\r\n";
 
297
    echo ' <tr>', "\r\n";
 
298
 
 
299
    // References
 
300
    echo '  <td class="label">References:</td>' . "\r\n";
 
301
    echo '  <td class="value">';
 
302
    switch (true) {
 
303
    case is_array($references):
 
304
        foreach ($references as $reference) {
 
305
        echo '   <a href="article.php?', query('article='.urlencode($reference)), '">#', ++$i, '</a>', "\r\n";
 
306
        }
 
307
        break;
 
308
    case is_string($references) && !empty($references):
 
309
        echo '   <a href="article.php?', query('article='.urlencode($reference)), '">#', ++$i, '</a>', "\r\n";
 
310
        break;
 
311
    }
 
312
    echo '  </td>', "\r\n";
 
313
 
 
314
 
 
315
    // Groups
 
316
    echo '  <td class="label">Groups:</td>' . "\r\n";
 
317
    echo '  <td class="value">', "\r\n";
 
318
    foreach (explode(',', $groups) as $group) {
 
319
        echo '<a href="./group.php?'. query('group='.urlencode($group)), '">', $group, '</a> ';
 
320
    }
 
321
    echo '  </td>', "\r\n";
 
322
    echo ' </tr>', "\r\n";
 
323
 
 
324
    //
 
325
    echo '</table>', "\r\n";
 
326
}
 
327
 
 
328
/**
 
329
 *
 
330
 */
 
331
function outputHeader()
 
332
{
 
333
    //
 
334
    extract($GLOBALS);
 
335
 
 
336
    echo '<blockquote id="article-header">', "\r\n";
 
337
    echo ' <pre>', "\r\n";
 
338
    echo preg_replace("/\r\n(\t| )+/", ' ', implode("\r\n", $header));
 
339
    echo ' </pre>', "\r\n";
 
340
    echo '</blockquote>', "\r\n";
 
341
}
 
342
 
 
343
/**
 
344
 *
 
345
 */
 
346
function outputBody()
 
347
{
 
348
    //
 
349
    extract($GLOBALS);
 
350
 
 
351
    echo '<blockquote id="article-body">', "\r\n";
 
352
    echo ' <pre>', "\r\n";
 
353
    foreach ($body as $line) {
 
354
 
 
355
        /* Code from news.php.net begins here */ 
 
356
    
 
357
        // this is some amazingly simplistic code to color quotes/signatures
 
358
        // differently, and turn links into real links. it actually appears
 
359
        // to work fairly well, but could easily be made more sophistimicated.
 
360
        $line = htmlentities($line, ENT_NOQUOTES, 'utf-8');
 
361
        $line = preg_replace("/((mailto|http|ftp|nntp|news):.+?)(&gt;|\\s|\\)|\\.\\s|$)/", "<a href=\"\\1\">\\1</a>\\3", $line);
 
362
        if (!$insig && $line == "-- \r\n") {
 
363
                echo '<span class="signature">';
 
364
                $insig = 1;
 
365
        }
 
366
        if ($insig && $line == "\r\n") {
 
367
                echo '</span>';
 
368
                $insig = 0;
 
369
        }
 
370
        if (!$insig && substr($line, 0, 4) == '&gt;') {
 
371
                echo '<span class="quote">', $line, '</span>';
 
372
        } else {
 
373
                echo $line;
 
374
        }
 
375
        /* Code from news.php.net ends here */ 
 
376
 
 
377
        echo "\r\n";
 
378
    }
 
379
    echo ' </pre>', "\r\n";
 
380
    echo '</blockquote>', "\r\n";
 
381
}
 
382
 
 
383
 
 
384
/**********/
 
385
/* Output */
 
386
/**********/
 
387
 
 
388
/**
 
389
 * Output header
 
390
 */
 
391
include 'header.inc.php';
 
392
 
 
393
 
 
394
$logger->dump();
 
395
outputHead();
 
396
outputBody();
 
397
 
 
398
/**
 
399
 * Output footer
 
400
 */
 
401
include 'footer.inc.php';
 
402
 
 
403
?>