3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\System;
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');
34 if (DI::args()->getArgc() > 1) {
35 $nick = DI::args()->getArgv()[1];
36 $owner = User::getOwnerDataByNick($nick);
37 if (!DBA::isResult($owner)) {
39 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
45 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
53 $page_owner_uid = $owner['uid'];
54 $page_owner_cid = $owner['id'];
55 $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
57 if (DI::userSession()->getLocalUserId() && (DI::userSession()->getLocalUserId() == $page_owner_uid)) {
59 } elseif ($community_page && !empty(DI::userSession()->getRemoteContactID($page_owner_uid))) {
60 $contact_id = DI::userSession()->getRemoteContactID($page_owner_uid);
61 $can_post = DBA::exists('contact', ['blocked' => false, 'pending' => false, 'id' => $contact_id, 'uid' => $page_owner_uid]);
66 System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
68 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
72 if (empty($_FILES['userfile'])) {
74 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
79 $src = $_FILES['userfile']['tmp_name'];
80 $filename = basename($_FILES['userfile']['name']);
81 $filesize = intval($_FILES['userfile']['size']);
83 $maxfilesize = DI::config()->get('system','maxfilesize');
85 /* Found html code written in text field of form,
86 * when trying to upload a file with filesize
87 * greater than upload_max_filesize. Cause is unknown.
88 * Then Filesize gets <= 0.
92 $msg = DI::l10n()->t('Sorry, maybe your upload is bigger than the PHP configuration allows') . '<br />' . (DI::l10n()->t('Or - did you try to upload an empty file?'));
95 System::jsonExit(['error' => $msg]);
97 DI::sysmsg()->addNotice($msg);
102 if ($maxfilesize && $filesize > $maxfilesize) {
103 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
106 System::jsonExit(['error' => $msg]);
108 echo $msg . '<br />';
113 $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
117 if ($newid === false) {
118 $msg = DI::l10n()->t('File upload failed.');
120 System::jsonExit(['error' => $msg]);
122 echo $msg . '<br />';
128 System::jsonExit(['ok' => true, 'id' => $newid]);
133 echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;