-
Notifications
You must be signed in to change notification settings - Fork 3
/
roundcube_caldav.php
1840 lines (1534 loc) · 69.9 KB
/
roundcube_caldav.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
*
* Rouncube calDAV handling plugin
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
ini_set("xdebug.var_display_max_children", '-1');
ini_set("xdebug.var_display_max_data", '-1');
ini_set("xdebug.var_display_max_depth", '-1');
use ICal\Event;
use ICal\ICal;
use it\thecsea\simple_caldav_client\CalDAVCalendar;
use it\thecsea\simple_caldav_client\CalDAVException;
use it\thecsea\simple_caldav_client\SimpleCalDAVClient;
require_once(__DIR__ . '/vendor/autoload.php');
require_once(__DIR__ . '/lib/php/password_encryption.php');
require_once(__DIR__ . '/lib/php/ics_file_modification.php');
require_once(__DIR__ . '/lib/php/event_comparison.php');
require_once(__DIR__ . '/lib/php/set_response.php');
class roundcube_caldav extends rcube_plugin
{
public $task = 'settings|mail';
public $rcube;
public $rcmail;
/** @var SimpleCalDAVClient $client */
protected $client;
protected $time_zone_offset;
protected $twenty_four_hour = 86400;
protected $arrayOfCalendars;
protected $all_events = [];
protected $connected = false;
function init()
{
$this->rcmail = rcmail::get_instance();
$this->rcube = rcube::get_instance();
$this->load_config();
$this->add_texts('localization/', true);
$this->include_script('roundcube_caldav.js');
$this->include_script('lib/js/display.js');
$this->include_stylesheet('skins/roundcube_caldav.css');
$this->include_script('vendor/fortawesome/font-awesome/js/all.js');
$this->include_stylesheet('vendor/fortawesome/font-awesome/css/all.css');
$this->add_hook('preferences_sections_list', [$this, 'preference_sections_list']);
$this->add_hook('preferences_list', [$this, 'preferences_list']);
$this->add_hook('preferences_save', [$this, 'preferences_save']);
$server = $this->rcube->config->get('server_caldav');
if (is_array($server) && array_key_exists('_connexion_status', $server)) {
$_connexion = $server['_connexion_status'];
if ($_connexion && $server['_main_calendar'] != null) {
$this->add_hook('message_objects', [$this, 'message_objects']);
$this->register_action(
'plugin.roundcube_caldav_get_info_server',
[$this, 'get_info_server']
);
$this->register_action(
'plugin.roundcube_caldav_import_event_on_server',
[$this, 'import_event_on_server']
);
$this->register_action(
'plugin.roundcube_caldav_decline_counter',
[$this, 'decline_counter']
);
}
}
}
/**
* Displaying the "CalDAV server configuration" section
* @param $param_list
* @return array
*/
function preference_sections_list($param_list): array
{
$param_list['list']['server_caldav'] = array(
'id' => 'server_caldav',
'section' => $this->gettext('server_caldav'),
);
return $param_list;
}
/**
* Display of the different fields of the section
* @param $param_list
* @return array
*/
function preferences_list($param_list): array
{
if ($param_list['section'] != 'server_caldav') {
return $param_list;
}
$param_list['blocks']['main']['name'] = $this->gettext('settings');
$param_list = $this->display_server_caldav_form($param_list);
$server = $this->rcube->config->get('server_caldav');
if (
!empty($server['_url_base'])
&& !empty($server['_login'])
&& !empty($server['_password'])
&& $server['_connexion_status']
) {
$param_list = $this->calendar_selection_form($param_list);
}
return $param_list;
}
/**
* Saving the preferences once the different fields are filled in.
* A new backup deletes all the data previously saved, so be sure to save everything again
* @param $save_params
* @return array : $save_param['prefs'] will be saved
* @throws Exception
*/
function preferences_save($save_params): array
{
$cipher = new password_encryption();
if ($save_params['section'] != 'server_caldav') {
return $save_params;
}
if (empty($_POST['_define_server_caldav']) || empty($_POST['_define_login'])) {
$this->rcmail->output->command(
'display_message',
$this->gettext('save_error_msg'),
'error'
);
$save_params['abort'] = true;
$save_params['result'] = false;
return $save_params;
}
// On récupère l'url et le login dans les champs (ils sont remplis par défault avec l'ancienne valeur)
$urlbase = rcube_utils::get_input_value('_define_server_caldav', rcube_utils::INPUT_POST);
$urlbase = preg_replace('/(.*)personal\/*$/', '$1', $urlbase);
$save_params['prefs']['server_caldav']['_url_base'] = $urlbase;
$login = rcube_utils::get_input_value('_define_login', rcube_utils::INPUT_POST);
$save_params['prefs']['server_caldav']['_login'] = $login;
$server = $this->rcube->config->get('server_caldav');
// Si le mot de passe est spécifié on le change et on teste la connexion sinon on récupère l'ancien
$new_password = false;
if (!empty($_POST['_define_password'])) {
$pwd = rcube_utils::get_input_value('_define_password', rcube_utils::INPUT_POST, true);
$save_params['prefs']['server_caldav']['_password'] =
$cipher->encrypt($pwd,$this->rcube->config->get('des_key'), true);
if ($cipher->decrypt($server['_password'], $this->rcube->config->get('des_key'),true) != $pwd) {
$new_password = true;
}
} elseif (array_key_exists('_password', $server)) {
$save_params['prefs']['server_caldav']['_password'] = $server['_password'];
}
try {
$save_params['prefs']['server_caldav']['_connexion_status'] = $this->try_connection(
$login,
$save_params['prefs']['server_caldav']['_password'],
$urlbase
);
} catch (Exception $e) {
$this->rcmail->output->command(
'display_message',
$this->gettext('save_error_msg') . "\n" . $e->getMessage(),
'error'
);
$save_params['abort'] = true;
$save_params['result'] = false;
return $save_params;
}
if ($save_params['prefs']['server_caldav']['_connexion_status'] && $new_password) {
return $save_params;
} elseif (!$save_params['prefs']['server_caldav']['_connexion_status']) {
$this->rcmail->output->command(
'display_message',
$this->gettext('save_error_msg'),
'error'
);
$save_params['abort'] = true;
$save_params['result'] = false;
return $save_params;
}
if (!isset($_POST['_define_main_calendar'])) {
$this->rcmail->output->command(
'display_message',
$this->gettext('main_calendar_error'),
'error'
);
$save_params['abort'] = true;
$save_params['result'] = false;
return $save_params;
}
// on récupère le calendrier principal que l'on ajoute également à la liste des calendriers utilisés
// si aucun calendrier principal n'est selectionné on annule la sauvegarde
$main_calendar = rcube_utils::get_input_value(
'_define_main_calendar',
rcube_utils::INPUT_POST
);
if ($main_calendar) {
$save_params['prefs']['server_caldav']['_main_calendar'] = $main_calendar;
$save_params['prefs']['server_caldav']['_used_calendars'][$main_calendar] = $main_calendar;
}
// On sauvegarde la liste des calendriers secondaires
$chosen_calendars = [
rcube_utils::get_input_value('_define_used_calendars',rcube_utils::INPUT_POST)
];
if (!empty($chosen_calendars[0])) {
foreach ($chosen_calendars[0] as $cal) {
$save_params['prefs']['server_caldav']['_used_calendars'][$cal] = $cal;
}
}
return $save_params;
}
/**
* Display url / login / password fields
* @param array $param_list
* @return array
*/
function display_server_caldav_form(array $param_list): array
{
$server = $this->rcube->config->get('server_caldav');
$_url_base_server = '';
$_login_server = '';
if (is_array($server) && array_key_exists('_url_base', $server)) {
$_url_base_server = $server['_url_base'];
}
if (is_array($server) && array_key_exists('_login', $server)) {
$_login_server = $server['_login'];
}
// Champs pour specifier l'url du serveur
$field_id = 'define_server_caldav';
$url_base = new html_inputfield(array('name' => '_' . $field_id, 'id' => $field_id));
$param_list['blocks']['main']['options']['url_base'] = array(
'title' => html::label($field_id, rcube::Q($this->gettext('url_base'))),
'content' => $url_base->show($_url_base_server),
);
// Champs pour specifier le login
$field_id = 'define_login';
$login = new html_inputfield(array('name' => '_' . $field_id, 'id' => $field_id));
$param_list['blocks']['main']['options']['login'] = array(
'title' => html::label($field_id, rcube::Q($this->gettext('login'))),
'content' => $login->show($_login_server),
);
// Champs pour specifier le mot de passe
$field_id = 'define_password';
$password = new html_passwordfield([
'name' => '_' . $field_id,
'id' => $field_id,
'autocomplete' => "off"
]);
$param_list['blocks']['main']['options']['password'] = array(
'title' => html::label($field_id, rcube::Q($this->gettext('password'))),
'content' => $password->show(),
);
return $param_list;
}
/**
* Connection with the calDAV server and display of the calendar selection fields if the connection is successful
* @param array $param_list
* @return array
*/
function calendar_selection_form(array $param_list): array
{
// Connexion to caldav server
$server = $this->rcube->config->get('server_caldav');
$_login = $server['_login'];
$_password = $server['_password'];
$_url_base = $server['_url_base'];
try {
$success = $this->try_connection($_login, $_password, $_url_base);
} catch (Exception $e) {
$this->rcmail->output->command(
'display_message',
$this->gettext('connect_error_msg') . "\n" . $e->getMessage(),
'error'
);
return $param_list;
}
if (!$success) {
$this->rcmail->output->command(
'display_message',
$this->gettext('connect_error_msg'),
'error'
);
return $param_list;
}
try {
$available_calendars = $this->client->findCalendars();
} catch (Exception $e) {
$this->rcmail->output->command(
'display_message',
$this->gettext('find_calendar_error'),
'error'
);
return $param_list;
}
if (empty($available_calendars)) {
$this->rcmail->output->command(
'display_message',
$this->gettext('no_calendar_found'),
'error'
);
return $param_list;
}
$param_list['blocks']['main']['options']['calendar_choice'] = array(
'title' => html::label('ojk', rcube::Q($this->gettext('calendar_choice'))),
);
foreach ($available_calendars as $available_calendar) {
$print = null;
if ($server['_main_calendar'] == $available_calendar->getCalendarID()) {
$print = $available_calendar->getCalendarID();
}
if (!empty($server['_used_calendars'])) {
foreach ($server['_used_calendars'] as $used_calendar) {
if ($used_calendar == $available_calendar->getCalendarID()) {
$print = $available_calendar->getCalendarID();
break;
}
}
}
$checkbox = new html_checkbox(array('name' => '_define_used_calendars[]', 'value' => $available_calendar->getCalendarID()));
$radiobutton = new html_radiobutton(array('name' => '_define_main_calendar', 'value' => $available_calendar->getCalendarID()));
$param_list['blocks']['main']['options'][$available_calendar->getCalendarID() . 'radiobutton'] = array(
'title' => html::label(
$available_calendar->getCalendarID(),
$this->gettext("use_this_calendar") . $available_calendar->getDisplayName()
),
'content' => $checkbox->show($print)
);
$param_list['blocks']['main']['options'][$available_calendar->getCalendarID() . 'checkbox'] = array(
'title' => html::label(
$available_calendar->getCalendarID(),
$this->gettext("make_this_calendar_default1") . $available_calendar->getDisplayName() . $this->gettext("make_this_calendar_default2")
),
'content' => $radiobutton->show($server['_main_calendar']),
);
}
return $param_list;
}
/**
* Check if the mail we want to watch contains or not a text/calendar attachment.
* If it does, we can proceed to the loading of the page (which will be recovered by the javascript)
* @param $args
* @return array : ['content'] = HTML code to display
*
*/
function message_objects($args): array
{
// Get arguments
$content = $args['content'];
$message = $args['message'];
$has_ICalendar_attachments = false;
if (empty($message) || empty($message->attachments)) {
return array('content' => $content);
}
foreach ($message->attachments as &$attachment) {
if ($attachment->mimetype == 'text/calendar') {
$has_ICalendar_attachments = true;
}
}
if ($has_ICalendar_attachments) {
ob_start();
include("plugins/roundcube_caldav/roundcube_caldav_display.php");
$html = ob_get_clean();
$content[] = $html;
}
return array('content' => $content);
}
/**
* Manages the connection to the calDAV server, returns the success or failure value and initializes
* the array with the calendars $this->arrayOfCalendars
* @return bool
* @throws Exception
*/
public function connection_to_calDAV_server(): bool
{
$server = $this->rcube->config->get('server_caldav');
$_login = $server['_login'];
$_password = $server['_password'];
$_url_base = $server['_url_base'];
$_connexion = $server['_connexion_status'];
if ($this->connected) {
return true;
}
if (!$_connexion) {
return false;
}
if (empty($server['_used_calendars'])) {
return false;
}
try {
$success = $this->try_connection($_login, $_password, $_url_base);
$available_calendars = $this->client->findCalendars();
foreach ($server['_used_calendars'] as $used_calendar) {
foreach ($available_calendars as $available_calendar) {
if ($used_calendar == $available_calendar->getCalendarID()) {
$this->arrayOfCalendars[$available_calendar->getCalendarID()] = $available_calendar;
}
}
}
$this->get_all_events();
$this->connected = true;
return $success;
} catch (Exception $e) {
$this->rcmail->output->command('display_message', $e->getMessage(), 'error');
return false;
}
}
/**
* Test of connection with the Caldav server
* @param string $_login
* @param string $_password
* @param string $_url_base
* @return bool
* @throws Exception
*/
function try_connection(string $_login, string $_password, string $_url_base): bool
{
if (!empty($_url_base) && !empty($_login) && !empty($_password)) {
// Récupération du mot de passe chiffré dans la bdd et décodage
$cipher = new password_encryption();
$plain_password = $cipher->decrypt(
$_password,
$this->rcube->config->get('des_key'),
true
);
// Connexion au serveur calDAV et récupération des calendriers dispos
$this->client = new SimpleCalDAVClient();
$this->client->connect($_url_base, $_login, $plain_password);
return true;
}
return false;
}
/**
* Get all events on the server in order to minimize the number of calls, and store them in $this->all_events[]
*/
public function get_all_events(): void
{
try {
$begin_of_unix_timestamp = date("Ymd\THis\Z", 0);
$end_of_unix_timestamp = date("Ymd\THis\Z", 2 ** 31);
if (empty($this->arrayOfCalendars)) {
throw new Exception($this->gettext("ErrorEmptyArrayOfCalendar"));
}
foreach ($this->arrayOfCalendars as $calendar) {
$this->client->setCalendar($this->arrayOfCalendars[$calendar->getCalendarID()]);
$this->all_events[$calendar->getCalendarID()] = $this->client->getEvents($begin_of_unix_timestamp,
$end_of_unix_timestamp);
}
} catch (Exception $e) {
$this->rcmail->output->command(
'display_message',
$this->gettext('something_happened_while_getting_events') . "\n" . $e->getMessage(),
'error'
);
}
}
/**
* Retrieve all the information necessary to display the event and send the answers to the client.
*/
function get_info_server()
{
$uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST);
$mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
// Récupération du mail
$message = new rcube_message($uid, $mbox);
if (empty($message->attachments)) {
return;
}
try {
foreach ($message->attachments as &$attachment) {
if ($attachment->mimetype == 'text/calendar') {
$this->send_info_to_be_displayed_to_client($message, $attachment);
}
}
} catch (Exception $e) {
$this->rcmail->output->command(
'display_message',
$this->gettext('something_happened_while_sending_information_to_client') . "\n" . $e->getMessage(),
'error'
);
}
}
/**
* Fill an array which will be send back to client with all information needed for display
* @param rcube_message $message
* @param $attachment
* @throws Exception
*/
function send_info_to_be_displayed_to_client(rcube_message &$message, $attachment)
{
$response = array();
$langs = $this->rcmail->config->get('language');
$this->connection_to_calDAV_server();
$ics = $message->get_part_body($attachment->mime_id);
$ical = new ICal();
$ical->initString($ics);
$array_events = $ical->cal;
$array_events = $array_events['VEVENT'] ?? $array_events;
if (count($array_events) < 10) {
$events = $ical->events();
} else {
$events = [];
for ($i = 0; $i < 10; $i++) {
$events[] = new Event(array_shift($array_events));
}
}
set_sender_and_receiver_email($message, $response);
$first_event = $events[array_key_first($events)];
if (empty($first_event)) {
$this->rcmail->output->command('display_message', $this->gettext('invitation_without_event_inside'), 'error');
return;
}
$same_uid = $first_event->uid;
foreach ($events as $i => &$event) {
if_no_dtend_add_one_to_event($event);
$response['recurrent_events'][$event->uid][] = $this->pretty_date(
$event->dtstart_array[1],
$event->dtend_array[1]
);
// Si l'evenement à le même uid que son prédecesseur on ne l'affiche pas
if ($same_uid == $event->uid && $i != 0) {
continue;
} else {
$same_uid = $event->uid;
}
$response['uid'] = $event->uid;
if (isset($event->rrule) && $event->rrule) {
$rrule = new Recurr\Rule($event->rrule, $event->dtstart_array[1]);
$array_match = [];
if (preg_match('/[a-z]+/', $langs, $array_match) > 0) {
$langs_short = $array_match[0];
}
try {
$transformer = new \Recurr\Transformer\TextTransformer(new \Recurr\Transformer\Translator($langs_short));
$response['rrule_to_text'] = $transformer->transform($rrule);
} catch (Exception $e) {
$this->rcube::write_log('errors', $e->getMessage());
}
}
$found_advance = $this->is_server_in_advance($event);
// On récupère les informations correspondant à l'identité qui a été solicité dans le champs attendee ou organisateur
if ($found_advance) {
$response['identity'] = find_identity_matching_with_attendee_or_organizer(
$event,
$this->rcmail->user->list_identities(null, true),
$found_advance[0]['event']
);
} else {
$response['identity'] = find_identity_matching_with_attendee_or_organizer(
$event,
$this->rcmail->user->list_identities(null, true)
);
}
$is_Organizer = false;
if ($response['identity']) {
$is_Organizer = strcmp($response['identity']['role'], 'ORGANIZER') == 0;
}
if ($is_Organizer) {
get_sender_s_partstat($event, $response);
}
set_method_field($ical, $response, $is_Organizer);
if (isset($event->comment) && $event->comment) {
$response['comment'] = nl2br($event->comment);
}
set_if_an_older_event_was_found_on_server(
$event,
$response,
$this->arrayOfCalendars,
$this->all_events
);
if ($found_advance) {
$response['found_advance'] = $found_advance;
$response['found_on_calendar']['display_name'] = $found_advance[1];
$response['found_on_calendar']['calendar_id'] = $found_advance[0]['calendar_id'];
$response['is_sequences_equal'] = $found_advance[2];
if ($is_Organizer && $response['is_sequences_equal'] && $response['METHOD'] != 'COUNTER') {
$response['used_event'] = $event;
} else {
$response['used_event'] = $found_advance[0]['event'];
}
} else {
$response['used_event'] = $event;
}
set_participants_characteristics_and_set_buttons_properties(
$response['used_event'],
$response
);
$new_attendees_array = [];
set_participants_characteristics_and_set_buttons_properties($event, $new_attendees_array);
$response['display_modification_made_by_organizer'] = false;
if (
!$is_Organizer
&& !empty($response['found_older_event_on_calendar'])
&& $response['found_older_event_on_calendar']
) {
$response['display_modification_made_by_organizer'] = true;
}
$date_time_already_set = false;
$attendees = !empty($new_attendees_array['attendees']) ? $new_attendees_array['attendees'] : null;
if (($found_advance && $response['METHOD'] == 'COUNTER')) {
set_if_modification_date_location_description_attendees(
$response,
$is_Organizer,
$event,
$langs,
$attendees
);
set_formated_date_time($response['used_event'], $response, $langs);
$date_time_already_set = true;
} elseif ($response['display_modification_made_by_organizer']) {
set_if_modification_date_location_description_attendees(
$response,
$is_Organizer,
$event,
$langs,
$attendees
);
set_formated_date_time($response['older_event'], $response, $langs);
$date_time_already_set = true;
}
$event = $response['used_event'];
if (!$date_time_already_set) {
set_formated_date_time($event, $response, $langs);
}
if (!empty($event->description) && $event->description) {
$response['description'] = nl2br($event->description);
}
if ($event->description) {
$response['description'] = nl2br($event->description);
}
$response['location'] = $event->location;
$msg = set_calendar_to_use_for_select_input(
$response,
$this->rcube->config->get('server_caldav'),
$this->arrayOfCalendars
);
if ($msg) {
$this->rcmail->output->command('display_message', $this->gettext($msg), 'error');
}
$timezone_array = find_time_zone($ics);
$this->time_zone_offset = $timezone_array[0];
// On affiche les autres informations concernant notre server caldav
$this->set_caldav_server_related_information($event, $ical, $response);
if ($found_advance) {
get_sender_s_partstat($found_advance[0]['event'], $response, true);
}
$role = !empty($response['identity']['role']) ? $response['identity']['role'] : '';
$this->select_buttons_to_display(
$role,
$response['METHOD'],
$response
);
$this->rcmail->output->command(
'plugin.undirect_rendering_js',
array('request' => $response)
);
}
}
/**
* Get POST from client and modify the ics file in order to save it on server and/or send it to attendees/organizer
* there are different behavior for each role( attendee / organizer / event with no participant) and for each method
* with which we want to send the ICalendar object.
* @throws Exception
*/
function import_event_on_server()
{
// On récupère toute les informations nécessaires depuis le js qu'ils soient ou non spécifiés
$mail_uid = rcube_utils::get_input_value('_mail_uid', rcube_utils::INPUT_POST);
$event_uid = rcube_utils::get_input_value('_event_uid', rcube_utils::INPUT_POST);
$mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
$status = rcube_utils::get_input_value('_status', rcube_utils::INPUT_POST);
$identity = rcube_utils::get_input_value('_identity', rcube_utils::INPUT_POST);
$method = rcube_utils::get_input_value('_method', rcube_utils::INPUT_POST);
$comment = rcube_utils::get_input_value('_comment', rcube_utils::INPUT_POST);
$chosen_calendar = rcube_utils::get_input_value('_calendar', rcube_utils::INPUT_POST);
$modification = rcube_utils::get_input_value('_modification', rcube_utils::INPUT_POST);
// Récupération du mail
$message = new rcube_message($mail_uid, $mbox);
// On se connecte au serveur et on récupèrere tous les evt pour ne plus avoir à échanger des infos avec lui
$this->connection_to_calDAV_server();
foreach ($message->attachments as &$attachment) {
if ($attachment->mimetype !== 'text/calendar') {
continue;
}
// On récupère la PJ
$ics = $message->get_part_body($attachment->mime_id);
// On regarde uniquement l'évenement qui nous interesse parmis les différents présent dans le fichier
$ical = new ICal();
$ical->initString($ics);
$array_event = $ical->events();
foreach ($array_event as $e) {
if ($e->uid == $event_uid) {
$event = $e;
break;
}
}
if (!$event) {
continue;
}
$timezone_array = find_time_zone($ics);
$this->time_zone_offset = $timezone_array[0];
$is_organizer = false;
$has_participants = ($identity !== 'NO_PARTICIPANTS');
if ($has_participants) {
$is_organizer = strcmp($identity['role'], 'ORGANIZER') == 0;
}
$update_event_on_server_only = strcmp($method, 'UPDATED') == 0;
$decline_counter = strcmp($method, 'DECLINECOUNTER') == 0;
$cancel_recurrent = strcmp($status, 'CANCELLED_ONE_EVENT') == 0;
$cancel_event_on_server_only = strcmp($method, 'EVENT_CANCELLED') == 0;
if ($cancel_event_on_server_only) {
$status = 'CANCELLED';
}
if (!$is_organizer || $update_event_on_server_only) {
// On Regarde si le serveur est en avance sur cet événement par rapport à l'ics
// et si oui on récupère la version la plus récente pour nos modifs
$found_advance = $this->is_server_in_advance($event); //1s
if (is_array($found_advance)) {
$ics = $found_advance[0]['ics'];
$event = $found_advance[0]['event'];
}
}
if ($update_event_on_server_only) {
$new_ics = change_partstat_ics($ics, $status, $message->sender['mailto']);
} else {
// On reforme un fichier ics avec uniquement l'événement qui nous interesse
$new_ics = extract_event_ics($ics, $event_uid);
}
// On change la date de dernière modif
$new_ics = change_last_modified_ics($new_ics);
if (!$is_organizer && $has_participants) {
// On change la valeur du champs PARTSTAT correspondant à l'utilisateur
$new_ics = change_partstat_ics($new_ics, $status, $identity['email']);
}
if ($is_organizer && $method == 'REQUEST' && $status = 'CONFIRMED') {
$new_ics = change_partstat_of_all_attendees_to_need_action($new_ics);
}
if ($cancel_recurrent) {
$status = 'CONFIRMED';
$new_ics = cancel_one_instance($new_ics, $event->dtstart_array[1]);
}
$send_event = true;
// On enregistre pas sur le serveur si la method est decline counter
if (!$decline_counter) {
if (!empty($modification)) {
// On change la date et l'emplacement dans le fichier ics si ceux si ont été modifié
$ics_with_modified_date_and_location = $this->change_date_and_location(
$modification,
$array_event,
$event,
$new_ics
);
} else {
$ics_with_modified_date_and_location = $new_ics;
}
if ($is_organizer || !$has_participants) {
// On sauvegarde sur le serveur avec les dates et lieu modifiés
$new_ics = $ics_with_modified_date_and_location;
// On change le numéro de sequence si l'utilisateur est l'organisateur de l'evenement ou si l'événement n'a pas de participants
$new_ics = change_sequence_ics($new_ics);
$send_event = $this->save_event_on_caldav_server_and_display_message(
$new_ics,
$status,
$event,
$chosen_calendar,
$event_uid
);
} else {
$send_event = $this->save_event_on_caldav_server_and_display_message(
$new_ics,
$status,
$event,
$chosen_calendar,
$event_uid
);
// On sauvegarde sur le serveur sans les dates et lieu modifiés
$new_ics = $ics_with_modified_date_and_location;
}
}
// On envoie une réponse uniquement si il y a des participants à qui répondre,
// et si ce n'est pas une simple mise a jour de l'événement sur le serveur de l'utilisateur
if ($send_event && $has_participants && !$update_event_on_server_only && !$cancel_event_on_server_only) {
$this->send_mail_and_display_message(
$is_organizer,
$method,
$status,
$new_ics,
$message,
$identity,
$comment,
(bool) $modification
);
}
$this->rcmail->output->command(
'plugin.display_after_response',
array('uid' => $event_uid)
);
}
}
/**
* Choice of the buttons that the client will have to display according to its role and according to the method of
* the file received
* @param string $role
* @param string $method
* @param array $response
*/
function select_buttons_to_display(string $role, string $method, array &$response)
{
$has_new_description = !empty($response['new_description']) ? true : false;
$has_new_location = !empty($response['new_location']) ? true : false;
$has_new_date = !empty($response['new_date']) ? true : false;
$has_modifications = $has_new_description || $has_new_location || $has_new_date;
$is_recurrent = count($response['recurrent_events'][$response['uid']]) > 1;
$buttons_to_display = [];
switch ($role) {
case 'ORGANIZER':
switch ($method) {
case 'REPLY':
$buttons_to_display = ['reschedule', 'cancel_button_organizer'];