0078b5eb58191a0f721d06769a27ec00da92a473
[friendica.git/.git] / src / Module / Calendar / Export.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Calendar;
23
24 use Friendica\App;
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;
38
39 /**
40  * Controller to export a calendar from a given user
41  */
42 class Export extends BaseModule
43 {
44         const EXPORT_ICAL = 'ical';
45         const EXPORT_CSV  = 'csv';
46
47         const DEFAULT_EXPORT = self::EXPORT_ICAL;
48
49         /** @var IHandleUserSessions */
50         protected $session;
51         /** @var SystemMessages */
52         protected $sysMessages;
53         /** @var App */
54         protected $app;
55
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 = [])
57         {
58                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
59
60                 $this->session     = $session;
61                 $this->sysMessages = $sysMessages;
62                 $this->app         = $app;
63         }
64
65         protected function rawContent(array $request = [])
66         {
67                 $nickname = $this->parameters['nickname'] ?? null;
68                 if (!$nickname) {
69                         throw new HTTPException\BadRequestException();
70                 }
71
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.'));
75                 }
76
77                 if (!$this->session->isAuthenticated() && $owner['hidewall']) {
78                         $this->baseUrl->redirect('profile/' . $nickname . '/restricted');
79                 }
80
81                 if (!$this->session->isAuthenticated() && !Feature::isEnabled($owner['uid'], Feature::PUBLIC_CALENDAR)) {
82                         $this->sysMessages->addNotice($this->t('Permission denied.'));
83                         $this->baseUrl->redirect('profile/' . $nickname);
84                 }
85
86                 $ownerUid = $owner['uid'];
87                 $format   = $this->parameters['format'] ?: static::DEFAULT_EXPORT;
88
89                 // Get the export data by uid
90                 $evexport = Event::exportListByUserId($ownerUid, $format);
91
92                 if (!$evexport["success"]) {
93                         if ($evexport["content"]) {
94                                 $this->sysMessages->addNotice($this->t('This calendar format is not supported'));
95                         } else {
96                                 $this->sysMessages->addNotice($this->t('No exportable data found'));
97                         }
98
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';
103                         } else {
104                                 $returnPath = 'calendar/show/' . $this->parameters['nickname'];
105                         }
106
107                         $this->baseUrl->redirect($returnPath);
108                 }
109
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"]
116                         ));
117
118                         switch ($format) {
119                                 case static::EXPORT_ICAL:
120                                         $this->response->setType(Response::TYPE_BLANK, 'text/ics');
121                                         break;
122                                 case static::EXPORT_CSV:
123                                         $this->response->setType(Response::TYPE_BLANK, 'text/csv');
124                                         break;
125                         }
126
127                         $this->response->addContent($evexport['content']);
128                 }
129         }
130 }