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