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