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/>.
20 * @package Friendica\modules
21 * @subpackage FileBrowser
22 * @author Fabio Comuni <fabrixxm@kirgroup.com>
26 use Friendica\Core\Renderer;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Photo;
31 use Friendica\Util\Images;
32 use Friendica\Util\Strings;
37 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39 function fbrowser_content(App $a)
41 if (!DI::userSession()->getLocalUserId()) {
45 if (DI::args()->getArgc() == 1) {
49 // Needed to match the correct template in a module that uses a different theme than the user/site/default
50 $theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? '');
51 if ($theme && is_file("view/theme/$theme/config.php")) {
52 $a->setCurrentTheme($theme);
55 $template_file = "filebrowser.tpl";
59 switch (DI::args()->getArgv()[1]) {
61 $path = ['' => DI::l10n()->t('Photos')];
64 $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
66 if (DI::args()->getArgc() == 2) {
67 $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?)",
68 DI::userSession()->getLocalUserId(),
69 Photo::CONTACT_AVATAR,
73 $albums = array_column($photos, 'album');
76 if (DI::args()->getArgc() == 3) {
77 $album = DI::args()->getArgv()[2];
78 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
80 $path[$album] = $album;
83 $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
84 min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
85 FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `photo-type` IN (?, ?)
86 GROUP BY `resource-id` $sql_extra2",
87 DI::userSession()->getLocalUserId(),
88 Photo::CONTACT_AVATAR,
92 function _map_files1($rr)
95 $types = Images::supportedTypes();
96 $ext = $types[$rr['type']];
97 $filename_e = $rr['filename'];
99 // Take the largest picture that is smaller or equal 640 pixels
100 $photo = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `height` <= ? AND `width` <= ?", $rr['resource-id'], 640, 640], ['order' => ['scale']]);
101 $scale = $photo['scale'] ?? $rr['loq'];
104 DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
106 DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext,
110 $files = array_map("_map_files1", $r);
112 $tpl = Renderer::getMarkupTemplate($template_file);
114 $o = Renderer::replaceMacros($tpl, [
117 '$folders' => $albums,
119 '$cancel' => DI::l10n()->t('Cancel'),
120 '$nickname' => $a->getLoggedInUserNickname(),
121 '$upload' => DI::l10n()->t('Upload')
126 if (DI::args()->getArgc()==2) {
127 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => DI::userSession()->getLocalUserId()]);
129 function _map_files2($rr)
131 list($m1, $m2) = explode("/", $rr['filetype']);
132 $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
133 $filename_e = $rr['filename'];
135 return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
137 $files = array_map("_map_files2", $files);
140 $tpl = Renderer::getMarkupTemplate($template_file);
141 $o = Renderer::replaceMacros($tpl, [
143 '$path' => ['' => DI::l10n()->t('Files')],
146 '$cancel' => DI::l10n()->t('Cancel'),
147 '$nickname' => $a->getLoggedInUserNickname(),
148 '$upload' => DI::l10n()->t('Upload')
155 if (!empty($_GET['mode'])) {
158 System::httpExit($o);