]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Merge pull request #10828 from nupplaphil/bug/photo_calls
[friendica.git] / mod / fbrowser.php
1 <?php
2 /**
3  * @package             Friendica\modules
4  * @subpackage  FileBrowser
5  * @author              Fabio Comuni <fabrixxm@kirgroup.com>
6  */
7
8 use Friendica\App;
9 use Friendica\Core\Renderer;
10 use Friendica\Database\DBA;
11 use Friendica\DI;
12 use Friendica\Model\Photo;
13 use Friendica\Util\Images;
14 use Friendica\Util\Strings;
15
16 /**
17  * @param App $a
18  * @return string
19  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
20  */
21 function fbrowser_content(App $a)
22 {
23         if (!local_user()) {
24                 exit();
25         }
26
27         if (DI::args()->getArgc() == 1) {
28                 exit();
29         }
30
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);
35         }
36
37         $template_file = "filebrowser.tpl";
38
39         $o = '';
40
41         switch (DI::args()->getArgv()[1]) {
42                 case "image":
43                         $path = ['' => DI::l10n()->t('Photos')];
44                         $albums = false;
45                         $sql_extra = "";
46                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
47
48                         if (DI::args()->getArgc()==2) {
49                                 $photos = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' ",
50                                         intval(local_user()),
51                                         DBA::escape(Photo::CONTACT_PHOTOS),
52                                         DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS))
53                                 );
54
55                                 $albums = array_column($photos, 'album');
56                         }
57
58                         if (DI::args()->getArgc() == 3) {
59                                 $album = DI::args()->getArgv()[2];
60                                 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
61                                 $sql_extra2 = "";
62                                 $path[$album] = $album;
63                         }
64
65                         $r = q("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` = %d $sql_extra AND `album` != '%s' AND `album` != '%s'
68                                         GROUP BY `resource-id` $sql_extra2",
69                                 intval(local_user()),
70                                 DBA::escape(Photo::CONTACT_PHOTOS),
71                                 DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS))
72                         );
73
74                         function _map_files1($rr)
75                         {
76                                 $a = DI::app();
77                                 $types = Images::supportedTypes();
78                                 $ext = $types[$rr['type']];
79                                 $filename_e = $rr['filename'];
80
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'];
84
85                                 return [
86                                         DI::baseUrl() . '/photos/' . $a->getLoggedInUserNickname() . '/image/' . $rr['resource-id'],
87                                         $filename_e,
88                                         DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
89                                 ];
90                         }
91                         $files = array_map("_map_files1", $r);
92
93                         $tpl = Renderer::getMarkupTemplate($template_file);
94
95                         $o =  Renderer::replaceMacros($tpl, [
96                                 '$type'     => 'image',
97                                 '$path'     => $path,
98                                 '$folders'  => $albums,
99                                 '$files'    => $files,
100                                 '$cancel'   => DI::l10n()->t('Cancel'),
101                                 '$nickname' => $a->getLoggedInUserNickname(),
102                                 '$upload'   => DI::l10n()->t('Upload')
103                         ]);
104
105                         break;
106                 case "file":
107                         if (DI::args()->getArgc()==2) {
108                                 $files = DBA::selectToArray('attach', ['id', 'filename', 'filetype'], ['uid' => local_user()]);
109
110                                 function _map_files2($rr)
111                                 {
112                                         list($m1, $m2) = explode("/", $rr['filetype']);
113                                         $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
114                                         $filename_e = $rr['filename'];
115
116                                         return [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
117                                 }
118                                 $files = array_map("_map_files2", $files);
119
120
121                                 $tpl = Renderer::getMarkupTemplate($template_file);
122                                 $o = Renderer::replaceMacros($tpl, [
123                                         '$type'     => 'file',
124                                         '$path'     => [ [ "", DI::l10n()->t("Files")] ],
125                                         '$folders'  => false,
126                                         '$files'    => $files,
127                                         '$cancel'   => DI::l10n()->t('Cancel'),
128                                         '$nickname' => $a->getLoggedInUserNickname(),
129                                         '$upload'   => DI::l10n()->t('Upload')
130                                 ]);
131                         }
132
133                         break;
134         }
135
136         if (!empty($_GET['mode'])) {
137                 return $o;
138         } else {
139                 echo $o;
140                 exit();
141         }
142 }