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