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