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