]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
New class "System"
[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
193         $limit = service_class_fetch($page_owner_uid, 'photo_upload_limit');
194
195         if ($limit) {
196                 $r = q("SELECT SUM(OCTET_LENGTH(`data`)) AS `total` FROM `photo`
197                         WHERE `uid` = %d AND `scale` = 0
198                         AND `album` != 'Contact Photos' ",
199                         intval($page_owner_uid)
200                 );
201                 $size = $r[0]['total'];
202
203                 if (($size + strlen($imagedata)) > $limit) {
204                         $msg = upgrade_message(true);
205                         if ($r_json) {
206                                 echo json_encode(array('error'=>$msg));
207                         } else {
208                                 echo  $msg. EOL;
209                         }
210                         @unlink($src);
211                         killme();
212                 }
213         }
214
215         $imagedata = @file_get_contents($src);
216         $ph = new Photo($imagedata, $filetype);
217
218         if (! $ph->is_valid()) {
219                 $msg = t('Unable to process image.');
220                 if ($r_json) {
221                         echo json_encode(array('error'=>$msg));
222                 } else {
223                         echo  $msg. EOL;
224                 }
225                 @unlink($src);
226                 killme();
227         }
228
229         $ph->orient($src);
230         @unlink($src);
231
232         $max_length = Config::get('system', 'max_image_length');
233         if (! $max_length) {
234                 $max_length = MAX_IMAGE_LENGTH;
235         }
236         if ($max_length > 0) {
237                 $ph->scaleImage($max_length);
238                 logger("File upload: Scaling picture to new size " . $max_length, LOGGER_DEBUG);
239         }
240
241         $width = $ph->getWidth();
242         $height = $ph->getHeight();
243
244         $hash = photo_new_resource();
245
246         $smallest = 0;
247
248         // If we don't have an album name use the Wall Photos album
249         if (! strlen($album)) {
250                 $album = t('Wall Photos');
251         }
252
253         $defperm = '<' . $default_cid . '>';
254
255         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 0, 0, $defperm);
256
257         if (! $r) {
258                 $msg = t('Image upload failed.');
259                 if ($r_json) {
260                         echo json_encode(array('error'=>$msg));
261                 } else {
262                         echo  $msg. EOL;
263                 }
264                 killme();
265         }
266
267         if ($width > 640 || $height > 640) {
268                 $ph->scaleImage(640);
269                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 1, 0, $defperm);
270                 if ($r) {
271                         $smallest = 1;
272                 }
273         }
274
275         if ($width > 320 || $height > 320) {
276                 $ph->scaleImage(320);
277                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, $album, 2, 0, $defperm);
278                 if ($r && ($smallest == 0)) {
279                         $smallest = 2;
280                 }
281         }
282
283         $basename = basename($filename);
284
285         if (!$desktopmode) {
286                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo`
287                         WHERE `resource-id` = '%s'
288                         ORDER BY `width` DESC LIMIT 1",
289                         $hash
290                 );
291                 if (!$r) {
292                         if ($r_json) {
293                                 echo json_encode(array('error'=>''));
294                                 killme();
295                         }
296                         return false;
297                 }
298                 $picture = array();
299
300                 $picture["id"]        = $r[0]["id"];
301                 $picture["size"]      = $r[0]["datasize"];
302                 $picture["width"]     = $r[0]["width"];
303                 $picture["height"]    = $r[0]["height"];
304                 $picture["type"]      = $r[0]["type"];
305                 $picture["albumpage"] = App::get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash;
306                 $picture["picture"]   = App::get_baseurl() . "/photo/{$hash}-0." . $ph->getExt();
307                 $picture["preview"]   = App::get_baseurl() . "/photo/{$hash}-{$smallest}." . $ph->getExt();
308
309                 if ($r_json) {
310                         echo json_encode(array('picture'=>$picture));
311                         killme();
312                 }
313                 return $picture;
314         }
315
316
317         if ($r_json) {
318                 echo json_encode(array('ok'=>true));
319                 killme();
320         }
321
322 /* mod Waitman Gobble NO WARRANTY */
323         // if we get the signal then return the image url info in BBCODE
324         if ($_REQUEST['hush']!='yeah') {
325                 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";
326         } else {
327                 $m = '[url='.App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
328                 return($m);
329         }
330 /* mod Waitman Gobble NO WARRANTY */
331
332         killme();
333         // NOTREACHED
334 }