]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
6dfa40b09588da9b58132deb82dddaca7966ec76
[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\Session;
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         $r_json = (!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                         if ($r_json) {
40                                 System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
41                         }
42                         return;
43                 }
44         } else {
45                 if ($r_json) {
46                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
47                 }
48
49                 return;
50         }
51
52         $can_post  = false;
53
54         $page_owner_uid = $owner['uid'];
55         $page_owner_cid = $owner['id'];
56         $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
57
58         if (local_user() && (local_user() == $page_owner_uid)) {
59                 $can_post = true;
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]);
63         }
64
65         if (!$can_post) {
66                 if ($r_json) {
67                         System::jsonExit(['error' => DI::l10n()->t('Permission denied.')]);
68                 }
69                 notice(DI::l10n()->t('Permission denied.') . EOL );
70                 DI::page()->logRuntime();
71                 exit();
72         }
73
74         if (empty($_FILES['userfile'])) {
75                 if ($r_json) {
76                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
77                 }
78                 DI::page()->logRuntime();
79                 exit();
80         }
81
82         $src      = $_FILES['userfile']['tmp_name'];
83         $filename = basename($_FILES['userfile']['name']);
84         $filesize = intval($_FILES['userfile']['size']);
85
86         $maxfilesize = DI::config()->get('system','maxfilesize');
87
88         /* Found html code written in text field of form,
89          * when trying to upload a file with filesize
90          * greater than upload_max_filesize. Cause is unknown.
91          * Then Filesize gets <= 0.
92          */
93
94         if ($filesize <= 0) {
95                 $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                 @unlink($src);
97                 if ($r_json) {
98                         System::jsonExit(['error' => $msg]);
99                 } else {
100                         notice($msg);
101                 }
102                 DI::page()->logRuntime();
103                 exit();
104         }
105
106         if ($maxfilesize && $filesize > $maxfilesize) {
107                 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
108                 @unlink($src);
109                 if ($r_json) {
110                         System::jsonExit(['error' => $msg]);
111                 } else {
112                         echo $msg . EOL;
113                 }
114                 DI::page()->logRuntime();
115                 exit();
116         }
117
118         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
119
120         @unlink($src);
121
122         if ($newid === false) {
123                 $msg =  DI::l10n()->t('File upload failed.');
124                 if ($r_json) {
125                         System::jsonExit(['error' => $msg]);
126                 } else {
127                         echo $msg . EOL;
128                 }
129                 DI::page()->logRuntime();
130                 exit();
131         }
132
133         if ($r_json) {
134                 System::jsonExit(['ok' => true, 'id' => $newid]);
135         }
136
137         $lf = "\n";
138
139         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
140         DI::page()->logRuntime();
141         exit();
142         // NOTREACHED
143 }