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