]> git.mxchange.org Git - friendica.git/blob - src/Module/Media/Attachment/Browser.php
spelling: one
[friendica.git] / src / Module / Media / Attachment / Browser.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Media\Attachment;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Core\System;
30 use Friendica\Model\Attach;
31 use Friendica\Module\Response;
32 use Friendica\Network\HTTPException\UnauthorizedException;
33 use Friendica\Util\Profiler;
34 use Friendica\Util\Strings;
35 use Psr\Log\LoggerInterface;
36
37 /**
38  * Browser for Attachments
39  */
40 class Browser extends BaseModule
41 {
42         /** @var IHandleUserSessions */
43         protected $session;
44         /** @var App */
45         protected $app;
46
47         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, App $app, array $server, array $parameters = [])
48         {
49                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->session = $session;
52                 $this->app     = $app;
53         }
54
55         protected function content(array $request = []): string
56         {
57                 if (!$this->session->getLocalUserId()) {
58                         throw new UnauthorizedException($this->t('You need to be logged in to access this page.'));
59                 }
60
61                 // Needed to match the correct template in a module that uses a different theme than the user/site/default
62                 $theme = Strings::sanitizeFilePathItem($request['theme'] ?? '');
63                 if ($theme && is_file("view/theme/$theme/config.php")) {
64                         $this->app->setCurrentTheme($theme);
65                 }
66
67                 $files = Attach::selectToArray(['id', 'filename', 'filetype'], ['uid' => $this->session->getLocalUserId()]);
68
69                 $fileArray = array_map([$this, 'map_files'], $files);
70
71                 $tpl    = Renderer::getMarkupTemplate('media/browser.tpl');
72                 $output = Renderer::replaceMacros($tpl, [
73                         '$type'     => 'attachment',
74                         '$path'     => ['' => $this->t('Files')],
75                         '$folders'  => false,
76                         '$files'    => $fileArray,
77                         '$cancel'   => $this->t('Cancel'),
78                         '$nickname' => $this->app->getLoggedInUserNickname(),
79                         '$upload'   => $this->t('Upload'),
80                 ]);
81
82                 if (empty($request['mode'])) {
83                         System::httpExit($output);
84                 }
85
86                 return $output;
87         }
88
89         protected function map_files(array $record): array
90         {
91                 list($m1, $m2) = explode('/', $record['filetype']);
92                 $filetype      = file_exists(sprintf('images/icons/%s.png', $m1) ? $m1 : 'text');
93
94                 return [
95                         sprintf('%s/attach/%s', $this->baseUrl, $record['id']),
96                         $record['filename'],
97                         sprintf('%s/images/icon/16/%s.png', $this->baseUrl, $filetype),
98                 ];
99         }
100 }