]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Hotfix: One `$` to much ...
[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\System;
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\Module\Response;
38 use Friendica\Network\HTTPException;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Temporal;
41
42 function cal_init(App $a)
43 {
44         if (DI::config()->get('system', 'block_public') && !DI::userSession()->isAuthenticated()) {
45                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
46         }
47
48         if (DI::args()->getArgc() < 2) {
49                 throw new HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
50         }
51
52         Nav::setSelected('events');
53
54         // if it's a json request abort here becaus we don't
55         // need the widget data
56         if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
57                 return;
58         }
59
60         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
61         if (empty($owner)) {
62                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
63         }
64
65         if (empty(DI::page()['aside'])) {
66                 DI::page()['aside'] = '';
67         }
68
69         DI::page()['aside'] .= Widget\VCard::getHTML($owner);
70         DI::page()['aside'] .= Widget\CalendarExport::getHTML($owner['uid']);
71
72         return;
73 }
74
75 function cal_content(App $a)
76 {
77         $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
78         if (empty($owner)) {
79                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
80         }
81
82         Nav::setSelected('events');
83
84         // get the translation strings for the callendar
85         $i18n = Event::getStrings();
86
87         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.min.css');
88         DI::page()->registerStylesheet('view/asset/fullcalendar/dist/fullcalendar.print.min.css', 'print');
89         DI::page()->registerFooterScript('view/asset/moment/min/moment-with-locales.min.js');
90         DI::page()->registerFooterScript('view/asset/fullcalendar/dist/fullcalendar.min.js');
91
92         $htpl = Renderer::getMarkupTemplate('event_head.tpl');
93         DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
94                 '$module_url' => '/cal/' . $owner['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 (DI::args()->getArgc() == 4 && DI::args()->getArgv()[2] == 'export') {
106                 $mode = 'export';
107                 $format = DI::args()->getArgv()[3];
108         }
109
110         // Setup permissions structures
111         $owner_uid = intval($owner['uid']);
112         $nick = $owner['nickname'];
113
114         $contact_id = DI::userSession()->getRemoteContactID($owner['uid']);
115
116         $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $owner['uid']]);
117
118         $is_owner = DI::userSession()->getLocalUserId() == $owner['uid'];
119
120         if ($owner['hidewall'] && !$is_owner && !$remote_contact) {
121                 DI::sysmsg()->addNotice(DI::l10n()->t('Access to this profile has been restricted.'));
122                 return;
123         }
124
125         // get the permissions
126         $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
127         // we only want to have the events of the profile owner
128         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
129
130         // get the tab navigation bar
131         $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $owner['nickname'], $owner['hide-friends']);
132
133         // The view mode part is similiar to /mod/events.php
134         if ($mode == 'view') {
135                 $thisyear = DateTimeFormat::localNow('Y');
136                 $thismonth = DateTimeFormat::localNow('m');
137                 if (!$y) {
138                         $y = intval($thisyear);
139                 }
140
141                 if (!$m) {
142                         $m = intval($thismonth);
143                 }
144
145                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
146                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
147
148                 if ($y < 1901) {
149                         $y = 1900;
150                 }
151
152                 if ($y > 2099) {
153                         $y = 2100;
154                 }
155
156                 $nextyear = $y;
157                 $nextmonth = $m + 1;
158                 if ($nextmonth > 12) {
159                         $nextmonth = 1;
160                         $nextyear ++;
161                 }
162
163                 $prevyear = $y;
164                 if ($m > 1) {
165                         $prevmonth = $m - 1;
166                 } else {
167                         $prevmonth = 12;
168                         $prevyear --;
169                 }
170
171                 $dim = Temporal::getDaysInMonth($y, $m);
172                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
173                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
174
175
176                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
177                         if (!empty($_GET['start'])) {
178                                 $start = $_GET['start'];
179                         }
180
181                         if (!empty($_GET['end'])) {
182                                 $finish = $_GET['end'];
183                         }
184                 }
185
186                 $start = DateTimeFormat::utc($start);
187                 $finish = DateTimeFormat::utc($finish);
188
189                 // put the event parametes in an array so we can better transmit them
190                 $event_params = [
191                         'event_id'      => intval($_GET['id'] ?? 0),
192                         'start'         => $start,
193                         'finish'        => $finish,
194                         'ignore'        => $ignored,
195                 ];
196
197                 // get events by id or by date
198                 if ($event_params['event_id']) {
199                         $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
200                 } else {
201                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
202                 }
203
204                 $links = [];
205
206                 if (DBA::isResult($r)) {
207                         $r = Event::sortByDate($r);
208                         foreach ($r as $rr) {
209                                 $j = DateTimeFormat::local($rr['start'], 'j');
210                                 if (empty($links[$j])) {
211                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
212                                 }
213                         }
214                 }
215
216                 // transform the event in a usable array
217                 $events = Event::prepareListForTemplate($r);
218
219                 if (!empty(DI::args()->getArgv()[2]) && (DI::args()->getArgv()[2] === 'json')) {
220                         System::jsonExit($events);
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                         System::httpExit($o);
257                 }
258
259                 return $o;
260         }
261
262         if ($mode == 'export') {
263                 if (!$owner_uid) {
264                         DI::sysmsg()->addNotice(DI::l10n()->t('User not found'));
265                         return;
266                 }
267
268                 // Get the export data by uid
269                 $evexport = Event::exportListByUserId($owner_uid, $format);
270
271                 if (!$evexport["success"]) {
272                         if ($evexport["content"]) {
273                                 DI::sysmsg()->addNotice(DI::l10n()->t('This calendar format is not supported'));
274                         } else {
275                                 DI::sysmsg()->addNotice(DI::l10n()->t('No exportable data found'));
276                         }
277
278                         // If it the own calendar return to the events page
279                         // otherwise to the profile calendar page
280                         if (DI::userSession()->getLocalUserId() === $owner_uid) {
281                                 $return_path = "events";
282                         } else {
283                                 $return_path = "cal/" . $nick;
284                         }
285
286                         DI::baseUrl()->redirect($return_path);
287                 }
288
289                 // If nothing went wrong we can echo the export content
290                 if ($evexport["success"]) {
291                         header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
292                         System::httpExit($evexport["content"], Response::TYPE_BLANK, 'text/calendar');
293                 }
294
295                 return;
296         }
297 }