]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
DE update to the strings
[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(! count($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                         $cid = 0;
52                         if(is_array($_SESSION['remote'])) {
53                                 foreach($_SESSION['remote'] as $v) {
54                                         if($v['uid'] == $page_owner_uid) {
55                                                 $cid = $v['cid'];
56                                                 break;
57                                         }
58                                 }
59                         }
60                         if($cid) {
61
62                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
63                                         intval($cid),
64                                         intval($page_owner_uid)
65                                 );
66                                 if(count($r)) {
67                                         $can_post = true;
68                                         $visitor = $cid;
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         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
162                 intval($page_owner_uid)
163         );
164
165         $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
166
167         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
168                 $msg = upgrade_message(true);
169                 if ($r_json) {
170                         echo json_encode(array('error'=>$msg));
171                 } else {
172                         echo  $msg. EOL;
173                 }
174                 @unlink($src);
175                 killme();
176         }
177
178
179         $imagedata = @file_get_contents($src);
180         $ph = new Photo($imagedata, $filetype);
181
182         if(! $ph->is_valid()) {
183                 $msg = t('Unable to process image.');
184                 if ($r_json) {
185                         echo json_encode(array('error'=>$msg));
186                 } else {
187                         echo  $msg. EOL;
188                 }
189                 @unlink($src);
190                 killme();
191         }
192
193         $ph->orient($src);
194         @unlink($src);
195
196         $max_length = get_config('system','max_image_length');
197         if(! $max_length)
198                 $max_length = MAX_IMAGE_LENGTH;
199         if($max_length > 0) {
200                 $ph->scaleImage($max_length);
201                 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
202         }
203
204         $width = $ph->getWidth();
205         $height = $ph->getHeight();
206
207         $hash = photo_new_resource();
208
209         $smallest = 0;
210
211         $defperm = '<' . $default_cid . '>';
212
213         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
214
215         if(! $r) {
216                 $msg = t('Image upload failed.');
217                 if ($r_json) {
218                         echo json_encode(array('error'=>$msg));
219                 } else {
220                         echo  $msg. EOL;
221                 }
222                 killme();
223         }
224
225         if($width > 640 || $height > 640) {
226                 $ph->scaleImage(640);
227                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
228                 if($r)
229                         $smallest = 1;
230         }
231
232         if($width > 320 || $height > 320) {
233                 $ph->scaleImage(320);
234                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
235                 if($r AND ($smallest == 0))
236                         $smallest = 2;
237         }
238
239         $basename = basename($filename);
240
241         if (!$desktopmode) {
242
243                 $r = q("SELECT `id`, `datasize`, `width`, `height`, `type` FROM `photo` WHERE `resource-id` = '%s' ORDER BY `width` DESC LIMIT 1", $hash);
244                 if (!$r){
245                         if ($r_json) {
246                             echo json_encode(array('error'=>''));
247                             killme();
248                         }
249                         return false;
250                 }
251                 $picture = array();
252
253                 $picture["id"] = $r[0]["id"];
254                 $picture["size"] = $r[0]["datasize"];
255                 $picture["width"] = $r[0]["width"];
256                 $picture["height"] = $r[0]["height"];
257                 $picture["type"] = $r[0]["type"];
258                 $picture["albumpage"] = $a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash;
259                 $picture["picture"] = $a->get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
260                 $picture["preview"] = $a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt();
261
262                 if ($r_json) {
263                     echo json_encode(array('picture'=>$picture));
264                     killme();
265                 }
266                 return $picture;
267         }
268
269         if ($r_json) {
270             echo json_encode(array('ok'=>true));
271             killme();
272         }
273
274 /* mod Waitman Gobble NO WARRANTY */
275
276 //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)
277         if ($_REQUEST['hush']!='yeah') {
278                 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
279                         echo  "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]\n\n";
280                 }
281                 else {
282                         echo  '<br /><br /><a href="' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '" ><img src="' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."\" alt=\"$basename\" /></a><br /><br />";
283                 }
284         }
285         else {
286                 $m = '[url='.$a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.$a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
287                 return($m);
288         }
289 /* mod Waitman Gobble NO WARRANTY */
290
291         killme();
292         // NOTREACHED
293 }