]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Reworked "remote" cookie handling
[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         } elseif ($community_page && !empty(remote_user($page_owner_uid))) {
78                 $contact_id = remote_user($page_owner_uid);
79
80                 $r = q("SELECT `uid` FROM `contact`
81                         WHERE `blocked` = 0 AND `pending` = 0
82                         AND `id` = %d AND `uid` = %d LIMIT 1",
83                         intval($contact_id),
84                         intval($page_owner_uid)
85                 );
86                 if (DBA::isResult($r)) {
87                         $can_post = true;
88                         $visitor = $contact_id;
89                 }
90         }
91
92         if (!$can_post) {
93                 if ($r_json) {
94                         echo json_encode(['error' => L10n::t('Permission denied.')]);
95                         exit();
96                 }
97                 notice(L10n::t('Permission denied.') . EOL);
98                 exit();
99         }
100
101         if (empty($_FILES['userfile']) && empty($_FILES['media'])) {
102                 if ($r_json) {
103                         echo json_encode(['error' => L10n::t('Invalid request.')]);
104                 }
105                 exit();
106         }
107
108         $src = '';
109         $filename = '';
110         $filesize = 0;
111         $filetype = '';
112         if (!empty($_FILES['userfile'])) {
113                 $src      = $_FILES['userfile']['tmp_name'];
114                 $filename = basename($_FILES['userfile']['name']);
115                 $filesize = intval($_FILES['userfile']['size']);
116                 $filetype = $_FILES['userfile']['type'];
117
118         } elseif (!empty($_FILES['media'])) {
119                 if (!empty($_FILES['media']['tmp_name'])) {
120                         if (is_array($_FILES['media']['tmp_name'])) {
121                                 $src = $_FILES['media']['tmp_name'][0];
122                         } else {
123                                 $src = $_FILES['media']['tmp_name'];
124                         }
125                 }
126
127                 if (!empty($_FILES['media']['name'])) {
128                         if (is_array($_FILES['media']['name'])) {
129                                 $filename = basename($_FILES['media']['name'][0]);
130                         } else {
131                                 $filename = basename($_FILES['media']['name']);
132                         }
133                 }
134
135                 if (!empty($_FILES['media']['size'])) {
136                         if (is_array($_FILES['media']['size'])) {
137                                 $filesize = intval($_FILES['media']['size'][0]);
138                         } else {
139                                 $filesize = intval($_FILES['media']['size']);
140                         }
141                 }
142
143                 if (!empty($_FILES['media']['type'])) {
144                         if (is_array($_FILES['media']['type'])) {
145                                 $filetype = $_FILES['media']['type'][0];
146                         } else {
147                                 $filetype = $_FILES['media']['type'];
148                         }
149                 }
150         }
151
152         if ($src == "") {
153                 if ($r_json) {
154                         echo json_encode(['error' => L10n::t('Invalid request.')]);
155                         exit();
156                 }
157                 notice(L10n::t('Invalid request.').EOL);
158                 exit();
159         }
160
161         // This is a special treatment for picture upload from Twidere
162         if (($filename == "octet-stream") && ($filetype != "")) {
163                 $filename = $filetype;
164                 $filetype = "";
165         }
166
167         if ($filetype == "") {
168                 $filetype = Image::guessType($filename);
169         }
170
171         // If there is a temp name, then do a manual check
172         // This is more reliable than the provided value
173
174         $imagedata = getimagesize($src);
175         if ($imagedata) {
176                 $filetype = $imagedata['mime'];
177         }
178
179         Logger::log("File upload src: " . $src . " - filename: " . $filename .
180                 " - size: " . $filesize . " - type: " . $filetype, Logger::DEBUG);
181
182         $maximagesize = Config::get('system', 'maximagesize');
183
184         if (($maximagesize) && ($filesize > $maximagesize)) {
185                 $msg = L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize));
186                 if ($r_json) {
187                         echo json_encode(['error' => $msg]);
188                 } else {
189                         echo  $msg. EOL;
190                 }
191                 @unlink($src);
192                 exit();
193         }
194
195         $imagedata = @file_get_contents($src);
196         $Image = new Image($imagedata, $filetype);
197
198         if (!$Image->isValid()) {
199                 $msg = L10n::t('Unable to process image.');
200                 if ($r_json) {
201                         echo json_encode(['error' => $msg]);
202                 } else {
203                         echo  $msg. EOL;
204                 }
205                 @unlink($src);
206                 exit();
207         }
208
209         $Image->orient($src);
210         @unlink($src);
211
212         $max_length = Config::get('system', 'max_image_length');
213         if (!$max_length) {
214                 $max_length = MAX_IMAGE_LENGTH;
215         }
216         if ($max_length > 0) {
217                 $Image->scaleDown($max_length);
218                 Logger::log("File upload: Scaling picture to new size " . $max_length, Logger::DEBUG);
219         }
220
221         $width = $Image->getWidth();
222         $height = $Image->getHeight();
223
224         $hash = Photo::newResource();
225
226         $smallest = 0;
227
228         // If we don't have an album name use the Wall Photos album
229         if (!strlen($album)) {
230                 $album = L10n::t('Wall Photos');
231         }
232
233         $defperm = '<' . $default_cid . '>';
234
235         $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
236
237         if (!$r) {
238                 $msg = L10n::t('Image upload failed.');
239                 if ($r_json) {
240                         echo json_encode(['error' => $msg]);
241                 } else {
242                         echo  $msg. EOL;
243                 }
244                 exit();
245         }
246
247         if ($width > 640 || $height > 640) {
248                 $Image->scaleDown(640);
249                 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
250                 if ($r) {
251                         $smallest = 1;
252                 }
253         }
254
255         if ($width > 320 || $height > 320) {
256                 $Image->scaleDown(320);
257                 $r = Photo::store($Image, $page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
258                 if ($r && ($smallest == 0)) {
259                         $smallest = 2;
260                 }
261         }
262
263         if (!$desktopmode) {
264                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
265                         WHERE `resource-id` = '%s'
266                         ORDER BY `width` DESC LIMIT 1",
267                         $hash
268                 );
269                 if (!$r) {
270                         if ($r_json) {
271                                 echo json_encode(['error' => '']);
272                                 exit();
273                         }
274                         return false;
275                 }
276                 $picture = [];
277
278                 $picture["id"]        = $r[0]["id"];
279                 $picture["size"]      = $r[0]["datasize"];
280                 $picture["width"]     = $r[0]["width"];
281                 $picture["height"]    = $r[0]["height"];
282                 $picture["type"]      = $r[0]["type"];
283                 $picture["albumpage"] = System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
284                 $picture["picture"]   = System::baseUrl() . "/photo/{$hash}-0." . $Image->getExt();
285                 $picture["preview"]   = System::baseUrl() . "/photo/{$hash}-{$smallest}." . $Image->getExt();
286
287                 if ($r_json) {
288                         echo json_encode(['picture' => $picture]);
289                         exit();
290                 }
291                 Logger::log("upload done", Logger::DEBUG);
292                 return $picture;
293         }
294
295         Logger::log("upload done", Logger::DEBUG);
296
297         if ($r_json) {
298                 echo json_encode(['ok' => true]);
299                 exit();
300         }
301
302         echo  "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . System::baseUrl() . "/photo/{$hash}-{$smallest}.".$Image->getExt()."[/img][/url]\n\n";
303         exit();
304         // NOTREACHED
305 }