]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Remove mod/wall_upload.php file
[friendica.git] / mod / wall_attach.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Logger;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Attach;
28 use Friendica\Model\User;
29 use Friendica\Util\Strings;
30
31 function wall_attach_post(App $a)
32 {
33         $isJson = (!empty($_GET['response']) && $_GET['response'] == 'json');
34
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]);
40                         if ($isJson) {
41                                 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
42                         }
43                         return;
44                 }
45         } else {
46                 Logger::warning('Argument count is zero or one (invalid)');
47                 if ($isJson) {
48                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
49                 }
50
51                 return;
52         }
53
54         $can_post = false;
55
56         $page_owner_uid = $owner['uid'];
57         $page_owner_cid = $owner['id'];
58         $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
59
60         if (DI::userSession()->getLocalUserId() && (DI::userSession()->getLocalUserId() == $page_owner_uid)) {
61                 $can_post = true;
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]);
65         }
66
67         if (!$can_post) {
68                 Logger::warning('User does not have required permissions', ['contact_id' => $contact_id, 'page_owner_uid' => $page_owner_uid]);
69                 if ($isJson) {
70                         System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
71                 }
72                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
73                 System::exit();
74         }
75
76         if (empty($_FILES['userfile'])) {
77                 Logger::warning('No file uploaded (empty userfile)');
78                 if ($isJson) {
79                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
80                 }
81                 System::exit();
82         }
83
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');
88
89         /*
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.
93          */
94         if ($fileSize <= 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);
98                 if ($isJson) {
99                         System::jsonExit(['error' => $msg]);
100                 } else {
101                         DI::sysmsg()->addNotice($msg);
102                 }
103                 System::exit();
104         }
105
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);
110                 if ($isJson) {
111                         System::jsonExit(['error' => $msg]);
112                 } else {
113                         echo $msg . '<br />';
114                 }
115                 System::exit();
116         }
117
118         $newid = Attach::storeFile($tempFileName, $page_owner_uid, $fileName, '<' . $page_owner_cid . '>');
119
120         @unlink($tempFileName);
121
122         if ($newid === false) {
123                 $msg = DI::l10n()->t('File upload failed.');
124                 Logger::warning($msg);
125                 if ($isJson) {
126                         System::jsonExit(['error' => $msg]);
127                 } else {
128                         echo $msg . '<br />';
129                 }
130                 System::exit();
131         }
132
133         if ($isJson) {
134                 System::jsonExit(['ok' => true, 'id' => $newid]);
135         }
136
137         $lf = "\n";
138
139         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
140         System::exit();
141         // NOTREACHED
142 }