]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
Merge https://github.com/friendica/friendica into pull
[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                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
41                                 intval(remote_user()),
42                                 intval($page_owner_uid)
43                         );
44                         if(count($r)) {
45                                 $can_post = true;
46                                 $visitor = remote_user();
47                                 $default_cid = $visitor;
48                         }
49                 }
50         }
51
52         if(! $can_post) {
53                 notice( t('Permission denied.') . EOL );
54                 killme();
55         }
56
57         if(! x($_FILES,'userfile') && ! x($_FILES,'media'))
58                 killme();
59
60         if(x($_FILES,'userfile')) {
61                 $src      = $_FILES['userfile']['tmp_name'];
62                 $filename = basename($_FILES['userfile']['name']);
63                 $filesize = intval($_FILES['userfile']['size']);
64                 $filetype = $_FILES['userfile']['type'];
65         }
66         elseif(x($_FILES,'media')) {
67                 $src = $_FILES['media']['tmp_name'];
68                 $filename = basename($_FILES['media']['name']);
69                 $filesize = intval($_FILES['media']['size']);
70                 $filetype = $_FILES['media']['type'];
71         }
72         
73     if ($filetype=="") $filetype=guess_image_type($filename);
74         $maximagesize = get_config('system','maximagesize');
75
76         if(($maximagesize) && ($filesize > $maximagesize)) {
77                 echo  sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
78                 @unlink($src);
79                 killme();
80         }
81
82         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
83                 intval($page_owner_uid)
84         );
85
86         $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
87
88         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
89                 echo upgrade_message(true) . EOL ;
90                 @unlink($src);
91                 killme();
92         }
93
94
95         $imagedata = @file_get_contents($src);
96         $ph = new Photo($imagedata, $filetype);
97
98         if(! $ph->is_valid()) {
99                 echo ( t('Unable to process image.') . EOL);
100                 @unlink($src);
101                 killme();
102         }
103
104         $ph->orient($src);
105         @unlink($src);
106
107         $max_length = get_config('system','max_image_length');
108         if(! $max_length)
109                 $max_length = MAX_IMAGE_LENGTH;
110         if($max_length > 0)
111                 $ph->scaleImage($max_length);
112
113         $width = $ph->getWidth();
114         $height = $ph->getHeight();
115
116         $hash = photo_new_resource();
117         
118         $smallest = 0;
119
120         $defperm = '<' . $default_cid . '>';
121
122         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
123
124         if(! $r) {
125                 echo ( t('Image upload failed.') . EOL);
126                 killme();
127         }
128
129         if($width > 640 || $height > 640) {
130                 $ph->scaleImage(640);
131                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
132                 if($r) 
133                         $smallest = 1;
134         }
135
136         if($width > 320 || $height > 320) {
137                 $ph->scaleImage(320);
138                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
139                 if($r)
140                         $smallest = 2;
141         }
142
143         $basename = basename($filename);
144
145
146 /* mod Waitman Gobble NO WARRANTY */
147
148 //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)
149         if ($_REQUEST['hush']!='yeah') {
150
151                 /*existing code*/
152                 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
153                         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";
154                 else
155                         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 />";
156                 /*existing code*/
157                 
158         } else {
159                 $m = '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
160                 return($m);
161         }
162 /* mod Waitman Gobble NO WARRANTY */
163
164         killme();
165         // NOTREACHED
166 }