]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
20e646cb9a0f59d0ba10f3fe311996252a056952
[friendica.git] / mod / wall_attach.php
1 <?php
2
3 require_once('include/attach.php');
4 require_once('include/datetime.php');
5
6 if(! function_exists('wall_attach_post')) {
7 function wall_attach_post(&$a) {
8
9         $r_json = (x($_GET,'response') && $_GET['response']=='json');
10
11         if($a->argc > 1) {
12                 $nick = $a->argv[1];
13                 $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",
14                         dbesc($nick)
15                 );
16                 if(! count($r)){
17                         if ($r_json) {
18                             echo json_encode(array('error'=>t('Invalid request.')));
19                             killme();
20                         }
21                         return;
22         }
23
24         } else {
25                 if ($r_json) {
26                     echo json_encode(array('error'=>t('Invalid request.')));
27                     killme();
28                 }
29                 return;
30     }
31
32         $can_post  = false;
33         $visitor   = 0;
34
35         $page_owner_uid   = $r[0]['uid'];
36         $page_owner_cid   = $r[0]['id'];
37         $page_owner_nick  = $r[0]['nickname'];
38         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
39
40         if((local_user()) && (local_user() == $page_owner_uid))
41                 $can_post = true;
42         else {
43                 if($community_page && remote_user()) {
44                         $cid = 0;
45                         if(is_array($_SESSION['remote'])) {
46                                 foreach($_SESSION['remote'] as $v) {
47                                         if($v['uid'] == $page_owner_uid) {
48                                                 $cid = $v['cid'];
49                                                 break;
50                                         }
51                                 }
52                         }
53                         if($cid) {
54
55                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
56                                         intval($cid),
57                                         intval($page_owner_uid)
58                                 );
59                                 if(count($r)) {
60                                         $can_post = true;
61                                         $visitor = $cid;
62                                 }
63                         }
64                 }
65         }
66         if(! $can_post) {
67                 if ($r_json) {
68                     echo json_encode(array('error'=>t('Permission denied.')));
69                     killme();
70                 }
71                 notice( t('Permission denied.') . EOL );
72                 killme();
73         }
74
75         if(! x($_FILES,'userfile')) {
76                 if ($r_json) {
77                     echo json_encode(array('error'=>t('Invalid request.')));
78                 }
79                 killme();
80         }
81
82         $src      = $_FILES['userfile']['tmp_name'];
83         $filename = basename($_FILES['userfile']['name']);
84         $filesize = intval($_FILES['userfile']['size']);
85
86         $maxfilesize = get_config('system','maxfilesize');
87
88         /* Found html code written in text field of form,
89          * when trying to upload a file with filesize
90          * greater than upload_max_filesize. Cause is unknown.
91          * Then Filesize gets <= 0.
92          */
93
94         if($filesize <=0) {
95                 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
96                 if ($r_json) {
97                         echo json_encode(array('error'=>$msg));
98                 } else {
99                         notice( $msg. EOL );
100                 }
101                 @unlink($src);
102                 killme();
103         }
104
105         if(($maxfilesize) && ($filesize > $maxfilesize)) {
106                 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
107                 if ($r_json) {
108                         echo json_encode(array('error'=>$msg));
109                 } else {
110                         echo  $msg. EOL ;
111                 }
112                 @unlink($src);
113                 killme();
114         }
115
116         $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
117                 intval($page_owner_uid)
118         );
119
120         $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
121
122         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
123                 $msg = upgrade_message(true);
124                 if ($r_json) {
125                         echo json_encode(array('error'=>$msg));
126                 } else {
127                         echo  $msg. EOL ;
128                 }
129                 @unlink($src);
130                 killme();
131         }
132
133
134         $filedata = @file_get_contents($src);
135         $mimetype = z_mime_content_type($filename);
136         $hash = get_guid(64);
137         $created = datetime_convert();
138         $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
139                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
140                 intval($page_owner_uid),
141                 dbesc($hash),
142                 dbesc($filename),
143                 dbesc($mimetype),
144                 intval($filesize),
145                 dbesc($filedata),
146                 dbesc($created),
147                 dbesc($created),
148                 dbesc('<' . $page_owner_cid . '>'),
149                 dbesc(''),
150                 dbesc(''),
151                 dbesc('')
152         );
153
154         @unlink($src);
155
156         if(! $r) {
157                 $msg =  t('File upload failed.');
158                 if ($r_json) {
159                         echo json_encode(array('error'=>$msg));
160                 } else {
161                         echo  $msg. EOL ;
162                 }
163                 killme();
164         }
165
166         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
167                 intval($page_owner_uid),
168                 dbesc($created),
169                 dbesc($hash)
170         );
171
172         if(! count($r)) {
173                 $msg = t('File upload failed.');
174                 if ($r_json) {
175                         echo json_encode(array('error'=>$msg));
176                 } else {
177                         echo  $msg. EOL ;
178                 }
179                 killme();
180         }
181
182         if ($r_json) {
183             echo json_encode(array('ok'=>true));
184             killme();
185         }
186
187         $lf = "\n";
188
189         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
190
191         killme();
192         // NOTREACHED
193 }
194 }