Merge pull request #10079 from annando/mobilizon
[friendica.git/.git] / mod / wall_attach.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Session;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Attach;
27 use Friendica\Model\User;
28 use Friendica\Util\Strings;
29
30 function wall_attach_post(App $a) {
31
32         $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
33
34         if ($a->argc > 1) {
35                 $nick = $a->argv[1];
36                 $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",
37                         DBA::escape($nick)
38                 );
39
40                 if (! DBA::isResult($r)) {
41                         if ($r_json) {
42                                 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
43                                 exit();
44                         }
45                         return;
46                 }
47         } else {
48                 if ($r_json) {
49                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
50                         exit();
51                 }
52
53                 return;
54         }
55
56         $can_post  = false;
57
58         $page_owner_uid   = $r[0]['uid'];
59         $page_owner_cid   = $r[0]['id'];
60         $community_page   = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
61
62         if (local_user() && (local_user() == $page_owner_uid)) {
63                 $can_post = true;
64         } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
65                 $contact_id = Session::getRemoteContactID($page_owner_uid);
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                 }
74         }
75
76         if (!$can_post) {
77                 if ($r_json) {
78                         echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
79                         exit();
80                 }
81                 notice(DI::l10n()->t('Permission denied.') . EOL );
82                 exit();
83         }
84
85         if (empty($_FILES['userfile'])) {
86                 if ($r_json) {
87                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
88                 }
89                 exit();
90         }
91
92         $src      = $_FILES['userfile']['tmp_name'];
93         $filename = basename($_FILES['userfile']['name']);
94         $filesize = intval($_FILES['userfile']['size']);
95
96         $maxfilesize = DI::config()->get('system','maxfilesize');
97
98         /* Found html code written in text field of form,
99          * when trying to upload a file with filesize
100          * greater than upload_max_filesize. Cause is unknown.
101          * Then Filesize gets <= 0.
102          */
103
104         if ($filesize <= 0) {
105                 $msg = DI::l10n()->t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(DI::l10n()->t('Or - did you try to upload an empty file?'));
106                 if ($r_json) {
107                         echo json_encode(['error' => $msg]);
108                 } else {
109                         notice($msg);
110                 }
111                 @unlink($src);
112                 exit();
113         }
114
115         if ($maxfilesize && $filesize > $maxfilesize) {
116                 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
117                 if ($r_json) {
118                         echo json_encode(['error' => $msg]);
119                 } else {
120                         echo $msg . EOL;
121                 }
122                 @unlink($src);
123                 exit();
124         }
125
126         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
127
128         @unlink($src);
129
130         if ($newid === false) {
131                 $msg =  DI::l10n()->t('File upload failed.');
132                 if ($r_json) {
133                         echo json_encode(['error' => $msg]);
134                 } else {
135                         echo $msg . EOL;
136                 }
137                 exit();
138         }
139
140         if ($r_json) {
141                 echo json_encode(['ok' => true, 'id' => $newid]);
142                 exit();
143         }
144
145         $lf = "\n";
146
147         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
148
149         exit();
150         // NOTREACHED
151 }