]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Merge remote-tracking branch 'upstream/2021.12-rc' into api-fixes
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * The calendar module
21  *
22  * This calendar is for profile visitors and contains only the events
23  * of the profile owner
24  */
25
26 use Friendica\App;
27 use Friendica\Content\Nav;
28 use Friendica\Content\Widget;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session;
31 use Friendica\Database\DBA;
32 use Friendica\DI;
33 use Friendica\Model\Event;
34 use Friendica\Model\Item;
35 use Friendica\Model\User;
36 use Friendica\Module\BaseProfile;
37 use Friendica\Network\HTTPException;
38 use Friendica\Util\DateTimeFormat;
39 use Friendica\Util\Temporal;
40
41 function cal_init(App $a)
42 {
43         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
44                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
45         }
46
47         if (DI::args()->getArgc() < 2) {
48                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
49         }
50
51         Nav::setSelected('events');
52
53         // if it's a json request abort here becaus we don't
54         // need the widget data
55         if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
56                 return;
57         }
58
59         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
60         if (empty($owner)) {
61                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
62         }
63
64         if (empty(DI::page()['aside'])) {
65                 DI::page()['aside'] = '';
66         }
67
68         DI::page()['aside'] .= Widget\VCard::getHTML($owner);
69         DI::page()['aside'] .= Widget\CalendarExport::getHTML($owner['uid']);
70
71         return;
72 }
73
74 function cal_content(App $a)
75 {
76         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
77         if (empty($owner)) {
78                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
79         }
80
81         Nav::setSelected('events');
82
83         // get the translation strings for the callendar
84         $i18n = Event::getStrings();
85
86         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
87         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
88         DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
89         DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
90
91         $htpl = Renderer::getMarkupTemplate('event_head.tpl');
92         DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
93                 '$module_url' => '/cal/' . $owner['nickname'],
94                 '$modparams' => 2,
95                 '$i18n' => $i18n,
96         ]);
97
98         $mode = 'view';
99         $y = 0;
100         $m = 0;
101         $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
102
103         $format = 'ical';
104         if (DI::args()->getArgc() == 4 && DI::args()->getArgv()[2] == 'export') {
105                 $mode = 'export';
106                 $format = DI::args()->getArgv()[3];
107         }
108
109         // Setup permissions structures
110         $owner_uid = intval($owner['uid']);
111         $nick = $owner['nickname'];
112
113         $contact_id = Session::getRemoteContactID($owner['uid']);
114
115         $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $owner['uid']]);
116
117         $is_owner = local_user() == $owner['uid'];
118
119         if ($owner['hidewall'] && !$is_owner && !$remote_contact) {
120                 notice(DI::l10n()->t('Access to this profile has been restricted.'));
121                 return;
122         }
123
124         // get the permissions
125         $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
126         // we only want to have the events of the profile owner
127         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
128
129         // get the tab navigation bar
130         $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $owner['nickname'], $owner['hide-friends']);
131
132         // The view mode part is similiar to /mod/events.php
133         if ($mode == 'view') {
134                 $thisyear = DateTimeFormat::localNow('Y');
135                 $thismonth = DateTimeFormat::localNow('m');
136                 if (!$y) {
137                         $y = intval($thisyear);
138                 }
139
140                 if (!$m) {
141                         $m = intval($thismonth);
142                 }
143
144                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
145                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
146
147                 if ($y < 1901) {
148                         $y = 1900;
149                 }
150
151                 if ($y > 2099) {
152                         $y = 2100;
153                 }
154
155                 $nextyear = $y;
156                 $nextmonth = $m + 1;
157                 if ($nextmonth > 12) {
158                         $nextmonth = 1;
159                         $nextyear ++;
160                 }
161
162                 $prevyear = $y;
163                 if ($m > 1) {
164                         $prevmonth = $m - 1;
165                 } else {
166                         $prevmonth = 12;
167                         $prevyear --;
168                 }
169
170                 $dim = Temporal::getDaysInMonth($y, $m);
171                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
172                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
173
174
175                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
176                         if (!empty($_GET['start'])) {
177                                 $start = $_GET['start'];
178                         }
179
180                         if (!empty($_GET['end'])) {
181                                 $finish = $_GET['end'];
182                         }
183                 }
184
185                 $start = DateTimeFormat::utc($start);
186                 $finish = DateTimeFormat::utc($finish);
187
188                 // put the event parametes in an array so we can better transmit them
189                 $event_params = [
190                         'event_id'      => intval($_GET['id'] ?? 0),
191                         'start'         => $start,
192                         'finish'        => $finish,
193                         'ignore'        => $ignored,
194                 ];
195
196                 // get events by id or by date
197                 if ($event_params['event_id']) {
198                         $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
199                 } else {
200                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
201                 }
202
203                 $links = [];
204
205                 if (DBA::isResult($r)) {
206                         $r = Event::sortByDate($r);
207                         foreach ($r as $rr) {
208                                 $j = DateTimeFormat::local($rr['start'], 'j');
209                                 if (empty($links[$j])) {
210                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
211                                 }
212                         }
213                 }
214
215                 // transform the event in a usable array
216                 $events = Event::prepareListForTemplate($r);
217
218                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
219                         echo json_encode($events);
220                         exit();
221                 }
222
223                 // links: array('href', 'text', 'extra css classes', 'title')
224                 if (!empty($_GET['id'])) {
225                         $tpl = Renderer::getMarkupTemplate("event.tpl");
226                 } else {
227                         $tpl = Renderer::getMarkupTemplate("events_js.tpl");
228                 }
229
230                 // Get rid of dashes in key names, Smarty3 can't handle them
231                 foreach ($events as $key => $event) {
232                         $event_item = [];
233                         foreach ($event['item'] as $k => $v) {
234                                 $k = str_replace('-', '_', $k);
235                                 $event_item[$k] = $v;
236                         }
237                         $events[$key]['item'] = $event_item;
238                 }
239
240                 $o = Renderer::replaceMacros($tpl, [
241                         '$tabs' => $tabs,
242                         '$title' => DI::l10n()->t('Events'),
243                         '$view' => DI::l10n()->t('View'),
244                         '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
245                         '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
246                         '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
247                         '$events' => $events,
248                         "today" => DI::l10n()->t("today"),
249                         "month" => DI::l10n()->t("month"),
250                         "week" => DI::l10n()->t("week"),
251                         "day" => DI::l10n()->t("day"),
252                         "list" => DI::l10n()->t("list"),
253                 ]);
254
255                 if (!empty($_GET['id'])) {
256                         echo $o;
257                         exit();
258                 }
259
260                 return $o;
261         }
262
263         if ($mode == 'export') {
264                 if (!$owner_uid) {
265                         notice(DI::l10n()->t('User not found'));
266                         return;
267                 }
268
269                 // Get the export data by uid
270                 $evexport = Event::exportListByUserId($owner_uid, $format);
271
272                 if (!$evexport["success"]) {
273                         if ($evexport["content"]) {
274                                 notice(DI::l10n()->t('This calendar format is not supported'));
275                         } else {
276                                 notice(DI::l10n()->t('No exportable data found'));
277                         }
278
279                         // If it the own calendar return to the events page
280                         // otherwise to the profile calendar page
281                         if (local_user() === $owner_uid) {
282                                 $return_path = "events";
283                         } else {
284                                 $return_path = "cal/" . $nick;
285                         }
286
287                         DI::baseUrl()->redirect($return_path);
288                 }
289
290                 // If nothing went wrong we can echo the export content
291                 if ($evexport["success"]) {
292                         header('Content-type: text/calendar');
293                         header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
294                         echo $evexport["content"];
295                         exit();
296                 }
297
298                 return;
299         }
300 }