+1
-1
@@ -19,7 +19,7 @@ require __DIR__ . '/header.php';
|
||||
<button class="pf-btn btn-secondary" id="ios-sync-btn">Synchroniser</button>
|
||||
</div>
|
||||
</div>
|
||||
<p class="pf-muted-note">Créez vos événements ici, puis synchronisez-les avec iCloud CalDAV.</p>
|
||||
<p class="pf-muted-note">Les événements affichés viennent de HouseHub après import. Cliquez sur <strong>Synchroniser</strong> pour récupérer ceux qui sont déjà dans ton calendrier iCloud (fenêtre d’environ trois ans en arrière à quatre ans en avant).</p>
|
||||
</section>
|
||||
|
||||
<section class="pf-panel-card">
|
||||
|
||||
@@ -135,15 +135,13 @@ if ($action === 'sync' && $method === 'POST') {
|
||||
|
||||
try {
|
||||
$ctx = ios_caldav_prepare($integration, $meta_pdo);
|
||||
} catch (Throwable $e) {
|
||||
ios_err('CalDAV: ' . $e->getMessage(), 400);
|
||||
}
|
||||
$integration = $ctx['integration'];
|
||||
$davPassword = $ctx['password'];
|
||||
|
||||
$remoteEvents = ios_fetch_remote_events($integration, $davPassword);
|
||||
$remoteByUid = [];
|
||||
foreach ($remoteEvents as $re) $remoteByUid[$re['external_uid']] = $re;
|
||||
foreach ($remoteEvents as $re) {
|
||||
$remoteByUid[$re['external_uid']] = $re;
|
||||
}
|
||||
|
||||
$localStmt = $pdo->query("SELECT * FROM pf_calendar_events WHERE deleted_at IS NULL");
|
||||
$localEvents = $localStmt->fetchAll();
|
||||
@@ -185,7 +183,11 @@ if ($action === 'sync' && $method === 'POST') {
|
||||
}
|
||||
|
||||
$meta_pdo->prepare("UPDATE user_calendar_integrations SET last_sync_at=NOW(), status='connected' WHERE id=?")->execute([$integration['id']]);
|
||||
ios_ok(['message' => 'Synchronisation terminée.']);
|
||||
$n = count($remoteEvents);
|
||||
ios_ok(['message' => 'Synchronisation terminée. ' . $n . ' événement(s) lu(s) depuis iCloud.']);
|
||||
} catch (Throwable $e) {
|
||||
ios_err('CalDAV: ' . $e->getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
ios_err('Action inconnue', 404);
|
||||
|
||||
@@ -37,17 +37,23 @@ function ios_make_ics(array $event): string
|
||||
function ios_caldav_request(string $url, string $username, string $password, string $method = 'GET', ?string $body = null, array $headers = []): array
|
||||
{
|
||||
$ch = curl_init($url);
|
||||
$defaultCt = ($method === 'REPORT' || $method === 'PROPFIND')
|
||||
? 'application/xml; charset=utf-8'
|
||||
: 'text/calendar; charset=utf-8';
|
||||
$requestHeaders = array_merge([
|
||||
'Content-Type: text/calendar; charset=utf-8',
|
||||
'Content-Type: ' . $defaultCt,
|
||||
], $headers);
|
||||
$timeout = ($method === 'REPORT' || $method === 'PROPFIND') ? 60 : 20;
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 20,
|
||||
CURLOPT_TIMEOUT => $timeout,
|
||||
CURLOPT_CUSTOMREQUEST => $method,
|
||||
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
|
||||
CURLOPT_USERPWD => $username . ':' . $password,
|
||||
CURLOPT_HTTPHEADER => $requestHeaders,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
]);
|
||||
if ($body !== null) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
|
||||
@@ -66,41 +72,156 @@ function ios_caldav_request(string $url, string $username, string $password, str
|
||||
];
|
||||
}
|
||||
|
||||
function ios_fetch_remote_events(array $integration, string $password): array
|
||||
/**
|
||||
* Déplie les lignes ICS pliées (RFC 5545).
|
||||
*/
|
||||
function ios_ics_unfold(string $ics): string
|
||||
{
|
||||
$res = ios_caldav_request($integration['calendar_url'], $integration['username'], $password, 'GET', null, ['Accept: text/calendar']);
|
||||
if ($res['code'] < 200 || $res['code'] >= 400) {
|
||||
throw new RuntimeException('Lecture CalDAV impossible (HTTP ' . $res['code'] . ')');
|
||||
$ics = str_replace("\r\n", "\n", $ics);
|
||||
return preg_replace("/\n[ \t]/", '', $ics);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extrait les blocs texte <calendar-data> d’une réponse 207 REPORT (CalDAV).
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
function ios_caldav_extract_calendar_data_bodies(string $xmlBody): array
|
||||
{
|
||||
$dom = new DOMDocument();
|
||||
if (!@$dom->loadXML($xmlBody)) {
|
||||
return [];
|
||||
}
|
||||
$blocks = preg_split('/BEGIN:VEVENT|END:VEVENT/', $res['body']);
|
||||
$xpath = new DOMXPath($dom);
|
||||
$nodes = $xpath->query("//*[local-name()='calendar-data']");
|
||||
$out = [];
|
||||
foreach ($nodes as $node) {
|
||||
$t = trim($node->textContent);
|
||||
if ($t !== '') {
|
||||
$out[] = $t;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{external_uid:string,title:string,description:string,location:string,start_at:string,end_at:string}>
|
||||
*/
|
||||
function ios_parse_vevents_from_ics(string $ics): array
|
||||
{
|
||||
$ics = ios_ics_unfold($ics);
|
||||
$events = [];
|
||||
foreach ($blocks as $chunk) {
|
||||
if (strpos($chunk, 'UID:') === false) {
|
||||
if (!preg_match_all('/BEGIN:VEVENT\s*(.*?)\s*END:VEVENT/s', $ics, $matches)) {
|
||||
return [];
|
||||
}
|
||||
foreach ($matches[1] as $chunk) {
|
||||
if (!preg_match('/^UID:([^\r\n]+)/m', $chunk, $uidM)) {
|
||||
continue;
|
||||
}
|
||||
preg_match('/UID:(.+)\R/', $chunk, $uidM);
|
||||
preg_match('/SUMMARY:(.+)\R/', $chunk, $sumM);
|
||||
preg_match('/DESCRIPTION:(.+)\R/', $chunk, $descM);
|
||||
preg_match('/LOCATION:(.+)\R/', $chunk, $locM);
|
||||
preg_match('/DTSTART(?:;VALUE=DATE)?:(.+)\R/', $chunk, $startM);
|
||||
preg_match('/DTEND(?:;VALUE=DATE)?:(.+)\R/', $chunk, $endM);
|
||||
if (empty($uidM[1]) || empty($startM[1])) {
|
||||
$uid = trim($uidM[1]);
|
||||
$summary = '';
|
||||
if (preg_match('/^SUMMARY:([^\r\n]*)/m', $chunk, $m)) {
|
||||
$summary = str_replace('\\,', ',', str_replace('\\;', ';', trim($m[1])));
|
||||
}
|
||||
$description = '';
|
||||
if (preg_match('/^DESCRIPTION:([^\r\n]*)/m', $chunk, $m)) {
|
||||
$description = str_replace('\\,', ',', str_replace('\\;', ';', str_replace('\\n', "\n", trim($m[1]))));
|
||||
}
|
||||
$location = '';
|
||||
if (preg_match('/^LOCATION:([^\r\n]*)/m', $chunk, $m)) {
|
||||
$location = str_replace('\\,', ',', str_replace('\\;', ';', trim($m[1])));
|
||||
}
|
||||
$startRaw = null;
|
||||
if (preg_match('/^DTSTART[^:]*:([^\r\n]+)/m', $chunk, $m)) {
|
||||
$startRaw = trim($m[1]);
|
||||
}
|
||||
if ($startRaw === null || $startRaw === '') {
|
||||
continue;
|
||||
}
|
||||
$start = date('Y-m-d H:i:s', strtotime(trim($startM[1])));
|
||||
$end = !empty($endM[1]) ? date('Y-m-d H:i:s', strtotime(trim($endM[1]))) : $start;
|
||||
$endRaw = null;
|
||||
if (preg_match('/^DTEND[^:]*:([^\r\n]+)/m', $chunk, $m)) {
|
||||
$endRaw = trim($m[1]);
|
||||
}
|
||||
if (preg_match('/^\d{8}$/', $startRaw)) {
|
||||
$startTs = strtotime($startRaw . 'T000000 UTC');
|
||||
} else {
|
||||
$norm = preg_replace('/^(\d{8})T(\d{6})Z?$/', '$1T$2 UTC', $startRaw);
|
||||
$startTs = strtotime($norm ?: $startRaw);
|
||||
if ($startTs === false) {
|
||||
$startTs = strtotime($startRaw);
|
||||
}
|
||||
}
|
||||
if ($startTs === false) {
|
||||
continue;
|
||||
}
|
||||
if ($endRaw !== null && $endRaw !== '') {
|
||||
if (preg_match('/^\d{8}$/', $endRaw)) {
|
||||
$endTs = strtotime($endRaw . 'T000000 UTC');
|
||||
} else {
|
||||
$normE = preg_replace('/^(\d{8})T(\d{6})Z?$/', '$1T$2 UTC', $endRaw);
|
||||
$endTs = strtotime($normE ?: $endRaw);
|
||||
if ($endTs === false) {
|
||||
$endTs = strtotime($endRaw);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$endTs = $startTs;
|
||||
}
|
||||
if ($endTs === false) {
|
||||
$endTs = $startTs;
|
||||
}
|
||||
$events[] = [
|
||||
'external_uid' => trim($uidM[1]),
|
||||
'title' => trim($sumM[1] ?? ''),
|
||||
'description' => trim($descM[1] ?? ''),
|
||||
'location' => trim($locM[1] ?? ''),
|
||||
'start_at' => $start,
|
||||
'end_at' => $end,
|
||||
'external_uid' => $uid,
|
||||
'title' => $summary,
|
||||
'description' => $description,
|
||||
'location' => $location,
|
||||
'start_at' => date('Y-m-d H:i:s', $startTs),
|
||||
'end_at' => date('Y-m-d H:i:s', $endTs),
|
||||
];
|
||||
}
|
||||
return $events;
|
||||
}
|
||||
|
||||
function ios_fetch_remote_events(array $integration, string $password): array
|
||||
{
|
||||
$calUrl = rtrim($integration['calendar_url'], '/') . '/';
|
||||
$tStart = gmdate('Ymd\THis\Z', strtotime('-3 years'));
|
||||
$tEnd = gmdate('Ymd\THis\Z', strtotime('+4 years'));
|
||||
$reportXml = '<?xml version="1.0" encoding="UTF-8"?>'
|
||||
. '<C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:D="DAV:">'
|
||||
. '<D:prop><C:calendar-data/></D:prop>'
|
||||
. '<C:filter>'
|
||||
. '<C:comp-filter name="VCALENDAR">'
|
||||
. '<C:comp-filter name="VEVENT">'
|
||||
. '<C:time-range start="' . htmlspecialchars($tStart, ENT_XML1 | ENT_QUOTES, 'UTF-8') . '" end="' . htmlspecialchars($tEnd, ENT_XML1 | ENT_QUOTES, 'UTF-8') . '"/>'
|
||||
. '</C:comp-filter></C:comp-filter></C:filter>'
|
||||
. '</C:calendar-query>';
|
||||
|
||||
$res = ios_caldav_request($calUrl, $integration['username'], $password, 'REPORT', $reportXml, [
|
||||
'Accept: application/xml, text/xml',
|
||||
'Depth: 1',
|
||||
]);
|
||||
if (!in_array($res['code'], [200, 207], true)) {
|
||||
throw new RuntimeException('Lecture CalDAV (REPORT) impossible (HTTP ' . $res['code'] . ').');
|
||||
}
|
||||
$bodies = ios_caldav_extract_calendar_data_bodies($res['body']);
|
||||
$byUid = [];
|
||||
foreach ($bodies as $fragment) {
|
||||
foreach (ios_parse_vevents_from_ics($fragment) as $ev) {
|
||||
$byUid[$ev['external_uid']] = $ev;
|
||||
}
|
||||
}
|
||||
if ($byUid === [] && strpos($res['body'], '<multistatus') === false && strpos($res['body'], 'multistatus') === false) {
|
||||
$get = ios_caldav_request($calUrl, $integration['username'], $password, 'GET', null, ['Accept: text/calendar']);
|
||||
if ($get['code'] >= 200 && $get['code'] < 400 && str_contains($get['body'], 'BEGIN:VEVENT')) {
|
||||
foreach (ios_parse_vevents_from_ics($get['body']) as $ev) {
|
||||
$byUid[$ev['external_uid']] = $ev;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values($byUid);
|
||||
}
|
||||
|
||||
function ios_push_event_to_remote(array $integration, array $event, string $password): array
|
||||
{
|
||||
$uid = $event['external_uid'] ?: ('hh-' . $event['id'] . '@househub');
|
||||
|
||||
Reference in New Issue
Block a user