-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbanner_ics.php
129 lines (109 loc) · 4.78 KB
/
banner_ics.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
<?php
// Require composer autoload for direct installs
@include __DIR__ . '/vendor/autoload.php';
/**
* Banner ICS
*
* Displays event information from ICS attachments
*
* @license MIT License: <http://opensource.org/licenses/MIT>
* @author Varun Patil
* @category Plugin for RoundCube WebMail
*/
class banner_ics extends rcube_plugin
{
public $task = 'mail';
function init()
{
$rcmail = rcmail::get_instance();
$this->load_config('config.inc.php.dist');
$this->load_config('config.inc.php');
$this->include_stylesheet('banner_ics.css');
if ($rcmail->config->get('banner_ics_description')) {
$this->include_script('banner_ics_description.js');
}
$this->add_hook('message_objects', array($this, 'ics_banner'));
}
public function ics_banner($args)
{
// Get arguments
$content = $args['content'];
$message = $args['message'];
foreach ($message->attachments as &$a) {
if (strtolower($a->mimetype) === 'text/calendar') {
try {
$this->process_attachment($content, $message, $a);
} catch (\Exception $e) {}
}
}
return array('content' => $content);
}
public function process_attachment(&$content, &$message, &$a)
{
$rcmail = rcmail::get_instance();
$date_format = $rcmail->config->get('date_format', 'D M d, Y');
$time_format = $rcmail->config->get('time_format', 'h:ia');
$combined_format = $date_format . ' ' . $time_format;
// Parse event
$ics = $message->get_part_body($a->mime_id);
$ical = new \ICal\ICal();
$ical->initString($ics);
// Make sure we have events
if (!$ical->hasEvents()) return;
// Get first event
foreach ($ical->events() as &$event) {
$dtstart = $event->dtstart_array[2];
$dtend = $event->dtend_array[2];
$dtstr = $rcmail->format_date($dtstart, $combined_format) . ' - ';
// Dont double date if same
$df = 'Y-m-d';
if (date($df, $dtstart) === date($df, $dtend)) {
$dtstr .= $rcmail->format_date($dtend, $time_format);
} else {
$dtstr .= $rcmail->format_date($dtend, $combined_format);
}
// Put timezone in date string
$dtstr .= ' (' . $rcmail->format_date($dtstart, 'T') . ')';
// Get attendees
$who = array();
foreach (array_merge($event->organizer_array ?? [], $event->attendee_array ?? []) as &$o) {
if (is_array($o) && array_key_exists('CN', $o)) {
array_push($who, $o['CN']);
}
}
// Get attendees string
if (count($who) > 0) {
$max_show = 10;
$others = count($who) - $max_show;
$who = array_slice($who, 0, $max_show);
$who = implode(', ', $who) . ($others > 0 ? " and $others others" : '');
} else {
$who = null;
}
// Output
$html = '<div class="info ics-event-container">';
$html .= '<div class="ics-icon">';
$html .= '<div class="m">' . $rcmail->format_date($dtstart, 'M') . '</div>';
$html .= '<div class="d">' . $rcmail->format_date($dtstart, 'd') . '</div>';
$html .= '<div class="day">' . $rcmail->format_date($dtstart, 'D') . '</div>';
$html .= '</div>';
$html .= '<div class="ics-event">';
$html .= '<span class="title">' . htmlspecialchars($event->summary) . '</span>';
$html .= '<br/><b>' . htmlspecialchars($dtstr) . '</b>';
if (isset($who)) {
$html .= '<br/>' . htmlspecialchars($who);
}
$html .= '</div>';
$html .= '</div>';
array_push($content, $html);
// Optional description block
if ($rcmail->config->get('banner_ics_description')) {
$html = '<div class="info ics-event-description">';
$description = htmlspecialchars_decode($event->description);
$html .= (new rcube_text2html($description))->get_html();
$html .= '</div>';
array_push($content, $html);
}
}
}
}