3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
23 use Friendica\Core\Session;
24 use Friendica\Database\DBA;
26 use Friendica\Model\Attach;
27 use Friendica\Model\User;
28 use Friendica\Util\Strings;
30 function wall_attach_post(App $a) {
32 $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
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",
40 if (! DBA::isResult($r)) {
42 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
49 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
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);
62 if (local_user() && (local_user() == $page_owner_uid)) {
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",
68 intval($page_owner_uid)
71 if (DBA::isResult($r)) {
78 echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
81 notice(DI::l10n()->t('Permission denied.') . EOL );
85 if (empty($_FILES['userfile'])) {
87 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
92 $src = $_FILES['userfile']['tmp_name'];
93 $filename = basename($_FILES['userfile']['name']);
94 $filesize = intval($_FILES['userfile']['size']);
96 $maxfilesize = DI::config()->get('system','maxfilesize');
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.
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?'));
107 echo json_encode(['error' => $msg]);
115 if ($maxfilesize && $filesize > $maxfilesize) {
116 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
118 echo json_encode(['error' => $msg]);
126 $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
130 if ($newid === false) {
131 $msg = DI::l10n()->t('File upload failed.');
133 echo json_encode(['error' => $msg]);
141 echo json_encode(['ok' => true, 'id' => $newid]);
147 echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;