]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Correct content type
[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\System;
11 use Friendica\Database\DBA;
12 use Friendica\Object\Image;
13
14 /**
15  * @param App $a
16  */
17 function fbrowser_content(App $a)
18 {
19         if (!local_user()) {
20                 killme();
21         }
22
23         if ($a->argc == 1) {
24                 killme();
25         }
26
27         $template_file = "filebrowser.tpl";
28         $mode = "";
29         if (x($_GET, 'mode')) {
30                 $mode  = "?mode=".$_GET['mode'];
31         }
32
33         switch ($a->argv[1]) {
34                 case "image":
35                         $path = [["", L10n::t("Photos")]];
36                         $albums = false;
37                         $sql_extra = "";
38                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
39
40                         if ($a->argc==2) {
41                                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' ",
42                                         intval(local_user()),
43                                         DBA::escape('Contact Photos'),
44                                         DBA::escape(L10n::t('Contact Photos'))
45                                 );
46
47                                 function _map_folder1($el)
48                                 {
49                                         return [bin2hex($el['album']),$el['album']];
50                                 };
51
52                                 $albums = array_map("_map_folder1", $albums);
53                         }
54
55                         $album = "";
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 = get_markup_template($template_file);
97
98                         $o =  replace_macros($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                                         $a = get_app();
119                                         list($m1,$m2) = explode("/", $rr['filetype']);
120                                         $filetype = ( (file_exists("images/icons/$m1.png"))?$m1:"zip");
121                                         $filename_e = $rr['filename'];
122
123                                         return [System::baseUrl() . '/attach/' . $rr['id'], $filename_e, System::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
124                                 }
125                                 $files = array_map("_map_files2", $files);
126
127
128                                 $tpl = get_markup_template($template_file);
129                                 $o = replace_macros($tpl, [
130                                         '$type'     => 'file',
131                                         '$baseurl'  => System::baseUrl(),
132                                         '$path'     => [ [ "", L10n::t("Files")] ],
133                                         '$folders'  => false,
134                                         '$files'    => $files,
135                                         '$cancel'   => L10n::t('Cancel'),
136                                         '$nickname' => $a->user['nickname'],
137                                         '$upload'   => L10n::t('Upload')
138                                 ]);
139                         }
140
141                         break;
142         }
143
144         if (x($_GET, 'mode')) {
145                 return $o;
146         } else {
147                 echo $o;
148                 killme();
149         }
150 }