]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Make frio more consistent by replacing textual links with icons everywhere. (#5415)
[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\Database\DBM;
12 use Friendica\Util\DateTimeFormat;
13 use Friendica\Util\Mimetype;
14
15 function wall_attach_post(App $a) {
16
17         $r_json = (x($_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                         dbesc($nick)
23                 );
24                 if (! DBM::is_result($r)) {
25                         if ($r_json) {
26                                 echo json_encode(['error'=>L10n::t('Invalid request.')]);
27                                 killme();
28                         }
29                         return;
30         }
31
32         } else {
33                 if ($r_json) {
34                         echo json_encode(['error'=>L10n::t('Invalid request.')]);
35                         killme();
36                 }
37                 return;
38         }
39
40         $can_post  = false;
41         $visitor   = 0;
42
43         $page_owner_uid   = $r[0]['uid'];
44         $page_owner_cid   = $r[0]['id'];
45         $page_owner_nick  = $r[0]['nickname'];
46         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
47
48         if((local_user()) && (local_user() == $page_owner_uid))
49                 $can_post = true;
50         else {
51                 if($community_page && remote_user()) {
52                         $contact_id = 0;
53                         if(is_array($_SESSION['remote'])) {
54                                 foreach($_SESSION['remote'] as $v) {
55                                         if($v['uid'] == $page_owner_uid) {
56                                                 $contact_id = $v['cid'];
57                                                 break;
58                                         }
59                                 }
60                         }
61                         if($contact_id) {
62
63                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
64                                         intval($contact_id),
65                                         intval($page_owner_uid)
66                                 );
67                                 if (DBM::is_result($r)) {
68                                         $can_post = true;
69                                         $visitor = $contact_id;
70                                 }
71                         }
72                 }
73         }
74         if(! $can_post) {
75                 if ($r_json) {
76                         echo json_encode(['error'=>L10n::t('Permission denied.')]);
77                         killme();
78                 }
79                 notice(L10n::t('Permission denied.') . EOL );
80                 killme();
81         }
82
83         if(! x($_FILES,'userfile')) {
84                 if ($r_json) {
85                         echo json_encode(['error'=>L10n::t('Invalid request.')]);
86                 }
87                 killme();
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                 killme();
111         }
112
113         if(($maxfilesize) && ($filesize > $maxfilesize)) {
114                 $msg = L10n::t('File exceeds size limit of %s', formatBytes($maxfilesize));
115                 if ($r_json) {
116                         echo json_encode(['error'=>$msg]);
117                 } else {
118                         echo  $msg. EOL ;
119                 }
120                 @unlink($src);
121                 killme();
122         }
123
124         $filedata = @file_get_contents($src);
125         $mimetype = Mimetype::getContentType($filename);
126         $hash = System::createGUID(64);
127         $created = DateTimeFormat::utcNow();
128
129         $fields = ['uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
130                 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
131                 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => ''];
132
133         $r = DBA::insert('attach', $fields);
134
135         @unlink($src);
136
137         if(! $r) {
138                 $msg =  L10n::t('File upload failed.');
139                 if ($r_json) {
140                         echo json_encode(['error'=>$msg]);
141                 } else {
142                         echo  $msg. EOL ;
143                 }
144                 killme();
145         }
146
147         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
148                 intval($page_owner_uid),
149                 dbesc($created),
150                 dbesc($hash)
151         );
152
153         if (! DBM::is_result($r)) {
154                 $msg = L10n::t('File upload failed.');
155                 if ($r_json) {
156                         echo json_encode(['error'=>$msg]);
157                 } else {
158                         echo  $msg. EOL ;
159                 }
160                 killme();
161         }
162
163         if ($r_json) {
164                 echo json_encode(['ok'=>true]);
165                 killme();
166         }
167
168         $lf = "\n";
169
170         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
171
172         killme();
173         // NOTREACHED
174 }