Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

536 lines
16 KiB

  1. <?php
  2. /**
  3. * @file mod/events.php
  4. * @brief The events module
  5. */
  6. require_once 'include/bbcode.php';
  7. require_once 'include/datetime.php';
  8. require_once 'include/event.php';
  9. require_once 'include/items.php';
  10. function events_init(App $a) {
  11. if (! local_user()) {
  12. return;
  13. }
  14. if ($a->argc == 1) {
  15. // If it's a json request abort here because we don't
  16. // need the widget data
  17. if ($a->argv[1] === 'json') {
  18. return;
  19. }
  20. $cal_widget = widget_events();
  21. if (! x($a->page,'aside')) {
  22. $a->page['aside'] = '';
  23. }
  24. $a->page['aside'] .= $cal_widget;
  25. }
  26. return;
  27. }
  28. function events_post(App $a) {
  29. logger('post: ' . print_r($_REQUEST, true), LOGGER_DATA);
  30. if (! local_user()) {
  31. return;
  32. }
  33. $event_id = ((x($_POST, 'event_id')) ? intval($_POST['event_id']) : 0);
  34. $cid = ((x($_POST, 'cid')) ? intval($_POST['cid']) : 0);
  35. $uid = local_user();
  36. $start_text = escape_tags($_REQUEST['start_text']);
  37. $finish_text = escape_tags($_REQUEST['finish_text']);
  38. $adjust = intval($_POST['adjust']);
  39. $nofinish = intval($_POST['nofinish']);
  40. // The default setting for the `private` field in event_store() is false, so mirror that
  41. $private_event = false;
  42. $start = '0000-00-00 00:00:00';
  43. $finish = '0000-00-00 00:00:00';
  44. if ($start_text) {
  45. $start = $start_text;
  46. }
  47. if ($finish_text) {
  48. $finish = $finish_text;
  49. }
  50. if ($adjust) {
  51. $start = datetime_convert(date_default_timezone_get(), 'UTC', $start);
  52. if (! $nofinish) {
  53. $finish = datetime_convert(date_default_timezone_get(), 'UTC', $finish);
  54. }
  55. } else {
  56. $start = datetime_convert('UTC', 'UTC', $start);
  57. if (! $nofinish) {
  58. $finish = datetime_convert('UTC', 'UTC', $finish);
  59. }
  60. }
  61. // Don't allow the event to finish before it begins.
  62. // It won't hurt anything, but somebody will file a bug report
  63. // and we'll waste a bunch of time responding to it. Time that
  64. // could've been spent doing something else.
  65. $summary = escape_tags(trim($_POST['summary']));
  66. $desc = escape_tags(trim($_POST['desc']));
  67. $location = escape_tags(trim($_POST['location']));
  68. $type = 'event';
  69. $action = ($event_id == '') ? 'new' : "event/" . $event_id;
  70. $onerror_url = App::get_baseurl() . "/events/" . $action . "?summary=$summary&description=$desc&location=$location&start=$start_text&finish=$finish_text&adjust=$adjust&nofinish=$nofinish";
  71. if (strcmp($finish, $start) < 0 && !$nofinish) {
  72. notice(t('Event can not end before it has started.') . EOL);
  73. if (intval($_REQUEST['preview'])) {
  74. echo t('Event can not end before it has started.');
  75. killme();
  76. }
  77. goaway($onerror_url);
  78. }
  79. if ((! $summary) || ($start === '0000-00-00 00:00:00')) {
  80. notice(t('Event title and start time are required.') . EOL);
  81. if (intval($_REQUEST['preview'])) {
  82. echo t('Event title and start time are required.');
  83. killme();
  84. }
  85. goaway($onerror_url);
  86. }
  87. $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
  88. $c = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
  89. intval(local_user())
  90. );
  91. if (count($c)) {
  92. $self = $c[0]['id'];
  93. } else {
  94. $self = 0;
  95. }
  96. if ($share) {
  97. $str_group_allow = perms2str($_POST['group_allow']);
  98. $str_contact_allow = perms2str($_POST['contact_allow']);
  99. $str_group_deny = perms2str($_POST['group_deny']);
  100. $str_contact_deny = perms2str($_POST['contact_deny']);
  101. // Undo the pseudo-contact of self, since there are real contacts now
  102. if (strpos($str_contact_allow, '<' . $self . '>') !== false ) {
  103. $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
  104. }
  105. // Make sure to set the `private` field as true. This is necessary to
  106. // have the posts show up correctly in Diaspora if an event is created
  107. // as visible only to self at first, but then edited to display to others.
  108. if (strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) {
  109. $private_event = true;
  110. }
  111. } else {
  112. // Note: do not set `private` field for self-only events. It will
  113. // keep even you from seeing them!
  114. $str_contact_allow = '<' . $self . '>';
  115. $str_group_allow = $str_contact_deny = $str_group_deny = '';
  116. }
  117. $datarray = array();
  118. $datarray['guid'] = get_guid(32);
  119. $datarray['start'] = $start;
  120. $datarray['finish'] = $finish;
  121. $datarray['summary'] = $summary;
  122. $datarray['desc'] = $desc;
  123. $datarray['location'] = $location;
  124. $datarray['type'] = $type;
  125. $datarray['adjust'] = $adjust;
  126. $datarray['nofinish'] = $nofinish;
  127. $datarray['uid'] = $uid;
  128. $datarray['cid'] = $cid;
  129. $datarray['allow_cid'] = $str_contact_allow;
  130. $datarray['allow_gid'] = $str_group_allow;
  131. $datarray['deny_cid'] = $str_contact_deny;
  132. $datarray['deny_gid'] = $str_group_deny;
  133. $datarray['private'] = (($private_event) ? 1 : 0);
  134. $datarray['id'] = $event_id;
  135. $datarray['created'] = $created;
  136. $datarray['edited'] = $edited;
  137. if (intval($_REQUEST['preview'])) {
  138. $html = format_event_html($datarray);
  139. echo $html;
  140. killme();
  141. }
  142. $item_id = event_store($datarray);
  143. if (! $cid) {
  144. proc_run(PRIORITY_HIGH, "include/notifier.php", "event", $item_id);
  145. }
  146. goaway($_SESSION['return_url']);
  147. }
  148. function events_content(App $a) {
  149. if (! local_user()) {
  150. notice(t('Permission denied.') . EOL);
  151. return;
  152. }
  153. if ($a->argc == 1) {
  154. $_SESSION['return_url'] = App::get_baseurl() . '/' . $a->cmd;
  155. }
  156. if (($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
  157. $r = q("UPDATE `event` SET `ignore` = 1 WHERE `id` = %d AND `uid` = %d",
  158. intval($a->argv[2]),
  159. intval(local_user())
  160. );
  161. }
  162. if (($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
  163. $r = q("UPDATE `event` SET `ignore` = 0 WHERE `id` = %d AND `uid` = %d",
  164. intval($a->argv[2]),
  165. intval(local_user())
  166. );
  167. }
  168. if ($a->theme_events_in_profile) {
  169. nav_set_selected('home');
  170. } else {
  171. nav_set_selected('events');
  172. }
  173. // get the translation strings for the callendar
  174. $i18n = get_event_strings();
  175. $htpl = get_markup_template('event_head.tpl');
  176. $a->page['htmlhead'] .= replace_macros($htpl, array(
  177. '$baseurl' => App::get_baseurl(),
  178. '$module_url' => '/events',
  179. '$modparams' => 1,
  180. '$i18n' => $i18n,
  181. ));
  182. $etpl = get_markup_template('event_end.tpl');
  183. $a->page['end'] .= replace_macros($etpl, array(
  184. '$baseurl' => App::get_baseurl(),
  185. ));
  186. $o = '';
  187. // tabs
  188. if ($a->theme_events_in_profile) {
  189. $tabs = profile_tabs($a, true);
  190. }
  191. $mode = 'view';
  192. $y = 0;
  193. $m = 0;
  194. $ignored = ((x($_REQUEST, 'ignored')) ? intval($_REQUEST['ignored']) : 0);
  195. if ($a->argc > 1) {
  196. if ($a->argc > 2 && $a->argv[1] == 'event') {
  197. $mode = 'edit';
  198. $event_id = intval($a->argv[2]);
  199. }
  200. if ($a->argc > 2 && $a->argv[1] == 'drop') {
  201. $mode = 'drop';
  202. $event_id = intval($a->argv[2]);
  203. }
  204. if ($a->argv[1] === 'new') {
  205. $mode = 'new';
  206. $event_id = 0;
  207. }
  208. if ($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
  209. $mode = 'view';
  210. $y = intval($a->argv[1]);
  211. $m = intval($a->argv[2]);
  212. }
  213. }
  214. // The view mode part is similiar to /mod/cal.php
  215. if ($mode == 'view') {
  216. $thisyear = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y');
  217. $thismonth = datetime_convert('UTC', date_default_timezone_get(), 'now', 'm');
  218. if (! $y) {
  219. $y = intval($thisyear);
  220. }
  221. if (! $m) {
  222. $m = intval($thismonth);
  223. }
  224. // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
  225. // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
  226. if ($y < 1901) {
  227. $y = 1900;
  228. }
  229. if ($y > 2099) {
  230. $y = 2100;
  231. }
  232. $nextyear = $y;
  233. $nextmonth = $m + 1;
  234. if ($nextmonth > 12) {
  235. $nextmonth = 1;
  236. $nextyear ++;
  237. }
  238. $prevyear = $y;
  239. if ($m > 1) {
  240. $prevmonth = $m - 1;
  241. } else {
  242. $prevmonth = 12;
  243. $prevyear --;
  244. }
  245. $dim = get_dim($y, $m);
  246. $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
  247. $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
  248. if ($a->argv[1] === 'json') {
  249. if (x($_GET, 'start')) {$start = $_GET['start'];}
  250. if (x($_GET, 'end')) {$finish = $_GET['end'];}
  251. }
  252. $start = datetime_convert('UTC', 'UTC', $start);
  253. $finish = datetime_convert('UTC', 'UTC', $finish);
  254. $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
  255. $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
  256. // put the event parametes in an array so we can better transmit them
  257. $event_params = array(
  258. 'event_id' => (x($_GET, 'id') ? $_GET['id'] : 0),
  259. 'start' => $start,
  260. 'finish' => $finish,
  261. 'adjust_start' => $adjust_start,
  262. 'adjust_finish' => $adjust_finish,
  263. 'ignored' => $ignored,
  264. );
  265. // get events by id or by date
  266. if (x($_GET, 'id')) {
  267. $r = event_by_id(local_user(), $event_params);
  268. } else {
  269. $r = events_by_date(local_user(), $event_params);
  270. }
  271. $links = array();
  272. if (dbm::is_result($r)) {
  273. $r = sort_by_date($r);
  274. foreach ($r as $rr) {
  275. $j = (($rr['adjust']) ? datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'j') : datetime_convert('UTC', 'UTC', $rr['start'], 'j'));
  276. if (! x($links,$j)) {
  277. $links[$j] = App::get_baseurl() . '/' . $a->cmd . '#link-' . $j;
  278. }
  279. }
  280. }
  281. $events = array();
  282. // transform the event in a usable array
  283. if (dbm::is_result($r)) {
  284. $r = sort_by_date($r);
  285. $events = process_events($r);
  286. }
  287. if ($a->argv[1] === 'json'){
  288. echo json_encode($events);
  289. killme();
  290. }
  291. if (x($_GET, 'id')) {
  292. $tpl = get_markup_template("event.tpl");
  293. } else {
  294. $tpl = get_markup_template("events_js.tpl");
  295. }
  296. // Get rid of dashes in key names, Smarty3 can't handle them
  297. foreach ($events as $key => $event) {
  298. $event_item = array();
  299. foreach ($event['item'] as $k => $v) {
  300. $k = str_replace('-' ,'_', $k);
  301. $event_item[$k] = $v;
  302. }
  303. $events[$key]['item'] = $event_item;
  304. }
  305. $o = replace_macros($tpl, array(
  306. '$baseurl' => App::get_baseurl(),
  307. '$tabs' => $tabs,
  308. '$title' => t('Events'),
  309. '$view' => t('View'),
  310. '$new_event' => array(App::get_baseurl() . '/events/new', t('Create New Event'), '', ''),
  311. '$previous' => array(App::get_baseurl() . '/events/$prevyear/$prevmonth', t('Previous'), '', ''),
  312. '$next' => array(App::get_baseurl() . '/events/$nextyear/$nextmonth', t('Next'), '', ''),
  313. '$calendar' => cal($y, $m, $links, ' eventcal'),
  314. '$events' => $events,
  315. '$today' => t('today'),
  316. '$month' => t('month'),
  317. '$week' => t('week'),
  318. '$day' => t('day'),
  319. '$list' => t('list'),
  320. ));
  321. if (x($_GET, 'id')) {
  322. echo $o;
  323. killme();
  324. }
  325. return $o;
  326. }
  327. if ($mode === 'edit' && $event_id) {
  328. $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  329. intval($event_id),
  330. intval(local_user())
  331. );
  332. if (dbm::is_result($r)) {
  333. $orig_event = $r[0];
  334. }
  335. }
  336. // Passed parameters overrides anything found in the DB
  337. if ($mode === 'edit' || $mode === 'new') {
  338. if (!x($orig_event)) {$orig_event = array();}
  339. // In case of an error the browser is redirected back here, with these parameters filled in with the previous values
  340. if (x($_REQUEST, 'nofinish')) {$orig_event['nofinish'] = $_REQUEST['nofinish'];}
  341. if (x($_REQUEST, 'adjust')) {$orig_event['adjust'] = $_REQUEST['adjust'];}
  342. if (x($_REQUEST, 'summary')) {$orig_event['summary'] = $_REQUEST['summary'];}
  343. if (x($_REQUEST, 'description')) {$orig_event['description'] = $_REQUEST['description'];}
  344. if (x($_REQUEST, 'location')) {$orig_event['location'] = $_REQUEST['location'];}
  345. if (x($_REQUEST, 'start')) {$orig_event['start'] = $_REQUEST['start'];}
  346. if (x($_REQUEST, 'finish')) {$orig_event['finish'] = $_REQUEST['finish'];}
  347. $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
  348. $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
  349. $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
  350. $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
  351. $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
  352. $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
  353. $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
  354. $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
  355. if (! x($orig_event)) {
  356. $sh_checked = '';
  357. } else {
  358. $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ');
  359. }
  360. if ($cid OR ($mode !== 'new')) {
  361. $sh_checked .= ' disabled="disabled" ';
  362. }
  363. $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
  364. $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
  365. $tz = date_default_timezone_get();
  366. if (x($orig_event)) {
  367. $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
  368. }
  369. $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
  370. $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
  371. $sday = datetime_convert('UTC', $tz, $sdt, 'd');
  372. $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
  373. $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
  374. $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
  375. $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
  376. $fday = datetime_convert('UTC', $tz, $fdt, 'd');
  377. $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
  378. $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
  379. $f = get_config('system','event_input_format');
  380. if (! $f) {
  381. $f = 'ymd';
  382. }
  383. require_once 'include/acl_selectors.php' ;
  384. if ($mode === 'new') {
  385. $acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
  386. }
  387. $tpl = get_markup_template('event_form.tpl');
  388. $o .= replace_macros($tpl,array(
  389. '$post' => App::get_baseurl() . '/events',
  390. '$eid' => $eid,
  391. '$cid' => $cid,
  392. '$uri' => $uri,
  393. '$title' => t('Event details'),
  394. '$desc' => t('Starting date and Title are required.'),
  395. '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
  396. '$s_dsel' => datetimesel($f, new DateTime(), DateTime::createFromFormat('Y', $syear+5), DateTime::createFromFormat('Y-m-d H:i', "$syear-$smonth-$sday $shour:$sminute"), t('Event Starts:'), 'start_text', true, true, '', '', true),
  397. '$n_text' => t('Finish date/time is not known or not relevant'),
  398. '$n_checked' => $n_checked,
  399. '$f_text' => t('Event Finishes:'),
  400. '$f_dsel' => datetimesel($f, new DateTime(), DateTime::createFromFormat('Y', $fyear+5), DateTime::createFromFormat('Y-m-d H:i', "$fyear-$fmonth-$fday $fhour:$fminute"), t('Event Finishes:'), 'finish_text', true, true, 'start_text'),
  401. '$a_text' => t('Adjust for viewer timezone'),
  402. '$a_checked' => $a_checked,
  403. '$d_text' => t('Description:'),
  404. '$d_orig' => $d_orig,
  405. '$l_text' => t('Location:'),
  406. '$l_orig' => $l_orig,
  407. '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
  408. '$t_orig' => $t_orig,
  409. '$summary' => array('summary', t('Title:'), $t_orig, '', '*'),
  410. '$sh_text' => t('Share this event'),
  411. '$share' => array('share', t('Share this event'), $sh_checked, ''),
  412. '$sh_checked' => $sh_checked,
  413. '$nofinish' => array('nofinish', t('Finish date/time is not known or not relevant'), $n_checked),
  414. '$adjust' => array('adjust', t('Adjust for viewer timezone'), $a_checked),
  415. '$preview' => t('Preview'),
  416. '$acl' => $acl,
  417. '$submit' => t('Submit'),
  418. '$basic' => t('Basic'),
  419. '$advanced' => t('Advanced'),
  420. '$permissions' => t('Permissions'),
  421. ));
  422. return $o;
  423. }
  424. // Remove an event from the calendar and its related items
  425. if ($mode === 'drop' && $event_id) {
  426. $del = 0;
  427. $params = array('event_id' => ($event_id));
  428. $ev = event_by_id(local_user(), $params);
  429. // Delete only real events (no birthdays)
  430. if (dbm::is_result($ev) && $ev[0]['type'] == 'event') {
  431. $del = drop_item($ev[0]['itemid'], false);
  432. }
  433. if ($del == 0) {
  434. notice(t('Failed to remove event' ) . EOL);
  435. } else {
  436. info(t('Event removed') . EOL);
  437. }
  438. goaway(App::get_baseurl() . '/events');
  439. }
  440. }