<?php

require_once("backend/functions.php");
  
  dbconn();
  
  global $CURUSER;

  # Headers are sent to prevent browsers from caching.. IE is still resistent sometimes
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header( "Cache-Control: no-cache, must-revalidate" ); 
header( "Pragma: no-cache" );
header("Content-Type: text/html; charset=UTF-8"); 
     


# if no id of the last known message id is set to 0
if (!$lastID) { $lastID = 0; }

# call to retrieve all messages with an id greater than $lastID
getData($lastID);

# function that do retrieve all messages with an id greater than $lastID
function getData($lastID) {
$path=dirname(__FILE__);
  require_once($path."/backend/ping.php"); # getting connection data
 global $CURUSER;
if ($CURUSER)

	{
        //check for new pm's
        $res = SQL_Query_exec("SELECT COUNT(*) FROM messages WHERE receiver=" . $CURUSER["id"] . " and unread='yes' AND location IN ('in','both')") or print(mysql_error());
        $arr = mysql_fetch_row($res);
        $unreadmail = $arr[0];
        if ($unreadmail)
		print("<font color=#FF0000 ><B><center>Vous avez reçu [<a href=mailbox.php?inbox class=link2 target=_blank><font color=#FF0000> $unreadmail </a>]  nouveaux messages ! Consultez les</center></b></a></font>&nbsp;&nbsp;");
    } 
 
 

    $sql =  "SELECT c.* FROM shoutbox c left join users u on c.uid=u.id left join groups g on g.group_id=u.class WHERE c.id > ".$lastID." ORDER BY c.id DESC LIMIT 10";
    $conn = getDBConnection(); # establishes the connection to the database
    $results = SQL_Query_exec($sql, $conn);
    
    # getting the data array
    while ($row = mysql_fetch_array($results)) {
    
    # getting the data array
        $id   = $row[id];
        $uid  = $row[uid];
        $time = date(' G:i ', 3600 + utc_to_tz_time($row['date']));
        //$name = $row[name];
        $text = $row[text];

if ( ($CURUSER["edit_users"]=="yes") || ($CURUSER['id'] == $row['uid']) ){
        $mid=$row['id'];
		$edit="<a href='javascript:delup($mid);' style='font-size: 9px'><img src=images/shout/delchat.gif border=0 title=Sup alt=del></a>";
		
	}else{
	$edit="#";
	
	}
	
$UClass=@mysql_fetch_array(@SQL_Query_exec("SELECT Color, level,  username, gender, avatar,uploaded, downloaded, privacy, country, added, age, class, donated, warned FROM users JOIN groups ON users.class=groups.group_id WHERE users.id=".$row[uid].""));

$don = $UClass["donated"] > 0 ? "<img src=".$site_config['SITEURL']."/images/users/money.png alt='' width='15' height='15' >" : "";

$warn = $UClass["warned"] == "yes" ? "<img src=".$site_config['SITEURL']."/images/users/warn.gif alt=''>" : "";

$av=$UClass['avatar'];
if(!empty($av)){
$av="<img src='".$UClass[avatar]."' alt='my_avatar' width='50 height='50'>";
}
else{
$av="<img src='images/default_avatar.png' alt='my_avatar' width='50' height='50'>";
}

if (!$UClass[avatar])
            $avatar = '<img border=0 width=100 src=images/default_avatar.png>';
                else $avatar = "<img border=0 width=100 src=$UClass[avatar]>";

 
if ($UClass['added'] == '0000-00-00 00:00:00')
                $UClass['added'] = '---';
                $added = date("d-M-Y", utc_to_tz_time($UClass['added']));

            if ($UClass['privacy'] == '')
                $UClass['privacy'] = '---';
                $privacylevel = ($UClass["privacy"]);  
            
 if($UClass["downloaded"] != 0){
                $ratio = number_format($UClass["uploaded"] / $UClass["downloaded"], 2);
                } else {
                    $ratio = "---";
                }
                $ratio = "<font color=white>$ratio</font>";    
@mysql_free_result($UClass);

$name = "".class_user($row['name'])."";
$name2 = "".$row['name']."";
  $mail = $CURUSER['username'] == $row['name'] ? "" : '<a href=mailbox.php?compose&id='.$row['uid'].' target="_parent"><img src="images/shout/pm.png" border="0" title="Envoyer un MP"></a>';
  #putting the chat together.  
      $chatout = "
                 <li><div class='chatoutput'><span class='name'><font color=#fff>".$time."</font>&nbsp;$av&nbsp;".$edit."&nbsp;$mail&nbsp;<a href=account-details.php?id=".$uid." \" onmouseover=\"return overlib('<table width=100% class=ttable_col1 cellspacing=0 cellpadding=4 align=center><tr style=background-color:#000000><td align=center><font color=#B7B7B7><b>$name2</b></font></td><td align=center></td></tr><tr valign=top><td align=center>$avatar</td><td colspan=2><div align=left></div><div style=margin-top:3px><font color=#B7B7B7><b>Inscrit le:</b></font> <font style=margin-left:24px color=#fff><small><b>$added</b></small></font></div><div style=margin-top:3px><font color=#fff>Uploader:</font> <font style=margin-left:24px color=limegreen><b>$useruploaded</b></font></div><div style=margin-top:3px><font color=#fff>Downloader:</font> <font style=margin-left:7px color=red><b>$userdownloaded</b></font></div><div style=margin-top:3px><font color=#fff>Ratio:</font> <font style=margin-left:19px><b>$ratio</b></font></div><div style=margin-top:3px><font color=#fff>Niveau de confidentialit&eacute;: </font><font style=margin-left:2px><b><font color=red>$privacylevel</font></b></font></div></td></tr></table>');\" onmouseout=\"return nd();\"><b>".$name."</b></a>".$warn." : <font color=\"".$UClass['Color']."\">".format_comment($text)."</font></span></div></li>

                 ";

         echo $chatout; # echo as known handles arrays very fast...
}
    } 

  function execcommand_message ($message = '<div style="background: #000000; border: 1px solid #EA5F00; padding-left: 5px; color:orangered;">Votre commande a été exécutée. (Les résultats peuvent apparaître dans le prochain refresh!)</div>', $forcemessage = false)
  {
    if ((mysql_affected_rows () OR $forcemessage))
    {
      echo $message;
    }

  }
  function execcommand_clean ($Data)
  {
    $Data = trim ($Data[0][1]);
    if (empty ($Data))
    {
	  
      (@SQL_Query_exec ("TRUNCATE ajshoutbox") OR sqlerr (__FILE__, 284));
	  
      execcommand_message ();
    }
    else
    {
	  
      $query = @SQL_Query_exec ("SELECT id FROM users WHERE username = " . sqlesc ($Data));
      if (0 < mysql_num_rows ($query))
      {
        $Userid = mysql_result ($query, 0, 'id');
		
        (@SQL_Query_exec ("delete from ajshoutbox where uid = " . sqlesc ($Userid)) OR sqlerr (__FILE__, 293));
        execcommand_message ();
      }
    }

    return true;
  }
  function execcommand_noclean ($Data)
  {
      
    (@SQL_Query_exec ("delete from ajshoutbox WHERE text='/clean'") OR sqlerr (__FILE__, 284));
      
   }
   
?>