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