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