]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Fix missing/wrong IHandleUserSessions usages
[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\System;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Attach;
27 use Friendica\Model\User;
28 use Friendica\Util\Strings;
29
30 function wall_attach_post(App $a) {
31
32         $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
33
34         if (DI::args()->getArgc() > 1) {
35                 $nick = DI::args()->getArgv()[1];
36                 $owner = User::getOwnerDataByNick($nick);
37                 if (!DBA::isResult($owner)) {
38                         if ($r_json) {
39                                 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
40                         }
41                         return;
42                 }
43         } else {
44                 if ($r_json) {
45                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
46                 }
47
48                 return;
49         }
50
51         $can_post  = false;
52
53         $page_owner_uid = $owner['uid'];
54         $page_owner_cid = $owner['id'];
55         $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
56
57         if (DI::userSession()->getLocalUserId() && (DI::userSession()->getLocalUserId() == $page_owner_uid)) {
58                 $can_post = true;
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]);
62         }
63
64         if (!$can_post) {
65                 if ($r_json) {
66                         System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
67                 }
68                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
69                 System::exit();
70         }
71
72         if (empty($_FILES['userfile'])) {
73                 if ($r_json) {
74                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
75                 }
76                 System::exit();
77         }
78
79         $src      = $_FILES['userfile']['tmp_name'];
80         $filename = basename($_FILES['userfile']['name']);
81         $filesize = intval($_FILES['userfile']['size']);
82
83         $maxfilesize = DI::config()->get('system','maxfilesize');
84
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.
89          */
90
91         if ($filesize <= 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?'));
93                 @unlink($src);
94                 if ($r_json) {
95                         System::jsonExit(['error' => $msg]);
96                 } else {
97                         DI::sysmsg()->addNotice($msg);
98                 }
99                 System::exit();
100         }
101
102         if ($maxfilesize && $filesize > $maxfilesize) {
103                 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
104                 @unlink($src);
105                 if ($r_json) {
106                         System::jsonExit(['error' => $msg]);
107                 } else {
108                         echo $msg . '<br />';
109                 }
110                 System::exit();
111         }
112
113         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
114
115         @unlink($src);
116
117         if ($newid === false) {
118                 $msg =  DI::l10n()->t('File upload failed.');
119                 if ($r_json) {
120                         System::jsonExit(['error' => $msg]);
121                 } else {
122                         echo $msg . '<br />';
123                 }
124                 System::exit();
125         }
126
127         if ($r_json) {
128                 System::jsonExit(['ok' => true, 'id' => $newid]);
129         }
130
131         $lf = "\n";
132
133         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
134         System::exit();
135         // NOTREACHED
136 }