// no par: show first page, assume parameter is 0
// if par%10 = 0 --> ok, show from there
// par=="all" --> show all messages
// other values are ignored and used as 0
$preffile=file("prefs.ini") or die("guru meditation: missing prefs.txt");
foreach ($preffile as $tmp){
$tmp=trim($tmp);
list($name, $value)=explode("=", $tmp);
$pref[$name]=$value;
}
// print_r($pref);
// echo "
mail (" . $sitemail . ")";
$pending = $pref['pending'];
$approved = $pref['approved'];
$commentfile = $pref['commentfile'];
$magic = $pref['magic'];
$messageperpage=$pref['messageperpage'];
$fieldseparator=$pref['fieldseparator'];
$offset=$_GET['n'];
if(strcmp($offset,"all") != 0) { // se diverse
$offset=intval($offset);
if(($offset % $messageperpage) != 0) $offset=0; // if(($offset%10) != 0) $offset=0;
}
//echo "!!
normalised offset: $offset";
// first check for new approved messages
// race conditions??...
$dir=".";
if (is_dir($dir)) {
if ($d = opendir($dir)) {
while (($file = readdir($d)) !== false) {
// $pattern = "/$pending$/"; // $pending
// $i=preg_match($pattern, $file);
// if($i==1){ echo "
PENDING filename: $file"; }
$pattern = "/$approved$/";
$i=preg_match($pattern, $file);
if($i==1){
// if it has been approved, add to all messages
$comments=fopen("$commentfile", "a"); // append only
if(flock($comments, LOCK_EX)){
//echo "
adding new approved comment: $file";
$newcomment=file($file); // read comment as array
$post=addnewcomment($newcomment, $magic, $file, $approved);
fwrite($comments, $post);
unlink($file);
}
else { echo "
file locked, please refresh later "; }
fclose($comments);
}
}
closedir($d);
}
}
if(!file_exists($commentfile)) die("GURU meditation: comment file is missing(?)");
$all=file($commentfile); // anyhow we read all the file
$nmessage=countmagic($all, $magic);
echo "
Total messages found: ".$nmessage;
if($offset==="all"){
// now show (all) comments, no more fear of new messages... except for race conditions...
echo "
ALL comments";
// foreach($all as $line){
// echo "
XX showing all $nmesage messages ".$line;
showmessagesLastToFirst($all, 1, $nmessage, $magic, $nmessage, $nmessage, $fieldseparator);
//}
}
else{ // show just 10 ($messageperpage) comments from the offset, in reverse order
if(($offset < $nmessage) and ($offset >=0)){
$npage=intval($nmessage/$messageperpage); // $npage=intval($nmessage/10);
if (($nmessage%$messageperpage)>0) $npage++; // if (($nmessage%10)>0) $npage++;
$actualpage=intval($offset/$messageperpage)+1; // $actualpage=intval($offset/10)+1;
echo "
page ".$actualpage." of ".$npage;
$lastmessage=$offset+$messageperpage; // $lastmessage=$offset+10;
if($lastmessage>$nmessage) $lastmessage=$nmessage; // avoid last page of 13 messages would show messages "10-20"
$firstmessage=$offset+1;
echo "
Showing messages from ".$firstmessage." to ".$lastmessage;
// showmessagesFirstToLast($all, $firstmessage, $lastmessage, $magic);
showmessagesLastToFirst($all, $firstmessage, $lastmessage, $magic, $nmessage, $messageperpage, $fieldseparator);
$prevpage=$offset-$messageperpage; // $prevpage=$offset-10;
if ($prevpage<0) $prevpage=0;
$nextpage=$offset+$messageperpage; // $nextpage=$offset+10;
if($nextpage>=$nmessage) $nextpage=$offset;
echo "
prev page ----- ";
echo " next page ";
}
}
// magic to tell messages apart
// file to have a simple time relation, sortable as string
function addnewcomment($newcomment, $magic, $file, $approved){
$stamp=str_replace($approved, "", $file);
$towrite="\n$magic".$stamp;
foreach ($newcomment as $line){
$towrite.=$line;
}
return $towrite;
}
// all is an array of lines
function countmagic($all, $magic){
$c=0;
foreach($all as $line){
$pos=strpos($line, $magic);
if($pos === false ){ // found at char 0, i.e. starting string is magic
// echo "
notfound"; //poor php, where are your head now?
}
else{
$c=$c+1;
}
}
return $c;
}
function showmessagesLastToFirst($all, $firstmessage, $lastmessage, $magic, $nmessage, $maxmessage, $fieldseparator){
$count=sizeof($all);
//echo "
lines count: ".$count;
$messageindex=array();
// (1, 5, 22, 45)
// messaggio1 va da 1 a 4 (in $all)
// messaggio2 va da 5 a 21
// messaggio3 va da 22 a 45
// messaggio4 va da 45 a $count **
$c=0;
$idx=0;
foreach($all as $line){
$pos=strpos($line, $magic);
$c=$c+1;
if($pos !== false ){
$idx=$idx+1;
$messageindex[$idx]=$c; // so to remove magic line
}
else{ }
}
// print_r($messageindex);
//echo "
request- first:".$firstmessage." last:".$lastmessage;
$j=0;
$shown=0;
for($i=$nmessage; $i>=0; $i--){
$j++;
//echo "
i= $i -- j= $j";
if(($j>=$firstmessage) and ($j<=$lastmessage)){
$beg=$messageindex[$i];
if($i<$nmessage){
$end=$messageindex[$i+1]-1;
}
else $end=$count;
// echo " FOUND -- lines to show: ".$beg."-".$end;
showentry($all, $beg, $end, $fieldseparator);
$shown++;
}
if($shown>=$maxmessage) break;
// if($shown>=10) break;
}
echo "
--- end of selected messages ---";
}
function showentry($all, $beg, $end, $fieldseparator){
echo "
\n";
echo "\n";
$comm=0;
for($i=$beg; $i<$end; $i++){
$current=$all[$i];
$part=explode($fieldseparator, $current);
$cc=count($part);
if($cc==2){ // separator found
$pos=strpos($current, "Comments".$fieldseparator);
if($pos !== false ){ // comments start here
$comm=1;
echo "\n ";
echo "\n ";
echo "\n ".$part[0].": | ";
echo "\n ".removeLineEnd($part[1]); //."";
}
else{ // not a comment, just other metadata
echo "\n ";
echo "\n |
";
echo "\n ".$part[0].": | ";
// echo "";
if($part[0] == "E-mail"){
echo "";
echo "\n ".$part[1]." | ";
}
else{
echo "\n ".removeLineEnd($part[1])." | ";
}
echo "\n
";
}
}
else if($cc==1){ // only for comments, from second line onwards
echo "\n ";
echo "\n ".$part[0]."
";
}
else { // error
die("sub-table definitions corrupted");
}
}
if($comm==1)
echo "\n";
echo "\n \n ";
echo '
';
}
function removeLineEnd($current){
$current=str_replace("\r\n", "
", $current);
$current=str_replace("\n", "
", $current); // certainly not the best solution...
$current=str_replace("\r", "
", $current); // ..
$current=str_replace("^M", "
", $current); // ..
return $current;
}
/*
echo "";
$str1="post<>:parola sds sdsdsdd "; // len 2
$str2="post <>:parola sds sdsdsdd "; // len 2
$str3="post <>: parola sds sdsdsdd "; // len 2
$str4="post:parola sds sdsdsdd "; // len 1
$str5="post<>: "; //len 2
$str6="post<>:"; //len 2
$str7=" "; //len 1
$str8=""; //len1
$xxx= explode("<>:", $str8);
echo "len = ".count($xxx)."\n";
print_r($xxx);
echo "\n\n ".str_replace("xx", "XX", "psdsuad");
echo "
";
*/
?>