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