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