<?php

/**
 *
 *  Library to send contact form by email
 * USE ONLY ON MOBILE
 *
 * @author flabadens & predecessor
 * @version 1.0
 * @see contact.inc, contact.php
 * @todo
 * 
 *
 */
if ($versionmobile){
    include_once("../config.php3");
    $RACINE_DIR="../";
    $CORE = "CORE/";
}
else {
    include_once("config.php3");
    $RACINE_DIR="";
}

include_once($RACINE_DIR.$MOBILE_DIR."configMobile.php3");
include_once($RACINE_DIR.$BIB_DIR_FROM_RACINE."bib_db.php3");
include_once($RACINE_DIR.$BIB_DIR_FROM_RACINE."bib_generic.php3");
include_once($RACINE_DIR.$BIB_DIR_FROM_RACINE."bib_mailing.php3");


/**
 *
 * Send a contact form
 *
 * @param string $email_expediteur
 * @param string $message
 * @param string $sujet
 * @return string
 */
function sendmail($email_expediteur,$message,$sujet, $sessid) {

$SUJET_PREFIXE = "SCl : ";

//$NB_SUJETS = 2;

$SUJET = $GAME_NAME.$sujet;

$SUJET_APPARENT = $sujet;

//$SUJET_MAIL_DEST = $CUSTOMER_SERVICE;


  $sess = open_page($sessid);

  if (checkemail($email_expediteur)==false)
	$error = "Vous devez saisir une adresse email correcte";
  else if(empty($message))
	$error = "Vous devez saisir un message";

  else {

	if ($sess->memberid > 0){
		$member = getmember($sess->memberid);
		$player = getplayer($sess->memberid);
		$joueur_logge = true;
  	}
  	else {
		$joueur_logge = false;
  	}

	if(isset($player)) {
		$playerId=$player[Id];
	}
	else {
		$playerId=0;
	}

	$date= date("Y-m-d H:i:s", my_local_time());

	main_db_query("INSERT INTO SAV (aDate,Traitement,PlayerId, Subject,Email,Contenu) VALUES ('".$date."','0','".$playerId."', '".$SUJET_APPARENT."','".$email_expediteur."','".$message."')");


  	$message_complet = "<html><head>\n";
  	$message_complet .= "<link rel=\"stylesheet\" href=\"".$SERVER_URL."common.css\">\n";
	$message_complet .= "</head><body><font size=2 face=Verdana color=black>";
  	$date = date("Y-m-d H:i:s", my_local_time());

	$message_complet .= "Date d'envoi : ".$date."<br><br>";


  	$subject = $SUJET_PREFIXE.$SUJET;
  	$message_complet .= "<br>---------------------------------------------------<br><br>";
  	$message_complet .= "Sujet : ".$subject."<br><br>";

	$message = nl2br($message);
  	$message_complet .= "$message<br>";
  	$message_complet .= "<br>---------------------------------------------------<br><br>";

	 if ($joueur_logge == true)
  	{
  	  $message_complet .= "joueur loggé :<br>";
  	  $message_complet .= "- pseudo : ".$player['NickName']." (PlayerId : ".$player['Id'].", MemberId : ".$player['MemberId']." )<br>";
  	  $message_complet .= "<br>";
  	}
  	else
  	{
  	  $message_complet .= "joueur non loggé.<br>";
  	}



  	$message_complet.="</body></html>";

	//debug("DEST:$SUJET_MAIL_DEST");
	generic_mail($SUJET_MAIL_DEST, $subject, $message_complet,
	       "From: $email_expediteur\n" .
	       "Reply-To: $email_expediteur\n" .
	       "X-Mailer: PHP/" . phpversion()."\n".
	       "MIME-Version: 1.0\n".
		"Content-Type: text/html; charset=iso-8859-1");

  	$error="Un email a été expédié au service clients.<br> Nous vous remercions et traitons votre demande dans les plus brefs délais.";


  }
  
  return $error;
}
?>
