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\Logger;
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 $isJson = (!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)) {
39 Logger::warning('owner is not a valid record:', ['owner' => $owner, 'nick' => $nick]);
41 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
46 Logger::warning('Argument count is zero or one (invalid)');
48 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
56 $page_owner_uid = $owner['uid'];
57 $page_owner_cid = $owner['id'];
58 $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
60 if (DI::userSession()->getLocalUserId() && (DI::userSession()->getLocalUserId() == $page_owner_uid)) {
62 } elseif ($community_page && !empty(DI::userSession()->getRemoteContactID($page_owner_uid))) {
63 $contact_id = DI::userSession()->getRemoteContactID($page_owner_uid);
64 $can_post = DBA::exists('contact', ['blocked' => false, 'pending' => false, 'id' => $contact_id, 'uid' => $page_owner_uid]);
68 Logger::warning('User does not have required permissions', ['contact_id' => $contact_id, 'page_owner_uid' => $page_owner_uid]);
70 System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
72 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
76 if (empty($_FILES['userfile'])) {
77 Logger::warning('No file uploaded (empty userfile)');
79 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
84 $tempFileName = $_FILES['userfile']['tmp_name'];
85 $fileName = basename($_FILES['userfile']['name']);
86 $fileSize = intval($_FILES['userfile']['size']);
87 $maxFileSize = DI::config()->get('system', 'maxfilesize');
90 * Found html code written in text field of form, when trying to upload a
91 * file with filesize greater than upload_max_filesize. Cause is unknown.
92 * Then Filesize gets <= 0.
95 $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?');
96 Logger::warning($msg, ['fileSize' => $fileSize]);
97 @unlink($tempFileName);
99 System::jsonExit(['error' => $msg]);
101 DI::sysmsg()->addNotice($msg);
106 if ($maxFileSize && $fileSize > $maxFileSize) {
107 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxFileSize));
108 Logger::warning($msg, ['fileSize' => $fileSize]);
109 @unlink($tempFileName);
111 System::jsonExit(['error' => $msg]);
113 echo $msg . '<br />';
118 $newid = Attach::storeFile($tempFileName, $page_owner_uid, $fileName, '<' . $page_owner_cid . '>');
120 @unlink($tempFileName);
122 if ($newid === false) {
123 $msg = DI::l10n()->t('File upload failed.');
124 Logger::warning($msg);
126 System::jsonExit(['error' => $msg]);
128 echo $msg . '<br />';
134 System::jsonExit(['ok' => true, 'id' => $newid]);
139 echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;