Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

cal.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. use Friendica\App;
  9. use Friendica\Core\Config;
  10. use Friendica\Core\PConfig;
  11. use Friendica\Core\System;
  12. require_once('include/event.php');
  13. require_once('include/redir.php');
  14. function cal_init(App $a) {
  15. if($a->argc > 1)
  16. auto_redir($a, $a->argv[1]);
  17. if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
  18. return;
  19. }
  20. nav_set_selected('events');
  21. $o = '';
  22. if($a->argc > 1) {
  23. $nick = $a->argv[1];
  24. $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
  25. dbesc($nick)
  26. );
  27. if(! count($user))
  28. return;
  29. $a->data['user'] = $user[0];
  30. $a->profile_uid = $user[0]['uid'];
  31. // if it's a json request abort here becaus we don't
  32. // need the widget data
  33. if ($a->argv[2] === 'json')
  34. return;
  35. $profile = get_profiledata_by_nick($nick, $a->profile_uid);
  36. $account_type = account_type($profile);
  37. $tpl = get_markup_template("vcard-widget.tpl");
  38. $vcard_widget .= replace_macros($tpl, array(
  39. '$name' => $profile['name'],
  40. '$photo' => $profile['photo'],
  41. '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
  42. '$account_type' => $account_type,
  43. '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
  44. ));
  45. $cal_widget = widget_events();
  46. if(! x($a->page,'aside'))
  47. $a->page['aside'] = '';
  48. $a->page['aside'] .= $vcard_widget;
  49. $a->page['aside'] .= $cal_widget;
  50. }
  51. return;
  52. }
  53. function cal_content(App $a) {
  54. nav_set_selected('events');
  55. // First day of the week (0 = Sunday)
  56. $firstDay = PConfig::get(local_user(),'system','first_day_of_week', 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' => System::baseUrl(),
  62. '$module_url' => '/cal/' . $a->data['user']['nickname'],
  63. '$modparams' => 2,
  64. '$i18n' => $i18n,
  65. ));
  66. $etpl = get_markup_template('event_end.tpl');
  67. $a->page['end'] .= replace_macros($etpl,array(
  68. '$baseurl' => System::baseUrl(),
  69. ));
  70. $o ="";
  71. $mode = 'view';
  72. $y = 0;
  73. $m = 0;
  74. $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
  75. if($a->argc == 4) {
  76. if($a->argv[2] == 'export') {
  77. $mode = 'export';
  78. $format = $a->argv[3];
  79. }
  80. }
  81. //
  82. // Setup permissions structures
  83. //
  84. $contact = null;
  85. $remote_contact = false;
  86. $contact_id = 0;
  87. $owner_uid = $a->data['user']['uid'];
  88. $nick = $a->data['user']['nickname'];
  89. if(is_array($_SESSION['remote'])) {
  90. foreach($_SESSION['remote'] as $v) {
  91. if($v['uid'] == $a->profile['profile_uid']) {
  92. $contact_id = $v['cid'];
  93. break;
  94. }
  95. }
  96. }
  97. if($contact_id) {
  98. $groups = init_groups_visitor($contact_id);
  99. $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  100. intval($contact_id),
  101. intval($a->profile['profile_uid'])
  102. );
  103. if (dbm::is_result($r)) {
  104. $contact = $r[0];
  105. $remote_contact = true;
  106. }
  107. }
  108. if(! $remote_contact) {
  109. if(local_user()) {
  110. $contact_id = $_SESSION['cid'];
  111. $contact = $a->contact;
  112. }
  113. }
  114. $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
  115. if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
  116. notice( t('Access to this profile has been restricted.') . EOL);
  117. return;
  118. }
  119. // get the permissions
  120. $sql_perms = item_permissions_sql($owner_uid,$remote_contact,$groups);
  121. // we only want to have the events of the profile owner
  122. $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
  123. // get the tab navigation bar
  124. $tabs .= profile_tabs($a,false, $a->data['user']['nickname']);
  125. // The view mode part is similiar to /mod/events.php
  126. if($mode == 'view') {
  127. $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
  128. $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
  129. if(! $y)
  130. $y = intval($thisyear);
  131. if(! $m)
  132. $m = intval($thismonth);
  133. // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
  134. // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
  135. if($y < 1901)
  136. $y = 1900;
  137. if($y > 2099)
  138. $y = 2100;
  139. $nextyear = $y;
  140. $nextmonth = $m + 1;
  141. if($nextmonth > 12) {
  142. $nextmonth = 1;
  143. $nextyear ++;
  144. }
  145. $prevyear = $y;
  146. if($m > 1)
  147. $prevmonth = $m - 1;
  148. else {
  149. $prevmonth = 12;
  150. $prevyear --;
  151. }
  152. $dim = get_dim($y,$m);
  153. $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
  154. $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
  155. if ($a->argv[2] === 'json'){
  156. if (x($_GET,'start')) $start = $_GET['start'];
  157. if (x($_GET,'end')) $finish = $_GET['end'];
  158. }
  159. $start = datetime_convert('UTC','UTC',$start);
  160. $finish = datetime_convert('UTC','UTC',$finish);
  161. $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
  162. $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
  163. // put the event parametes in an array so we can better transmit them
  164. $event_params = array(
  165. 'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
  166. 'start' => $start,
  167. 'finish' => $finish,
  168. 'adjust_start' => $adjust_start,
  169. 'adjust_finish' => $adjust_finish,
  170. 'ignored' => $ignored,
  171. );
  172. // get events by id or by date
  173. if (x($_GET,'id')){
  174. $r = event_by_id($owner_uid, $event_params, $sql_extra);
  175. } else {
  176. $r = events_by_date($owner_uid, $event_params, $sql_extra);
  177. }
  178. $links = array();
  179. if (dbm::is_result($r)) {
  180. $r = sort_by_date($r);
  181. foreach ($r as $rr) {
  182. $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
  183. if (! x($links,$j)) {
  184. $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
  185. }
  186. }
  187. }
  188. $events=array();
  189. // transform the event in a usable array
  190. if (dbm::is_result($r))
  191. $r = sort_by_date($r);
  192. $events = process_events($r);
  193. if ($a->argv[2] === 'json'){
  194. echo json_encode($events); killme();
  195. }
  196. // links: array('href', 'text', 'extra css classes', 'title')
  197. if (x($_GET,'id')){
  198. $tpl = get_markup_template("event.tpl");
  199. } else {
  200. // if (Config::get('experimentals','new_calendar')==1){
  201. $tpl = get_markup_template("events_js.tpl");
  202. // } else {
  203. // $tpl = get_markup_template("events.tpl");
  204. // }
  205. }
  206. // Get rid of dashes in key names, Smarty3 can't handle them
  207. foreach($events as $key => $event) {
  208. $event_item = array();
  209. foreach($event['item'] as $k => $v) {
  210. $k = str_replace('-','_',$k);
  211. $event_item[$k] = $v;
  212. }
  213. $events[$key]['item'] = $event_item;
  214. }
  215. $o = replace_macros($tpl, array(
  216. '$baseurl' => System::baseUrl(),
  217. '$tabs' => $tabs,
  218. '$title' => t('Events'),
  219. '$view' => t('View'),
  220. '$previous' => array(System::baseUrl()."/events/$prevyear/$prevmonth", t('Previous'),'',''),
  221. '$next' => array(System::baseUrl()."/events/$nextyear/$nextmonth", t('Next'),'',''),
  222. '$calendar' => cal($y,$m,$links, ' eventcal'),
  223. '$events' => $events,
  224. "today" => t("today"),
  225. "month" => t("month"),
  226. "week" => t("week"),
  227. "day" => t("day"),
  228. "list" => t("list"),
  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. }