]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
3f249da0aca21c457146fd4243eebc669ea06430
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\System;
32 use Friendica\Database\DBA;
33 use Friendica\DI;
34 use Friendica\Model\Event;
35 use Friendica\Model\Item;
36 use Friendica\Model\User;
37 use Friendica\Module\BaseProfile;
38 use Friendica\Module\Response;
39 use Friendica\Network\HTTPException;
40 use Friendica\Util\DateTimeFormat;
41 use Friendica\Util\Temporal;
42
43 function cal_init(App $a)
44 {
45         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
46                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
47         }
48
49         if (DI::args()->getArgc() < 2) {
50                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
51         }
52
53         Nav::setSelected('events');
54
55         // if it's a json request abort here becaus we don't
56         // need the widget data
57         if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
58                 return;
59         }
60
61         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
62         if (empty($owner)) {
63                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
64         }
65
66         if (empty(DI::page()['aside'])) {
67                 DI::page()['aside'] = '';
68         }
69
70         DI::page()['aside'] .= Widget\VCard::getHTML($owner);
71         DI::page()['aside'] .= Widget\CalendarExport::getHTML($owner['uid']);
72
73         return;
74 }
75
76 function cal_content(App $a)
77 {
78         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
79         if (empty($owner)) {
80                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
81         }
82
83         Nav::setSelected('events');
84
85         // get the translation strings for the callendar
86         $i18n = Event::getStrings();
87
88         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
89         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
90         DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
91         DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
92
93         $htpl = Renderer::getMarkupTemplate('event_head.tpl');
94         DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
95                 '$module_url' => '/cal/' . $owner['nickname'],
96                 '$modparams' => 2,
97                 '$i18n' => $i18n,
98         ]);
99
100         $mode = 'view';
101         $y = 0;
102         $m = 0;
103         $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
104
105         $format = 'ical';
106         if (DI::args()->getArgc() == 4 && DI::args()->getArgv()[2] == 'export') {
107                 $mode = 'export';
108                 $format = DI::args()->getArgv()[3];
109         }
110
111         // Setup permissions structures
112         $owner_uid = intval($owner['uid']);
113         $nick = $owner['nickname'];
114
115         $contact_id = Session::getRemoteContactID($owner['uid']);
116
117         $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $owner['uid']]);
118
119         $is_owner = local_user() == $owner['uid'];
120
121         if ($owner['hidewall'] && !$is_owner && !$remote_contact) {
122                 notice(DI::l10n()->t('Access to this profile has been restricted.'));
123                 return;
124         }
125
126         // get the permissions
127         $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
128         // we only want to have the events of the profile owner
129         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
130
131         // get the tab navigation bar
132         $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $owner['nickname'], $owner['hide-friends']);
133
134         // The view mode part is similiar to /mod/events.php
135         if ($mode == 'view') {
136                 $thisyear = DateTimeFormat::localNow('Y');
137                 $thismonth = DateTimeFormat::localNow('m');
138                 if (!$y) {
139                         $y = intval($thisyear);
140                 }
141
142                 if (!$m) {
143                         $m = intval($thismonth);
144                 }
145
146                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
147                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
148
149                 if ($y < 1901) {
150                         $y = 1900;
151                 }
152
153                 if ($y > 2099) {
154                         $y = 2100;
155                 }
156
157                 $nextyear = $y;
158                 $nextmonth = $m + 1;
159                 if ($nextmonth > 12) {
160                         $nextmonth = 1;
161                         $nextyear ++;
162                 }
163
164                 $prevyear = $y;
165                 if ($m > 1) {
166                         $prevmonth = $m - 1;
167                 } else {
168                         $prevmonth = 12;
169                         $prevyear --;
170                 }
171
172                 $dim = Temporal::getDaysInMonth($y, $m);
173                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
174                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
175
176
177                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
178                         if (!empty($_GET['start'])) {
179                                 $start = $_GET['start'];
180                         }
181
182                         if (!empty($_GET['end'])) {
183                                 $finish = $_GET['end'];
184                         }
185                 }
186
187                 $start = DateTimeFormat::utc($start);
188                 $finish = DateTimeFormat::utc($finish);
189
190                 // put the event parametes in an array so we can better transmit them
191                 $event_params = [
192                         'event_id'      => intval($_GET['id'] ?? 0),
193                         'start'         => $start,
194                         'finish'        => $finish,
195                         'ignore'        => $ignored,
196                 ];
197
198                 // get events by id or by date
199                 if ($event_params['event_id']) {
200                         $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
201                 } else {
202                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
203                 }
204
205                 $links = [];
206
207                 if (DBA::isResult($r)) {
208                         $r = Event::sortByDate($r);
209                         foreach ($r as $rr) {
210                                 $j = DateTimeFormat::local($rr['start'], 'j');
211                                 if (empty($links[$j])) {
212                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
213                                 }
214                         }
215                 }
216
217                 // transform the event in a usable array
218                 $events = Event::prepareListForTemplate($r);
219
220                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
221                         System::jsonExit($events);
222                 }
223
224                 // links: array('href', 'text', 'extra css classes', 'title')
225                 if (!empty($_GET['id'])) {
226                         $tpl = Renderer::getMarkupTemplate("event.tpl");
227                 } else {
228                         $tpl = Renderer::getMarkupTemplate("events_js.tpl");
229                 }
230
231                 // Get rid of dashes in key names, Smarty3 can't handle them
232                 foreach ($events as $key => $event) {
233                         $event_item = [];
234                         foreach ($event['item'] as $k => $v) {
235                                 $k = str_replace('-', '_', $k);
236                                 $event_item[$k] = $v;
237                         }
238                         $events[$key]['item'] = $event_item;
239                 }
240
241                 $o = Renderer::replaceMacros($tpl, [
242                         '$tabs' => $tabs,
243                         '$title' => DI::l10n()->t('Events'),
244                         '$view' => DI::l10n()->t('View'),
245                         '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
246                         '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
247                         '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
248                         '$events' => $events,
249                         "today" => DI::l10n()->t("today"),
250                         "month" => DI::l10n()->t("month"),
251                         "week" => DI::l10n()->t("week"),
252                         "day" => DI::l10n()->t("day"),
253                         "list" => DI::l10n()->t("list"),
254                 ]);
255
256                 if (!empty($_GET['id'])) {
257                         System::httpExit($o);
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-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
293                         System::httpExit($evexport["content"], Response::TYPE_BLANK, 'text/calendar');
294                 }
295
296                 return;
297         }
298 }