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