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