]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Switch Api\Mastodon\FollowRequests to list introductions instead of pending contacts
[friendica.git] / mod / wall_attach.php
1 <?php
2 /**
3  * @file mod/wall_attach.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Session;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Attach;
12 use Friendica\Model\User;
13 use Friendica\Util\Strings;
14
15 function wall_attach_post(App $a) {
16
17         $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
18
19         if ($a->argc > 1) {
20                 $nick = $a->argv[1];
21                 $r = q("SELECT `user`.*, `contact`.`id` FROM `user` LEFT JOIN `contact` on `user`.`uid` = `contact`.`uid`  WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
22                         DBA::escape($nick)
23                 );
24
25                 if (! DBA::isResult($r)) {
26                         if ($r_json) {
27                                 echo json_encode(['error' => L10n::t('Invalid request.')]);
28                                 exit();
29                         }
30                         return;
31                 }
32         } else {
33                 if ($r_json) {
34                         echo json_encode(['error' => L10n::t('Invalid request.')]);
35                         exit();
36                 }
37
38                 return;
39         }
40
41         $can_post  = false;
42
43         $page_owner_uid   = $r[0]['uid'];
44         $page_owner_cid   = $r[0]['id'];
45         $community_page   = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
46
47         if (local_user() && (local_user() == $page_owner_uid)) {
48                 $can_post = true;
49         } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
50                 $contact_id = Session::getRemoteContactID($page_owner_uid);
51                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
52                         intval($contact_id),
53                         intval($page_owner_uid)
54                 );
55
56                 if (DBA::isResult($r)) {
57                         $can_post = true;
58                 }
59         }
60
61         if (!$can_post) {
62                 if ($r_json) {
63                         echo json_encode(['error' => L10n::t('Permission denied.')]);
64                         exit();
65                 }
66                 notice(L10n::t('Permission denied.') . EOL );
67                 exit();
68         }
69
70         if (empty($_FILES['userfile'])) {
71                 if ($r_json) {
72                         echo json_encode(['error' => L10n::t('Invalid request.')]);
73                 }
74                 exit();
75         }
76
77         $src      = $_FILES['userfile']['tmp_name'];
78         $filename = basename($_FILES['userfile']['name']);
79         $filesize = intval($_FILES['userfile']['size']);
80
81         $maxfilesize = Config::get('system','maxfilesize');
82
83         /* Found html code written in text field of form,
84          * when trying to upload a file with filesize
85          * greater than upload_max_filesize. Cause is unknown.
86          * Then Filesize gets <= 0.
87          */
88
89         if ($filesize <= 0) {
90                 $msg = L10n::t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(L10n::t('Or - did you try to upload an empty file?'));
91                 if ($r_json) {
92                         echo json_encode(['error' => $msg]);
93                 } else {
94                         notice($msg . EOL);
95                 }
96                 @unlink($src);
97                 exit();
98         }
99
100         if ($maxfilesize && $filesize > $maxfilesize) {
101                 $msg = L10n::t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
102                 if ($r_json) {
103                         echo json_encode(['error' => $msg]);
104                 } else {
105                         echo $msg . EOL;
106                 }
107                 @unlink($src);
108                 exit();
109         }
110
111         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
112
113         @unlink($src);
114
115         if ($newid === false) {
116                 $msg =  L10n::t('File upload failed.');
117                 if ($r_json) {
118                         echo json_encode(['error' => $msg]);
119                 } else {
120                         echo $msg . EOL;
121                 }
122                 exit();
123         }
124
125         if ($r_json) {
126                 echo json_encode(['ok' => true, 'id' => $newid]);
127                 exit();
128         }
129
130         $lf = "\n";
131
132         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
133
134         exit();
135         // NOTREACHED
136 }