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