calDAV v2
Deploy HouseHub / deploy (push) Successful in 2s

This commit is contained in:
Cedric
2026-05-13 15:54:10 +02:00
parent 912734d2cc
commit d660d2cc7a
4 changed files with 282 additions and 28 deletions
+11 -3
View File
@@ -133,7 +133,15 @@ if ($action === 'sync' && $method === 'POST') {
ios_require_csrf();
if (!$integration) ios_err('Connexion iCloud non configurée.', 400);
$remoteEvents = ios_fetch_remote_events($integration);
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;
@@ -142,7 +150,7 @@ if ($action === 'sync' && $method === 'POST') {
foreach ($localEvents as $evt) {
if ($evt['sync_state'] === 'pending_push' || empty($evt['external_uid'])) {
$push = ios_push_event_to_remote($integration, $evt);
$push = ios_push_event_to_remote($integration, $evt, $davPassword);
if ($push['code'] >= 200 && $push['code'] < 300) {
$uid = $evt['external_uid'] ?: ('hh-' . $evt['id'] . '@househub');
$pdo->prepare("UPDATE pf_calendar_events SET external_uid=?, sync_state='synced', updated_at=NOW() WHERE id=?")->execute([$uid, $evt['id']]);
@@ -155,7 +163,7 @@ if ($action === 'sync' && $method === 'POST') {
$pendingDelete = $pdo->query("SELECT id, external_uid FROM pf_calendar_events WHERE deleted_at IS NOT NULL AND sync_state='pending_delete'")->fetchAll();
foreach ($pendingDelete as $evt) {
if (!empty($evt['external_uid'])) {
ios_delete_remote_event($integration, $evt['external_uid']);
ios_delete_remote_event($integration, $evt['external_uid'], $davPassword);
}
$pdo->prepare("DELETE FROM pf_calendar_event_links WHERE calendar_event_id=?")->execute([$evt['id']]);
$pdo->prepare("DELETE FROM pf_calendar_events WHERE id=?")->execute([$evt['id']]);
+23 -6
View File
@@ -1,5 +1,25 @@
<?php
require_once dirname(__DIR__, 2) . '/includes/crypto.php';
require_once dirname(__DIR__, 2) . '/includes/icloud_caldav.php';
/**
* Mot de passe normalisé + URL calendrier (découverte si racine iCloud).
*
* @return array{integration: array, password: string}
*/
function ios_caldav_prepare(array $integration, ?PDO $metaPdo = null): array
{
$password = hh_normalize_apple_app_password(hh_decrypt_secret($integration['secret_encrypted']));
$url = trim($integration['calendar_url'] ?? '');
$resolvedUrl = hh_icloud_resolve_calendar_url_if_needed($integration['username'], $password, $url);
$out = $integration;
$out['calendar_url'] = $resolvedUrl;
if ($metaPdo && !empty($integration['id']) && rtrim($url, '/') !== rtrim($resolvedUrl, '/')) {
$metaPdo->prepare('UPDATE user_calendar_integrations SET calendar_url = ? WHERE id = ?')
->execute([$resolvedUrl, $integration['id']]);
}
return ['integration' => $out, 'password' => $password];
}
function ios_make_ics(array $event): string
{
@@ -46,9 +66,8 @@ function ios_caldav_request(string $url, string $username, string $password, str
];
}
function ios_fetch_remote_events(array $integration): array
function ios_fetch_remote_events(array $integration, string $password): array
{
$password = hh_decrypt_secret($integration['secret_encrypted']);
$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'] . ')');
@@ -82,18 +101,16 @@ function ios_fetch_remote_events(array $integration): array
return $events;
}
function ios_push_event_to_remote(array $integration, array $event): array
function ios_push_event_to_remote(array $integration, array $event, string $password): array
{
$password = hh_decrypt_secret($integration['secret_encrypted']);
$uid = $event['external_uid'] ?: ('hh-' . $event['id'] . '@househub');
$url = rtrim($integration['calendar_url'], '/') . '/' . rawurlencode($uid) . '.ics';
$ics = ios_make_ics($event);
return ios_caldav_request($url, $integration['username'], $password, 'PUT', $ics);
}
function ios_delete_remote_event(array $integration, string $externalUid): array
function ios_delete_remote_event(array $integration, string $externalUid, string $password): array
{
$password = hh_decrypt_secret($integration['secret_encrypted']);
$url = rtrim($integration['calendar_url'], '/') . '/' . rawurlencode($externalUid) . '.ics';
return ios_caldav_request($url, $integration['username'], $password, 'DELETE', null, ['Accept: */*']);
}