]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
6279bc24116b9ca2c0f84f61264c435417ed4fe5
[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\Widget;
14 use Friendica\Core\Config;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Session;
18 use Friendica\Database\DBA;
19 use Friendica\DI;
20 use Friendica\Model\Contact;
21 use Friendica\Model\Event;
22 use Friendica\Model\Item;
23 use Friendica\Model\Profile;
24 use Friendica\Util\DateTimeFormat;
25 use Friendica\Util\Temporal;
26
27 function cal_init(App $a)
28 {
29         if (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'] != "") ? $profile['addr'] : ""),
64                 '$account_type' => $account_type,
65                 '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
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         $remote_contact = false;
107         $contact_id = 0;
108
109         $owner_uid = intval($a->data['user']['uid']);
110         $nick = $a->data['user']['nickname'];
111
112         if (!empty(Session::getRemoteContactID($a->profile['profile_uid']))) {
113                 $contact_id = Session::getRemoteContactID($a->profile['profile_uid']);
114         }
115
116         if ($contact_id) {
117                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
118                         intval($contact_id),
119                         intval($a->profile['profile_uid'])
120                 );
121                 if (DBA::isResult($r)) {
122                         $remote_contact = true;
123                 }
124         }
125
126         $is_owner = local_user() == $a->profile['profile_uid'];
127
128         if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
129                 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
130                 return;
131         }
132
133         // get the permissions
134         $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
135         // we only want to have the events of the profile owner
136         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
137
138         // get the tab navigation bar
139         $tabs = Profile::getTabs($a, 'cal', false, $a->data['user']['nickname']);
140
141         // The view mode part is similiar to /mod/events.php
142         if ($mode == 'view') {
143                 $thisyear = DateTimeFormat::localNow('Y');
144                 $thismonth = DateTimeFormat::localNow('m');
145                 if (!$y) {
146                         $y = intval($thisyear);
147                 }
148
149                 if (!$m) {
150                         $m = intval($thismonth);
151                 }
152
153                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
154                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
155
156                 if ($y < 1901) {
157                         $y = 1900;
158                 }
159
160                 if ($y > 2099) {
161                         $y = 2100;
162                 }
163
164                 $nextyear = $y;
165                 $nextmonth = $m + 1;
166                 if ($nextmonth > 12) {
167                         $nextmonth = 1;
168                         $nextyear ++;
169                 }
170
171                 $prevyear = $y;
172                 if ($m > 1) {
173                         $prevmonth = $m - 1;
174                 } else {
175                         $prevmonth = 12;
176                         $prevyear --;
177                 }
178
179                 $dim = Temporal::getDaysInMonth($y, $m);
180                 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
181                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
182
183
184                 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
185                         if (!empty($_GET['start'])) {
186                                 $start = $_GET['start'];
187                         }
188
189                         if (!empty($_GET['end'])) {
190                                 $finish = $_GET['end'];
191                         }
192                 }
193
194                 $start = DateTimeFormat::utc($start);
195                 $finish = DateTimeFormat::utc($finish);
196
197                 $adjust_start = DateTimeFormat::local($start);
198                 $adjust_finish = DateTimeFormat::local($finish);
199
200                 // put the event parametes in an array so we can better transmit them
201                 $event_params = [
202                         'event_id'      => intval($_GET['id'] ?? 0),
203                         'start'         => $start,
204                         'finish'        => $finish,
205                         'adjust_start'  => $adjust_start,
206                         'adjust_finish' => $adjust_finish,
207                         'ignore'        => $ignored,
208                 ];
209
210                 // get events by id or by date
211                 if ($event_params['event_id']) {
212                         $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
213                 } else {
214                         $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
215                 }
216
217                 $links = [];
218
219                 if (DBA::isResult($r)) {
220                         $r = Event::sortByDate($r);
221                         foreach ($r as $rr) {
222                                 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
223                                 if (empty($links[$j])) {
224                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
225                                 }
226                         }
227                 }
228
229                 // transform the event in a usable array
230                 $events = Event::prepareListForTemplate($r);
231
232                 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
233                         echo json_encode($events);
234                         exit();
235                 }
236
237                 // links: array('href', 'text', 'extra css classes', 'title')
238                 if (!empty($_GET['id'])) {
239                         $tpl = Renderer::getMarkupTemplate("event.tpl");
240                 } else {
241 //                      if (Config::get('experimentals','new_calendar')==1){
242                         $tpl = Renderer::getMarkupTemplate("events_js.tpl");
243 //                      } else {
244 //                              $tpl = Renderer::getMarkupTemplate("events.tpl");
245 //                      }
246                 }
247
248                 // Get rid of dashes in key names, Smarty3 can't handle them
249                 foreach ($events as $key => $event) {
250                         $event_item = [];
251                         foreach ($event['item'] as $k => $v) {
252                                 $k = str_replace('-', '_', $k);
253                                 $event_item[$k] = $v;
254                         }
255                         $events[$key]['item'] = $event_item;
256                 }
257
258                 $o = Renderer::replaceMacros($tpl, [
259                         '$tabs' => $tabs,
260                         '$title' => DI::l10n()->t('Events'),
261                         '$view' => DI::l10n()->t('View'),
262                         '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
263                         '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
264                         '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
265                         '$events' => $events,
266                         "today" => DI::l10n()->t("today"),
267                         "month" => DI::l10n()->t("month"),
268                         "week" => DI::l10n()->t("week"),
269                         "day" => DI::l10n()->t("day"),
270                         "list" => DI::l10n()->t("list"),
271                 ]);
272
273                 if (!empty($_GET['id'])) {
274                         echo $o;
275                         exit();
276                 }
277
278                 return $o;
279         }
280
281         if ($mode == 'export') {
282                 if (!$owner_uid) {
283                         notice(DI::l10n()->t('User not found'));
284                         return;
285                 }
286
287                 // Test permissions
288                 // Respect the export feature setting for all other /cal pages if it's not the own profile
289                 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
290                         notice(DI::l10n()->t('Permission denied.') . EOL);
291                         DI::baseUrl()->redirect('cal/' . $nick);
292                 }
293
294                 // Get the export data by uid
295                 $evexport = Event::exportListByUserId($owner_uid, $format);
296
297                 if (!$evexport["success"]) {
298                         if ($evexport["content"]) {
299                                 notice(DI::l10n()->t('This calendar format is not supported'));
300                         } else {
301                                 notice(DI::l10n()->t('No exportable data found'));
302                         }
303
304                         // If it the own calendar return to the events page
305                         // otherwise to the profile calendar page
306                         if (local_user() === $owner_uid) {
307                                 $return_path = "events";
308                         } else {
309                                 $return_path = "cal/" . $nick;
310                         }
311
312                         DI::baseUrl()->redirect($return_path);
313                 }
314
315                 // If nothing went wrong we can echo the export content
316                 if ($evexport["success"]) {
317                         header('Content-type: text/calendar');
318                         header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
319                         echo $evexport["content"];
320                         exit();
321                 }
322
323                 return;
324         }
325 }