]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge pull request #1338 from annando/1501-global-contacts
[friendica.git] / mod / wall_upload.php
1 <?php
2
3 require_once('include/Photo.php');
4
5 function wall_upload_post(&$a) {
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                 echo  sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
123                 @unlink($src);
124                 killme();
125         }
126
127         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
128                 intval($page_owner_uid)
129         );
130
131         $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
132
133         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
134                 echo upgrade_message(true) . EOL ;
135                 @unlink($src);
136                 killme();
137         }
138
139
140         $imagedata = @file_get_contents($src);
141         $ph = new Photo($imagedata, $filetype);
142
143         if(! $ph->is_valid()) {
144                 echo ( t('Unable to process image.') . EOL);
145                 @unlink($src);
146                 killme();
147         }
148
149         $ph->orient($src);
150         @unlink($src);
151
152         $max_length = get_config('system','max_image_length');
153         if(! $max_length)
154                 $max_length = MAX_IMAGE_LENGTH;
155         if($max_length > 0) {
156                 $ph->scaleImage($max_length);
157                 logger("File upload: Scaling picture to new size ".$max_length, LOGGER_DEBUG);
158         }
159
160         $width = $ph->getWidth();
161         $height = $ph->getHeight();
162
163         $hash = photo_new_resource();
164
165         $smallest = 0;
166
167         $defperm = '<' . $default_cid . '>';
168
169         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
170
171         if(! $r) {
172                 echo ( t('Image upload failed.') . EOL);
173                 killme();
174         }
175
176         if($width > 640 || $height > 640) {
177                 $ph->scaleImage(640);
178                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
179                 if($r)
180                         $smallest = 1;
181         }
182
183         if($width > 320 || $height > 320) {
184                 $ph->scaleImage(320);
185                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
186                 if($r AND ($smallest == 0))
187                         $smallest = 2;
188         }
189
190         $basename = basename($filename);
191
192
193 /* mod Waitman Gobble NO WARRANTY */
194
195 //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)
196         if ($_REQUEST['hush']!='yeah') {
197                 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
198                         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";
199                 }
200                 else {
201                         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 />";
202                 }
203         }
204         else {
205                 $m = '[url='.$a->get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash.'][img]'.$a->get_baseurl()."/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
206                 return($m);
207         }
208 /* mod Waitman Gobble NO WARRANTY */
209
210         killme();
211         // NOTREACHED
212 }