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