]> git.mxchange.org Git - friendica.git/blob - mod/fbrowser.php
Added documentation
[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\Renderer;
10 use Friendica\Database\DBA;
11 use Friendica\DI;
12 use Friendica\Model\Photo;
13 use Friendica\Util\Images;
14 use Friendica\Util\Strings;
15
16 /**
17  * @param App $a
18  * @return string
19  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
20  */
21 function fbrowser_content(App $a)
22 {
23         if (!local_user()) {
24                 exit();
25         }
26
27         if ($a->argc == 1) {
28                 exit();
29         }
30
31         // Needed to match the correct template in a module that uses a different theme than the user/site/default
32         $theme = Strings::sanitizeFilePathItem($_GET['theme'] ?? null);
33         if ($theme && is_file("view/theme/$theme/config.php")) {
34                 $a->setCurrentTheme($theme);
35         }
36
37         $template_file = "filebrowser.tpl";
38
39         $o = '';
40
41         switch ($a->argv[1]) {
42                 case "image":
43                         $path = ['' => DI::l10n()->t('Photos')];
44                         $albums = false;
45                         $sql_extra = "";
46                         $sql_extra2 = " ORDER BY created DESC LIMIT 0, 10";
47
48                         if ($a->argc==2) {
49                                 $photos = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' ",
50                                         intval(local_user()),
51                                         DBA::escape(Photo::CONTACT_PHOTOS),
52                                         DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS))
53                                 );
54
55                                 $albums = array_column($photos, 'album');
56                         }
57
58                         if ($a->argc == 3) {
59                                 $album = $a->argv[2];
60                                 $sql_extra = sprintf("AND `album` = '%s' ", DBA::escape($album));
61                                 $sql_extra2 = "";
62                                 $path[$album] = $album;
63                         }
64
65                         $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`type`) AS `type`,
66                                         min(`scale`) AS `hiq`, max(`scale`) AS `loq`, ANY_VALUE(`desc`) AS `desc`, ANY_VALUE(`created`) AS `created`
67                                         FROM `photo` WHERE `uid` = %d $sql_extra AND `album` != '%s' AND `album` != '%s'
68                                         GROUP BY `resource-id` $sql_extra2",
69                                 intval(local_user()),
70                                 DBA::escape(Photo::CONTACT_PHOTOS),
71                                 DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS))
72                         );
73
74                         function _map_files1($rr)
75                         {
76                                 $a = DI::app();
77                                 $types = Images::supportedTypes();
78                                 $ext = $types[$rr['type']];
79                                 $filename_e = $rr['filename'];
80
81                                 // Take the largest picture that is smaller or equal 640 pixels
82                                 $p = q("SELECT `scale` FROM `photo` WHERE `resource-id` = '%s' AND `height` <= 640 AND `width` <= 640 ORDER BY `resource-id`, `scale` LIMIT 1",
83                                         DBA::escape($rr['resource-id']));
84                                 if ($p) {
85                                         $scale = $p[0]["scale"];
86                                 } else {
87                                         $scale = $rr['loq'];
88                                 }
89
90                                 return [
91                                         DI::baseUrl() . '/photos/' . $a->user['nickname'] . '/image/' . $rr['resource-id'],
92                                         $filename_e,
93                                         DI::baseUrl() . '/photo/' . $rr['resource-id'] . '-' . $scale . '.'. $ext
94                                 ];
95                         }
96                         $files = array_map("_map_files1", $r);
97
98                         $tpl = Renderer::getMarkupTemplate($template_file);
99
100                         $o =  Renderer::replaceMacros($tpl, [
101                                 '$type'     => 'image',
102                                 '$path'     => $path,
103                                 '$folders'  => $albums,
104                                 '$files'    => $files,
105                                 '$cancel'   => DI::l10n()->t('Cancel'),
106                                 '$nickname' => $a->user['nickname'],
107                                 '$upload'   => DI::l10n()->t('Upload')
108                         ]);
109
110                         break;
111                 case "file":
112                         if ($a->argc==2) {
113                                 $files = q("SELECT `id`, `filename`, `filetype` FROM `attach` WHERE `uid` = %d ",
114                                         intval(local_user())
115                                 );
116
117                                 function _map_files2($rr)
118                                 {
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 [DI::baseUrl() . '/attach/' . $rr['id'], $filename_e, DI::baseUrl() . '/images/icons/16/' . $filetype . '.png'];
124                                 }
125                                 $files = array_map("_map_files2", $files);
126
127
128                                 $tpl = Renderer::getMarkupTemplate($template_file);
129                                 $o = Renderer::replaceMacros($tpl, [
130                                         '$type'     => 'file',
131                                         '$path'     => [ [ "", DI::l10n()->t("Files")] ],
132                                         '$folders'  => false,
133                                         '$files'    => $files,
134                                         '$cancel'   => DI::l10n()->t('Cancel'),
135                                         '$nickname' => $a->user['nickname'],
136                                         '$upload'   => DI::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 }