aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Widget/Cdav.php
blob: 3a86f0d258fe83bff4ef2fa0f379429ff5d76777 (plain) (blame)
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
<?php

/**
 *   * Name: CalDAV/CardDAV tools
 *   * Description: A widget with various CalDAV and CardDAV tools
 *   * Requires: cdav
 */

namespace Zotlabs\Widget;

class Cdav {

	function widget() {
		if(!local_channel())
			return;

		$channel = \App::get_channel();
		$principalUri = 'principals/' . $channel['channel_address'];

		if(!cdav_principal($principalUri))
			return;

		$pdo = \DBA::$dba->db;

		require_once 'vendor/autoload.php';

		$o = '';

		if(argc() <= 3 && argv(1) === 'calendar') {

			$caldavBackend = new \Sabre\CalDAV\Backend\PDO($pdo);

			$sabrecals = $caldavBackend->getCalendarsForUser($principalUri);

			//TODO: we should probably also check for permission to send stream here
			$local_channels = q("SELECT * FROM channel LEFT JOIN abook ON abook_xchan = channel_hash WHERE channel_system = 0 AND channel_removed = 0 AND channel_hash != '%s' AND abook_channel = %d",
				dbesc($channel['channel_hash']),
				intval($channel['channel_id'])
			);

			$sharee_options = '<option value="">' . t('Select Channel') . '</option>' . "\r\n";
			foreach($local_channels as $local_channel) {
				$sharee_options .= '<option value="' . $local_channel['channel_hash'] . '">' . $local_channel['channel_name'] . '</option>' . "\r\n";
			}

			$access_options = '<option value="3">' . t('Read-write') . '</option>' . "\r\n";
			$access_options .= '<option value="2">' . t('Read-only') . '</option>' . "\r\n";

			$shared_calendars = [];
			$my_calendars = [];

			//list calendars
			foreach($sabrecals as $sabrecal) {
				if($sabrecal['share-access'] == 1)
					$access = '';
				if($sabrecal['share-access'] == 2)
					$access = 'read';
				if($sabrecal['share-access'] == 3)
					$access = 'read-write';

				$invites = $caldavBackend->getInvites($sabrecal['id']);

				$json_source = '/cdav/calendar/json/' . $sabrecal['id'][0] . '/' . $sabrecal['id'][1];

				$switch = get_pconfig(local_channel(), 'cdav_calendar', $sabrecal['id'][0]);

				$color = (($sabrecal['{http://apple.com/ns/ical/}calendar-color']) ? $sabrecal['{http://apple.com/ns/ical/}calendar-color'] : '#6cad39');

				$editable = (($sabrecal['share-access'] == 2) ? 'false' : 'true'); // false/true must be string since we're passing it to javascript

				$sharees = [];
				$share_displayname = [];

				foreach($invites as $invite) {
					if(strpos($invite->href, 'mailto:') !== false) {
						$sharee = channelx_by_nick(substr($invite->principal, 11));
						$sharees[] = [
							'name' => $sharee['channel_name'],
							'access' => (($invite->access == 3) ? ' (RW)' : ' (R)'),
							'hash' => $sharee['channel_hash']
						];
					}
				}

				if(!$access) {
					$my_calendars[] = [
						'ownernick' => $channel['channel_address'],
						'uri' => $sabrecal['uri'],
						'displayname' => $sabrecal['{DAV:}displayname'],
						'calendarid' => $sabrecal['id'][0],
						'instanceid' => $sabrecal['id'][1],
						'json_source' => $json_source,
						'color' => $color,
						'editable' => $editable,
						'switch' => $switch,
						'sharees' => $sharees
					];
				}
				else {
					$shared_calendars[] = [
						'ownernick' => $channel['channel_address'],
						'uri' => $sabrecal['uri'],
						'displayname' => $sabrecal['{DAV:}displayname'],
						'calendarid' => $sabrecal['id'][0],
						'instanceid' => $sabrecal['id'][1],
						'json_source' => $json_source,
						'color' => $color,
						'editable' => $editable,
						'switch' => $switch,
						'sharer' => $sabrecal['{urn:ietf:params:xml:ns:caldav}calendar-description'],
						'access' => $access
					];
				}

				if(!$access || $access === 'read-write') {
					$writable_calendars[] = [
						'displayname' => ((!$access) ? $sabrecal['{DAV:}displayname'] : $share_displayname[0]),
						'id' => $sabrecal['id']
					];
				}
			}

			$channel_calendars[] = [
				'ownernick' => $channel['channel_address'],
				'displayname' => $channel['channel_name'],
				'calendarid' => 'channel_calendar',
				'json_source' => '/channel_calendar/json',
				'color' => '#3a87ad',
				'editable' => true,
				'switch' => get_pconfig(local_channel(), 'cdav_calendar', 'channel_calendar')
			];

			$o .= replace_macros(get_markup_template('cdav_widget_calendar.tpl'), [
				'$channel_calendars_label' => t('Channel Calendar'),
				'$channel_calendars' => $channel_calendars,
				'$my_calendars_label' => t('CalDAV Calendars'),
				'$my_calendars' => $my_calendars,
				'$shared_calendars_label' => t('Shared CalDAV Calendars'),
				'$shared_calendars' => $shared_calendars,
				'$sharee_options' => $sharee_options,
				'$access_options' => $access_options,
				'$share_label' => t('Share this calendar'),
				'$share' => t('Share'),
				'$edit_label' => t('Calendar name and color'),
				'$edit' => t('Edit'),
				'$create_label' => t('Create new CalDAV calendar'),
				'$create' => t('Create'),
				'$create_placeholder' => t('Calendar Name'),
				'$tools_label' => t('Calendar Tools'),
				'$tools_options_label' => [t('Channel Calendars'), t('CalDAV Calendars')],
				'$import_label' => t('Import calendar'),
				'$import_placeholder' => t('Select a calendar to import to'),
				'$upload' => t('Upload'),
				'$writable_calendars' => $writable_calendars
			]);

			return $o;

		}

		if(argc() >= 2 && argv(1) === 'addressbook') {

			$carddavBackend = new \Sabre\CardDAV\Backend\PDO($pdo);

			$sabreabooks = $carddavBackend->getAddressBooksForUser($principalUri);

			//list addressbooks
			foreach($sabreabooks as $sabreabook) {
				$addressbooks[] = [
					'ownernick' => $channel['channel_address'],
					'uri' => $sabreabook['uri'],
					'displayname' => $sabreabook['{DAV:}displayname'],
					'id' => $sabreabook['id']

				];
			}

			$o .= replace_macros(get_markup_template('cdav_widget_addressbook.tpl'), [
				'$addressbooks_label' => t('Addressbooks'),
				'$addressbooks' => $addressbooks,
				'$edit_label' => t('Addressbook name'),
				'$edit' => t('Edit'),
				'$create_label' => t('Create new addressbook'),
				'$create_placeholder' => t('Addressbook Name'),
				'$create' => t('Create'),
				'$tools_label' => t('Addressbook Tools'),
				'$import_label' => t('Import addressbook'),
				'$import_placeholder' => t('Select an addressbook to import to'),
				'$upload' => t('Upload')
			]);

			return $o;

		}

	}
}