3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 * This calendar is for profile visitors and contains only the events
23 * of the profile owner
27 use Friendica\Content\Feature;
28 use Friendica\Content\Nav;
29 use Friendica\Content\Widget;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session;
32 use Friendica\Database\DBA;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Event;
36 use Friendica\Model\Item;
37 use Friendica\Model\Profile;
38 use Friendica\Module\BaseProfile;
39 use Friendica\Util\DateTimeFormat;
40 use Friendica\Util\Temporal;
42 function cal_init(App $a)
44 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
45 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
49 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
52 Nav::setSelected('events');
55 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
56 if (!DBA::isResult($user)) {
57 throw new \Friendica\Network\HTTPException\NotFoundException();
60 $a->data['user'] = $user;
61 $a->profile_uid = $user['uid'];
63 // if it's a json request abort here becaus we don't
64 // need the widget data
65 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
69 $profile = Profile::getByNickname($nick, $a->profile_uid);
71 $account_type = Contact::getAccountType($profile);
73 $tpl = Renderer::getMarkupTemplate("widget/vcard.tpl");
75 $vcard_widget = Renderer::replaceMacros($tpl, [
76 '$name' => $profile['name'],
77 '$photo' => $profile['photo'],
78 '$addr' => $profile['addr'] ?: '',
79 '$account_type' => $account_type,
80 '$about' => $profile['about'] ?: '',
83 $cal_widget = Widget\CalendarExport::getHTML();
85 if (empty(DI::page()['aside'])) {
86 DI::page()['aside'] = '';
89 DI::page()['aside'] .= $vcard_widget;
90 DI::page()['aside'] .= $cal_widget;
95 function cal_content(App $a)
97 Nav::setSelected('events');
99 // get the translation strings for the callendar
100 $i18n = Event::getStrings();
102 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
103 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
104 '$module_url' => '/cal/' . $a->data['user']['nickname'],
112 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
115 if ($a->argc == 4 && $a->argv[2] == 'export') {
117 $format = $a->argv[3];
120 // Setup permissions structures
121 $owner_uid = intval($a->data['user']['uid']);
122 $nick = $a->data['user']['nickname'];
124 $contact_id = Session::getRemoteContactID($a->profile['uid']);
126 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
128 $is_owner = local_user() == $a->profile['uid'];
130 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
131 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
135 // get the permissions
136 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
137 // we only want to have the events of the profile owner
138 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
140 // get the tab navigation bar
141 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
143 // The view mode part is similiar to /mod/events.php
144 if ($mode == 'view') {
145 $thisyear = DateTimeFormat::localNow('Y');
146 $thismonth = DateTimeFormat::localNow('m');
148 $y = intval($thisyear);
152 $m = intval($thismonth);
155 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
156 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
168 if ($nextmonth > 12) {
181 $dim = Temporal::getDaysInMonth($y, $m);
182 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
183 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
186 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
187 if (!empty($_GET['start'])) {
188 $start = $_GET['start'];
191 if (!empty($_GET['end'])) {
192 $finish = $_GET['end'];
196 $start = DateTimeFormat::utc($start);
197 $finish = DateTimeFormat::utc($finish);
199 $adjust_start = DateTimeFormat::local($start);
200 $adjust_finish = DateTimeFormat::local($finish);
202 // put the event parametes in an array so we can better transmit them
204 'event_id' => intval($_GET['id'] ?? 0),
207 'adjust_start' => $adjust_start,
208 'adjust_finish' => $adjust_finish,
209 'ignore' => $ignored,
212 // get events by id or by date
213 if ($event_params['event_id']) {
214 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
216 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
221 if (DBA::isResult($r)) {
222 $r = Event::sortByDate($r);
223 foreach ($r as $rr) {
224 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
225 if (empty($links[$j])) {
226 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
231 // transform the event in a usable array
232 $events = Event::prepareListForTemplate($r);
234 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
235 echo json_encode($events);
239 // links: array('href', 'text', 'extra css classes', 'title')
240 if (!empty($_GET['id'])) {
241 $tpl = Renderer::getMarkupTemplate("event.tpl");
243 // if (DI::config()->get('experimentals','new_calendar')==1){
244 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
246 // $tpl = Renderer::getMarkupTemplate("events.tpl");
250 // Get rid of dashes in key names, Smarty3 can't handle them
251 foreach ($events as $key => $event) {
253 foreach ($event['item'] as $k => $v) {
254 $k = str_replace('-', '_', $k);
255 $event_item[$k] = $v;
257 $events[$key]['item'] = $event_item;
260 $o = Renderer::replaceMacros($tpl, [
262 '$title' => DI::l10n()->t('Events'),
263 '$view' => DI::l10n()->t('View'),
264 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
265 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
266 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
267 '$events' => $events,
268 "today" => DI::l10n()->t("today"),
269 "month" => DI::l10n()->t("month"),
270 "week" => DI::l10n()->t("week"),
271 "day" => DI::l10n()->t("day"),
272 "list" => DI::l10n()->t("list"),
275 if (!empty($_GET['id'])) {
283 if ($mode == 'export') {
285 notice(DI::l10n()->t('User not found'));
290 // Respect the export feature setting for all other /cal pages if it's not the own profile
291 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
292 notice(DI::l10n()->t('Permission denied.') . EOL);
293 DI::baseUrl()->redirect('cal/' . $nick);
296 // Get the export data by uid
297 $evexport = Event::exportListByUserId($owner_uid, $format);
299 if (!$evexport["success"]) {
300 if ($evexport["content"]) {
301 notice(DI::l10n()->t('This calendar format is not supported'));
303 notice(DI::l10n()->t('No exportable data found'));
306 // If it the own calendar return to the events page
307 // otherwise to the profile calendar page
308 if (local_user() === $owner_uid) {
309 $return_path = "events";
311 $return_path = "cal/" . $nick;
314 DI::baseUrl()->redirect($return_path);
317 // If nothing went wrong we can echo the export content
318 if ($evexport["success"]) {
319 header('Content-type: text/calendar');
320 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
321 echo $evexport["content"];