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