<?php

	header( 'content-type: text/html; charset=utf-8' );

function sendIcalEvent($from_name, $from_address, $to_name, $to_address, $startTime, $endTime, $subject, $description, $location, $str_date , $heure_debut, $heure_fin, $nb_crenaux )
{
    $domain = 'exchangecore.com';

	date_default_timezone_set('Europe/Paris');
//	date_default_timezone_set('Europe/Bucharest');
	
    //Create Email Headers
    $mime_boundary = "----Meeting Booking----".MD5(TIME());

    $headers = "From: ".$from_name." <".$from_address.">\n";
    $headers .= "Reply-To: ".$from_name." <".$from_address.">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
    $headers .= "Content-class: urn:content-classes:calendarmessage\n";
    
    //Create Email Body (HTML)
    $message = "--$mime_boundary\r\n";
    $message .= "Content-Type: text/html; charset=UTF-8\n";
    $message .= "Content-Transfer-Encoding: 8bit\n\n";
    $message .= "<html>\n";
    $message .= "<body>\n";
    $message .= '<p>'.$to_name.',</p>';
    //$message .= '<p>'.$corps_message.'</p>';
	$message .= "<p> Tu viens de réserver un ou plusieurs créneaux pour la date suivante: </p>";
    $message .= "<p> Le $str_date de $heure_debut à $heure_fin soit $nb_crenaux créneaux. </p>";
	$message .= '<p>'."Tu peux annuler un rendez-vous en utilisant l'interface Web.".'</p>';
    $message .= '<p>'."A bientôt :)".'</p>';
	$message .= "</body>\n";
    $message .= "</html>\n";
    $message .= "--$mime_boundary\r\n";

	//echo $message;
	
	
//	TZID:Europe/London
//TZURL:http://tzurl.org/zoneinfo-global/Europe/London
//X-LIC-LOCATION:Europe/London
	
    $ical = 'BEGIN:VCALENDAR' . "\r\n" .
    'PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN' . "\r\n" .
    'VERSION:2.0' . "\r\n" .
    'METHOD:REQUEST' . "\r\n" .
    'BEGIN:VTIMEZONE' . "\r\n" .
	'TZID:Central Europe Standard Time' . "\r\n" .
	'BEGIN:STANDARD' . "\r\n" .
 	'DTSTART:16011028T030000' . "\r\n" .
	'RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10' . "\r\n" .
	'TZOFFSETFROM:+0200' . "\r\n" .
	'TZOFFSETTO:+0100' . "\r\n" .
	'END:STANDARD' . "\r\n" .
	'BEGIN:DAYLIGHT' . "\r\n" .
	'DTSTART:16010325T020000' . "\r\n" .
	'RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3' . "\r\n" .
	'TZOFFSETFROM:+0100' . "\r\n" .
	'TZOFFSETTO:+0200' . "\r\n" .
	'END:DAYLIGHT' . "\r\n" .
	'END:VTIMEZONE' . "\r\n" .
	'BEGIN:VEVENT' . "\r\n" .
    'ORGANIZER;CN="'.$from_name.'":MAILTO:'.$from_address. "\r\n" .
    'ATTENDEE;CN="'.$to_name.'";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:'.$to_address. "\r\n" .
    'LAST-MODIFIED:' . date("Ymd\TGis") . "\r\n" .
    'UID:'.date("Ymd\TGis", strtotime($startTime)).rand()."@".$domain."\r\n" .
    'DTSTAMP:'.date("Ymd\TGis"). "\r\n" .
    'DTSTART;TZID="Central Europe Standard Time":'.date("Ymd\THis", strtotime($startTime)). "\r\n" .
    'DTEND;TZID="Central Europe Standard Time":'.date("Ymd\THis", strtotime($endTime)). "\r\n" .
    'TRANSP:OPAQUE'. "\r\n" .
    'SEQUENCE:1'. "\r\n" .
    'SUMMARY:' . $subject . "\r\n" .
    'LOCATION:' . $location . "\r\n" .
    'CLASS:PUBLIC'. "\r\n" .
    'PRIORITY:5'. "\r\n" .
    'BEGIN:VALARM' . "\r\n" .
    'TRIGGER:-PT15M' . "\r\n" .
    'ACTION:DISPLAY' . "\r\n" .
    'DESCRIPTION:Reminder' . "\r\n" .
    'END:VALARM' . "\r\n" .
    'END:VEVENT'. "\r\n" .
    'END:VCALENDAR'. "\r\n";
    $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
    $message .= "Content-Transfer-Encoding: 8bit\n\n";
    $message .= $ical;

    $mailsent = mail($to_address, $subject, $message, $headers);
	//echo $mailsent;
	//return ($mailsent)?(true):(false);
}
////////////////////////////////////////////
function create_time($heure)
{
		if(strlen($heure) == 5)
		{  	$pieces = explode("H",$heure);
			$heure_debut = $pieces[0].":".$pieces[1].":00";
		}
	
		if(strlen($heure) == 3)
		{	$heure_debut = substr($heure,0,-1).":00:00";
		}
		
		return($heure_debut);
}
/////////////////////////////////////////////////////
function create_stop_time($heure)
{
		switch ($heure)
			{
				case	"12:20:00":
						$heure_fin = "12:40:00";
						break;
				case	"12:40:00":
						$heure_fin = "13:00:00";
						break;
				case 	"13:00:00":
						$heure_fin = "13:20:00";
						break;
				case 	"13:20:00":
						$heure_fin = "13:40:00";
						break;	
				case	"13:40:00":
						$heure_fin = "14:00:00";
						break;
				case	"14:00:00":
						$heure_fin = "14:20:00";
						break;
				case 	"14:20:00":
						$heure_fin = "14:40:00";
						break;
				case 	"14:40:00":
						$heure_fin = "15:00:00";
						break;		
				
			} 
			
			return($heure_fin);
}
///////////////////////////////////////////////////////////////////////////////////////////////

function outlook($prenom, $email , $date, $str)
{
// construire les dates
	// remplacer la virgule par un espace
	$heure = str_replace(',', ' ', $str);
	
	//$date = "20-03-2015";

	$pieces = explode("-",$date);
	$str_date = $pieces[1]."/".$pieces[0]."/".$pieces[2];
    //echo $str_date;
		
	//echo '<br>';
	//$heure = "12H40 13H";
	//echo "---> " .$heure;
	
	$nb_crenaux = mb_substr_count($heure, "H");
	if(mb_substr_count($heure, "H") >= 2)
	{ // plusieurs creneau
		$pieces = explode(" ",$heure);
		
		$heure_debut = create_time($pieces[0]);
		//echo '<br>';
		//echo "Start ->" .$heure_debut;
		
		$val = $pieces[count($pieces)-1];
		$heure_fin = create_time($val);
				
		$heure_fin = create_stop_time($heure_fin);
		//echo '<br>';
		//echo "Stop ->" .$heure_fin;
	}
	else
	{ 	// 	un seul creneau
		
		$heure_debut = "";
		$heure_fin = "";
		
		$heure_debut = create_time($heure);
		//echo '<br>';
		//echo "Start ->" .$heure_debut;
		
		$heure_fin = create_stop_time($heure_debut);
		//echo '<br>';
		//echo "Stop ->" .$heure_fin;
		
	}	

	$startTime = $str_date ." " .$heure_debut;        // mois jour annee
	$endTime = $str_date ." " .$heure_fin;
	
	//echo '<br>';
	//echo $startTime ." -- " .$endTime;
	
	$from_name = "Robot Massage - no reply";        
	$from_address = $email; //"Nathalie.Janez@onsemi.com";        
	$to_name = $prenom; 		//"Blase";        
	$to_address = $email;	//"patrice.delpy@onsemi.com";    
	
	//$startTime = "03/20/2015 13:00:00";        // mois jour annee
	//$endTime = "03/20/2015 14:00:00";        
	$subject = "Massage - Confirmation de votre réservation";        
	$corps_message = "Tu viens de réserver un ou plusieurs créneax pour la date suivante:";        
	$location = "Salle de repos";
	
	echo '<br>';
	echo '<br>';
	
	//echo $startTime ." *** " .$endTime ." *** " .  $heure_debut ." *** " . $heure_fin;

	
	sendIcalEvent($from_name, $from_address, $to_name, $to_address, $startTime, $endTime, $subject, $corps_message, $location,  $date , $heure_debut, $heure_fin, $nb_crenaux );  
	
}	