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