3 * @copyright Copyright (C) 2010-2023, the Friendica project
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 namespace Friendica\Module\Calendar;
25 use Friendica\BaseModule;
26 use Friendica\Content\Feature;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Model\Event;
30 use Friendica\Model\Profile;
31 use Friendica\Model\User;
32 use Friendica\Module\Response;
33 use Friendica\Module\Security\Login;
34 use Friendica\Navigation\SystemMessages;
35 use Friendica\Network\HTTPException;
36 use Friendica\Util\Profiler;
37 use Psr\Log\LoggerInterface;
40 * Controller to export a calendar from a given user
42 class Export extends BaseModule
44 const EXPORT_ICAL = 'ical';
45 const EXPORT_CSV = 'csv';
47 const DEFAULT_EXPORT = self::EXPORT_ICAL;
49 /** @var IHandleUserSessions */
51 /** @var SystemMessages */
52 protected $sysMessages;
56 public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, array $server, array $parameters = [])
58 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
60 $this->session = $session;
61 $this->sysMessages = $sysMessages;
65 protected function rawContent(array $request = [])
67 $nickname = $this->parameters['nickname'] ?? null;
69 throw new HTTPException\BadRequestException();
72 $owner = Profile::load($this->app, $nickname, false);
73 if (!$owner || $owner['account_expired'] || $owner['account_removed']) {
74 throw new HTTPException\NotFoundException($this->t('User not found.'));
77 if (!$this->session->isAuthenticated() && $owner['hidewall']) {
78 $this->baseUrl->redirect('profile/' . $nickname . '/restricted');
81 if (!$this->session->isAuthenticated() && !Feature::isEnabled($owner['uid'], 'public_calendar')) {
82 $this->sysMessages->addNotice($this->t('Permission denied.'));
83 $this->baseUrl->redirect('profile/' . $nickname);
86 $ownerUid = $owner['uid'];
87 $format = $this->parameters['format'] ?: static::DEFAULT_EXPORT;
89 // Get the export data by uid
90 $evexport = Event::exportListByUserId($ownerUid, $format);
92 if (!$evexport["success"]) {
93 if ($evexport["content"]) {
94 $this->sysMessages->addNotice($this->t('This calendar format is not supported'));
96 $this->sysMessages->addNotice($this->t('No exportable data found'));
99 // If it is the own calendar return to the events page
100 // otherwise to the profile calendar page
101 if ($this->session->getLocalUserId() === $ownerUid) {
102 $returnPath = 'calendar';
104 $returnPath = 'calendar/show/' . $this->parameters['nickname'];
107 $this->baseUrl->redirect($returnPath);
110 // If nothing went wrong we can echo the export content
111 if ($evexport["success"]) {
112 $this->response->setHeader(sprintf('Content-Disposition: attachment; filename="%s-%s.%s"',
113 $this->t('calendar'),
114 $this->parameters['nickname'],
115 $evexport["extension"]
119 case static::EXPORT_ICAL:
120 $this->response->setType(Response::TYPE_BLANK, 'text/ics');
122 case static::EXPORT_CSV:
123 $this->response->setType(Response::TYPE_BLANK, 'text/csv');
127 $this->response->addContent($evexport['content']);