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