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