3 * @package Friendica\modules
4 * @subpackage FileBrowser
5 * @author Fabio Comuni <fabrixxm@kirgroup.com>
9 use Friendica\Core\Renderer;
10 use Friendica\Database\DBA;
12 use Friendica\Model\Photo;
13 use Friendica\Util\Images;
14 use Friendica\Util\Strings;
19 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
21 function fbrowser_content(App $a)
27 if (DI::args()->getArgc() == 1) {
31 // Needed to match the correct template in a module that uses a different theme than the user/site/default
32 $theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? null);
33 if ($theme && is_file("view/theme/$theme/config.php")) {
34 $a->setCurrentTheme($theme);
37 $template_file = "filebrowser.tpl";
41 switch (DI::args()->getArgv()[1]) {
43 $path = ['' => DI::l10n()->t('Photos')];
46 $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
48 if (DI::args()->getArgc() == 2) {
49 $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?)",
51 Photo::CONTACT_AVATAR,
55 $albums = array_column($photos, 'album');
58 if (DI::args()->getArgc() == 3) {
59 $album = DI::args()->getArgv()[2];
60 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
62 $path[$album] = $album;
65 $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
66 min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
67 FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `photo-type` IN (?, ?)
68 GROUP BY `resource-id` $sql_extra2",
70 Photo::CONTACT_AVATAR,
74 function _map_files1($rr)
77 $types = Images::supportedTypes();
78 $ext = $types[$rr['type']];
79 $filename_e = $rr['filename'];
81 // Take the largest picture that is smaller or equal 640 pixels
82 $photo = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `height` <= ? AND `width` <= ?", $rr['resource-id'], 640, 640], ['order' => ['scale']]);
83 $scale = $photo['scale'] ?? $rr['loq'];
86 DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
88 DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
91 $files = array_map("_map_files1", $r);
93 $tpl = Renderer::getMarkupTemplate($template_file);
95 $o = Renderer::replaceMacros($tpl, [
98 '$folders' => $albums,
100 '$cancel' => DI::l10n()->t('Cancel'),
101 '$nickname' => $a->getLoggedInUserNickname(),
102 '$upload' => DI::l10n()->t('Upload')
107 if (DI::args()->getArgc()==2) {
108 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => local_user()]);
110 function _map_files2($rr)
112 list($m1, $m2) = explode("/", $rr['filetype']);
113 $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
114 $filename_e = $rr['filename'];
116 return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
118 $files = array_map("_map_files2", $files);
121 $tpl = Renderer::getMarkupTemplate($template_file);
122 $o = Renderer::replaceMacros($tpl, [
124 '$path' => ['' => DI::l10n()->t('Files')],
127 '$cancel' => DI::l10n()->t('Cancel'),
128 '$nickname' => $a->getLoggedInUserNickname(),
129 '$upload' => DI::l10n()->t('Upload')
136 if (!empty($_GET['mode'])) {