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