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.
 
 
 
 
 
 

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