]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Merge pull request #11241 from annando/timing
[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\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Photo;
30 use Friendica\Util\Images;
31 use Friendica\Util\Strings;
32
33 /**
34  * @param App $a
35  * @return string
36  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
37  */
38 function fbrowser_content(App $a)
39 {
40         if (!local_user()) {
41                 exit();
42         }
43
44         if (DI::args()->getArgc() == 1) {
45                 exit();
46         }
47
48         // Needed to match the correct template in a module that uses a different theme than the user/site/default
49         $theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? null);
50         if ($theme && is_file("view/theme/$theme/config.php")) {
51                 $a->setCurrentTheme($theme);
52         }
53
54         $template_file = "filebrowser.tpl";
55
56         $o = '';
57
58         switch (DI::args()->getArgv()[1]) {
59                 case "image":
60                         $path = ['' => DI::l10n()->t('Photos')];
61                         $albums = false;
62                         $sql_extra = "";
63                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
64
65                         if (DI::args()->getArgc() == 2) {
66                                 $photos = DBA::toArray(DBA::p("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = ? AND NOT `photo-type` IN (?, ?)",
67                                         local_user(),
68                                         Photo::CONTACT_AVATAR,
69                                         Photo::CONTACT_BANNER
70                                 ));
71
72                                 $albums = array_column($photos, 'album');
73                         }
74
75                         if (DI::args()->getArgc() == 3) {
76                                 $album = DI::args()->getArgv()[2];
77                                 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
78                                 $sql_extra2 = "";
79                                 $path[$album] = $album;
80                         }
81
82                         $r = DBA::toArray(DBA::p("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
83                                         min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
84                                         FROM `photo` WHERE `uid` = ? $sql_extra AND NOT `photo-type` IN (?, ?)
85                                         GROUP BY `resource-id` $sql_extra2",
86                                 local_user(),
87                                 Photo::CONTACT_AVATAR,
88                                 Photo::CONTACT_BANNER
89                         ));
90
91                         function _map_files1($rr)
92                         {
93                                 $a = DI::app();
94                                 $types = Images::supportedTypes();
95                                 $ext = $types[$rr['type']];
96                                 $filename_e = $rr['filename'];
97
98                                 // Take the largest picture that is smaller or equal 640 pixels
99                                 $photo = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `height` <= ? AND `width` <= ?", $rr['resource-id'], 640, 640], ['order' => ['scale']]);
100                                 $scale = $photo['scale'] ?? $rr['loq'];
101
102                                 return [
103                                         DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
104                                         $filename_e,
105                                         DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
106                                 ];
107                         }
108                         $files = array_map("_map_files1", $r);
109
110                         $tpl = Renderer::getMarkupTemplate($template_file);
111
112                         $o =  Renderer::replaceMacros($tpl, [
113                                 '$type'     => 'image',
114                                 '$path'     => $path,
115                                 '$folders'  => $albums,
116                                 '$files'    => $files,
117                                 '$cancel'   => DI::l10n()->t('Cancel'),
118                                 '$nickname' => $a->getLoggedInUserNickname(),
119                                 '$upload'   => DI::l10n()->t('Upload')
120                         ]);
121
122                         break;
123                 case "file":
124                         if (DI::args()->getArgc()==2) {
125                                 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => local_user()]);
126
127                                 function _map_files2($rr)
128                                 {
129                                         list($m1, $m2) = explode("/", $rr['filetype']);
130                                         $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
131                                         $filename_e = $rr['filename'];
132
133                                         return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
134                                 }
135                                 $files = array_map("_map_files2", $files);
136
137
138                                 $tpl = Renderer::getMarkupTemplate($template_file);
139                                 $o = Renderer::replaceMacros($tpl, [
140                                         '$type'     => 'file',
141                                         '$path'     => ['' => DI::l10n()->t('Files')],
142                                         '$folders'  => false,
143                                         '$files'    => $files,
144                                         '$cancel'   => DI::l10n()->t('Cancel'),
145                                         '$nickname' => $a->getLoggedInUserNickname(),
146                                         '$upload'   => DI::l10n()->t('Upload')
147                                 ]);
148                         }
149
150                         break;
151         }
152
153         if (!empty($_GET['mode'])) {
154                 return $o;
155         } else {
156                 echo $o;
157                 exit();
158         }
159 }