forked from hypery2k/owncloud
/
calendar.php
385 lines (346 loc) · 12 KB
/
calendar.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
<?php
/**
* Copyright (c) 2011 Jakob Sack <mail@jakobsack.de>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
/**
*
* The following SQL statement is just a help for developers and will not be
* executed!
*
* CREATE TABLE clndr_calendars (
* id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
* userid VARCHAR(255),
* displayname VARCHAR(100),
* uri VARCHAR(100),
* active INTEGER UNSIGNED NOT NULL DEFAULT '0',
* ctag INTEGER UNSIGNED NOT NULL DEFAULT '0',
* calendarorder INTEGER UNSIGNED NOT NULL DEFAULT '0',
* calendarcolor VARCHAR(10),
* timezone TEXT,
* components VARCHAR(20)
* );
*
*/
/**
* This class manages our calendars
*/
class OC_Calendar_Calendar{
/**
* @brief Returns the list of calendars for a specific user.
* @param string $uid User ID
* @param boolean $active Only return calendars with this $active state, default(=false) is don't care
* @return array
*/
public static function allCalendars($uid, $active=false) {
$values = array($uid);
$active_where = '';
if (!is_null($active) && $active) {
$active_where = ' AND `active` = ?';
$values[] = (int)$active;
}
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_calendars` WHERE `userid` = ?' . $active_where );
$result = $stmt->execute($values);
$calendars = array();
while( $row = $result->fetchRow()) {
$row['permissions'] = OCP\PERMISSION_CREATE
| OCP\PERMISSION_READ | OCP\PERMISSION_UPDATE
| OCP\PERMISSION_DELETE | OCP\PERMISSION_SHARE;
$calendars[] = $row;
}
$calendars = array_merge($calendars, OCP\Share::getItemsSharedWith('calendar', OC_Share_Backend_Calendar::FORMAT_CALENDAR));
return $calendars;
}
/**
* @brief Returns the list of calendars for a principal (DAV term of user)
* @param string $principaluri
* @return array
*/
public static function allCalendarsWherePrincipalURIIs($principaluri) {
$uid = self::extractUserID($principaluri);
return self::allCalendars($uid);
}
/**
* @brief Gets the data of one calendar
* @param integer $id
* @return associative array
*/
public static function find($id) {
$stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_calendars` WHERE `id` = ?' );
$result = $stmt->execute(array($id));
$row = $result->fetchRow();
if($row['userid'] != OCP\USER::getUser() && !OC_Group::inGroup(OCP\User::getUser(), 'admin')) {
$sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $id);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & OCP\PERMISSION_READ)) {
return $row; // I have to return the row so e.g. OC_Calendar_Object::getowner() works.
}
$row['permissions'] = $sharedCalendar['permissions'];
} else {
$row['permissions'] = OCP\PERMISSION_ALL;
}
return $row;
}
/**
* @brief Creates a new calendar
* @param string $userid
* @param string $name
* @param string $components Default: "VEVENT,VTODO,VJOURNAL"
* @param string $timezone Default: null
* @param integer $order Default: 1
* @param string $color Default: null, format: '#RRGGBB(AA)'
* @return insertid
*/
public static function addCalendar($userid,$name,$components='VEVENT,VTODO,VJOURNAL',$timezone=null,$order=0,$color=null) {
$all = self::allCalendars($userid);
$uris = array();
foreach($all as $i) {
$uris[] = $i['uri'];
}
$uri = self::createURI($name, $uris );
$stmt = OCP\DB::prepare( 'INSERT INTO `*PREFIX*clndr_calendars` (`userid`,`displayname`,`uri`,`ctag`,`calendarorder`,`calendarcolor`,`timezone`,`components`) VALUES(?,?,?,?,?,?,?,?)' );
$result = $stmt->execute(array($userid,$name,$uri,1,$order,$color,$timezone,$components));
$insertid = OCP\DB::insertid('*PREFIX*clndr_calendars');
OCP\Util::emitHook('OC_Calendar', 'addCalendar', $insertid);
return $insertid;
}
/**
* @brief Creates default calendars
* @param string $userid
* @return boolean
*/
public static function addDefaultCalendars($userid = null) {
if(is_null($userid)) {
$userid = OCP\USER::getUser();
}
$id = self::addCalendar($userid,'Default calendar');
return true;
}
/**
* @brief Creates a new calendar from the data sabredav provides
* @param string $principaluri
* @param string $uri
* @param string $name
* @param string $components
* @param string $timezone
* @param integer $order
* @param string $color format: '#RRGGBB(AA)'
* @return insertid
*/
public static function addCalendarFromDAVData($principaluri,$uri,$name,$components,$timezone,$order,$color) {
$userid = self::extractUserID($principaluri);
$stmt = OCP\DB::prepare( 'INSERT INTO `*PREFIX*clndr_calendars` (`userid`,`displayname`,`uri`,`ctag`,`calendarorder`,`calendarcolor`,`timezone`,`components`) VALUES(?,?,?,?,?,?,?,?)' );
$result = $stmt->execute(array($userid,$name,$uri,1,$order,$color,$timezone,$components));
$insertid = OCP\DB::insertid('*PREFIX*clndr_calendars');
OCP\Util::emitHook('OC_Calendar', 'addCalendar', $insertid);
return $insertid;
}
/**
* @brief Edits a calendar
* @param integer $id
* @param string $name Default: null
* @param string $components Default: null
* @param string $timezone Default: null
* @param integer $order Default: null
* @param string $color Default: null, format: '#RRGGBB(AA)'
* @return boolean
*
* Values not null will be set
*/
public static function editCalendar($id,$name=null,$components=null,$timezone=null,$order=null,$color=null) {
// Need these ones for checking uri
$calendar = self::find($id);
if ($calendar['userid'] != OCP\User::getUser() && !OC_Group::inGroup(OCP\User::getUser(), 'admin')) {
$sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $id);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & OCP\PERMISSION_UPDATE)) {
throw new Exception(
OC_Calendar_App::$l10n->t(
'You do not have the permissions to update this calendar.'
)
);
}
}
// Keep old stuff
if(is_null($name)) $name = $calendar['displayname'];
if(is_null($components)) $components = $calendar['components'];
if(is_null($timezone)) $timezone = $calendar['timezone'];
if(is_null($order)) $order = $calendar['calendarorder'];
if(is_null($color)) $color = $calendar['calendarcolor'];
$stmt = OCP\DB::prepare( 'UPDATE `*PREFIX*clndr_calendars` SET `displayname`=?,`calendarorder`=?,`calendarcolor`=?,`timezone`=?,`components`=?,`ctag`=`ctag`+1 WHERE `id`=?' );
$result = $stmt->execute(array($name,$order,$color,$timezone,$components,$id));
OCP\Util::emitHook('OC_Calendar', 'editCalendar', $id);
return true;
}
/**
* @brief Sets a calendar (in)active
* @param integer $id
* @param boolean $active
* @return boolean
*/
public static function setCalendarActive($id,$active) {
$calendar = self::find($id);
if ($calendar['userid'] != OCP\User::getUser()) {
$sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $id);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & OCP\PERMISSION_UPDATE)) {
throw new Exception(
OC_Calendar_App::$l10n->t(
'You do not have the permissions to update this calendar.'
)
);
}
}
$stmt = OCP\DB::prepare( 'UPDATE `*PREFIX*clndr_calendars` SET `active` = ? WHERE `id` = ?' );
$stmt->execute(array((int)$active, $id));
return true;
}
/**
* @brief Updates ctag for calendar
* @param integer $id
* @return boolean
*/
public static function touchCalendar($id) {
$stmt = OCP\DB::prepare( 'UPDATE `*PREFIX*clndr_calendars` SET `ctag` = `ctag` + 1 WHERE `id` = ?' );
$stmt->execute(array($id));
return true;
}
/**
* @brief removes a calendar
* @param integer $id
* @return boolean
*/
public static function deleteCalendar($id) {
$calendar = self::find($id);
if ($calendar['userid'] != OCP\User::getUser() && !OC_Group::inGroup(OCP\User::getUser(), 'admin')) {
$sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $id);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & OCP\PERMISSION_DELETE)) {
throw new Exception(
OC_Calendar_App::$l10n->t(
'You do not have the permissions to delete this calendar.'
)
);
}
}
$stmt = OCP\DB::prepare( 'DELETE FROM `*PREFIX*clndr_calendars` WHERE `id` = ?' );
$stmt->execute(array($id));
$stmt = OCP\DB::prepare( 'DELETE FROM `*PREFIX*clndr_objects` WHERE `calendarid` = ?' );
$stmt->execute(array($id));
OCP\Share::unshareAll('calendar', $id);
OCP\Util::emitHook('OC_Calendar', 'deleteCalendar', $id);
if(OCP\USER::isLoggedIn() and count(self::allCalendars(OCP\USER::getUser())) == 0) {
self::addDefaultCalendars(OCP\USER::getUser());
}
return true;
}
/**
* @brief merges two calendars
* @param integer $id1
* @param integer $id2
* @return boolean
*/
public static function mergeCalendar($id1, $id2) {
$calendar = self::find($id1);
if ($calendar['userid'] != OCP\User::getUser() && !OC_Group::inGroup(OCP\User::getUser(), 'admin')) {
$sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $id1);
if (!$sharedCalendar || !($sharedCalendar['permissions'] & OCP\PERMISSION_UPDATE)) {
throw new Exception(
OC_Calendar_App::$l10n->t(
'You do not have the permissions to add to this calendar.'
)
);
}
}
$stmt = OCP\DB::prepare('UPDATE `*PREFIX*clndr_objects` SET `calendarid` = ? WHERE `calendarid` = ?');
$stmt->execute(array($id1, $id2));
self::touchCalendar($id1);
self::deleteCalendar($id2);
}
/**
* @brief Creates a URI for Calendar
* @param string $name name of the calendar
* @param array $existing existing calendar URIs
* @return string uri
*/
public static function createURI($name,$existing) {
$strip=array(' ','/','?','&');//these may break sync clients
$name=str_replace($strip,'',$name);
$name = strtolower($name);
$newname = $name;
$i = 1;
while(in_array($newname,$existing)) {
$newname = $name.$i;
$i = $i + 1;
}
return $newname;
}
/**
* @brief gets the userid from a principal path
* @return string
*/
public static function extractUserID($principaluri) {
list($prefix,$userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
return $userid;
}
/**
* @brief returns the possible color for calendars
* @return array
*/
public static function getCalendarColorOptions() {
return array(
'#ff0000', // "Red"
'#b3dc6c', // "Green"
'#ffff00', // "Yellow"
'#808000', // "Olive"
'#ffa500', // "Orange"
'#ff7f50', // "Coral"
'#ee82ee', // "Violet"
'#9fc6e7', // "light blue"
);
}
/**
* @brief generates the Event Source Info for our JS
* @param array $calendar calendar data
* @return array
*/
public static function getEventSourceInfo($calendar) {
return array(
'url' => OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id='.$calendar['id'],
'backgroundColor' => $calendar['calendarcolor'],
'borderColor' => '#888',
'textColor' => self::generateTextColor($calendar['calendarcolor']),
'cache' => true,
);
}
/*
* @brief checks if a calendar name is available for a user
* @param string $calendarname
* @param string $userid
* @return boolean
*/
public static function isCalendarNameavailable($calendarname, $userid) {
$calendars = self::allCalendars($userid);
foreach($calendars as $calendar) {
if($calendar['displayname'] == $calendarname) {
return false;
}
}
return true;
}
/*
* @brief generates the text color for the calendar
* @param string $calendarcolor rgb calendar color code in hex format (with or without the leading #)
* (this function doesn't pay attention on the alpha value of rgba color codes)
* @return boolean
*/
public static function generateTextColor($calendarcolor) {
if(substr_count($calendarcolor, '#') == 1) {
$calendarcolor = substr($calendarcolor,1);
}
$red = hexdec(substr($calendarcolor,0,2));
$green = hexdec(substr($calendarcolor,2,2));
$blue = hexdec(substr($calendarcolor,4,2));
//recommendation by W3C
$computation = ((($red * 299) + ($green * 587) + ($blue * 114)) / 1000);
return ($computation > 130)?'#000000':'#FAFAFA';
}
}