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