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)
42 DI::page()->logRuntime();
46 if (DI::args()->getArgc() == 1) {
47 DI::page()->logRuntime();
51 // Needed to match the correct template in a module that uses a different theme than the user/site/default
52 $theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? null);
53 if ($theme && is_file("view/theme/$theme/config.php")) {
54 $a->setCurrentTheme($theme);
57 $template_file = "filebrowser.tpl";
61 switch (DI::args()->getArgv()[1]) {
63 $path = ['' => DI::l10n()->t('Photos')];
66 $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
68 if (DI::args()->getArgc() == 2) {
69 $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?)",
71 Photo::CONTACT_AVATAR,
75 $albums = array_column($photos, 'album');
78 if (DI::args()->getArgc() == 3) {
79 $album = DI::args()->getArgv()[2];
80 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
82 $path[$album] = $album;
85 $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
86 min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
87 FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `photo-type` IN (?, ?)
88 GROUP BY `resource-id` $sql_extra2",
90 Photo::CONTACT_AVATAR,
94 function _map_files1($rr)
97 $types = Images::supportedTypes();
98 $ext = $types[$rr['type']];
99 $filename_e = $rr['filename'];
101 // Take the largest picture that is smaller or equal 640 pixels
102 $photo = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `height` <= ? AND `width` <= ?", $rr['resource-id'], 640, 640], ['order' => ['scale']]);
103 $scale = $photo['scale'] ?? $rr['loq'];
106 DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
108 DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
111 $files = array_map("_map_files1", $r);
113 $tpl = Renderer::getMarkupTemplate($template_file);
115 $o = Renderer::replaceMacros($tpl, [
118 '$folders' => $albums,
120 '$cancel' => DI::l10n()->t('Cancel'),
121 '$nickname' => $a->getLoggedInUserNickname(),
122 '$upload' => DI::l10n()->t('Upload')
127 if (DI::args()->getArgc()==2) {
128 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => local_user()]);
130 function _map_files2($rr)
132 list($m1, $m2) = explode("/", $rr['filetype']);
133 $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
134 $filename_e = $rr['filename'];
136 return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
138 $files = array_map("_map_files2", $files);
141 $tpl = Renderer::getMarkupTemplate($template_file);
142 $o = Renderer::replaceMacros($tpl, [
144 '$path' => ['' => DI::l10n()->t('Files')],
147 '$cancel' => DI::l10n()->t('Cancel'),
148 '$nickname' => $a->getLoggedInUserNickname(),
149 '$upload' => DI::l10n()->t('Upload')
156 if (!empty($_GET['mode'])) {
159 System::httpExit($o);