3 * @copyright Copyright (C) 2010-2022, 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\Media\Attachment;
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;
38 * Browser for Attachments
40 class Browser extends BaseModule
42 /** @var IHandleUserSessions */
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 = [])
49 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
51 $this->session = $session;
55 protected function content(array $request = []): string
57 if (!$this->session->getLocalUserId()) {
58 throw new UnauthorizedException($this->t('Permission denied.'));
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);
67 $files = Attach::selectToArray(['id', 'filename', 'filetype'], ['uid' => $this->session->getLocalUserId()]);
70 $fileArray = array_map([$this, 'map_files'], $files);
72 $tpl = Renderer::getMarkupTemplate('media/filebrowser.tpl');
73 $output = Renderer::replaceMacros($tpl, [
74 '$type' => 'attachment',
75 '$path' => ['' => $this->t('Files')],
77 '$files' => $fileArray,
78 '$cancel' => $this->t('Cancel'),
79 '$nickname' => $this->app->getLoggedInUserNickname(),
80 '$upload' => $this->t('Upload'),
83 if (empty($request['mode'])) {
84 System::httpExit($output);
90 protected function map_files(array $record): array
92 [$m1, $m2] = explode('/', $record['filetype']);
93 $filetype = file_exists(sprintf('images/icons/%s.png', $m1) ? $m1 : 'text');
96 sprintf('%s/attach/%s', $this->baseUrl, $record['id']),
98 sprintf('%s/images/icon/16/%s.png', $this->baseUrl, $filetype),