Burn notices, burn
[friendica.git/.git] / mod / cal.php
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
9 use Friendica\App;
10 use Friendica\Content\Feature;
11 use Friendica\Content\Nav;
12 use Friendica\Content\Widget;
13 use Friendica\Core\Config;
14 use Friendica\Core\L10n;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Event;
19 use Friendica\Model\Group;
20 use Friendica\Model\Profile;
21 use Friendica\Protocol\DFRN;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Temporal;
24
25 function cal_init(App $a)
26 {
27         if ($a->argc > 1) {
28                 DFRN::autoRedir($a, $a->argv[1]);
29         }
30
31         if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
32                 System::httpExit(403, ['title' => L10n::t('Access denied.')]);
33         }
34
35         if ($a->argc < 2) {
36                 System::httpExit(403, ['title' => L10n::t('Access denied.')]);
37         }
38
39         Nav::setSelected('events');
40
41         $nick = $a->argv[1];
42         $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
43         if (!DBA::isResult($user)) {
44                 System::httpExit(404, ['title' => L10n::t('Page not found.')]);
45         }
46
47         $a->data['user'] = $user;
48         $a->profile_uid = $user['uid'];
49
50         // if it's a json request abort here becaus we don't
51         // need the widget data
52         if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
53                 return;
54         }
55
56         $profile = Profile::getByNickname($nick, $a->profile_uid);
57
58         $account_type = Contact::getAccountType($profile);
59
60         $tpl = get_markup_template("vcard-widget.tpl");
61
62         $vcard_widget = replace_macros($tpl, [
63                 '$name' => $profile['name'],
64                 '$photo' => $profile['photo'],
65                 '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
66                 '$account_type' => $account_type,
67                 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
68         ]);
69
70         $cal_widget = Widget\CalendarExport::getHTML();
71
72         if (!x($a->page, 'aside')) {
73                 $a->page['aside'] = '';
74         }
75
76         $a->page['aside'] .= $vcard_widget;
77         $a->page['aside'] .= $cal_widget;
78
79         return;
80 }
81
82 function cal_content(App $a)
83 {
84         Nav::setSelected('events');
85
86         // get the translation strings for the callendar
87         $i18n = Event::getStrings();
88
89         $htpl = get_markup_template('event_head.tpl');
90         $a->page['htmlhead'] .= replace_macros($htpl, [
91                 '$baseurl' => System::baseUrl(),
92                 '$module_url' => '/cal/' . $a->data['user']['nickname'],
93                 '$modparams' => 2,
94                 '$i18n' => $i18n,
95         ]);
96
97         $etpl = get_markup_template('event_end.tpl');
98         $a->page['end'] .= replace_macros($etpl, [
99                 '$baseurl' => System::baseUrl(),
100         ]);
101
102         $mode = 'view';
103         $y = 0;
104         $m = 0;
105         $ignored = (x($_REQUEST, 'ignored') ? intval($_REQUEST['ignored']) : 0);
106
107         $format = 'ical';
108         if ($a->argc == 4 && $a->argv[2] == 'export') {
109                 $mode = 'export';
110                 $format = $a->argv[3];
111         }
112
113         // Setup permissions structures
114         $remote_contact = false;
115         $contact_id = 0;
116
117         $owner_uid = $a->data['user']['uid'];
118         $nick = $a->data['user']['nickname'];
119
120         if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
121                 foreach ($_SESSION['remote'] as $v) {
122                         if ($v['uid'] == $a->profile['profile_uid']) {
123                                 $contact_id = $v['cid'];
124                                 break;
125                         }
126                 }
127         }
128
129         $groups = [];
130         if ($contact_id) {
131                 $groups = Group::getIdsByContactId($contact_id);
132                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
133                         intval($contact_id),
134                         intval($a->profile['profile_uid'])
135                 );
136                 if (DBA::isResult($r)) {
137                         $remote_contact = true;
138                 }
139         }
140
141         $is_owner = local_user() == $a->profile['profile_uid'];
142
143         if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
144                 notice(L10n::t('Access to this profile has been restricted.') . EOL);
145                 return;
146         }
147
148         // get the permissions
149         $sql_perms = item_permissions_sql($owner_uid, $remote_contact, $groups);
150         // we only want to have the events of the profile owner
151         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
152
153         // get the tab navigation bar
154         $tabs = Profile::getTabs($a, false, $a->data['user']['nickname']);
155
156         // The view mode part is similiar to /mod/events.php
157         if ($mode == 'view') {
158                 $thisyear = DateTimeFormat::localNow('Y');
159                 $thismonth = DateTimeFormat::localNow('m');
160                 if (!$y) {
161                         $y = intval($thisyear);
162                 }
163
164                 if (!$m) {
165                         $m = intval($thismonth);
166                 }
167
168                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
169                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
170
171                 if ($y < 1901) {
172                         $y = 1900;
173                 }
174
175                 if ($y > 2099) {
176                         $y = 2100;
177                 }
178
179                 $nextyear = $y;
180                 $nextmonth = $m + 1;
181                 if ($nextmonth > 12) {
182                         $nextmonth = 1;
183                         $nextyear ++;
184                 }
185
186                 $prevyear = $y;
187                 if ($m > 1) {
188                         $prevmonth = $m - 1;
189                 } else {
190                         $prevmonth = 12;
191                         $prevyear --;
192                 }
193
194                 $dim = Temporal::getDaysInMonth($y, $m);
195                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
196                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
197
198
199                 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
200                         if (x($_GET, 'start')) {
201                                 $start = $_GET['start'];
202                         }
203
204                         if (x($_GET, 'end')) {
205                                 $finish = $_GET['end'];
206                         }
207                 }
208
209                 $start = DateTimeFormat::utc($start);
210                 $finish = DateTimeFormat::utc($finish);
211
212                 $adjust_start = DateTimeFormat::local($start);
213                 $adjust_finish = DateTimeFormat::local($finish);
214
215                 // put the event parametes in an array so we can better transmit them
216                 $event_params = [
217                         'event_id'      => intval(defaults($_GET, 'id', 0)),
218                         'start'         => $start,
219                         'finish'        => $finish,
220                         'adjust_start'  => $adjust_start,
221                         'adjust_finish' => $adjust_finish,
222                         'ignore'        => $ignored,
223                 ];
224
225                 // get events by id or by date
226                 if ($event_params['event_id']) {
227                         $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
228                 } else {
229                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
230                 }
231
232                 $links = [];
233
234                 if (DBA::isResult($r)) {
235                         $r = Event::sortByDate($r);
236                         foreach ($r as $rr) {
237                                 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
238                                 if (!x($links, $j)) {
239                                         $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
240                                 }
241                         }
242                 }
243
244                 // transform the event in a usable array
245                 $events = Event::prepareListForTemplate($r);
246
247                 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
248                         echo json_encode($events);
249                         killme();
250                 }
251
252                 // links: array('href', 'text', 'extra css classes', 'title')
253                 if (x($_GET, 'id')) {
254                         $tpl = get_markup_template("event.tpl");
255                 } else {
256 //                      if (Config::get('experimentals','new_calendar')==1){
257                         $tpl = get_markup_template("events_js.tpl");
258 //                      } else {
259 //                              $tpl = get_markup_template("events.tpl");
260 //                      }
261                 }
262
263                 // Get rid of dashes in key names, Smarty3 can't handle them
264                 foreach ($events as $key => $event) {
265                         $event_item = [];
266                         foreach ($event['item'] as $k => $v) {
267                                 $k = str_replace('-', '_', $k);
268                                 $event_item[$k] = $v;
269                         }
270                         $events[$key]['item'] = $event_item;
271                 }
272
273                 $o = replace_macros($tpl, [
274                         '$baseurl' => System::baseUrl(),
275                         '$tabs' => $tabs,
276                         '$title' => L10n::t('Events'),
277                         '$view' => L10n::t('View'),
278                         '$previous' => [System::baseUrl() . "/events/$prevyear/$prevmonth", L10n::t('Previous'), '', ''],
279                         '$next' => [System::baseUrl() . "/events/$nextyear/$nextmonth", L10n::t('Next'), '', ''],
280                         '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
281                         '$events' => $events,
282                         "today" => L10n::t("today"),
283                         "month" => L10n::t("month"),
284                         "week" => L10n::t("week"),
285                         "day" => L10n::t("day"),
286                         "list" => L10n::t("list"),
287                 ]);
288
289                 if (x($_GET, 'id')) {
290                         echo $o;
291                         killme();
292                 }
293
294                 return $o;
295         }
296
297         if ($mode == 'export') {
298                 if (!intval($owner_uid)) {
299                         notice(L10n::t('User not found'));
300                         return;
301                 }
302
303                 // Test permissions
304                 // Respect the export feature setting for all other /cal pages if it's not the own profile
305                 if ((local_user() !== intval($owner_uid)) && !Feature::isEnabled($owner_uid, "export_calendar")) {
306                         notice(L10n::t('Permission denied.') . EOL);
307                         goaway('cal/' . $nick);
308                 }
309
310                 // Get the export data by uid
311                 $evexport = Event::exportListByUserId($owner_uid, $format);
312
313                 if (!$evexport["success"]) {
314                         if ($evexport["content"]) {
315                                 notice(L10n::t('This calendar format is not supported'));
316                         } else {
317                                 notice(L10n::t('No exportable data found'));
318                         }
319
320                         // If it the own calendar return to the events page
321                         // otherwise to the profile calendar page
322                         if (local_user() === intval($owner_uid)) {
323                                 $return_path = "events";
324                         } else {
325                                 $return_path = "cal/" . $nick;
326                         }
327
328                         goaway($return_path);
329                 }
330
331                 // If nothing went wrong we can echo the export content
332                 if ($evexport["success"]) {
333                         header('Content-type: text/calendar');
334                         header('content-disposition: attachment; filename="' . L10n::t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
335                         echo $evexport["content"];
336                         killme();
337                 }
338
339                 return;
340         }
341 }