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