]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Merge pull request #6977 from annando/fix-reshare
[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\Database\DBA;
10 use Friendica\Model\Attach;
11 use Friendica\Model\User;
12 use Friendica\Util\Strings;
13
14 function wall_attach_post(App $a) {
15
16         $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
17
18         if ($a->argc > 1) {
19                 $nick = $a->argv[1];
20                 $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",
21                         DBA::escape($nick)
22                 );
23
24                 if (! DBA::isResult($r)) {
25                         if ($r_json) {
26                                 echo json_encode(['error' => L10n::t('Invalid request.')]);
27                                 exit();
28                         }
29                         return;
30                 }
31         } else {
32                 if ($r_json) {
33                         echo json_encode(['error' => L10n::t('Invalid request.')]);
34                         exit();
35                 }
36
37                 return;
38         }
39
40         $can_post  = false;
41
42         $page_owner_uid   = $r[0]['uid'];
43         $page_owner_cid   = $r[0]['id'];
44         $community_page   = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
45
46         if ((local_user()) && (local_user() == $page_owner_uid)) {
47                 $can_post = true;
48         } else {
49                 if ($community_page && remote_user()) {
50                         $contact_id = 0;
51
52                         if (is_array($_SESSION['remote'])) {
53                                 foreach ($_SESSION['remote'] as $v) {
54                                         if ($v['uid'] == $page_owner_uid) {
55                                                 $contact_id = $v['cid'];
56                                                 break;
57                                         }
58                                 }
59                         }
60
61                         if ($contact_id > 0) {
62                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
63                                         intval($contact_id),
64                                         intval($page_owner_uid)
65                                 );
66
67                                 if (DBA::isResult($r)) {
68                                         $can_post = true;
69                                 }
70                         }
71                 }
72         }
73
74         if (! $can_post) {
75                 if ($r_json) {
76                         echo json_encode(['error' => L10n::t('Permission denied.')]);
77                         exit();
78                 }
79                 notice(L10n::t('Permission denied.') . EOL );
80                 exit();
81         }
82
83         if (empty($_FILES['userfile'])) {
84                 if ($r_json) {
85                         echo json_encode(['error' => L10n::t('Invalid request.')]);
86                 }
87                 exit();
88         }
89
90         $src      = $_FILES['userfile']['tmp_name'];
91         $filename = basename($_FILES['userfile']['name']);
92         $filesize = intval($_FILES['userfile']['size']);
93
94         $maxfilesize = Config::get('system','maxfilesize');
95
96         /* Found html code written in text field of form,
97          * when trying to upload a file with filesize
98          * greater than upload_max_filesize. Cause is unknown.
99          * Then Filesize gets <= 0.
100          */
101
102         if ($filesize <= 0) {
103                 $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?'));
104                 if ($r_json) {
105                         echo json_encode(['error' => $msg]);
106                 } else {
107                         notice($msg . EOL);
108                 }
109                 @unlink($src);
110                 exit();
111         }
112
113         if ($maxfilesize && $filesize > $maxfilesize) {
114                 $msg = L10n::t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
115                 if ($r_json) {
116                         echo json_encode(['error' => $msg]);
117                 } else {
118                         echo $msg . EOL;
119                 }
120                 @unlink($src);
121                 exit();
122         }
123
124         $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
125
126         @unlink($src);
127
128         if ($newid === false) {
129                 $msg =  L10n::t('File upload failed.');
130                 if ($r_json) {
131                         echo json_encode(['error' => $msg]);
132                 } else {
133                         echo $msg . EOL;
134                 }
135                 exit();
136         }
137
138         if ($r_json) {
139                 echo json_encode(['ok' => true, 'id' => $newid]);
140                 exit();
141         }
142
143         $lf = "\n";
144
145         echo  $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
146
147         exit();
148         // NOTREACHED
149 }