]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Log the execution time
[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                 DI::page()->logRuntime();
43                 exit();
44         }
45
46         if (DI::args()->getArgc() == 1) {
47                 DI::page()->logRuntime();
48                 exit();
49         }
50
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);
55         }
56
57         $template_file = "filebrowser.tpl";
58
59         $o = '';
60
61         switch (DI::args()->getArgv()[1]) {
62                 case "image":
63                         $path = ['' => DI::l10n()->t('Photos')];
64                         $albums = false;
65                         $sql_extra = "";
66                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
67
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 (?, ?)",
70                                         local_user(),
71                                         Photo::CONTACT_AVATAR,
72                                         Photo::CONTACT_BANNER
73                                 ));
74
75                                 $albums = array_column($photos, 'album');
76                         }
77
78                         if (DI::args()->getArgc() == 3) {
79                                 $album = DI::args()->getArgv()[2];
80                                 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
81                                 $sql_extra2 = "";
82                                 $path[$album] = $album;
83                         }
84
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",
89                                 local_user(),
90                                 Photo::CONTACT_AVATAR,
91                                 Photo::CONTACT_BANNER
92                         ));
93
94                         function _map_files1($rr)
95                         {
96                                 $a = DI::app();
97                                 $types = Images::supportedTypes();
98                                 $ext = $types[$rr['type']];
99                                 $filename_e = $rr['filename'];
100
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'];
104
105                                 return [
106                                         DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
107                                         $filename_e,
108                                         DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
109                                 ];
110                         }
111                         $files = array_map("_map_files1", $r);
112
113                         $tpl = Renderer::getMarkupTemplate($template_file);
114
115                         $o =  Renderer::replaceMacros($tpl, [
116                                 '$type'     => 'image',
117                                 '$path'     => $path,
118                                 '$folders'  => $albums,
119                                 '$files'    => $files,
120                                 '$cancel'   => DI::l10n()->t('Cancel'),
121                                 '$nickname' => $a->getLoggedInUserNickname(),
122                                 '$upload'   => DI::l10n()->t('Upload')
123                         ]);
124
125                         break;
126                 case "file":
127                         if (DI::args()->getArgc()==2) {
128                                 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => local_user()]);
129
130                                 function _map_files2($rr)
131                                 {
132                                         list($m1, $m2) = explode("/", $rr['filetype']);
133                                         $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
134                                         $filename_e = $rr['filename'];
135
136                                         return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
137                                 }
138                                 $files = array_map("_map_files2", $files);
139
140
141                                 $tpl = Renderer::getMarkupTemplate($template_file);
142                                 $o = Renderer::replaceMacros($tpl, [
143                                         '$type'     => 'file',
144                                         '$path'     => ['' => DI::l10n()->t('Files')],
145                                         '$folders'  => false,
146                                         '$files'    => $files,
147                                         '$cancel'   => DI::l10n()->t('Cancel'),
148                                         '$nickname' => $a->getLoggedInUserNickname(),
149                                         '$upload'   => DI::l10n()->t('Upload')
150                                 ]);
151                         }
152
153                         break;
154         }
155
156         if (!empty($_GET['mode'])) {
157                 return $o;
158         } else {
159                 System::httpExit($o);
160         }
161 }