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.

338 lines
8.4 KiB

  1. <?php
  2. /**
  3. * @file mod/cal.php
  4. * @brief The calendar module
  5. * This calendar is for profile visitors and contains only the events
  6. * of the profile owner
  7. */
  8. require_once('include/event.php');
  9. require_once('include/redir.php');
  10. function cal_init(&$a) {
  11. if($a->argc > 1)
  12. auto_redir($a, $a->argv[1]);
  13. if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
  14. return;
  15. }
  16. nav_set_selected('events');
  17. $o = '';
  18. if($a->argc > 1) {
  19. $nick = $a->argv[1];
  20. $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
  21. dbesc($nick)
  22. );
  23. if(! count($user))
  24. return;
  25. $a->data['user'] = $user[0];
  26. $a->profile_uid = $user[0]['uid'];
  27. // if it's a json request abort here becaus we don't
  28. // need the widget data
  29. if ($a->argv[2] === 'json')
  30. return;
  31. $profile = get_profiledata_by_nick($nick, $a->profile_uid);
  32. $account_type = account_type($profile);
  33. $tpl = get_markup_template("vcard-widget.tpl");
  34. $vcard_widget .= replace_macros($tpl, array(
  35. '$name' => $profile['name'],
  36. '$photo' => $profile['photo'],
  37. '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
  38. '$account_type' => $account_type,
  39. '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
  40. ));
  41. $cal_widget = widget_events();
  42. if(! x($a->page,'aside'))
  43. $a->page['aside'] = '';
  44. $a->page['aside'] .= $vcard_widget;
  45. $a->page['aside'] .= $cal_widget;
  46. }
  47. return;
  48. }
  49. function cal_content(&$a) {
  50. nav_set_selected('events');
  51. $editselect = 'none';
  52. if( feature_enabled(local_user(), 'richtext') )
  53. $editselect = 'textareas';
  54. // First day of the week (0 = Sunday)
  55. $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
  56. if ($firstDay === false) $firstDay=0;
  57. // get the translation strings for the callendar
  58. $i18n = get_event_strings();
  59. $htpl = get_markup_template('event_head.tpl');
  60. $a->page['htmlhead'] .= replace_macros($htpl,array(
  61. '$baseurl' => $a->get_baseurl(),
  62. '$module_url' => '/cal/' . $a->data['user']['nickname'],
  63. '$modparams' => 2,
  64. '$i18n' => $i18n,
  65. '$editselect' => $editselect
  66. ));
  67. $etpl = get_markup_template('event_end.tpl');
  68. $a->page['end'] .= replace_macros($etpl,array(
  69. '$baseurl' => $a->get_baseurl(),
  70. '$editselect' => $editselect
  71. ));
  72. $o ="";
  73. $mode = 'view';
  74. $y = 0;
  75. $m = 0;
  76. $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
  77. if($a->argc == 4) {
  78. if($a->argv[2] == 'export') {
  79. $mode = 'export';
  80. $format = $a->argv[3];
  81. }
  82. }
  83. //
  84. // Setup permissions structures
  85. //
  86. $contact = null;
  87. $remote_contact = false;
  88. $contact_id = 0;
  89. $owner_uid = $a->data['user']['uid'];
  90. $nick = $a->data['user']['nickname'];
  91. if(is_array($_SESSION['remote'])) {
  92. foreach($_SESSION['remote'] as $v) {
  93. if($v['uid'] == $a->profile['profile_uid']) {
  94. $contact_id = $v['cid'];
  95. break;
  96. }
  97. }
  98. }
  99. if($contact_id) {
  100. $groups = init_groups_visitor($contact_id);
  101. $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  102. intval($contact_id),
  103. intval($a->profile['profile_uid'])
  104. );
  105. if(count($r)) {
  106. $contact = $r[0];
  107. $remote_contact = true;
  108. }
  109. }
  110. if(! $remote_contact) {
  111. if(local_user()) {
  112. $contact_id = $_SESSION['cid'];
  113. $contact = $a->contact;
  114. }
  115. }
  116. $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
  117. if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
  118. notice( t('Access to this profile has been restricted.') . EOL);
  119. return;
  120. }
  121. // get the permissions
  122. $sql_perms = item_permissions_sql($owner_uid,$remote_contact,$groups);
  123. // we only want to have the events of the profile owner
  124. $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
  125. // get the tab navigation bar
  126. $tabs .= profile_tabs($a,false, $a->data['user']['nickname']);
  127. // The view mode part is similiar to /mod/events.php
  128. if($mode == 'view') {
  129. $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
  130. $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
  131. if(! $y)
  132. $y = intval($thisyear);
  133. if(! $m)
  134. $m = intval($thismonth);
  135. // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
  136. // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
  137. if($y < 1901)
  138. $y = 1900;
  139. if($y > 2099)
  140. $y = 2100;
  141. $nextyear = $y;
  142. $nextmonth = $m + 1;
  143. if($nextmonth > 12) {
  144. $nextmonth = 1;
  145. $nextyear ++;
  146. }
  147. $prevyear = $y;
  148. if($m > 1)
  149. $prevmonth = $m - 1;
  150. else {
  151. $prevmonth = 12;
  152. $prevyear --;
  153. }
  154. $dim = get_dim($y,$m);
  155. $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
  156. $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
  157. if ($a->argv[2] === 'json'){
  158. if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
  159. if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
  160. }
  161. $start = datetime_convert('UTC','UTC',$start);
  162. $finish = datetime_convert('UTC','UTC',$finish);
  163. $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
  164. $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
  165. // put the event parametes in an array so we can better transmit them
  166. $event_params = array(
  167. 'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
  168. 'start' => $start,
  169. 'finish' => $finish,
  170. 'adjust_start' => $adjust_start,
  171. 'adjust_finish' => $adjust_finish,
  172. 'ignored' => $ignored,
  173. );
  174. // get events by id or by date
  175. if (x($_GET,'id')){
  176. $r = event_by_id($owner_uid, $event_params, $sql_extra);
  177. } else {
  178. $r = events_by_date($owner_uid, $event_params, $sql_extra);
  179. }
  180. $links = array();
  181. if(count($r)) {
  182. $r = sort_by_date($r);
  183. foreach($r as $rr) {
  184. $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
  185. if(! x($links,$j))
  186. $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
  187. }
  188. }
  189. $events=array();
  190. // transform the event in a usable array
  191. if(count($r))
  192. $r = sort_by_date($r);
  193. $events = process_events($r);
  194. if ($a->argv[2] === 'json'){
  195. echo json_encode($events); killme();
  196. }
  197. // links: array('href', 'text', 'extra css classes', 'title')
  198. if (x($_GET,'id')){
  199. $tpl = get_markup_template("event.tpl");
  200. } else {
  201. // if (get_config('experimentals','new_calendar')==1){
  202. $tpl = get_markup_template("events-js.tpl");
  203. // } else {
  204. // $tpl = get_markup_template("events.tpl");
  205. // }
  206. }
  207. // Get rid of dashes in key names, Smarty3 can't handle them
  208. foreach($events as $key => $event) {
  209. $event_item = array();
  210. foreach($event['item'] as $k => $v) {
  211. $k = str_replace('-','_',$k);
  212. $event_item[$k] = $v;
  213. }
  214. $events[$key]['item'] = $event_item;
  215. }
  216. $o = replace_macros($tpl, array(
  217. '$baseurl' => $a->get_baseurl(),
  218. '$tabs' => $tabs,
  219. '$title' => t('Events'),
  220. '$view' => t('View'),
  221. '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
  222. '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
  223. '$calendar' => cal($y,$m,$links, ' eventcal'),
  224. '$events' => $events,
  225. "today" => t("today"),
  226. "month" => t("month"),
  227. "week" => t("week"),
  228. "day" => t("day"),
  229. ));
  230. if (x($_GET,'id')){ echo $o; killme(); }
  231. return $o;
  232. }
  233. if($mode == 'export') {
  234. if(! (intval($owner_uid))) {
  235. notice( t('User not found'));
  236. return;
  237. }
  238. // Test permissions
  239. // Respect the export feature setting for all other /cal pages if it's not the own profile
  240. if( ((local_user() !== intval($owner_uid))) && ! feature_enabled($owner_uid, "export_calendar")) {
  241. notice( t('Permission denied.') . EOL);
  242. goaway('cal/' . $nick);
  243. }
  244. // Get the export data by uid
  245. $evexport = event_export($owner_uid, $format);
  246. if (!$evexport["success"]) {
  247. if($evexport["content"])
  248. notice( t('This calendar format is not supported') );
  249. else
  250. notice( t('No exportable data found'));
  251. // If it the own calendar return to the events page
  252. // otherwise to the profile calendar page
  253. if (local_user() === intval($owner_uid))
  254. $return_path = "events";
  255. else
  256. $returnpath = "cal/".$nick;
  257. goaway($return_path);
  258. }
  259. // If nothing went wrong we can echo the export content
  260. if ($evexport["success"]) {
  261. header('Content-type: text/calendar');
  262. header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"' );
  263. echo $evexport["content"];
  264. killme();
  265. }
  266. return;
  267. }
  268. }