function event_import_ical($ical, $uid) { $c = q("select * from channel where channel_id = %d limit 1", intval($uid)); if (!$c) { return false; } $channel = $c[0]; $ev = array(); if (!isset($ical->DTSTART)) { logger('no event start'); return false; } $dtstart = $ical->DTSTART->getDateTime(); // logger('dtstart: ' . var_export($dtstart,true)); if ($dtstart->timezone_type == 2 || $dtstart->timezone_type == 3 && $dtstart->timezone === 'UTC') { $ev['adjust'] = 1; } else { $ev['adjust'] = 0; } $ev['start'] = datetime_convert($ev['adjust'] ? 'UTC' : date_default_timezone_get(), 'UTC', $dtstart->format(\DateTime::W3C)); if (isset($ical->DTEND)) { $dtend = $ical->DTEND->getDateTime(); $ev['finish'] = datetime_convert($ev['adjust'] ? 'UTC' : date_default_timezone_get(), 'UTC', $dtend->format(\DateTime::W3C)); } else { $ev['nofinish'] = 1; } if ($ev['start'] === $ev['finish']) { $ev['nofinish'] = 1; } if (isset($ical->CREATED)) { $created = $ical->CREATED->getDateTime(); $ev['created'] = datetime_convert('UTC', 'UTC', $created->format(\DateTime::W3C)); } if (isset($ical->{'LAST-MODIFIED'})) { $edited = $ical->{'LAST-MODIFIED'}->getDateTime(); $ev['edited'] = datetime_convert('UTC', 'UTC', $edited->format(\DateTime::W3C)); } if (isset($ical->LOCATION)) { $ev['location'] = (string) $ical->LOCATION; } if (isset($ical->DESCRIPTION)) { $ev['description'] = (string) $ical->DESCRIPTION; } if (isset($ical->SUMMARY)) { $ev['summary'] = (string) $ical->SUMMARY; } if (isset($ical->UID)) { $evuid = (string) $ical->UID; $r = q("SELECT * FROM event WHERE event_hash = '%s' AND uid = %d LIMIT 1", dbesc($evuid), intval($uid)); if ($r) { $ev['event_hash'] = $evuid; } else { $ev['external_id'] = $evuid; } } if ($ev['summary'] && $ev['start']) { $ev['event_xchan'] = $channel['channel_hash']; $ev['uid'] = $channel['channel_id']; $ev['account'] = $channel['channel_account_id']; $ev['private'] = 1; $ev['allow_cid'] = '<' . $channel['channel_hash'] . '>'; logger('storing event: ' . print_r($ev, true), LOGGER_ALL); $event = event_store_event($ev); if ($event) { $item_id = event_store_item($ev, $event); return true; } } return false; }
function events_post(&$a) { logger('post: ' . print_r($_REQUEST, true)); if (!local_channel()) { return; } $event_id = x($_POST, 'event_id') ? intval($_POST['event_id']) : 0; $event_hash = x($_POST, 'event_hash') ? $_POST['event_hash'] : ''; $xchan = x($_POST, 'xchan') ? dbesc($_POST['xchan']) : ''; $uid = local_channel(); $start_text = escape_tags($_REQUEST['start_text']); $finish_text = escape_tags($_REQUEST['finish_text']); $adjust = intval($_POST['adjust']); $nofinish = intval($_POST['nofinish']); $categories = escape_tags(trim($_POST['category'])); // only allow editing your own events. if ($xchan && $xchan !== get_observer_hash()) { return; } // The default setting for the `private` field in event_store() is false, so mirror that $private_event = false; if ($start_text) { $start = $start_text; } else { $start = sprintf('%d-%d-%d %d:%d:0', $startyear, $startmonth, $startday, $starthour, $startminute); } if ($nofinish) { $finish = NULL_DATE; } if ($finish_text) { $finish = $finish_text; } else { $finish = sprintf('%d-%d-%d %d:%d:0', $finishyear, $finishmonth, $finishday, $finishhour, $finishminute); } if ($adjust) { $start = datetime_convert(date_default_timezone_get(), 'UTC', $start); if (!$nofinish) { $finish = datetime_convert(date_default_timezone_get(), 'UTC', $finish); } } else { $start = datetime_convert('UTC', 'UTC', $start); if (!$nofinish) { $finish = datetime_convert('UTC', 'UTC', $finish); } } // 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. $summary = escape_tags(trim($_POST['summary'])); $desc = escape_tags(trim($_POST['desc'])); $location = escape_tags(trim($_POST['location'])); $type = 'event'; require_once 'include/text.php'; linkify_tags($a, $desc, local_channel()); linkify_tags($a, $location, local_channel()); $action = $event_hash == '' ? 'new' : "event/" . $event_hash; $onerror_url = $a->get_baseurl() . "/events/" . $action . "?summary={$summary}&description={$desc}&location={$location}&start={$start_text}&finish={$finish_text}&adjust={$adjust}&nofinish={$nofinish}"; if (strcmp($finish, $start) < 0 && !$nofinish) { notice(t('Event can not end before it has started.') . EOL); if (intval($_REQUEST['preview'])) { echo t('Unable to generate preview.'); killme(); } goaway($onerror_url); } if (!$summary || !$start) { notice(t('Event title and start time are required.') . EOL); if (intval($_REQUEST['preview'])) { echo t('Unable to generate preview.'); killme(); } goaway($onerror_url); } $share = intval($_POST['share']) ? intval($_POST['share']) : 0; $channel = $a->get_channel(); if ($event_id) { $x = q("select * from event where id = %d and uid = %d limit 1", intval($event_id), intval(local_channel())); if (!$x) { notice(t('Event not found.') . EOL); if (intval($_REQUEST['preview'])) { echo t('Unable to generate preview.'); killme(); } return; } if ($x[0]['allow_cid'] === '<' . $channel['channel_hash'] . '>' && $x[0]['allow_gid'] === '' && $x[0]['deny_cid'] === '' && $x[0]['deny_gid'] === '') { $share = false; } else { $share = true; $str_group_allow = $x[0]['allow_gid']; $str_contact_allow = $x[0]['allow_cid']; $str_group_deny = $x[0]['deny_gid']; $str_contact_deny = $x[0]['deny_cid']; if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) { $private_event = true; } } } else { if ($share) { $str_group_allow = perms2str($_POST['group_allow']); $str_contact_allow = perms2str($_POST['contact_allow']); $str_group_deny = perms2str($_POST['group_deny']); $str_contact_deny = perms2str($_POST['contact_deny']); if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) { $private_event = true; } } else { $str_contact_allow = '<' . $channel['channel_hash'] . '>'; $str_group_allow = $str_contact_deny = $str_group_deny = ''; $private_event = true; } } $post_tags = array(); $channel = $a->get_channel(); if (strlen($categories)) { $cats = explode(',', $categories); foreach ($cats as $cat) { $post_tags[] = array('uid' => $profile_uid, 'type' => TERM_CATEGORY, 'otype' => TERM_OBJ_POST, 'term' => trim($cat), 'url' => $channel['xchan_url'] . '?f=&cat=' . urlencode(trim($cat))); } } $datarray = array(); $datarray['start'] = $start; $datarray['finish'] = $finish; $datarray['summary'] = $summary; $datarray['description'] = $desc; $datarray['location'] = $location; $datarray['type'] = $type; $datarray['adjust'] = $adjust; $datarray['nofinish'] = $nofinish; $datarray['uid'] = local_channel(); $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'] = $private_event ? 1 : 0; $datarray['id'] = $event_id; $datarray['created'] = $created; $datarray['edited'] = $edited; 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 ($share) { proc_run('php', "include/notifier.php", "event", "{$item_id}"); } }
function events_post(&$a) { logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA); if (!local_channel()) { return; } if ($_FILES && array_key_exists('userfile', $_FILES) && intval($_FILES['userfile']['size'])) { $src = $_FILES['userfile']['tmp_name']; if ($src) { $result = parse_ical_file($src, local_channel()); if ($result) { info(t('Calendar entries imported.') . EOL); } else { notice(t('No calendar entries found.') . EOL); } @unlink($src); } goaway(z_root() . '/events'); } $event_id = x($_POST, 'event_id') ? intval($_POST['event_id']) : 0; $event_hash = x($_POST, 'event_hash') ? $_POST['event_hash'] : ''; $xchan = x($_POST, 'xchan') ? dbesc($_POST['xchan']) : ''; $uid = local_channel(); $start_text = escape_tags($_REQUEST['start_text']); $finish_text = escape_tags($_REQUEST['finish_text']); $adjust = intval($_POST['adjust']); $nofinish = intval($_POST['nofinish']); $categories = escape_tags(trim($_POST['category'])); // only allow editing your own events. if ($xchan && $xchan !== get_observer_hash()) { return; } if ($start_text) { $start = $start_text; } else { $start = sprintf('%d-%d-%d %d:%d:0', $startyear, $startmonth, $startday, $starthour, $startminute); } if ($nofinish) { $finish = NULL_DATE; } if ($finish_text) { $finish = $finish_text; } else { $finish = sprintf('%d-%d-%d %d:%d:0', $finishyear, $finishmonth, $finishday, $finishhour, $finishminute); } if ($adjust) { $start = datetime_convert(date_default_timezone_get(), 'UTC', $start); if (!$nofinish) { $finish = datetime_convert(date_default_timezone_get(), 'UTC', $finish); } } else { $start = datetime_convert('UTC', 'UTC', $start); if (!$nofinish) { $finish = datetime_convert('UTC', 'UTC', $finish); } } // 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. $summary = escape_tags(trim($_POST['summary'])); $desc = escape_tags(trim($_POST['desc'])); $location = escape_tags(trim($_POST['location'])); $type = escape_tags(trim($_POST['type'])); require_once 'include/text.php'; linkify_tags($a, $desc, local_channel()); linkify_tags($a, $location, local_channel()); //$action = ($event_hash == '') ? 'new' : "event/" . $event_hash; //fixme: this url gives a wsod if there is a linebreak detected in one of the variables ($desc or $location) //$onerror_url = $a->get_baseurl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish&type=$type"; $onerror_url = $a->get_baseurl() . "/events"; if (strcmp($finish, $start) < 0 && !$nofinish) { notice(t('Event can not end before it has started.') . EOL); if (intval($_REQUEST['preview'])) { echo t('Unable to generate preview.'); killme(); } goaway($onerror_url); } if (!$summary || !$start) { notice(t('Event title and start time are required.') . EOL); if (intval($_REQUEST['preview'])) { echo t('Unable to generate preview.'); killme(); } goaway($onerror_url); } $share = intval($_POST['share']) ? intval($_POST['share']) : 0; $channel = $a->get_channel(); $acl = new AccessList(false); if ($event_id) { $x = q("select * from event where id = %d and uid = %d limit 1", intval($event_id), intval(local_channel())); 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(); if ($x[0]['allow_cid'] === '<' . $channel['channel_hash'] . '>' && $x[0]['allow_gid'] === '' && $x[0]['deny_cid'] === '' && $x[0]['deny_gid'] === '') { $share = false; } else { $share = true; } } else { $created = $edited = datetime_convert(); if ($share) { $acl->set_from_array($_POST); } else { $acl->set(array('allow_cid' => '<' . $channel['channel_hash'] . '>', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '')); } } $post_tags = array(); $channel = $a->get_channel(); $ac = $acl->get(); if (strlen($categories)) { $cats = explode(',', $categories); foreach ($cats as $cat) { $post_tags[] = array('uid' => $profile_uid, 'type' => TERM_CATEGORY, 'otype' => TERM_OBJ_POST, 'term' => trim($cat), 'url' => $channel['xchan_url'] . '?f=&cat=' . urlencode(trim($cat))); } } $datarray = array(); $datarray['start'] = $start; $datarray['finish'] = $finish; $datarray['summary'] = $summary; $datarray['description'] = $desc; $datarray['location'] = $location; $datarray['type'] = $type; $datarray['adjust'] = $adjust; $datarray['nofinish'] = $nofinish; $datarray['uid'] = local_channel(); $datarray['account'] = get_account_id(); $datarray['event_xchan'] = $channel['channel_hash']; $datarray['allow_cid'] = $ac['allow_cid']; $datarray['allow_gid'] = $ac['allow_gid']; $datarray['deny_cid'] = $ac['deny_cid']; $datarray['deny_gid'] = $ac['deny_gid']; $datarray['private'] = $acl->is_private() ? 1 : 0; $datarray['id'] = $event_id; $datarray['created'] = $created; $datarray['edited'] = $edited; 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) { build_sync_packet($channel['channel_id'], array('event_item' => array(encode_item($sync_item[0], true)), 'event' => $z)); } } } if ($share) { proc_run('php', "include/notifier.php", "event", "{$item_id}"); } }
/** * @brief Create a birthday event for any connections with a birthday in the next 1-2 weeks. * * Update the year so that we don't create another event until next year. */ function update_birthdays() { require_once 'include/event.php'; require_once 'include/permissions.php'; $r = q("SELECT * FROM abook left join xchan on abook_xchan = xchan_hash \n\t\tWHERE abook_dob > %s + interval %s and abook_dob < %s + interval %s", db_utcnow(), db_quoteinterval('7 day'), db_utcnow(), db_quoteinterval('14 day')); if ($r) { foreach ($r as $rr) { if (!perm_is_allowed($rr['abook_channel'], $rr['xchan_hash'], 'send_stream')) { continue; } $ev = array(); $ev['uid'] = $rr['abook_channel']; $ev['account'] = $rr['abook_account']; $ev['event_xchan'] = $rr['xchan_hash']; $ev['dtstart'] = datetime_convert('UTC', 'UTC', $rr['abook_dob']); $ev['dtend'] = datetime_convert('UTC', 'UTC', $rr['abook_dob'] . ' + 1 day '); $ev['adjust'] = intval(feature_enabled($rr['abook_channel'], 'smart_birthdays')); $ev['summary'] = sprintf(t('%1$s\'s birthday'), $rr['xchan_name']); $ev['description'] = sprintf(t('Happy Birthday %1$s'), '[zrl=' . $rr['xchan_url'] . ']' . $rr['xchan_name'] . '[/zrl]'); $ev['etype'] = 'birthday'; $z = event_store_event($ev); if ($z) { $item_id = event_store_item($ev, $z); q("update abook set abook_dob = '%s' where abook_id = %d", dbesc(intval($rr['abook_dob']) + 1 . substr($rr['abook_dob'], 4)), intval($rr['abook_id'])); } } } }
function event_import_ical_task($ical, $uid) { $c = q("select * from channel where channel_id = %d limit 1", intval($uid)); if (!$c) { return false; } $channel = $c[0]; $ev = array(); if (!isset($ical->DTSTART)) { logger('no event start'); return false; } $dtstart = $ical->DTSTART->getDateTime(); $ev['adjust'] = $ical->DTSTART->isFloating() ? 1 : 0; // logger('dtstart: ' . var_export($dtstart,true)); $ev['dtstart'] = datetime_convert($ev['adjust'] ? 'UTC' : date_default_timezone_get(), 'UTC', $dtstart->format(\DateTime::W3C)); if (isset($ical->DUE)) { $dtend = $ical->DUE->getDateTime(); $ev['dtend'] = datetime_convert($ev['adjust'] ? 'UTC' : date_default_timezone_get(), 'UTC', $dtend->format(\DateTime::W3C)); } else { $ev['nofinish'] = 1; } if ($ev['dtstart'] === $ev['dtend']) { $ev['nofinish'] = 1; } if (isset($ical->CREATED)) { $created = $ical->CREATED->getDateTime(); $ev['created'] = datetime_convert('UTC', 'UTC', $created->format(\DateTime::W3C)); } if (isset($ical->{'DTSTAMP'})) { $edited = $ical->{'DTSTAMP'}->getDateTime(); $ev['edited'] = datetime_convert('UTC', 'UTC', $edited->format(\DateTime::W3C)); } if (isset($ical->{'LAST-MODIFIED'})) { $edited = $ical->{'LAST-MODIFIED'}->getDateTime(); $ev['edited'] = datetime_convert('UTC', 'UTC', $edited->format(\DateTime::W3C)); } if (isset($ical->LOCATION)) { $ev['location'] = (string) $ical->LOCATION; } if (isset($ical->DESCRIPTION)) { $ev['description'] = (string) $ical->DESCRIPTION; } if (isset($ical->SUMMARY)) { $ev['summary'] = (string) $ical->SUMMARY; } if (isset($ical->PRIORITY)) { $ev['event_priority'] = intval((string) $ical->PRIORITY); } $stored_event = null; if (isset($ical->UID)) { $evuid = (string) $ical->UID; $r = q("SELECT * FROM event WHERE event_hash = '%s' AND uid = %d LIMIT 1", dbesc($evuid), intval($uid)); if ($r) { $ev['event_hash'] = $evuid; $stored_event = $r[0]; } else { $ev['external_id'] = $evuid; } } if (isset($ical->SEQUENCE)) { $ev['event_sequence'] = (string) $ical->SEQUENCE; // see if our stored event is more current than the one we're importing if (intval($ev['event_sequence']) <= intval($stored_event['event_sequence']) && $ev['edited'] <= $stored_event['edited']) { return false; } } if (isset($ical->STATUS)) { $ev['event_status'] = (string) $ical->STATUS; } if (isset($ical->{'COMPLETED'})) { $completed = $ical->{'COMPLETED'}->getDateTime(); $ev['event_status_date'] = datetime_convert('UTC', 'UTC', $completed->format(\DateTime::W3C)); } if (isset($ical->{'PERCENT-COMPLETE'})) { $ev['event_percent'] = (string) $ical->{'PERCENT-COMPLETE'}; } $ev['etype'] = 'task'; if ($ev['summary'] && $ev['dtstart']) { $ev['event_xchan'] = $channel['channel_hash']; $ev['uid'] = $channel['channel_id']; $ev['account'] = $channel['channel_account_id']; $ev['private'] = 1; $ev['allow_cid'] = '<' . $channel['channel_hash'] . '>'; logger('storing event: ' . print_r($ev, true), LOGGER_ALL); $event = event_store_event($ev); if ($event) { $item_id = event_store_item($ev, $event); return true; } } return false; }