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