DSB-PlugIn/AddOn/Calender_Office365_User/OutlookCalendar.php

130 lines
4.0 KiB
PHP

<?php
class OutlookCalendar {
private $clientId;
private $clientSecret;
private $redirectUri;
private $accessToken;
private $refreshToken;
public function __construct($clientId, $clientSecret, $redirectUri, $accessToken, $refreshToken) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->redirectUri = $redirectUri;
$this->accessToken = $accessToken;
$this->refreshToken = $refreshToken;
}
// ------------------------------------------
// 🔁 Refresh Token verwenden
public function refreshAccessToken() {
$url = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
$data = [
'grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
'scope' => 'https://graph.microsoft.com/.default'
];
$response = $this->sendRequest($url, $data, true);
if (isset($response['access_token'])) {
$this->accessToken = $response['access_token'];
$this->refreshToken = $response['refresh_token'] ?? $this->refreshToken;
}
return $response;
}
// ------------------------------------------
// Termin erstellen
public function createEvent($eventData) {
$url = "https://graph.microsoft.com/v1.0/me/events";
return $this->sendRequest($url, $eventData, false, 'POST');
}
// Termin bearbeiten
public function updateEvent($eventId, $eventData) {
$url = "https://graph.microsoft.com/v1.0/me/events/{$eventId}";
return $this->sendRequest($url, $eventData, false, 'PATCH');
}
// Termin löschen
public function deleteEvent($eventId) {
$url = "https://graph.microsoft.com/v1.0/me/events/{$eventId}";
return $this->sendRequest($url, [], false, 'DELETE');
}
// Termin laden
public function getEvents($top = 10) {
$url = "https://graph.microsoft.com/v1.0/me/events?\$top=" . intval($top);
$headers = [
"Authorization: Bearer " . $this->accessToken,
"Accept: application/json"
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
return json_decode($response, true);
} else {
return [
"error" => true,
"status" => $httpCode,
"message" => $response
];
}
}
// ------------------------------------------
// 🔧 API Request senden (intern)
private function sendRequest($url, $data = [], $isForm = false, $method = 'POST') {
$ch = curl_init($url);
$headers = [
'Authorization: Bearer ' . $this->accessToken,
];
if ($isForm) {
$body = http_build_query($data);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
} else {
$body = json_encode($data);
$headers[] = 'Content-Type: application/json';
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($method !== 'GET' && !empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
return ['error' => $err];
}
return json_decode($response, true);
}
// 🔓 Getter für AccessToken
public function getAccessToken() {
return $this->accessToken;
}
public function getRefreshToken() {
return $this->refreshToken;
}
}