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.
 
 
 
 
 
 

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