]> git.mxchange.org Git - friendica.git/blob - mod/wall_upload.php
OStatus conversation are now checked every 30 minutes for new answers.
[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` LEFT 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` LEFT 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                 $src = $_FILES['media']['tmp_name'];
81                 $filename = basename($_FILES['media']['name']);
82                 $filesize = intval($_FILES['media']['size']);
83                 $filetype = $_FILES['media']['type'];
84         }
85         
86     if ($filetype=="") $filetype=guess_image_type($filename);
87         $maximagesize = get_config('system','maximagesize');
88
89         if(($maximagesize) && ($filesize > $maximagesize)) {
90                 echo  sprintf( t('Image exceeds size limit of %d'), $maximagesize) . EOL;
91                 @unlink($src);
92                 killme();
93         }
94
95         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
96                 intval($page_owner_uid)
97         );
98
99         $limit = service_class_fetch($page_owner_uid,'photo_upload_limit');
100
101         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
102                 echo upgrade_message(true) . EOL ;
103                 @unlink($src);
104                 killme();
105         }
106
107
108         $imagedata = @file_get_contents($src);
109         $ph = new Photo($imagedata, $filetype);
110
111         if(! $ph->is_valid()) {
112                 echo ( t('Unable to process image.') . EOL);
113                 @unlink($src);
114                 killme();
115         }
116
117         $ph->orient($src);
118         @unlink($src);
119
120         $max_length = get_config('system','max_image_length');
121         if(! $max_length)
122                 $max_length = MAX_IMAGE_LENGTH;
123         if($max_length > 0)
124                 $ph->scaleImage($max_length);
125
126         $width = $ph->getWidth();
127         $height = $ph->getHeight();
128
129         $hash = photo_new_resource();
130         
131         $smallest = 0;
132
133         $defperm = '<' . $default_cid . '>';
134
135         $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 0, 0, $defperm);
136
137         if(! $r) {
138                 echo ( t('Image upload failed.') . EOL);
139                 killme();
140         }
141
142         if($width > 640 || $height > 640) {
143                 $ph->scaleImage(640);
144                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 1, 0, $defperm);
145                 if($r) 
146                         $smallest = 1;
147         }
148
149         if($width > 320 || $height > 320) {
150                 $ph->scaleImage(320);
151                 $r = $ph->store($page_owner_uid, $visitor, $hash, $filename, t('Wall Photos'), 2, 0, $defperm);
152                 if($r)
153                         $smallest = 2;
154         }
155
156         $basename = basename($filename);
157
158
159 /* mod Waitman Gobble NO WARRANTY */
160
161 //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)
162         if ($_REQUEST['hush']!='yeah') {
163                 if(local_user() && (! feature_enabled(local_user(),'richtext') || x($_REQUEST['nomce'])) ) {
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                 }
166                 else {
167                         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 />";
168                 }
169         }
170         else {
171                 $m = '[url=' . $a->get_baseurl() . '/photos/' . $page_owner_nick . '/image/' . $hash . '][img]' . $a->get_baseurl() . "/photo/{$hash}-{$smallest}.".$ph->getExt()."[/img][/url]";
172                 return($m);
173         }
174 /* mod Waitman Gobble NO WARRANTY */
175
176         killme();
177         // NOTREACHED
178 }