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