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