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