]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Merge pull request #11575 from annando/issue-11469
[friendica.git] / mod / fbrowser.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * @package             Friendica\modules
21  * @subpackage  FileBrowser
22  * @author              Fabio Comuni <fabrixxm@kirgroup.com>
23  */
24
25 use Friendica\App;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Photo;
31 use Friendica\Util\Images;
32 use Friendica\Util\Strings;
33
34 /**
35  * @param App $a
36  * @return string
37  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38  */
39 function fbrowser_content(App $a)
40 {
41         if (!local_user()) {
42                 System::exit();
43         }
44
45         if (DI::args()->getArgc() == 1) {
46                 System::exit();
47         }
48
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'] ?? null);
51         if ($theme && is_file("view/theme/$theme/config.php")) {
52                 $a->setCurrentTheme($theme);
53         }
54
55         $template_file = "filebrowser.tpl";
56
57         $o = '';
58
59         switch (DI::args()->getArgv()[1]) {
60                 case "image":
61                         $path = ['' => DI::l10n()->t('Photos')];
62                         $albums = false;
63                         $sql_extra = "";
64                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
65
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                                         local_user(),
69                                         Photo::CONTACT_AVATAR,
70                                         Photo::CONTACT_BANNER
71                                 ));
72
73                                 $albums = array_column($photos, 'album');
74                         }
75
76                         if (DI::args()->getArgc() == 3) {
77                                 $album = DI::args()->getArgv()[2];
78                                 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
79                                 $sql_extra2 = "";
80                                 $path[$album] = $album;
81                         }
82
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                                 local_user(),
88                                 Photo::CONTACT_AVATAR,
89                                 Photo::CONTACT_BANNER
90                         ));
91
92                         function _map_files1($rr)
93                         {
94                                 $a = DI::app();
95                                 $types = Images::supportedTypes();
96                                 $ext = $types[$rr['type']];
97                                 $filename_e = $rr['filename'];
98
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'];
102
103                                 return [
104                                         DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
105                                         $filename_e,
106                                         DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
107                                 ];
108                         }
109                         $files = array_map("_map_files1", $r);
110
111                         $tpl = Renderer::getMarkupTemplate($template_file);
112
113                         $o =  Renderer::replaceMacros($tpl, [
114                                 '$type'     => 'image',
115                                 '$path'     => $path,
116                                 '$folders'  => $albums,
117                                 '$files'    => $files,
118                                 '$cancel'   => DI::l10n()->t('Cancel'),
119                                 '$nickname' => $a->getLoggedInUserNickname(),
120                                 '$upload'   => DI::l10n()->t('Upload')
121                         ]);
122
123                         break;
124                 case "file":
125                         if (DI::args()->getArgc()==2) {
126                                 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => local_user()]);
127
128                                 function _map_files2($rr)
129                                 {
130                                         list($m1, $m2) = explode("/", $rr['filetype']);
131                                         $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
132                                         $filename_e = $rr['filename'];
133
134                                         return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
135                                 }
136                                 $files = array_map("_map_files2", $files);
137
138
139                                 $tpl = Renderer::getMarkupTemplate($template_file);
140                                 $o = Renderer::replaceMacros($tpl, [
141                                         '$type'     => 'file',
142                                         '$path'     => ['' => DI::l10n()->t('Files')],
143                                         '$folders'  => false,
144                                         '$files'    => $files,
145                                         '$cancel'   => DI::l10n()->t('Cancel'),
146                                         '$nickname' => $a->getLoggedInUserNickname(),
147                                         '$upload'   => DI::l10n()->t('Upload')
148                                 ]);
149                         }
150
151                         break;
152         }
153
154         if (!empty($_GET['mode'])) {
155                 return $o;
156         } else {
157                 System::httpExit($o);
158         }
159 }