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