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