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