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