<?php
function getAppointments($username, $password, $url)
{
$request = '<?xml version="1.0"?>';
$request.= '<d:searchrequest xmlns:d="DAV:" xmlns:cal="urn:schemas:calendar:">';
$request.= '<d:sql> SELECT "DAV:href", "urn:schemas:calendar:dtstart", "urn:schemas:calendar:dtend", "urn:schemas:httpmail:subject" FROM Scope(\'SHALLOW TRAVERSAL OF "'.$url.'" \') WHERE "DAV:contentclass" = \'urn:content-classes:appointment\' AND "urn:schemas:calendar:dtstart" >= CAST("'.date("c").'" AS "dateTime.tz") ORDER BY "urn:schemas:calendar:dtstart" ASC</d:sql>';
$request.= '</d:searchrequest>';
$context = stream_context_create(array(
'http' => array(
'method' => 'SEARCH',
'header' => array("Content-type: text/xml", "Depth: 1,noroot", "Authorization: Basic " . base64_encode($username . ":" . $password)),
'content' => $request,
)
));
$data = str_replace(":", "_", file_get_contents($url, false, $context));
return $data;
}
$appointments = array();
for ($i = 0 ; $i < 2 ; $i++)
{
switch ($i)
{
case 0:
$url = 'https://server/exchange/erik/Calender/';
$username = "erik";
$password = "";
break;
case 1:
$url = 'https://server/exchange/marloes/Calender/';
$username = "marloes";
$password = "";
break;
}
$xml = simplexml_load_string(getAppointments($username, $password, $url));
foreach ($xml->a_response as $waarde)
{
$dt = str_replace("_", ":", $waarde->a_propstat->a_prop->d_dtstart);
$dt = strtotime($dt);
if (array_key_exists($dt, $appointments))
{
if ($appointments[$dt]["title"] == (string)$waarde->a_propstat->a_prop->e_subject)
{
$appointments[$dt]["both"] = true;
}
else
{
$dt++;
}
}
$appointments[$dt]["title"] = (string)$waarde->a_propstat->a_prop->e_subject;
$appointments[$dt]["username"] = $username;
}
}
ksort($appointments, SORT_NUMERIC);
foreach ($appointments as $time => $value)
{
$appt_json[] = array("date" => date('c', $time), "title" => $value["title"], "username" => ($value["both"] ? "both" : $value["username"]));
}
echo json_encode($appt_json);
?>