]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
ET translation updated THX Rain Hawk
[friendica.git] / mod / wall_attach.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\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                                 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
40                                 exit();
41                         }
42                         return;
43                 }
44         } else {
45                 if ($r_json) {
46                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
47                         exit();
48                 }
49
50                 return;
51         }
52
53         $can_post  = false;
54
55         $page_owner_uid = $owner['uid'];
56         $page_owner_cid = $owner['id'];
57         $community_page = $owner['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
58
59         if (local_user() && (local_user() == $page_owner_uid)) {
60                 $can_post = true;
61         } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
62                 $contact_id = Session::getRemoteContactID($page_owner_uid);
63                 $can_post = DBA::exists('contact', ['blocked' => false, 'pending' => false, 'id' => $contact_id, 'uid' => $page_owner_uid]);
64         }
65
66         if (!$can_post) {
67                 if ($r_json) {
68                         echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
69                         exit();
70                 }
71                 notice(DI::l10n()->t('Permission denied.') . EOL );
72                 exit();
73         }
74
75         if (empty($_FILES['userfile'])) {
76                 if ($r_json) {
77                         echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
78                 }
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                 if ($r_json) {
97                         echo json_encode(['error' => $msg]);
98                 } else {
99                         notice($msg);
100                 }
101                 @unlink($src);
102                 exit();
103         }
104
105         if ($maxfilesize && $filesize > $maxfilesize) {
106                 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
107                 if ($r_json) {
108                         echo json_encode(['error' => $msg]);
109                 } else {
110                         echo $msg . EOL;
111                 }
112                 @unlink($src);
113                 exit();
114         }
115
116         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
117
118         @unlink($src);
119
120         if ($newid === false) {
121                 $msg =  DI::l10n()->t('File upload failed.');
122                 if ($r_json) {
123                         echo json_encode(['error' => $msg]);
124                 } else {
125                         echo $msg . EOL;
126                 }
127                 exit();
128         }
129
130         if ($r_json) {
131                 echo json_encode(['ok' => true, 'id' => $newid]);
132                 exit();
133         }
134
135         $lf = "\n";
136
137         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
138
139         exit();
140         // NOTREACHED
141 }