Download and display
nntp newsgroup postings
//**************************************
// Author: Jeremy Martin
// nntp newsgroup downloader
//**************************************
<?php
$varServer = "news.server.com";
$varPort = 119;
$varTimeOut = 10;
// open a socket
$usenet_handle = fsockopen($varServer, $varPort, &$errno, &$errstr,
$varTimeOut);
if(!$usenet_handle) {
echo "<b><center>Connection failed</center></b>\n";
exit();
}
else {
echo "<b><center>Connected</center></b>\n";
$tmp = fgets($usenet_handle, 1024);
}
//$varUser = "username";
//$varPasswd = "password";
$varNewsGroup = "group.name";
// identification required on private server
if($varUser) {
fputs($usenet_handle, "AUTHINFO USER ".$varUser."\n");
$tmp = fgets($usenet_handle, 1024);
fputs($usenet_handle, "AUTHINFO PASS ".$varPasswd."\n");
$tmp = fgets($usenet_handle, 1024);
// check error
if($tmp != "281 Ok\r\n") {
echo "502 Authentication error\n";
exit();
}
}
// select newsgroup
fputs($usenet_handle, "GROUP ".$varNewsGroup."\n");
$tmp = fgets($usenet_handle, 1024);
if($tmp == "480 Authentication required for command\r\n")
{
echo "$tmp\n";
exit();
}
$info = split(" ", $tmp);
$first = $info[2];
$last = $info[3];
// limit of posts to retrieve
$varLimit = 50;
$boucle=$last-$varLimit;
print "<b><center>Last $varLimit Posts: $boucle
to $last</b></center>\n";
while ($boucle <= $last) {
set_time_limit(0);
fputs($usenet_handle, "ARTICLE $last\n");
$article="";
$tmp = fgets($usenet_handle, 4096);
if(substr($tmp,0,3) != "220") {
echo "<br>+----------------------+<br>";
echo "Article $last not able to be read";
echo "<br>+----------------------+<br>";
}
else {
while($tmp!=".\r\n") {
$tmp = fgets($usenet_handle, 4096);
$article = $article.$tmp;
}
$trans = get_html_translation_table (HTML_ENTITIES);
$str = $article;
$encoded = strtr ($str, $trans);
$encoded = nl2br ($encoded);
echo "<br>+----------------------+<br>";
echo "Article $last";
echo "<br>+----------------------+<br><br>";
echo "$encoded\n";
}
$last--;
}
// close connexion
fclose($usenet_handle);
?> |