aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Channel_calendar.php
blob: 289e3a734a222ab269fabc5a4cbf74f629c41344 (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
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
<?php

namespace Zotlabs\Module;

use App;
use Zotlabs\Web\Controller;
use Zotlabs\Lib\Libsync;
use Zotlabs\Access\AccessList;
use Zotlabs\Daemon\Master;

require_once('include/conversation.php');
require_once('include/bbcode.php');
require_once('include/datetime.php');
require_once('include/event.php');
require_once('include/items.php');
require_once('include/html2plain.php');

class Channel_calendar extends Controller {

	function post() {

		logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);

		$uid = local_channel();

		if (!$uid)
			return;

		$event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);

		$xchan = ((x($_POST, 'xchan')) ? dbesc($_POST['xchan']) : '');

		// only allow editing your own events.
		if (($xchan) && ($xchan !== get_observer_hash()))
			return;

		$categories = escape_tags(trim($_POST['categories']));

		// allday events have adjust = 0, normal events have adjust = 1
		$adjust = intval($_POST['adjust']);

		$timezone = ((x($_POST, 'timezone_select')) ? escape_tags(trim($_POST['timezone_select'])) : '');
		$tz       = (($timezone) ? $timezone : date_default_timezone_get());

		$start  = datetime_convert((($adjust) ? $tz : 'UTC'), 'UTC', escape_tags($_REQUEST['dtstart']));
		$finish = datetime_convert((($adjust) ? $tz : 'UTC'), 'UTC', escape_tags($_REQUEST['dtend']));

		if (!$adjust)
			$tz = 'UTC';

		$summary  = escape_tags(trim($_POST['summary']));
		$desc     = escape_tags(trim($_POST['desc']));
		$location = escape_tags(trim($_POST['location']));
		$type     = escape_tags(trim($_POST['type']));

		// Don't allow the event to finish before it begins.
		// It won't hurt anything, but somebody will file a bug report
		// and we'll waste a bunch of time responding to it. Time that
		// could've been spent doing something else.

		if (strcmp($finish, $start) < 0) {
			notice(t('Event can not end before it has started.') . EOL);
			if (intval($_REQUEST['preview'])) {
				echo(t('Unable to generate preview.'));
			}
			killme();
		}

		if ((!$summary) || (!$start)) {
			notice(t('Event title and start time are required.') . EOL);
			if (intval($_REQUEST['preview'])) {
				echo(t('Unable to generate preview.'));
			}
			killme();
		}

		$acl = new AccessList([]);

		if ($event_id) {
			$x = q("select * from event where id = %d and uid = %d limit 1",
				intval($event_id),
				intval($uid)
			);
			if (!$x) {
				notice(t('Event not found.') . EOL);
				if (intval($_REQUEST['preview'])) {
					echo(t('Unable to generate preview.'));
					killme();
				}
				return;
			}

			$acl->set($x[0]);

			$created = $x[0]['created'];
			$edited  = datetime_convert();
		}
		else {
			$created = $edited = datetime_convert();
			$acl->set_from_array($_POST);
		}

		$post_tags = array();
		$channel   = App::get_channel();
		$ac        = $acl->get();

		$str_contact_allow = $ac['allow_cid'];
		$str_group_allow   = $ac['allow_gid'];
		$str_contact_deny  = $ac['deny_cid'];
		$str_group_deny    = $ac['deny_gid'];

		$private = $acl->is_private();

		require_once('include/text.php');
		$results = linkify_tags($desc, $uid);

		if ($results) {
			// Set permissions based on tag replacements
			set_linkified_perms($results, $str_contact_allow, $str_group_allow, $uid, $private);

			foreach ($results as $result) {
				$success = $result['success'];
				if ($success['replaced']) {
					$post_tags[] = array(
						'uid'   => $uid,
						'ttype' => $success['termtype'],
						'otype' => TERM_OBJ_POST,
						'term'  => $success['term'],
						'url'   => $success['url']
					);
				}
			}
		}

		if (strlen($categories)) {
			$cats = explode(',', $categories);
			foreach ($cats as $cat) {
				$post_tags[] = array(
					'uid'   => $uid,
					'ttype' => TERM_CATEGORY,
					'otype' => TERM_OBJ_POST,
					'term'  => trim($cat),
					'url'   => $channel['xchan_url'] . '?f=&cat=' . urlencode(trim($cat))
				);
			}
		}

		$datarray                = array();
		$datarray['dtstart']     = $start;
		$datarray['dtend']       = $finish;
		$datarray['summary']     = $summary;
		$datarray['description'] = $desc;
		$datarray['location']    = $location;
		$datarray['etype']       = $type;
		$datarray['adjust']      = $adjust;
		$datarray['nofinish']    = 0;
		$datarray['uid']         = $uid;
		$datarray['account']     = get_account_id();
		$datarray['event_xchan'] = $channel['channel_hash'];
		$datarray['allow_cid']   = $str_contact_allow;
		$datarray['allow_gid']   = $str_group_allow;
		$datarray['deny_cid']    = $str_contact_deny;
		$datarray['deny_gid']    = $str_group_deny;
		$datarray['private']     = intval($private);
		$datarray['id']          = $event_id;
		$datarray['created']     = $created;
		$datarray['edited']      = $edited;
		$datarray['timezone']    = $tz;


		if (intval($_REQUEST['preview'])) {
			$html = format_event_html($datarray);
			echo $html;
			killme();
		}

		$event = event_store_event($datarray);

		if ($post_tags)
			$datarray['term'] = $post_tags;

		$item_id = event_store_item($datarray, $event);

		if ($item_id) {
			$r = q("select * from item where id = %d",
				intval($item_id)
			);
			if ($r) {
				xchan_query($r);
				$sync_item = fetch_post_tags($r);
				$z         = q("select * from event where event_hash = '%s' and uid = %d limit 1",
					dbesc($r[0]['resource_id']),
					intval($channel['channel_id'])
				);
				if ($z) {
					Libsync::build_sync_packet($channel['channel_id'], array('event_item' => array(encode_item($sync_item[0], true)), 'event' => $z));
				}
			}
		}

		Master::Summon(array('Notifier', 'event', $item_id));

		killme();

	}


	function get() {

		if (argc() > 2 && argv(1) == 'ical') {
			$event_id = argv(2);

			require_once('include/security.php');
			$sql_extra = permissions_sql(local_channel());

			$r = q("select * from event where event_hash = '%s' $sql_extra limit 1",
				dbesc($event_id)
			);
			if ($r) {
				header('Content-type: text/calendar');
				header('content-disposition: attachment; filename="' . t('event') . '-' . $event_id . '.ics"');
				echo ical_wrapper($r);
				killme();
			}
			else {
				notice(t('Event not found.') . EOL);
				return;
			}
		}

		if (!local_channel()) {
			notice(t('Permission denied.') . EOL);
			return;
		}

		if ((argc() > 2) && (argv(1) === 'ignore') && intval(argv(2))) {
			q("update event set dismissed = 1 where id = %d and uid = %d",
				intval(argv(2)),
				intval(local_channel())
			);
		}

		if ((argc() > 2) && (argv(1) === 'unignore') && intval(argv(2))) {
			q("update event set dismissed = 0 where id = %d and uid = %d",
				intval(argv(2)),
				intval(local_channel())
			);
		}

		$mode    = 'view';
		$export  = false;
		$ignored = ((x($_REQUEST, 'ignored')) ? " and dismissed = " . intval($_REQUEST['ignored']) . " " : '');

		if (argc() > 1) {
			if (argc() > 2 && argv(1) === 'add') {
				$mode    = 'add';
				$item_id = intval(argv(2));
			}
			if (argc() > 2 && argv(1) === 'drop') {
				$mode     = 'drop';
				$event_id = argv(2);
			}
			if (argc() <= 2 && argv(1) === 'export') {
				$export = true;
			}
			if (argc() > 2 && intval(argv(1)) && intval(argv(2))) {
				$mode = 'view';
			}
			if (argc() <= 2) {
				$mode     = 'view';
				$event_id = argv(1);
			}
		}

		if ($mode === 'add') {
			event_addtocal($item_id, local_channel());
			killme();
		}

		if ($mode == 'view') {

			/* edit/create form */
			if ($event_id) {
				q("SELECT * FROM event WHERE event_hash = '%s' AND uid = %d LIMIT 1",
					dbesc($event_id),
					intval(local_channel())
				);
			}

			$channel = App::get_channel();

			if (argv(1) === 'json') {
				if (x($_GET, 'start')) $start = $_GET['start'];
				if (x($_GET, 'end')) $finish = $_GET['end'];
			}

			$start         = datetime_convert('UTC', 'UTC', $start);
			$finish        = datetime_convert('UTC', 'UTC', $finish);
			$adjust_start  = datetime_convert('UTC', date_default_timezone_get(), $start);
			$adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);

			if (x($_GET, 'id')) {
				$r = q("SELECT event.*, item.plink, item.item_flags, item.author_xchan, item.owner_xchan, item.id as item_id
	                                from event left join item on item.resource_id = event.event_hash
					where item.resource_type = 'event' and event.uid = %d and event.id = %d limit 1",
					intval(local_channel()),
					intval($_GET['id'])
				);
			}
			elseif ($export) {
				$r = q("SELECT event.*, item.id as item_id
					from event left join item on item.resource_id = event.event_hash
					where event.uid = %d and event.dtstart > '%s' and event.dtend > event.dtstart",
					intval(local_channel()),
					dbesc(NULL_DATE)
				);
			}
			else {
				// fixed an issue with "nofinish" events not showing up in the calendar.
				// There's still an issue if the finish date crosses the end of month.
				// Noting this for now - it will need to be fixed here and in Friendica.
				// Ultimately the finish date shouldn't be involved in the query.

				$r = q("SELECT event.*, item.plink, item.item_flags, item.author_xchan, item.owner_xchan, item.id as item_id
					from event left join item on event.event_hash = item.resource_id
					where item.resource_type = 'event' and event.uid = %d and event.uid = item.uid $ignored
					AND (( event.adjust = 0 AND ( event.dtend >= '%s' or event.nofinish = 1 ) AND event.dtstart <= '%s' )
					OR  (  event.adjust = 1 AND ( event.dtend >= '%s' or event.nofinish = 1 ) AND event.dtstart <= '%s' )) ",
					intval(local_channel()),
					dbesc($start),
					dbesc($finish),
					dbesc($adjust_start),
					dbesc($adjust_finish)
				);
			}

			if ($r && !$export) {
				xchan_query($r);
				$r = fetch_post_tags($r, true);
				$r = sort_by_date($r);
			}

			$events = [];

			if ($r) {

				foreach ($r as $rr) {

					$start = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['dtstart'], 'c') : datetime_convert('UTC', 'UTC', $rr['dtstart'], 'c'));
					if ($rr['nofinish']) {
						$end = null;
					}
					else {
						$end = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['dtend'], 'c') : datetime_convert('UTC', 'UTC', $rr['dtend'], 'c'));
					}

					$catsenabled = feature_enabled(local_channel(), 'categories');
					$categories  = '';
					if ($catsenabled) {
						if (isset($rr['term']) && $rr['term']) {
							$cats = get_terms_oftype($rr['term'], TERM_CATEGORY);
							foreach ($cats as $cat) {
								if (strlen($categories))
									$categories .= ', ';
								$categories .= $cat['term'];
							}
						}
					}

					$edit = ((local_channel() && $rr['author_xchan'] == get_observer_hash()) ? array(z_root() . '/events/' . $rr['event_hash'] . '?expandform=1', t('Edit event'), '', '') : false);

					$drop = array(z_root() . '/events/drop/' . $rr['event_hash'], t('Delete event'), '', '');

					$tz = get_iconfig($rr, 'event', 'timezone');

					if (!$tz)
						$tz = 'UTC';

					$events[] = array(
						'calendar_id' => 'channel_calendar',
						'rw'          => true,
						'id'          => $rr['id'],
						'uri'         => $rr['event_hash'],
						'timezone'    => $tz,
						'start'       => $start,
						'end'         => $end,
						'drop'        => $drop,
						'allDay'      => (($rr['adjust']) ? 0 : 1),
						'title'       => html_entity_decode($rr['summary'], ENT_COMPAT, 'UTF-8'),
						'editable'    => $edit ? true : false,
						'item'        => $rr,
						'plink'       => [$rr['plink'], t('Link to source')],
						'description' => html_entity_decode($rr['description'], ENT_COMPAT, 'UTF-8'),
						'location'    => html_entity_decode($rr['location'], ENT_COMPAT, 'UTF-8'),
						'allow_cid'   => expand_acl($rr['allow_cid']),
						'allow_gid'   => expand_acl($rr['allow_gid']),
						'deny_cid'    => expand_acl($rr['deny_cid']),
						'deny_gid'    => expand_acl($rr['deny_gid']),
						'categories'  => $categories
					);
				}
			}

			if ($export) {
				header('Content-type: text/calendar');
				header('content-disposition: attachment; filename="' . t('calendar') . '-' . $channel['channel_address'] . '.ics"');
				echo ical_wrapper($r);
				killme();
			}

			if (App::$argv[1] === 'json') {
				json_return_and_die($events);
			}
		}


		if ($mode === 'drop' && $event_id) {
			$r = q("SELECT * FROM event WHERE event_hash = '%s' AND uid = %d LIMIT 1",
				dbesc($event_id),
				intval(local_channel())
			);

			$sync_event = $r[0];

			if ($r) {
				$r = q("delete from event where event_hash = '%s' and uid = %d",
					dbesc($event_id),
					intval(local_channel())
				);

				if ($r) {

					$sync_event['event_deleted'] = 1;
					Libsync::build_sync_packet(0, array('event' => array($sync_event)));

					$i = q("select * from item where resource_type = 'event' and resource_id = '%s' and uid = %d",
						dbesc($event_id),
						intval(local_channel())
					);

					if ($i) {

						$can_delete   = false;
						$local_delete = true;

						$ob_hash = get_observer_hash();
						if ($ob_hash && ($ob_hash === $i[0]['author_xchan'] || $ob_hash === $i[0]['owner_xchan'] || $ob_hash === $i[0]['source_xchan'])) {
							$can_delete = true;
						}

						// The site admin can delete any post/item on the site.
						// If the item originated on this site+channel the deletion will propagate downstream.
						// Otherwise just the local copy is removed.

						if (is_site_admin()) {
							$local_delete = true;
							if (intval($i[0]['item_origin']))
								$can_delete = true;
						}

						if ($can_delete || $local_delete) {

							// if this is a different page type or it's just a local delete
							// but not by the item author or owner, do a simple deletion

							$complex = false;

							if (intval($i[0]['item_type']) || ($local_delete && (!$can_delete))) {
								drop_item($i[0]['id']);
							}
							else {
								// complex deletion that needs to propagate and be performed in phases
								drop_item($i[0]['id'], true, DROPITEM_PHASE1);
								$complex = true;
							}

							$ii = q("select * from item where id = %d",
								intval($i[0]['id'])
							);
							if ($ii) {
								xchan_query($ii);
								$sync_item = fetch_post_tags($ii);
								Libsync::build_sync_packet($i[0]['uid'], array('item' => array(encode_item($sync_item[0], true))));
							}

							if ($complex) {
								tag_deliver($i[0]['uid'], $i[0]['id']);
							}
						}
					}
					killme();
				}
				notice(t('Failed to remove event') . EOL);
				killme();
			}
		}

	}

}