]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge branch 'develop' into rewrites/dbm_is_result
[friendica.git] / mod / wall_upload.php
1 <?php
2
3 require_once('include/Photo.php');
4
5 function wall_upload_post(&$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         } else
170                 $size = 0;
171
172         if(($limit !== false) && (($size + strlen($imagedata)) > $limit)) {
173                 $msg = upgrade_message(true);
174                 if ($r_json) {
175                         echo json_encode(array('error'=>$msg));
176                 } else {
177                         echo  $msg. EOL;
178                 }
179                 @unlink($src);
180                 killme();
181         }
182
183
184         $imagedata = @file_get_contents($src);
185         $ph = new Photo($imagedata, $filetype);
186
187         if(! $ph->is_valid()) {
188                 $msg = t('Unable to process image.');
189                 if ($r_json) {
190                         echo json_encode(array('error'=>$msg));
191                 } else {
192                         echo  $msg. EOL;
193                 }
194                 @unlink($src);
195                 killme();
196         }
197
198         $ph->orient($src);
199         @unlink($src);
200
201         $max_length = get_config('system','max_image_length');
202         if(! $max_length)
203                 $max_length = MAX_IMAGE_LENGTH;
204         if($max_length > 0) {
205                 $ph->scaleImage($max_length);
206                 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
207         }
208
209         $width = $ph->getWidth();
210         $height = $ph->getHeight();
211
212         $hash = photo_new_resource();
213
214         $smallest = 0;
215
216         $defperm = '<' . $default_cid . '>';
217
218         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
219
220         if(! $r) {
221                 $msg = t('Image upload failed.');
222                 if ($r_json) {
223                         echo json_encode(array('error'=>$msg));
224                 } else {
225                         echo  $msg. EOL;
226                 }
227                 killme();
228         }
229
230         if($width > 640 || $height > 640) {
231                 $ph->scaleImage(640);
232                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
233                 if($r)
234                         $smallest = 1;
235         }
236
237         if($width > 320 || $height > 320) {
238                 $ph->scaleImage(320);
239                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
240                 if($r AND ($smallest == 0))
241                         $smallest = 2;
242         }
243
244         $basename = basename($filename);
245
246         if (!$desktopmode) {
247
248                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo` WHERE `resource-id` = '%s' ORDER BY `width` DESC LIMIT 1", $hash);
249                 if (!$r){
250                         if ($r_json) {
251                                 echo json_encode(array('error'=>''));
252                                 killme();
253                         }
254                         return false;
255                 }
256                 $picture = array();
257
258                 $picture["id"] = $r[0]["id"];
259                 $picture["size"] = $r[0]["datasize"];
260                 $picture["width"] = $r[0]["width"];
261                 $picture["height"] = $r[0]["height"];
262                 $picture["type"] = $r[0]["type"];
263                 $picture["albumpage"] = App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash;
264                 $picture["picture"] = App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
265                 $picture["preview"] = App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt();
266
267                 if ($r_json) {
268                         echo json_encode(array('picture'=>$picture));
269                         killme();
270                 }
271                 return $picture;
272         }
273
274
275         if ($r_json) {
276                 echo json_encode(array('ok'=>true));
277                 killme();
278         }
279
280 /* mod Waitman Gobble NO WARRANTY */
281
282 //if we get the signal then return the image url info in BBCODE, otherwise this outputs the info and bails (for the ajax image uploader on wall post)
283         if ($_REQUEST['hush']!='yeah') {
284                 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
285                         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";
286                 }
287                 else {
288                         echo  '<br /><br /><a href="' . App::get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '" ><img src="' . App::get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."\" alt=\"$basename\" /></a><br /><br />";
289                 }
290         }
291         else {
292                 $m = '[url='.App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.App::get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
293                 return($m);
294         }
295 /* mod Waitman Gobble NO WARRANTY */
296
297         killme();
298         // NOTREACHED
299 }