Files
thetool/application/Api/v1/CalendarApicontroller.php
Daniel Spitzer f36bb73cb7 API Anpassungen
* Abwesenenheiten für Kalender implementiert
2025-05-25 18:49:15 +02:00

299 lines
12 KiB
PHP

<?php
class CalendarApicontroller extends mfBaseApicontroller
{
protected function init()
{
$this->addRoute("/calendar/calendarStream", "calendarStream", "GET");
$this->addRoute("/calendar/calendarNotify", "calendarNotify", "POST");
$this->addRoute("/calendar/calendarUserSubscriptions", "getcalendarUserSubscriptions", "GET");
$this->addRoute("/calendar/calendarUserSubscriptions", "updatecalendarUserSubscriptions", "POST");
$this->addRoute("/calendar/calendarUsers", "getcalendarUsers", "GET");
$this->addRoute("/calendar/calendarAbsence", "getCalendarAbsence", "GET");
$this->addRoute("/calendar/calendarAbsence", "updateCalendarAbsence", "POST");
}
protected function calendarNotify()
{
$myfile = fopen(__DIR__ . "/../../../var/log/microsoft_notify_log.txt", "a") or die("Unable to open file!");
$get = json_encode($_GET);
fwrite($myfile, "\nGET: " . $get);
$post = serialize($_POST);
fwrite($myfile, "\nPOST: " . $post);
if ($_GET['validationToken']) {
echo $_GET['validationToken'];
die();
}
$input = file_get_contents('php://input');
fwrite($myfile, "\nINPUT: " . $input);
$result = 'ok';
CalendarModel::insertMicrosoftCalendarEvent($input);
die();
}
protected function getcalendarUsers()
{
$users = UserModel::search(['employee' => 'true']);
$json['data'] = $users;
$json['status'] = "success";
$result = json_encode($json);
echo $result;
die();
}
protected function getCalendarAbsence()
{
$starttime = time() - 86400 * 30;
$db = FronkDB::singleton();
$sql = "SELECT `Timerecording`.`id`, `Timerecording`.`start` start_time, `Timerecording`.`end` end_time,`Calendar`.`microsoft_id`,`Timerecording`.`microsoft_event_id`,`Calendar`.`go_calendar_id` calendar_id,`TimerecordingCategory`.`name`,`TimerecordingCategory`.`id` CategoryId,`Timerecording`.`create` ctime,`Timerecording`.`edit` mtime FROM `Timerecording`
INNER JOIN `TimerecordingCategory` ON `TimerecordingCategory`.`id`=`Timerecording`.`timerecordingCategory_id`
INNER JOIN `Calendar` ON `Calendar`.`user_id`=`Timerecording`.`user_id`
WHERE `TimerecordingCategory`.`hourday`!='1' AND `TimerecordingCategory`.`hourday`!='7' AND `TimerecordingCategory`.`hourday`!='5' AND `Timerecording`.start >= $starttime
";
//AND `Timerecording`.`user_id` = '173'
$res = $db->query($sql);
if ($db->num_rows($res)) {
while ($data = $db->fetch_array($res)) {
if (!$data['end_time']) {
continue;
}
if ($data['CategoryId'] != 11) {
$data['start_time'] = $data['start_time'];
$all_day_event = 1;
} else {
$starttime = date("Y-m-d H:i", $data['start_time']);
$endtime = date("Y-m-d H:i", $data['end_time']);
$all_day_event = 0;
}
$rows[] = array(
'id' => $data['id'],
'name' => $data['name'],
'start_time' => $data['start_time'],
'end_time' => $data['end_time'],
'all_day_event' => $all_day_event,
'calendar_id' => $data['calendar_id'],
'microsoft_id' => $data['microsoft_id'],
'microsoft_event_id' => $data['microsoft_event_id'],
);
}
}
$json['success'] = true;
$json['data'] = $rows;
$json['status'] = "success";
$result = json_encode($json, JSON_UNESCAPED_UNICODE);
echo $result;
die();
}
protected function updateCalendarAbsence()
{
$id = $this->post['id'];
$microsoft_event_id = $this->post['microsoft_event_id'];
$timercording = new Timerecording($id);
if (!($timercording->id) || $timercording->id != $id) {
die();
}
$data = [];
if ($microsoft_event_id) {
$data['microsoft_event_id'] = $microsoft_event_id;
} else {
$data['microsoft_event_id'] = null;
}
$timercording->update($data);
$timercording->save();
$json['status'] = "success";
$result = json_encode($json);
echo $result;
die();
}
protected function getcalendarUserSubscriptions()
{
if ($_GET['onlymsuser']) {
$userSubscriptions = CalendarModel::search(array("checkSubscriptions" => 1));
} else {
$userSubscriptions = CalendarModel::getAll();
}
$json['data'] = $userSubscriptions;
$json['status'] = "success";
$result = json_encode($json);
echo $result;
die();
}
protected function updatecalendarUserSubscriptions()
{
$id = $this->post['id'];
$user_id = $this->post['user_id'];
$go_calendar_id = $this->post['go_calendar_id'];
$microsoft_id = $this->post['microsoft_id'];
$subscription_id = $this->post['subscription_id'];
$expirationDateTime = $this->post['expirationDateTime'];
$Calendar = new Calendar($id);
if (!($Calendar->id) || $Calendar->id != $id) {
$mode = "add";
} else {
$mode = "update";
}
$data = [];
if ($user_id) {
$data['user_id'] = $user_id;
}
if ($go_calendar_id) {
$data['go_calendar_id'] = $go_calendar_id;
}
if ($microsoft_id) {
$data['microsoft_id'] = $microsoft_id;
}
if ($subscription_id) {
$data['subscription_id'] = $subscription_id;
}
if ($expirationDateTime) {
$data['expirationDateTime'] = $expirationDateTime;
}
if ($mode == "add") {
$data['create_by'] = '154';
}
$data['edit_by'] = '154';
$Calendar->update($data);
$Calendar->save();
$json['status'] = "success";
$result = json_encode($json);
echo $result;
die();
}
protected function decryptString($cipherText, $password, $salt)
{
// Definiere den Algorithmus und die Länge des Initialisierungsvektors
$cipher = "aes-256-cbc";
$ivlen = openssl_cipher_iv_length($cipher);
// Dekodiere den verschlüsselten Text von base64
$cipherText = base64_decode($cipherText);
// Extrahiere den Initialisierungsvektor und den verschlüsselten Text
$iv = substr($cipherText, 0, $ivlen);
$cipherText = substr($cipherText, $ivlen);
// Erzeuge einen Schlüssel aus dem Passwort und dem Salt
$key = hash_pbkdf2("sha256", $password, $salt, 1000, 32, true);
// Entschlüssele den Text
$plainText = openssl_decrypt($cipherText, $cipher, $key, 0, $iv);
return $plainText;
}
protected function calendarStream()
{
$decstring = $this->decryptString($_GET['user'], "testpw", "testsalt");
date_default_timezone_set('Europe/Berlin');
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
$timestamp = time();
$Calendar = CalendarModel::search(array("user_id" => $decstring));
$rights = json_decode($Calendar[0]->rights, true);
if (!$Calendar[0]->rights) {
$allowedCalendar[] = $Calendar[0]->calendar_id;
} else {
foreach (json_decode($Calendar[0]->rights, true) as $key => $value) {
$allowedCalendar[] = $key;
}
}
if ($Calendar[0]->colors) {
$calendarColors = json_decode($Calendar[0]->colors, true);
}
$redis = new Redis();
//Connecting to Redis
$redis->connect('172.16.5.5', '6379');
//$redis->auth('password');
$oldoutput = "";
$now = floor(microtime(true) * 1000);
$timenow = time();
while (true) {
$colors = $redis->get('thetool_calendar_usercolors_' . $decstring);
if ($colors) {
$calendarColors = json_decode($colors, true);
}
$output = $redis->get('thetool_calendar_event_tigger');
$json = json_decode($output, true);
$message[0] = $json;
if ($json['time'] > $now && $output !== $oldoutput) {
$timenow = time();
// $Calendar=CalendarModel::getCalendarEvents($decstring, $json['cal_events_id']);
$Calendarevent = json_decode(CalendarModel::getCalendarEvents($decstring, $json['cal_events_id']), true);
if ($json['all_day_event'] == 1) {
$message[0]['start_time'] = date('Y-m-d', $json['start_time']);
$message[0]['end_time'] = date('Y-m-d', $json['end_time']);
} else {
$message[0]['start_time'] = date('Y-m-d H:i', $json['start_time']);
$message[0]['end_time'] = date('Y-m-d H:i', $json['end_time']);
}
$message[0]['name'] = $Calendarevent['data'][0]['category']['category'];
$message[0]['description'] = $Calendarevent['data'][0]['description']['description'];
$message[0]['location'] = $Calendarevent['data'][0]['location']['location'];
$message[0]['event_type'] = $Calendarevent['data'][0]['event_type']['event_type'];
$message[0]['attachment'] = $Calendarevent['data'][0]['attachment']['attachment'];
$message[0]['attachments'] = $Calendarevent['data'][0]['attachments']['attachments'];
$message[0]['calendar_name'] = $Calendarevent['data'][0]['calendar_name']['calendar_name'];
$message[0]['isorganizer'] = $Calendarevent['data'][0]['isorganizer']['isorganizer'];
$message[0]['privateflag'] = $Calendarevent['data'][0]['privateflag']['privateflag'];
$message[0]['rrule'] = $Calendarevent['data'][0]['rrule']['rrule'];
$message[0]['duration'] = $Calendarevent['data'][0]['duration']['duration'];
$message[0]['mtime'] = $Calendarevent['data'][0]['mtime']['mtime'];
$message[0]['mname'] = $Calendarevent['data'][0]['mname']['mname'];
$message[0]['ctime'] = $Calendarevent['data'][0]['ctime']['ctime'];
$message[0]['cname'] = $Calendarevent['data'][0]['cname']['cname'];
$message[0]['busy'] = $Calendarevent['data'][0]['busy']['busy'];
$message[0]['canceld'] = $Calendarevent['data'][0]['canceld']['canceld'];
$message[0]['calendar_id_check'] = array('calendar_id' => $json['calendar_id'], 'order' => $json['calendar_id']);;
$message[0]['userr'] = $Calendar[0]->calendar_id;
$message[0]['bgColor'] = $calendarColors[$json['calendar_id']]['bgcolor'];
$message[0]['txtColor'] = $calendarColors[$json['calendar_id']]['txtcolor'];
$message[0]['rights'] = $rights[$json['calendar_id']];
$message[0]['me'] = $decstring;
$result = json_encode($message);
if (in_array($message[0]['calendar_id'], $allowedCalendar)) {
echo "data: " . ($result) . "\n\n";
}
unset($Array);
$now = floor(microtime(true) * 1000);;
}
if (ob_get_level() > 0) {
ob_flush();
}
if ($timenow + 300 < time()) {
die();
}
flush();
if (connection_aborted()) die();
usleep(100000);
$oldoutput = $output;;
}
die();
}
}