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\Text\BBCode;
30 use Friendica\Content\Widget;
31 use Friendica\Core\Renderer;
32 use Friendica\Core\Session;
33 use Friendica\Database\DBA;
35 use Friendica\Model\Contact;
36 use Friendica\Model\Event;
37 use Friendica\Model\Item;
38 use Friendica\Model\Profile;
39 use Friendica\Module\BaseProfile;
40 use Friendica\Util\DateTimeFormat;
41 use Friendica\Util\Temporal;
43 function cal_init(App $a)
45 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
46 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
50 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
53 Nav::setSelected('events');
56 $user = DBA::selectFirst('user', [], ['nickname' => $nick, 'blocked' => false]);
57 if (!DBA::isResult($user)) {
58 throw new \Friendica\Network\HTTPException\NotFoundException();
61 $a->data['user'] = $user;
62 $a->profile_uid = $user['uid'];
64 // if it's a json request abort here becaus we don't
65 // need the widget data
66 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
70 $profile = Profile::getByNickname($nick, $a->profile_uid);
72 $account_type = Contact::getAccountType($profile);
74 $tpl = Renderer::getMarkupTemplate('widget/vcard.tpl');
76 $vcard_widget = Renderer::replaceMacros($tpl, [
77 '$name' => $profile['name'],
78 '$photo' => $profile['photo'],
79 '$addr' => $profile['addr'] ?: '',
80 '$account_type' => $account_type,
81 '$about' => BBCode::convert($profile['about'] ?: ''),
84 $cal_widget = Widget\CalendarExport::getHTML();
86 if (empty(DI::page()['aside'])) {
87 DI::page()['aside'] = '';
90 DI::page()['aside'] .= $vcard_widget;
91 DI::page()['aside'] .= $cal_widget;
96 function cal_content(App $a)
98 Nav::setSelected('events');
100 // get the translation strings for the callendar
101 $i18n = Event::getStrings();
103 $htpl = Renderer::getMarkupTemplate('event_head.tpl');
104 DI::page()['htmlhead'] .= Renderer::replaceMacros($htpl, [
105 '$module_url' => '/cal/' . $a->data['user']['nickname'],
113 $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
116 if ($a->argc == 4 && $a->argv[2] == 'export') {
118 $format = $a->argv[3];
121 // Setup permissions structures
122 $owner_uid = intval($a->data['user']['uid']);
123 $nick = $a->data['user']['nickname'];
125 $contact_id = Session::getRemoteContactID($a->profile['uid']);
127 $remote_contact = $contact_id && DBA::exists('contact', ['id' => $contact_id, 'uid' => $a->profile['uid']]);
129 $is_owner = local_user() == $a->profile['uid'];
131 if ($a->profile['hidewall'] && !$is_owner && !$remote_contact) {
132 notice(DI::l10n()->t('Access to this profile has been restricted.') . EOL);
136 // get the permissions
137 $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
138 // we only want to have the events of the profile owner
139 $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
141 // get the tab navigation bar
142 $tabs = BaseProfile::getTabsHTML($a, 'cal', false, $a->data['user']['nickname']);
144 // The view mode part is similiar to /mod/events.php
145 if ($mode == 'view') {
146 $thisyear = DateTimeFormat::localNow('Y');
147 $thismonth = DateTimeFormat::localNow('m');
149 $y = intval($thisyear);
153 $m = intval($thismonth);
156 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
157 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
169 if ($nextmonth > 12) {
182 $dim = Temporal::getDaysInMonth($y, $m);
183 $start = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
184 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
187 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
188 if (!empty($_GET['start'])) {
189 $start = $_GET['start'];
192 if (!empty($_GET['end'])) {
193 $finish = $_GET['end'];
197 $start = DateTimeFormat::utc($start);
198 $finish = DateTimeFormat::utc($finish);
200 $adjust_start = DateTimeFormat::local($start);
201 $adjust_finish = DateTimeFormat::local($finish);
203 // put the event parametes in an array so we can better transmit them
205 'event_id' => intval($_GET['id'] ?? 0),
208 'adjust_start' => $adjust_start,
209 'adjust_finish' => $adjust_finish,
210 'ignore' => $ignored,
213 // get events by id or by date
214 if ($event_params['event_id']) {
215 $r = Event::getListById($owner_uid, $event_params['event_id'], $sql_extra);
217 $r = Event::getListByDate($owner_uid, $event_params, $sql_extra);
222 if (DBA::isResult($r)) {
223 $r = Event::sortByDate($r);
224 foreach ($r as $rr) {
225 $j = $rr['adjust'] ? DateTimeFormat::local($rr['start'], 'j') : DateTimeFormat::utc($rr['start'], 'j');
226 if (empty($links[$j])) {
227 $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
232 // transform the event in a usable array
233 $events = Event::prepareListForTemplate($r);
235 if (!empty($a->argv[2]) && ($a->argv[2] === 'json')) {
236 echo json_encode($events);
240 // links: array('href', 'text', 'extra css classes', 'title')
241 if (!empty($_GET['id'])) {
242 $tpl = Renderer::getMarkupTemplate("event.tpl");
244 // if (DI::config()->get('experimentals','new_calendar')==1){
245 $tpl = Renderer::getMarkupTemplate("events_js.tpl");
247 // $tpl = Renderer::getMarkupTemplate("events.tpl");
251 // Get rid of dashes in key names, Smarty3 can't handle them
252 foreach ($events as $key => $event) {
254 foreach ($event['item'] as $k => $v) {
255 $k = str_replace('-', '_', $k);
256 $event_item[$k] = $v;
258 $events[$key]['item'] = $event_item;
261 $o = Renderer::replaceMacros($tpl, [
263 '$title' => DI::l10n()->t('Events'),
264 '$view' => DI::l10n()->t('View'),
265 '$previous' => [DI::baseUrl() . "/events/$prevyear/$prevmonth", DI::l10n()->t('Previous'), '', ''],
266 '$next' => [DI::baseUrl() . "/events/$nextyear/$nextmonth", DI::l10n()->t('Next'), '', ''],
267 '$calendar' => Temporal::getCalendarTable($y, $m, $links, ' eventcal'),
268 '$events' => $events,
269 "today" => DI::l10n()->t("today"),
270 "month" => DI::l10n()->t("month"),
271 "week" => DI::l10n()->t("week"),
272 "day" => DI::l10n()->t("day"),
273 "list" => DI::l10n()->t("list"),
276 if (!empty($_GET['id'])) {
284 if ($mode == 'export') {
286 notice(DI::l10n()->t('User not found'));
291 // Respect the export feature setting for all other /cal pages if it's not the own profile
292 if ((local_user() !== $owner_uid) && !Feature::isEnabled($owner_uid, "export_calendar")) {
293 notice(DI::l10n()->t('Permission denied.') . EOL);
294 DI::baseUrl()->redirect('cal/' . $nick);
297 // Get the export data by uid
298 $evexport = Event::exportListByUserId($owner_uid, $format);
300 if (!$evexport["success"]) {
301 if ($evexport["content"]) {
302 notice(DI::l10n()->t('This calendar format is not supported'));
304 notice(DI::l10n()->t('No exportable data found'));
307 // If it the own calendar return to the events page
308 // otherwise to the profile calendar page
309 if (local_user() === $owner_uid) {
310 $return_path = "events";
312 $return_path = "cal/" . $nick;
315 DI::baseUrl()->redirect($return_path);
318 // If nothing went wrong we can echo the export content
319 if ($evexport["success"]) {
320 header('Content-type: text/calendar');
321 header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
322 echo $evexport["content"];