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