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