]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge pull request #481 from pixelroot/master
[friendica.git] / mod / wall_upload.php
1 <?php
2
3 require_once('Photo.php');
4
5 function wall_upload_post(&$a) {
6
7         if($a->argc > 1) {
8                 if(! x($_FILES,'media')) {
9                         $nick = $a->argv[1];
10                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user` LEFT JOIN `contact` on `user`.`uid` = `contact`.`uid`  WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
11                                 dbesc($nick)
12                         );
13
14                         if(! count($r))
15                                 return;
16                 }
17                 else {
18                         $user_info = api_get_user($a);
19                         $r = q("SELECT `user`.*, `contact`.`id` FROM `user` LEFT JOIN `contact` on `user`.`uid` = `contact`.`uid`  WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
20                                 dbesc($user_info['screen_name'])
21                         );
22                 }
23         }
24         else
25                 return;
26
27
28         $can_post  = false;
29         $visitor   = 0;
30
31         $page_owner_uid   = $r[0]['uid'];
32         $default_cid      = $r[0]['id'];
33         $page_owner_nick  = $r[0]['nickname'];
34         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
35
36         if((local_user()) && (local_user() == $page_owner_uid))
37                 $can_post = true;
38         else {
39                 if($community_page && remote_user()) {
40                         $cid = 0;
41                         if(is_array($_SESSION['remote'])) {
42                                 foreach($_SESSION['remote'] as $v) {
43                                         if($v['uid'] == $page_owner_uid) {
44                                                 $cid = $v['cid'];
45                                                 break;
46                                         }
47                                 }
48                         }
49                         if($cid) {
50
51                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
52                                         intval($cid),
53                                         intval($page_owner_uid)
54                                 );
55                                 if(count($r)) {
56                                         $can_post = true;
57                                         $visitor = $cid;
58                                 }
59                         }
60                 }
61         }
62
63         if(! $can_post) {
64                 notice( t('Permission denied.') . EOL );
65                 killme();
66         }
67
68         if(! x($_FILES,'userfile') && ! x($_FILES,'media'))
69                 killme();
70
71         if(x($_FILES,'userfile')) {
72                 $src      = $_FILES['userfile']['tmp_name'];
73                 $filename = basename($_FILES['userfile']['name']);
74                 $filesize = intval($_FILES['userfile']['size']);
75                 $filetype = $_FILES['userfile']['type'];
76         }
77         elseif(x($_FILES,'media')) {
78                 $src = $_FILES['media']['tmp_name'];
79                 $filename = basename($_FILES['media']['name']);
80                 $filesize = intval($_FILES['media']['size']);
81                 $filetype = $_FILES['media']['type'];
82         }
83         
84     if ($filetype=="") $filetype=guess_image_type($filename);
85         $maximagesize = get_config('system','maximagesize');
86
87         if(($maximagesize) && ($filesize > $maximagesize)) {
88                 echo  sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
89                 @unlink($src);
90                 killme();
91         }
92
93         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
94                 intval($page_owner_uid)
95         );
96
97         $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
98
99         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
100                 echo upgrade_message(true) . EOL ;
101                 @unlink($src);
102                 killme();
103         }
104
105
106         $imagedata = @file_get_contents($src);
107         $ph = new Photo($imagedata, $filetype);
108
109         if(! $ph->is_valid()) {
110                 echo ( t('Unable to process image.') . EOL);
111                 @unlink($src);
112                 killme();
113         }
114
115         $ph->orient($src);
116         @unlink($src);
117
118         $max_length = get_config('system','max_image_length');
119         if(! $max_length)
120                 $max_length = MAX_IMAGE_LENGTH;
121         if($max_length > 0)
122                 $ph->scaleImage($max_length);
123
124         $width = $ph->getWidth();
125         $height = $ph->getHeight();
126
127         $hash = photo_new_resource();
128         
129         $smallest = 0;
130
131         $defperm = '<' . $default_cid . '>';
132
133         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
134
135         if(! $r) {
136                 echo ( t('Image upload failed.') . EOL);
137                 killme();
138         }
139
140         if($width > 640 || $height > 640) {
141                 $ph->scaleImage(640);
142                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
143                 if($r) 
144                         $smallest = 1;
145         }
146
147         if($width > 320 || $height > 320) {
148                 $ph->scaleImage(320);
149                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
150                 if($r)
151                         $smallest = 2;
152         }
153
154         $basename = basename($filename);
155
156
157 /* mod Waitman Gobble NO WARRANTY */
158
159 //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)
160         if ($_REQUEST['hush']!='yeah') {
161
162                 /*existing code*/
163                 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
164                         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";
165                 else
166                         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 />";
167                 /*existing code*/
168                 
169         } else {
170                 $m = '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
171                 return($m);
172         }
173 /* mod Waitman Gobble NO WARRANTY */
174
175         killme();
176         // NOTREACHED
177 }