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\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('You need to be logged in to access this page.'));
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()]);
69 $fileArray = array_map([$this, 'map_files'], $files);
71 $tpl = Renderer::getMarkupTemplate('media/browser.tpl');
72 $output = Renderer::replaceMacros($tpl, [
73 '$type' => 'attachment',
74 '$path' => ['' => $this->t('Files')],
76 '$files' => $fileArray,
77 '$cancel' => $this->t('Cancel'),
78 '$nickname' => $this->app->getLoggedInUserNickname(),
79 '$upload' => $this->t('Upload'),
82 if (empty($request['mode'])) {
83 System::httpExit($output);
89 protected function map_files(array $record): array
91 list($m1, $m2) = explode('/', $record['filetype']);
92 $filetype = file_exists(sprintf('images/icons/%s.png', $m1) ? $m1 : 'text');
95 sprintf('%s/attach/%s', $this->baseUrl, $record['id']),
97 sprintf('%s/images/icon/16/%s.png', $this->baseUrl, $filetype),