]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Merge pull request #11544 from annando/image-proxy
[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                 System::exit();
71         }
72
73         if (empty($_FILES['userfile'])) {
74                 if ($r_json) {
75                         System::jsonExit(['error' => DI::l10n()->t('Invalid request.')]);
76                 }
77                 System::exit();
78         }
79
80         $src      = $_FILES['userfile']['tmp_name'];
81         $filename = basename($_FILES['userfile']['name']);
82         $filesize = intval($_FILES['userfile']['size']);
83
84         $maxfilesize = DI::config()->get('system','maxfilesize');
85
86         /* Found html code written in text field of form,
87          * when trying to upload a file with filesize
88          * greater than upload_max_filesize. Cause is unknown.
89          * Then Filesize gets <= 0.
90          */
91
92         if ($filesize <= 0) {
93                 $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?'));
94                 @unlink($src);
95                 if ($r_json) {
96                         System::jsonExit(['error' => $msg]);
97                 } else {
98                         notice($msg);
99                 }
100                 System::exit();
101         }
102
103         if ($maxfilesize && $filesize > $maxfilesize) {
104                 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
105                 @unlink($src);
106                 if ($r_json) {
107                         System::jsonExit(['error' => $msg]);
108                 } else {
109                         echo $msg . EOL;
110                 }
111                 System::exit();
112         }
113
114         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
115
116         @unlink($src);
117
118         if ($newid === false) {
119                 $msg =  DI::l10n()->t('File upload failed.');
120                 if ($r_json) {
121                         System::jsonExit(['error' => $msg]);
122                 } else {
123                         echo $msg . EOL;
124                 }
125                 System::exit();
126         }
127
128         if ($r_json) {
129                 System::jsonExit(['ok' => true, 'id' => $newid]);
130         }
131
132         $lf = "\n";
133
134         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
135         System::exit();
136         // NOTREACHED
137 }