- added logger for any reason when upload is aborted
- renamed variables a bit to camel-case style
This commit is contained in:
Roland Häder 2022-09-25 18:15:28 +02:00
parent db82bdfc44
commit 71e82bc861
No known key found for this signature in database
GPG Key ID: C82EDE5DDFA0BA77

View File

@ -27,21 +27,23 @@ use Friendica\Model\Attach;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Util\Strings; use Friendica\Util\Strings;
function wall_attach_post(App $a) { function wall_attach_post(App $a)
{
$r_json = (!empty($_GET['response']) && $_GET['response']=='json'); $isJson = (!empty($_GET['response']) && $_GET['response'] == 'json');
if (DI::args()->getArgc() > 1) { if (DI::args()->getArgc() > 1) {
$nick = DI::args()->getArgv()[1]; $nick = DI::args()->getArgv()[1];
$owner = User::getOwnerDataByNick($nick); $owner = User::getOwnerDataByNick($nick);
if (!DBA::isResult($owner)) { if (!DBA::isResult($owner)) {
if ($r_json) { DI::logger()->warning('owner is not a valid record:', ['owner' => $owner]);
if ($isJson) {
System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]); System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
} }
return; return;
} }
} else { } else {
if ($r_json) { DI::logger()->warning('Argument count is zero');
if ($isJson) {
System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]); System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
} }
@ -62,7 +64,7 @@ function wall_attach_post(App $a) {
} }
if (!$can_post) { if (!$can_post) {
if ($r_json) { if ($isJson) {
System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]); System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
} }
DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.')); DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
@ -70,28 +72,27 @@ function wall_attach_post(App $a) {
} }
if (empty($_FILES['userfile'])) { if (empty($_FILES['userfile'])) {
if ($r_json) { DI::logger()->warning('No file uploaded (empty userfile)');
if ($isJson) {
System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]); System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
} }
System::exit(); System::exit();
} }
$src = $_FILES['userfile']['tmp_name']; $tempFileName = $_FILES['userfile']['tmp_name'];
$filename = basename($_FILES['userfile']['name']); $fileName = basename($_FILES['userfile']['name']);
$filesize = intval($_FILES['userfile']['size']); $fileSize = intval($_FILES['userfile']['size']);
$maxFileSize = DI::config()->get('system', 'maxfilesize');
$maxfilesize = DI::config()->get('system','maxfilesize'); /*
* Found html code written in text field of form, when trying to upload a
/* Found html code written in text field of form, * file with filesize greater than upload_max_filesize. Cause is unknown.
* when trying to upload a file with filesize
* greater than upload_max_filesize. Cause is unknown.
* Then Filesize gets <= 0. * Then Filesize gets <= 0.
*/ */
if ($fileSize <= 0) {
if ($filesize <= 0) {
$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?')); $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?'));
@unlink($src); @unlink($tempFileName);
if ($r_json) { if ($isJson) {
System::jsonExit(['error' => $msg]); System::jsonExit(['error' => $msg]);
} else { } else {
DI::sysmsg()->addNotice($msg); DI::sysmsg()->addNotice($msg);
@ -99,10 +100,10 @@ function wall_attach_post(App $a) {
System::exit(); System::exit();
} }
if ($maxfilesize && $filesize > $maxfilesize) { if ($maxFileSize && $fileSize > $maxFileSize) {
$msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize)); $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxFileSize));
@unlink($src); @unlink($tempFileName);
if ($r_json) { if ($isJson) {
System::jsonExit(['error' => $msg]); System::jsonExit(['error' => $msg]);
} else { } else {
echo $msg . '<br />'; echo $msg . '<br />';
@ -110,13 +111,13 @@ function wall_attach_post(App $a) {
System::exit(); System::exit();
} }
$newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>'); $newid = Attach::storeFile($tempFileName, $page_owner_uid, $fileName, '<' . $page_owner_cid . '>');
@unlink($src); @unlink($tempFileName);
if ($newid === false) { if ($newid === false) {
$msg = DI::l10n()->t('File upload failed.'); $msg = DI::l10n()->t('File upload failed.');
if ($r_json) { if ($isJson) {
System::jsonExit(['error' => $msg]); System::jsonExit(['error' => $msg]);
} else { } else {
echo $msg . '<br />'; echo $msg . '<br />';
@ -124,7 +125,7 @@ function wall_attach_post(App $a) {
System::exit(); System::exit();
} }
if ($r_json) { if ($isJson) {
System::jsonExit(['ok' => true, 'id' => $newid]); System::jsonExit(['ok' => true, 'id' => $newid]);
} }