/* Unread Messages Indicator */

//options

$cLimitTopics=100; //how many read topics to store in a cookie or database. Cookie size limit is about 4 Kb of data incl. cookie name and time. Unread topics hold topic id and last post id, so it depends on how big IDs have topics and posts. With DB, this is not limited, however you must keep in mind as more records are in a table, as more slower it will be.

$unreadMsgMode=0; //1 means we will use mySQL for storing the data; 0 - cookies

$cookieexptoptime=time()+31536000; //1 year

$Tun_point='minibbtable_unreadpoint'; //table name if mySQL is used
$Tun_topics='minibbtable_unreadtopics'; //table name if mySQL is used

if($user_id>0) $resetReadLink="<a href=\"{$main_url}/{$indexphp}action=resetread\" class=\"mnblnk\">Mark All Messages Read</a>";
else $resetReadLink='';

$showIconInForums=FALSE; //TRUE or FALSE - will show the Unread icon also in forum lists; it will take 2 extra requests from DB. Currently works only for $unreadMsgMode=0; mode.

//end of options

if($user_id>0){

if($unreadMsgMode==0){
//cookies mode

if(!isset($_COOKIE[$cookiename.'_mslv'])) $mslvPoint=0;
else $mslvPoint=(int)$_COOKIE[$cookiename.'_mslv'];

if($action=='vthread' and is_array($topicData) and isset($topicData[9]) and $topicData[9]>$mslvPoint){

$currtopics=array();

if(!isset($_COOKIE[$cookiename.'_tread'])){
$treadVal=$topic.'-'.$topicData[9];
}
elseif(preg_match("#[0-9_-]+#", $_COOKIE[$cookiename.'_tread'])){
if(substr_count($_COOKIE[$cookiename.'_tread'], '_')==0) {
$q=explode('-', $_COOKIE[$cookiename.'_tread']);
$currtopics[$q[0]]=$q[1];
}
else {
$ct=explode('_', $_COOKIE[$cookiename.'_tread']);
foreach($ct as $cc){
$q=explode('-', $cc);
$currtopics[$q[0]]=$q[1];
}

//$currtopics=explode('_', $_COOKIE[$cookiename.'_tread']);

}

//store up to 100 topics
if(is_array($currtopics) and (!isset($currtopics[$topic]) or $currtopics[$topic]<$topicData[9]) and sizeof($currtopics)+1>$cLimitTopics) {

$f=sizeof($currtopics)-$cLimitTopics;

$newcurrtopics=array();

$ee=1;
foreach($currtopics as $key=>$val){
if($f>=$ee) {
$ee++;
continue;
}
else{
$newcurrtopics[]=$key.'-'.$val;
}
}

//for($i=sizeof($currtopics)-1; $i>=$f; $i--) 

$treadVal=implode('_', $newcurrtopics);

}
elseif(!isset($currtopics[$topic]) or $currtopics[$topic]<$topicData[9]){
$currtopics[$topic]=$topicData[9];
$newcurrtopics=array();
foreach($currtopics as $key=>$val) $newcurrtopics[]=$key.'-'.$val;
//for($i=0; $i<sizeof($currtopics); $i++) $newcurrtopics[]=$currtopics[$i].'-'.$currposts[$i];
$treadVal=implode('_', $newcurrtopics);
}
else $treadVal=-1;

}

if($treadVal>=0) {
setGenCookie('', (time()-2592000), $cookiename.'_tread');
setGenCookie($treadVal, $cookieexptoptime, $cookiename.'_tread');
}
}

if($action=='resetread'){

if($row=db_simpleSelect(0, $Tp, 'post_id', '', '', '', 'post_id DESC', 1)) $mslvPoint=$row[0]; else $mslvPoint=0;

setGenCookie('', (time()-2592000), $cookiename.'_mslv');
setGenCookie($mslvPoint, $cookieexptoptime, $cookiename.'_mslv');
setGenCookie('', (time()-2592000), $cookiename.'_tread');

header("{$rheader}{$main_url}/{$indexphp}");
exit;
}

if($action=='' or $action=='vtopic'){

$readTopics=array();

if(!isset($_COOKIE[$cookiename.'_tread']) or !preg_match("#[0-9_-]+#", $_COOKIE[$cookiename.'_tread'])) {}
else{
$currtopics=explode('_', $_COOKIE[$cookiename.'_tread']);

$ct=explode('_', $_COOKIE[$cookiename.'_tread']);
foreach($ct as $cc){
$q=explode('-', $cc);
$readTopics[$q[0]]=$q[1];
}

}

//print_r($readTopics);
//echo $mslvPoint;

if($showIconInForums){

$forumPosts=array();
$forumIcons=array();

if (isset($clForumsUsers)) $closedForums=getAccess($clForums, $clForumsUsers, $user_id); else $closedForums='n';
if ($closedForums!='n') {
$xtr=getClForums($closedForums,'where','','forum_id','and','!=');
$xtr2=getClForums($closedForums,'and','','forum_id','and','!=');
}
else {
$xtr='';
$xtr2='';
}

if($row=db_simpleSelect(0, $Tf, 'count(*)')) $forumsAmount=$row[0]; else $forumsAmount=0;

$uSql="select topic_last_post_id, forum_id, topic_id from {$Tt} where topic_last_post_id>'{$mslvPoint}' {$xtr2} order by topic_last_post_id desc";

if(($DB=='mysql' and $res=mysql_query($uSql) and mysql_num_rows($res)>0 and $row=mysql_fetch_row($res)) OR ($DB=='mysqli' and $res=mysqli_query($mysqlink, $uSql) and mysqli_num_rows($res)>0 and $row=mysqli_fetch_row($res))){
do{

if(!isset($forumPosts[$row[1]])){
if(!isset($readTopics[$row[2]]) or (isset($readTopics[$row[2]]) and $readTopics[$row[2]]<$row[0]) ) $forumPosts[$row[1]]="&nbsp;<img src=\"{$GLOBALS['main_url']}/img/unread.svg\" style=\"width:8px;height:8px\" alt=\"Unread\" title=\"Unread\">";
}

if(sizeof($forumPosts)==$forumsAmount) break;

}
while( ($DB=='mysql' and $row=mysql_fetch_row($res)) OR ($DB=='mysqli' and $row=mysqli_fetch_row($res)) );
}

unset($xtr);
}

}

}//cookies mode

else{
//mysql mode

if($row=db_simpleSelect(0, $Tun_point, 'last_id', 'user_id', '=', $user_id)) $mslvPoint=$row[0]; else $mslvPoint=0;

if($action=='vthread' and is_array($topicData) and isset($topicData[9]) and $topicData[9]>$mslvPoint){

if($row=db_simpleSelect(0, $Tun_topics, 'post_id', 'topic_id', '=', $topic, '', '', 'user_id', '=', $user_id)) {
$sql_q="update {$Tun_topics} set post_id={$topicData[9]} where topic_id={$topic} and user_id={$user_id}";
if($DB=='mysql') mysql_query($sql_q);
elseif($DB=='mysqli') mysqli_query($mysqlink, $sql_q);
}
else{
$sql_q="insert {$Tun_topics} (topic_id, post_id, user_id) values ({$topic}, {$topicData[9]}, {$user_id})";
if($DB=='mysql') mysql_query($sql_q);
elseif($DB=='mysqli') mysqli_query($mysqlink, $sql_q);
}

//fix table limit
if($row=db_simpleSelect(0, $Tun_topics, 'count(*)', 'user_id', '=', $user_id)) $currStore=$row[0]; else $currStore=0;
if($currStore>$cLimitTopics){

$clm=$currStore;
if($row=db_simpleSelect(0, $Tun_topics, 'topic_id, post_id', 'user_id', '=', $user_id, 'post_id asc')) {
do{
$sql_q="delete from $Tun_topics where topic_id={$row[0]} and user_id=$user_id and post_id={$row[1]}";
if($DB=='mysql') mysql_query($sql_q);
elseif($DB=='mysqli') mysqli_query($mysqlink, $sql_q);
$clm--;
}
while($clm>$cLimitTopics and $row=db_simpleSelect(1));
}

}

}

if($action=='resetread'){

if($row=db_simpleSelect(0, $Tp, 'post_id', '', '', '', 'post_id DESC', 1)) $mslvPoint=$row[0]; else $mslvPoint=0;

$sql_q="delete from $Tun_topics where user_id=$user_id";
if($DB=='mysql') mysql_query($sql_q);
elseif($DB=='mysqli') mysqli_query($mysqlink, $sql_q);

if($row=db_simpleSelect(0, $Tun_point, 'last_id', 'user_id', '=', $user_id)) {
$sql_q="update {$Tun_point} set last_id={$mslvPoint} where user_id={$user_id}";
if($DB=='mysql') mysql_query($sql_q);
elseif($DB=='mysqli') mysqli_query($mysqlink, $sql_q);
}
else{
$sql_q="insert {$Tun_point} (last_id, user_id) values ({$mslvPoint}, {$user_id})";
if($DB=='mysql') mysql_query($sql_q);
elseif($DB=='mysqli') mysqli_query($mysqlink, $sql_q);
}

header("{$rheader}{$main_url}/{$indexphp}");
exit;
}

if($action=='' or $action=='vtopic'){

$readTopics=array();

if($row=db_simpleSelect(0, $Tun_topics, 'topic_id, post_id', 'user_id', '=', $user_id)){
do{
$readTopics[$row[0]]=$row[1];
}
while($row=db_simpleSelect(1));
}

//print_r($readTopics);
//echo $mslvPoint;

}

}//mysql mode

}//user_id>0


/* --Unread Messages Indicator */