]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Don't process empty hash tags in Model\Item
[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\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
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 ($a->argc == 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 ($a->argv[1]) {
42                 case "image":
43                         $path = [["", L10n::t("Photos")]];
44                         $albums = false;
45                         $sql_extra = "";
46                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
47
48                         if ($a->argc==2) {
49                                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' ",
50                                         intval(local_user()),
51                                         DBA::escape('Contact Photos'),
52                                         DBA::escape(L10n::t('Contact Photos'))
53                                 );
54
55                                 function _map_folder1($el)
56                                 {
57                                         return [bin2hex($el['album']),$el['album']];
58                                 };
59
60                                 $albums = array_map("_map_folder1", $albums);
61                         }
62
63                         if ($a->argc == 3) {
64                                 $album = hex2bin($a->argv[2]);
65                                 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
66                                 $sql_extra2 = "";
67                                 $path[] = [$a->argv[2], $album];
68                         }
69
70                         $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
71                                         min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
72                                         FROM `photo` WHERE `uid` = %d $sql_extra AND `album` != '%s' AND `album` != '%s'
73                                         GROUP BY `resource-id` $sql_extra2",
74                                 intval(local_user()),
75                                 DBA::escape('Contact Photos'),
76                                 DBA::escape(L10n::t('Contact Photos'))
77                         );
78
79                         function _map_files1($rr)
80                         {
81                                 $a = \get_app();
82                                 $types = Images::supportedTypes();
83                                 $ext = $types[$rr['type']];
84                                 $filename_e = $rr['filename'];
85
86                                 // Take the largest picture that is smaller or equal 640 pixels
87                                 $p = q("SELECT `scale` FROM `photo` WHERE `resource-id` = '%s' AND `height` <= 640 AND `width` <= 640 ORDER BY `resource-id`, `scale` LIMIT 1",
88                                         DBA::escape($rr['resource-id']));
89                                 if ($p) {
90                                         $scale = $p[0]["scale"];
91                                 } else {
92                                         $scale = $rr['loq'];
93                                 }
94
95                                 return [
96                                         System::baseUrl() . '/photos/' . $a->user['nickname'] . '/image/' . $rr['resource-id'],
97                                         $filename_e,
98                                         System::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
99                                 ];
100                         }
101                         $files = array_map("_map_files1", $r);
102
103                         $tpl = Renderer::getMarkupTemplate($template_file);
104
105                         $o =  Renderer::replaceMacros($tpl, [
106                                 '$type'     => 'image',
107                                 '$path'     => $path,
108                                 '$folders'  => $albums,
109                                 '$files'    => $files,
110                                 '$cancel'   => L10n::t('Cancel'),
111                                 '$nickname' => $a->user['nickname'],
112                                 '$upload'   => L10n::t('Upload')
113                         ]);
114
115                         break;
116                 case "file":
117                         if ($a->argc==2) {
118                                 $files = q("SELECT `id`, `filename`, `filetype` FROM `attach` WHERE `uid` = %d ",
119                                         intval(local_user())
120                                 );
121
122                                 function _map_files2($rr)
123                                 {
124                                         list($m1, $m2) = explode("/", $rr['filetype']);
125                                         $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
126                                         $filename_e = $rr['filename'];
127
128                                         return [System::baseUrl() . '/attach/' . $rr['id'], $filename_e, System::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
129                                 }
130                                 $files = array_map("_map_files2", $files);
131
132
133                                 $tpl = Renderer::getMarkupTemplate($template_file);
134                                 $o = Renderer::replaceMacros($tpl, [
135                                         '$type'     => 'file',
136                                         '$path'     => [ [ "", L10n::t("Files")] ],
137                                         '$folders'  => false,
138                                         '$files'    => $files,
139                                         '$cancel'   => L10n::t('Cancel'),
140                                         '$nickname' => $a->user['nickname'],
141                                         '$upload'   => L10n::t('Upload')
142                                 ]);
143                         }
144
145                         break;
146         }
147
148         if (!empty($_GET['mode'])) {
149                 return $o;
150         } else {
151                 echo $o;
152                 exit();
153         }
154 }