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