<?php
include_once 'includes/bdd.php';
function isConnected($sid = NULL, $mdp = NULL) {
	if (! isset ( $sid ) or ! isset ( $mdp )) {
		$sid = $_SESSION ['session_id'];
		$mdp = $_SESSION ['session_mdp'];
	}
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE ID=?' );
	$req->execute ( array (
			$sid 
	) );
	if ($rep = $req->fetch ()) {
		$connected = password_verify ( $mdp, $rep ['mdp'] );
	} else {
		$connected = FALSE;
	}
	$req->closeCursor ();
	return $connected;
}
function getAdminLevel($sid = NULL, $mdp = NULL) {
	if (! isset ( $sid ) or ! isset ( $mdp )) {
		$sid = $_SESSION ['session_id'];
		$mdp = $_SESSION ['session_mdp'];
	}
	if (! isConnected ( $sid, $mdp ))
		return - 1;
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE ID=?' );
	$req->execute ( array (
			$sid 
	) );
	if ($result = $req->fetch ()) {
		$adminLevel = $result ['administration'];
	} else {
		$adminLevel = - 1;
	}
	$req->closeCursor ();
	return $adminLevel;
}
function getPseudo($sid = NULL, $mdp = NULL) {
	if (! isset ( $sid ) or ! isset ( $mdp )) {
		$sid = $_SESSION ['session_id'];
		$mdp = $_SESSION ['session_mdp'];
	}
	if (! isConnected ( $sid, $mdp ))
		return "neant";
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE ID=?' );
	$req->execute ( array (
			$sid 
	) );
	$pseudo = $req->fetch () ['pseudo'];
	$req->closeCursor ();
	return $pseudo;
}
function getPseudoOf($sid) {
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE ID=?' );
	$req->execute ( array (
			$sid 
	) );
	$pseudo = $req->fetch () ['pseudo'];
	$req->closeCursor ();
	return $pseudo;
}
function tryToConnect($pseudo = NULL, $mdp = NULL) {
	if (! isset ( $pseudo ) or ! isset ( $mdp )) {
		$pseudo = $_POST ['pseudo'];
		$mdp = $_POST ['mdp'];
	}
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE pseudo=?' );
	$req->execute ( array (
			$pseudo 
	) );
	$reponce = $req->fetch ();
	if ($reponce != NULL) {
		if (password_verify ( $mdp, $reponce ['mdp'] )) {
			$req->closeCursor ();
			return $reponce ['ID'];
		} else {
			$req->closeCursor ();
			return 'errormdp';
		}
	} else {
		$req->closeCursor ();
		return 'errorpseudo';
	}
}
function getProjectVersionData($project = NULL, $version = NULL) {
	if (! isset ( $project ) or ! isset ( $version )) {
		$project = $_GET ['id'];
		$version = $_GET ['v'];
	}
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT p.ID AS pID ,' . ' ' . 'v.versionAbs AS versionAbs , ' . ' ' . 'p.name AS name ,' . ' ' . 'v.version AS version ,' . ' ' . 'v.language AS language ,' . ' ' . 'p.permissions AS permissions ,' . ' ' . 'p.ownersID AS owners' . ' ' . 'FROM projets AS p' . ' ' . 'INNER JOIN versions AS v' . ' ' . 'ON v.project_id = p.ID' . ' ' . 'WHERE v.project_id = ? AND v.versionAbs = ?' );
	$req->execute ( array (
			$project,
			$version 
	) );
	if (($data = $req->fetch ())) {
		$req->closeCursor ();
		return $data;
	} else {
		$req->closeCursor ();
		return NULL;
	}
}
function getProjectData($project = NULL) {
	if (! isset ( $project )) {
		$project = $_GET ['id'];
	}
	$req = $GLOBALS ['bdd']->prepare ( 'SELECT p.ID AS ID ,' . ' ' . 'p.name AS name ,' . ' ' . 'p.permissions AS permissions ,' . ' ' . 'p.ownersID AS owners' . ' ' . 'FROM projets AS p' . ' ' . 'WHERE p.ID = ?' );
	$req->execute ( array (
			$project 
	) );
	if (($data = $req->fetch ())) {
		$req->closeCursor ();
		return $data;
	} else {
		$req->closeCursor ();
		return NULL;
	}
}
function getPermissions($data) {
	$permissions = array (
			FALSE,
			FALSE,
			FALSE,
			FALSE 
	);
	$permissions [0] = preg_match ( '#^1#', $data ['permissions'] );
	$permissions [1] = preg_match ( '#^(0|1)1#', $data ['permissions'] );
	$permissions [2] = preg_match ( '#^(0|1){2}1#', $data ['permissions'] );
	$permissions [3] = preg_match ( '#^(0|1){3}1#', $data ['permissions'] );
	return $permissions;
}
function getProjectsWithFirstPermission() {
	$requete = 'SELECT p.ID AS projectID , v.versionAbs AS versionAbs , p.name AS projectName , v.version AS versionName ' . 'FROM projets AS p ' . 'INNER JOIN versions AS v ' . 'ON v.project_id = p.ID ' . 'WHERE p.permissions LIKE "1%" ';
	$req = $GLOBALS ['bdd']->query ( $requete );
	$projects = array ();
	
	while ( $rep = $req->fetch () ) {
		if (! isset ( $projects [$rep ['projectID']] )) {
			$projects [$rep ['projectID']] = $rep;
		} else if ($projects [$rep ['projectID']] ['versionAbs'] < $rep ['versionAbs']) {
			$projects [$rep ['projectID']] = $rep;
		}
	}
	$req->closeCursor ();
	return $projects;
}
function getVersions($pid = NULL) {
	if (! isset ( $pid )) {
		$pid = $_GET ['id'];
	}
	
	$requete = 'SELECT * FROM versions WHERE project_id = ?';
	$req = $GLOBALS ['bdd']->prepare ( $requete );
	$req->execute ( array (
			$pid 
	) );
	// ADDPOINT langage
	$versions = array (
			array (),
			array () 
	);
	while ( $rep = $req->fetch () ) {
		$versions [$rep ['language']] [] = array (
				'versionAbs' => $rep ['versionAbs'],
				'versionName' => $rep ['version'] 
		);
	}
	$req->closeCursor ();
	return $versions;
}
function getOwnersisProjects($sid = NULL) {
	if (! isset ( $sid )) {
		$sid = $_SESSION ['session_id'];
	}
	$requete = 'SELECT p.ownersID AS ownersID , p.ID AS projectID , v.versionAbs AS versionAbs , p.name AS projectName , v.version AS versionName ' . 'FROM projets AS p ' . 'INNER JOIN versions AS v ' . 'ON v.project_id = p.ID ' . 'WHERE p.ownersID REGEXP \'[^0-9]?' . $sid . '[^0-9]?\' ';
	$req = $GLOBALS ['bdd']->query ( $requete );
	$projects = array ();
	while ( $rep = $req->fetch () ) {
		if (! isset ( $projects [$rep ['projectID']] )) {
			$projects [$rep ['projectID']] = $rep;
		} else if ($projects [$rep ['projectID']] ['versionAbs'] < $rep ['versionAbs']) {
			$projects [$rep ['projectID']] = $rep;
		}
	}
	return $projects;
}
function registerPerson($pseudo, $mdp) {
	$req = $GLOBALS ['bdd']->prepare ( "SELECT * FROM users WHERE pseudo=?" );
	$req->execute ( array (
			$_POST ['pseudo'] 
	) );
	if ($req->fetch ()) {
		return 'usedPseudo';
	} else {
		$req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO users(pseudo,mdp,date_creation) VALUES (?,?,NOW())' );
		$req->execute ( array (
				$_POST ['pseudo'],
				password_hash ( $_POST ['mdp'], PASSWORD_DEFAULT ) 
		) );
		return 'ok';
	}
}
function getMessages($discut = NULL) {
	if (! isset ( $discut )) {
		$discut = $_GET ['d'];
	}
	$req = $GLOBALS ['bdd']->prepare ( "SELECT * FROM messages WHERE discussion_id=? ORDER BY sendTime DESC" );
	$req->execute ( array (
			$discut 
	) );
	$rep = array ();
	while ( $rep [] = $req->fetch () )
		;
	if (count ( $rep ) <= 0)
		$rep = NULL;
	$req->closeCursor ();
	return array_slice ( $rep, 0, - 1, TRUE );
}
function getDiscuts() {
	$req = $GLOBALS ['bdd']->query ( "SELECT * FROM discussions ORDER BY date_creation DESC" );
	$rep = array ();
	while ( $rep [] = $req->fetch () )
		;
	if (count ( $rep ) <= 1)
		$rep = NULL;
		$req->closeCursor ();
		return array_slice ( $rep, 0, - 1, TRUE );
}
function getDiscutInfos($did = NULL) {
	if(!isset($did)){
		$did = $_GET['d'];
	}
	$req = $GLOBALS ['bdd']->query ( "SELECT * FROM discussions WHERE ID=?" );
	return $req->fetch();
}
function sendMessage($discut = NULL, $text = NULL, $sender = NULL) {
	if (! isset ( $discut ) || ! isset ( $text ) || ! isset ( $sender )) {
		$discut = $_GET ['d'];
		$text = $_POST ['msg'];
		$sender = $_SESSION ['session_id'];
	}
	$req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO messages(discussion_id,texte,senderID,sendTime) VALUES (?,?,?,NOW())' );
	$req->execute ( array (
			$discut,
			htmlspecialchars ( $text ),
			$sender 
	) );
	return 'ok';
}
function createDiscut($name = NULL, $owner = NULL) {
	if (! isset ( $name ) || ! isset ( $owner )) {
		$name = $_POST ['name'];
		$owner = $_SESSION ['session_id'];
	}
	$req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO discussions(name,creator_id,date_creation) VALUES (?,?,NOW())' );
	$req->execute ( array (
			$name,
			$owner 
	) );
	return 'ok';
}
