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