<?php
session_start();
include_once 'includes/inter.php';
$query = 'SELECT m.ID AS messageID,u.ID AS senderID, u.pseudo AS pseudo, m.texte AS texte, m.sendTime AS sendTime, UNIX_TIMESTAMP(m.sendTime) AS sendTimestamp FROM users AS u INNER JOIN messages AS m ON u.ID = m.senderID WHERE m.discussion_id=?';

$data = array ();
$pseudoID = -1;
$adminLevel = -1;
if(isConnected()){
	$pseudoID = $_SESSION['session_id'];
	$adminLevel = getAdminLevel();
}

$req = $GLOBALS ['bdd']->prepare ( $query );
$req->execute ( array (
		$_GET ['d'] 
) );
while ( $donnees = $req->fetch () ) {
	$donnees['rights'] = $donnees['senderID'] == $pseudoID || $adminLevel>=6;
	array_push ( $data, $donnees );
	$bool = true;
}

$req->closeCursor ();
echo json_encode ( $data );

flush ();