Location: PHPKode > projects > PHP Newsgroup Reader > left.php
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<link href="javascript/style1.css" type=text/css rel=stylesheet>
<body bgcolor="#f3f3f3">
<?php
	$tm1 = time();
	if ( !isset($_GET["server"])||!isset($_GET["group"]) )
	{
			include("intro_l.php");
			exit(0);
	}

	print("<table width=100% cellpadding=0 cellspacing=0 align=center border=0>\n");
	include("init.inc");
	include("decode_mime.inc");

	$server = $_GET["server"];
	$newsgroup = $_GET["group"];
	$page = $_GET['page'];

	$replace1 = array(' ', '"', '\'', '<', '>', chr(13), chr(10));
	$replace2 = array('&#32;', "&quot;", "&quot;", "&lt;", "&gt;", '', '');
	$newsgroup = str_replace($replace1, $replace2, $newsgroup);

/*	$fp_pop_news = fopen("file/pop_news.$server.html", "rb");
print(fgets($fp_pop_news));
	$pop_news = unserialize(str_replace($replace1, $replace2, fgets($fp_pop_news)));
	fclose($fp_pop_news);
	++$pop_news[$server][$newsgroup];
	$fp_pop_news = fopen("file/pop_news.$server.html", "wb");
	fputs($fp_pop_news, serialize($pop_news));
	fclose($fp_pop_news);
exit(0);
*/
	$fp = fsockopen($server, 119);
	fputs($fp, "GROUP ".$newsgroup."\n");
	fgets($fp);
	list($code, $n, $first, $last, $t) = split(" ", fgets($fp) );
	$first0 = $first;
	$last0 = $last;
	$num_reply_post0 = $num_reply_post = 0;
	$file_oldpost_exist = 0;
//	$newpost_in_file = 0;
	if ( file_exists("file/".$server."/".$newsgroup.".html") )
	{
			$fp_oldpost = fopen("file/".$server."/".$newsgroup.".html", 'rb');

			$file_oldpost_exist = 1;
			$first_in_file = (int)str_replace($replace1, $replace2, fgets($fp_oldpost));
			$last_in_file = (int)str_replace($replace1, $replace2, fgets($fp_oldpost));
//			$newpost_in_file = (int)str_replace($replace1, $replace2, fgets($fp_oldpost));
//			$replypost_in_file = (int)str_replace($replace1, $replace2, fgets($fp_oldpost));
			$first = $last_in_file+1;
//			print("first = $first
/*
			for ($i=1; $i<=$replypost_in_file; ++$i)
			{
					$arr1[++$num_reply_post][6] = fgets($fp1);
					$arr1[++$num_reply_post][6] = fgets($fp1);
*/
//			fclose($fp1);
	}else
	{
			@mkdir("file",0755);
			@mkdir("file/".$server,0755);
	}
	
$put_get_number = 1000;
	
	include("javascript.php");
	
	$month = array('jan'=>'1','feb'=>'2','mar'=>'3','apr'=>'4','may'=>'5','jun'=>'6','jul'=>'7',
  					'aug'=>'8','sep'=>'9','oct'=>'10','nov'=>'11','dec'=>'12');

	$num_new_post = 0;  //number of new posts
	$obtained_article = 0;   //number of fetched posts


//print("first=$first//last=$last");

	$t='.'.chr(13).chr(10);
	$u='423 no such article number in this group'.chr(13).chr(10);

   	$s = "dummy^_^";
	$replace1 = array(chr(13), chr(10));
	$replace2 = array('', '');

	for ( $i1=1; $i1==1||$page>=2&&$i1==2&&count($new_post)<=($page*$num_display_article); ++$i1)
	{
			while ( ($file_oldpost_exist&&($i1==1)||($num_new_post<$page*$num_display_article))&&($last>=$first)&&($last>0) )
			{
					$last1 = $last;
					if ( $last-$first+1<$put_get_number )//total no of post is small
							for (;$last>=$first; --$last)
									fputs($fp, "head $last\r\n");
					else//large no of post in that newsgroup
							for ($tmp=$last; $tmp-$last<$put_get_number; --$last)
									fputs($fp, "head $last\r\n");

					while ( $last1>$last )
					{
							include("tmpsave.php");
					}//while

			}//while

			if ( $i1==2 )
					break;
			if ( $file_oldpost_exist )
			{
/*print("**");
$st = fgets($fp_oldpost);
for ($i=1; $i<=strlen($st); ++$i)
		print($st[$i-1]."|".ord($st[$i-1]."<BR>"));
fclose($fp_oldpost);exit(0);*/

							for ($i=max(1, $num_reply_post0), $j=0; isset($arr1)&&$i<=count($arr1); ++$i)
									if ( $flag2[$i] )
											$arr[++$j] = $arr1[$i];
							if ( ($tmp=str_replace($replace1, $replace2, fgets($fp_oldpost)))!="^_^" )
							{
									$old_reply = unserialize($tmp);
									if ( isset($arr1) )
									{
											for ($i=1; $i<=count($old_reply); ++$i)
													array_push($arr, $old_reply[$i]);
											$arr1 = $arr;
											unset($arr);
									}else
											$arr1 = $old_reply;
									unset($flag2);
									for ( $i=1; $i<=count($arr1); ++$i)
											$flag2[$i] = 1;//all are reply posts alone
									$num_reply_post0 = $num_reply_post = count($arr1);
							}
/*					if ( $num_new_post<$num_display_article )//need to obtain old post
					{
*/
					if ( ($s=str_replace($replace1, $replace2, fgets($fp_oldpost)))!="^_^" )
					{
//							$s = str_replace( $replace1, $replace2, $s);
							$old_post = unserialize($s);
							for ($j=1; $j<=min(count($old_post),$num_display_article); ++$j)
								for($i=$num_reply_post;$i;--$i)
									if($flag2[$i])/*if($arr1[$i][6]>=$new_post[$j][6])*/
										sorting($old_post[$j]);

							if ( isset($new_post) )
							{
								for ($i=1;$i<=count($old_post); ++$i)
									array_push($new_post, $old_post[$i]);
							}else
								$new_post = $old_post;
							$num_new_post = count($new_post);
/*
  print("<PRE>");
  print_r($new_post);
  print("</PRE>");
  fclose($fp_oldpost);
  exit(0);
*/
					}
					$last = $first_in_file-1;
					$first = $first0;
//print($last."||".$first);
//					}
					fclose($fp_oldpost);
			}//if
	}//for
	
/*  print("<PRE>");
  print_r($new_post);
  print("</PRE>");*/
//exit(0);
/*	if no change to file then no need to write to file */
//	if ( isset($new_post)||isset($arr) )
//	{
	$fp_oldpost = fopen("file/".$server."/".$newsgroup.".html", 'wb');
	fputs($fp_oldpost, ($last+1)."\n");
	fputs($fp_oldpost, $last0."\n");
/*
	if ( isset($new_post) )
			fputs($fp_oldpost, count($new_post)."\n");
	else
			fputs($fp_oldpost, "0\n");
	if ( isset($arr1) )
			fputs($fp_oldpost, count($arr1)."\n");
	else
			fputs($fp_oldpost, "0\n");
*/
	unset($arr);
	if ( isset($arr1) )
		for ($i=1, $j=0; $i<=count($arr1); ++$i)
			if ( $flag2[$i] )
				$arr[++$j] = $arr1[$i];
	if ( isset($arr) )
	{

/*  print("<PRE>");
  print_r($arr);
  print("</PRE>");*/
			fputs($fp_oldpost, serialize($arr)."\n");
	}else
			fputs($fp_oldpost, "^_^\n");
	if ( isset($new_post) )
			fputs($fp_oldpost, serialize($new_post));
	else
	{
			fputs($fp_oldpost, "^_^\n");
			die("<BR><BR><div align=center>This newsgroup has no message<BR><a href=javascript:history.go(-1)>BACK</a></div>");
	}
	fclose($fp_oldpost);

//print("//new=$num_new_post//reply=$num_reply_post//");
//print("//TIME1 = ".(time()-$tm1));
/*
for ($i=1,$ctx=0; $i<=$num_reply_post; ++$i)
	if ( !$flag2[$i] )
		++$ctx;
*/
//print("//new=$num_new_post//reply=$ctx//TIME2 = ".(time()-$tm1));
//print("<BR><BR>TODO NEXT=more efficiency on fgets, while loop, sorting:if(arr1[i][6]>=new_post[j][6]), article size;<BR>debug fgets different cases, sorting;<BR>anything important!!!<BR><BR>");
function sorting(&$tmp)
{
		global $num_article_field,$i,$arr1,$flag2;
		if(isset($tmp))
		{
				for($l=$num_article_field,$m=count($tmp);$l<=$m;++$l)
						if(@$arr1[$i][3]==$tmp[1])
						{
								$tmp[++$m]=$arr1[$i];
								$flag2[$i]=0;
								return;
						}else if($l>$num_article_field)
								sorting($tmp[$l]);
				return;
		}else
				return;
}


/*
  print("<PRE>");
  print_r($newpost);
  print("</PRE>");
*/




/*

The translations performed are: 
	'&' (ampersand) becomes '&amp;' 
	'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set. 
	''' (single quote) becomes '&#039;' only when ENT_QUOTES is set. 
	'<' (less than) becomes '&lt;' 
	'>' (greater than) becomes '&gt;' 
*/
  
/*
  print("<PRE>");
  print_r($new_post);
  print("</PRE>");
*/
/*
  print("<font size=-2><TABLE width=50% border=1 cellpadding=0 cellspacing=0 onMouseover=changeto('00ccff')
  			onMouseout=changeback('white') >\n<TBODY><TR><TD nowrap>Subject</TD><TD>Author</TD><TD nowrap><div align=right>
			Date</div></TD></TR>\n");
*/
	print("<TR><TD nowrap>");
	print("<div align=right><a href=post.php?server=$server&newsgroup=$newsgroup&post=1 target=right>POST A NEW MESSAGE<BR></a>");
	if ( $page>1 )
			print("<a href=left.php?server=$server&group=$newsgroup&page=".($page-1).">");
	print("PREVIOUS");
	if ( $page>1 )
			print("</a>");
	print("<strong> Page $page </strong>");
	if ( $last>$first0||count($new_post)>$page*$num_display_article )
			print("<a href=left.php?server=$server&group=$newsgroup&page=".($page+1).">");
	print("NEXT<BR><BR>");
	if ( $last>$first0||count($new_post)>$page*$num_display_article )
			print("</a>");
	print("</div></TD></TR>");

	print("<TR><TD><div align=center><TABLE width=100% border=1 bordercolor=#BECCE7 cellpadding=0 cellspacing=0>\n<TBODY>
			<TR><TD><strong>Subject</strong></TD><TD><strong>Author</strong></TD><TD nowrap><strong>Date</strong></TD></TR>\n");
			
	$replace1 = array(' ', '"', '\'', '<', '>', chr(13), chr(10));
	$replace2 = array('&#32;', "&quot;", "&quot;", "&lt;", "&gt;", '', '');
	$newsgroup = str_replace( $replace1, $replace2, $newsgroup);
	
	@mkdir("file",0755);
	@mkdir("file/".$server,0755);

	$fp = fopen("file/tmp.html", 'wb');
	fputs($fp, "$server\r\n$newsgroup");
	fclose($fp);
	
	for ($i=($page-1)*$num_display_article+1,$o=min($page*$num_display_article, count($new_post)); $i<=$o; ++$i)
	{
		print("<TR class=root ");
		_print($new_post[$i], 1, $i, count($new_post[$i])>$num_article_field?"uBranch":"leaf");
		for($k=$num_article_field+1; $k<=count($new_post[$i]); ++$k)
			print_child($new_post[$i][$k], 2, $k-$num_article_field, count($new_post[$i][$k])>$num_article_field?"uBranch":"leaf");
	}
	print("</TBODY></TABLE></div></TD></TR></table></body>");


function _print($tmp, $no, $dep, $st)
{
global $num_article_field,$replace1,$replace2,$server,$newsgroup;
	$subject_display = $tmp[4];//substr($tmp[4],0,$max_subject_word);
	$sd = str_replace( $replace1, $replace2, $subject_display);
    $sender_display = $tmp[5];//substr($tmp[5],0,$max_sender_word);
    $j = substr($tmp[2],0,strlen($tmp[2]));
//	$j = str_replace( $replace1, $replace2, $j);
	$tmp7 = $tmp[7]==""?$sender_display:$tmp[7];
	$st1 = $st=="leaf"?"1ptrans.gif":"plus.gif";
/*
	print("onmouseover=\"if('function'==typeof(Toc1_click)) Toc1_click('#FFFFFF','lightskyblue')\" GroupNumber=".($no+1)." depth=$dep>");
	print("<TD onmouseover=\"window.status='$sd';return true\" onmouseout=\"window.status='';return true\" noWrap style='overflow:hidden'>".
	print_space($no)."<IMG class=$st height=11 src=pic/$st1 width=11 /><A href=right.php?server=$server&group=$newsgroup&msgno=$j title=$sd 
	target=right><font size=-1>$subject_display</font></A></TD>");
	print("<TD noWrap><A href=mailto:$tmp7 title=$tmp7><font size=-1>$sender_display</font></A></TD>");
	print("<TD noWrap><div align=right><font size=-1>"._dateshow($tmp[6])."</font></div></TD></TR>");
*/
	print("onmouseover=c() GroupNumber=".($no+1)." depth=$dep><TD>".print_space($no).
	"<IMG class=$st src=pic/$st1><A href=right.php?n=$j target=right>$subject_display</A></TD><TD><A href=mailto:$tmp7>$sender_display</A></TD><TD nowrap>"._dateshow($tmp[6])."</TD></TR>\n");
}

function print_space($tmp)
{
	$st = "";
	for ($i=2; $i<=$tmp; ++$i)
		$st = $st."ยก@";
	return $st;
}

function print_child($tmp, $no, $dep, $st)
{
global $num_article_field;
	print("<TR class=child style='DISPLAY:none'");
	_print($tmp, $no, $dep, count($tmp)>$num_article_field?"uBranch":"leaf");
    for($i=$num_article_field+1,$o=count($tmp); $i<=$o; ++$i)
		print_child($tmp[$i], $no+1, $i-$num_article_field, count($tmp[$i])>$num_article_field?"uBranch":"leaf");
}

function _dateshow($tmp)
{
	//arr[6]:02/12/19 00:29:57
	//arr[9]:19/12 00:29
	list($tmp5, $tmp2, $tmp1) = split("/", $tmp, 3);
	list($tmp1, $time) = split(" ", $tmp1, 2);
	list($tmp3, $tmp4, $time) = split(":", $time, 3);
	return $tmp1."/".$tmp2." ".$tmp3.":".$tmp4;
	
}
//	print($server."//".$newsgroup."//TIME3 = ".(time()-$tm1));
?>
Return current item: PHP Newsgroup Reader