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