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\Session;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Attach;
28 use Friendica\Model\User;
29 use Friendica\Util\Strings;
31 function wall_attach_post(App $a) {
33 $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
35 if (DI::args()->getArgc() > 1) {
36 $nick = DI::args()->getArgv()[1];
37 $owner = User::getOwnerDataByNick($nick);
38 if (!DBA::isResult($owner)) {
40 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
46 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
54 $page_owner_uid = $owner['uid'];
55 $page_owner_cid = $owner['id'];
56 $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
58 if (local_user() && (local_user() == $page_owner_uid)) {
60 } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
61 $contact_id = Session::getRemoteContactID($page_owner_uid);
62 $can_post = DBA::exists('contact', ['blocked' => false, 'pending' => false, 'id' => $contact_id, 'uid' => $page_owner_uid]);
67 System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
69 notice(DI::l10n()->t('Permission denied.') . EOL );
73 if (empty($_FILES['userfile'])) {
75 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
80 $src = $_FILES['userfile']['tmp_name'];
81 $filename = basename($_FILES['userfile']['name']);
82 $filesize = intval($_FILES['userfile']['size']);
84 $maxfilesize = DI::config()->get('system','maxfilesize');
86 /* Found html code written in text field of form,
87 * when trying to upload a file with filesize
88 * greater than upload_max_filesize. Cause is unknown.
89 * Then Filesize gets <= 0.
93 $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?'));
96 System::jsonExit(['error' => $msg]);
103 if ($maxfilesize && $filesize > $maxfilesize) {
104 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
107 System::jsonExit(['error' => $msg]);
114 $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
118 if ($newid === false) {
119 $msg = DI::l10n()->t('File upload failed.');
121 System::jsonExit(['error' => $msg]);
129 System::jsonExit(['ok' => true, 'id' => $newid]);
134 echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;