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