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