]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / wall_upload.php
1 <?php
2 /**
3  * @file mod/wall_upload.php
4  * @brief Module for uploading a picture to the profile wall
5  *
6  * By default the picture will be stored in the photo album with the name Wall Photos.
7  * You can specify a different album by adding an optional query string "album="
8  * to the url
9  */
10
11 use Friendica\App;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\System;
15 use Friendica\Core\Config;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\Photo;
19 use Friendica\Model\User;
20 use Friendica\Object\Image;
21 use Friendica\Util\Strings;
22
23 function wall_upload_post(App $a, $desktopmode = true)
24 {
25         Logger::log("wall upload: starting new upload", Logger::DEBUG);
26
27         $r_json = (!empty($_GET['response']) && $_GET['response'] == 'json');
28         $album = (!empty($_GET['album']) ? Strings::escapeTags(trim($_GET['album'])) : '');
29
30         if ($a->argc > 1) {
31                 if (empty($_FILES['media'])) {
32                         $nick = $a->argv[1];
33                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
34                                 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
35                                 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
36                                 AND `contact`.`self` = 1 LIMIT 1",
37                                 DBA::escape($nick)
38                         );
39
40                         if (!DBA::isResult($r)) {
41                                 if ($r_json) {
42                                         echo json_encode(['error' => L10n::t('Invalid request.')]);
43                                         exit();
44                                 }
45                                 return;
46                         }
47                 } else {
48                         $user_info = api_get_user($a);
49                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user`
50                                 INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
51                                 WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0
52                                 AND `contact`.`self` = 1 LIMIT 1",
53                                 DBA::escape($user_info['screen_name'])
54                         );
55                 }
56         } else {
57                 if ($r_json) {
58                         echo json_encode(['error' => L10n::t('Invalid request.')]);
59                         exit();
60                 }
61                 return;
62         }
63
64         /*
65          * Setup permissions structures
66          */
67         $can_post  = false;
68         $visitor   = 0;
69
70         $page_owner_uid   = $r[0]['uid'];
71         $default_cid      = $r[0]['id'];
72         $page_owner_nick  = $r[0]['nickname'];
73         $community_page   = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
74
75         if ((local_user()) && (local_user() == $page_owner_uid)) {
76                 $can_post = true;
77         } else {
78                 if ($community_page && remote_user()) {
79                         $contact_id = 0;
80                         if (is_array($_SESSION['remote'])) {
81                                 foreach ($_SESSION['remote'] as $v) {
82                                         if ($v['uid'] == $page_owner_uid) {
83                                                 $contact_id = $v['cid'];
84                                                 break;
85                                         }
86                                 }
87                         }
88
89                         if ($contact_id) {
90                                 $r = q("SELECT `uid` FROM `contact`
91                                         WHERE `blocked` = 0 AND `pending` = 0
92                                         AND `id` = %d AND `uid` = %d LIMIT 1",
93                                         intval($contact_id),
94                                         intval($page_owner_uid)
95                                 );
96                                 if (DBA::isResult($r)) {
97                                         $can_post = true;
98                                         $visitor = $contact_id;
99                                 }
100                         }
101                 }
102         }
103
104
105         if (!$can_post) {
106                 if ($r_json) {
107                         echo json_encode(['error' => L10n::t('Permission denied.')]);
108                         exit();
109                 }
110                 notice(L10n::t('Permission denied.') . EOL);
111                 exit();
112         }
113
114         if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
115                 if ($r_json) {
116                         echo json_encode(['error' => L10n::t('Invalid request.')]);
117                 }
118                 exit();
119         }
120
121         $src = '';
122         $filename = '';
123         $filesize = 0;
124         $filetype = '';
125         if (!empty($_FILES['userfile'])) {
126                 $src      = $_FILES['userfile']['tmp_name'];
127                 $filename = basename($_FILES['userfile']['name']);
128                 $filesize = intval($_FILES['userfile']['size']);
129                 $filetype = $_FILES['userfile']['type'];
130
131         } elseif (!empty($_FILES['media'])) {
132                 if (!empty($_FILES['media']['tmp_name'])) {
133                         if (is_array($_FILES['media']['tmp_name'])) {
134                                 $src = $_FILES['media']['tmp_name'][0];
135                         } else {
136                                 $src = $_FILES['media']['tmp_name'];
137                         }
138                 }
139
140                 if (!empty($_FILES['media']['name'])) {
141                         if (is_array($_FILES['media']['name'])) {
142                                 $filename = basename($_FILES['media']['name'][0]);
143                         } else {
144                                 $filename = basename($_FILES['media']['name']);
145                         }
146                 }
147
148                 if (!empty($_FILES['media']['size'])) {
149                         if (is_array($_FILES['media']['size'])) {
150                                 $filesize = intval($_FILES['media']['size'][0]);
151                         } else {
152                                 $filesize = intval($_FILES['media']['size']);
153                         }
154                 }
155
156                 if (!empty($_FILES['media']['type'])) {
157                         if (is_array($_FILES['media']['type'])) {
158                                 $filetype = $_FILES['media']['type'][0];
159                         } else {
160                                 $filetype = $_FILES['media']['type'];
161                         }
162                 }
163         }
164
165         if ($src == "") {
166                 if ($r_json) {
167                         echo json_encode(['error' => L10n::t('Invalid request.')]);
168                         exit();
169                 }
170                 notice(L10n::t('Invalid request.').EOL);
171                 exit();
172         }
173
174         // This is a special treatment for picture upload from Twidere
175         if (($filename == "octet-stream") && ($filetype != "")) {
176                 $filename = $filetype;
177                 $filetype = "";
178         }
179
180         if ($filetype == "") {
181                 $filetype = Image::guessType($filename);
182         }
183
184         // If there is a temp name, then do a manual check
185         // This is more reliable than the provided value
186
187         $imagedata = getimagesize($src);
188         if ($imagedata) {
189                 $filetype = $imagedata['mime'];
190         }
191
192         Logger::log("File upload src: " . $src . " - filename: " . $filename .
193                 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
194
195         $maximagesize = Config::get('system', 'maximagesize');
196
197         if (($maximagesize) && ($filesize > $maximagesize)) {
198                 $msg = L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
199                 if ($r_json) {
200                         echo json_encode(['error' => $msg]);
201                 } else {
202                         echo  $msg. EOL;
203                 }
204                 @unlink($src);
205                 exit();
206         }
207
208         $imagedata = @file_get_contents($src);
209         $Image = new Image($imagedata, $filetype);
210
211         if (!$Image->isValid()) {
212                 $msg = L10n::t('Unable to process image.');
213                 if ($r_json) {
214                         echo json_encode(['error' => $msg]);
215                 } else {
216                         echo  $msg. EOL;
217                 }
218                 @unlink($src);
219                 exit();
220         }
221
222         $Image->orient($src);
223         @unlink($src);
224
225         $max_length = Config::get('system', 'max_image_length');
226         if (!$max_length) {
227                 $max_length = MAX_IMAGE_LENGTH;
228         }
229         if ($max_length > 0) {
230                 $Image->scaleDown($max_length);
231                 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
232         }
233
234         $width = $Image->getWidth();
235         $height = $Image->getHeight();
236
237         $hash = Photo::newResource();
238
239         $smallest = 0;
240
241         // If we don't have an album name use the Wall Photos album
242         if (!strlen($album)) {
243                 $album = L10n::t('Wall Photos');
244         }
245
246         $defperm = '<' . $default_cid . '>';
247
248         $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
249
250         if (!$r) {
251                 $msg = L10n::t('Image upload failed.');
252                 if ($r_json) {
253                         echo json_encode(['error' => $msg]);
254                 } else {
255                         echo  $msg. EOL;
256                 }
257                 exit();
258         }
259
260         if ($width > 640 || $height > 640) {
261                 $Image->scaleDown(640);
262                 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
263                 if ($r) {
264                         $smallest = 1;
265                 }
266         }
267
268         if ($width > 320 || $height > 320) {
269                 $Image->scaleDown(320);
270                 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
271                 if ($r && ($smallest == 0)) {
272                         $smallest = 2;
273                 }
274         }
275
276         if (!$desktopmode) {
277                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
278                         WHERE `resource-id` = '%s'
279                         ORDER BY `width` DESC LIMIT 1",
280                         $hash
281                 );
282                 if (!$r) {
283                         if ($r_json) {
284                                 echo json_encode(['error' => '']);
285                                 exit();
286                         }
287                         return false;
288                 }
289                 $picture = [];
290
291                 $picture["id"]        = $r[0]["id"];
292                 $picture["size"]      = $r[0]["datasize"];
293                 $picture["width"]     = $r[0]["width"];
294                 $picture["height"]    = $r[0]["height"];
295                 $picture["type"]      = $r[0]["type"];
296                 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
297                 $picture["picture"]   = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
298                 $picture["preview"]   = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
299
300                 if ($r_json) {
301                         echo json_encode(['picture' => $picture]);
302                         exit();
303                 }
304                 Logger::log("upload done", Logger::DEBUG);
305                 return $picture;
306         }
307
308         Logger::log("upload done", Logger::DEBUG);
309
310         if ($r_json) {
311                 echo json_encode(['ok' => true]);
312                 exit();
313         }
314
315         echo  "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";
316         exit();
317         // NOTREACHED
318 }