]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Improve Console/Config display for array values
[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\DBM;
11 use Friendica\Util\DateTimeFormat;
12 use Friendica\Util\Mimetype;
13
14 function wall_attach_post(App $a) {
15
16         $r_json = (x($_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                         dbesc($nick)
22                 );
23                 if (! DBM::is_result($r)) {
24                         if ($r_json) {
25                                 echo json_encode(['error'=>L10n::t('Invalid request.')]);
26                                 killme();
27                         }
28                         return;
29         }
30
31         } else {
32                 if ($r_json) {
33                         echo json_encode(['error'=>L10n::t('Invalid request.')]);
34                         killme();
35                 }
36                 return;
37         }
38
39         $can_post  = false;
40         $visitor   = 0;
41
42         $page_owner_uid   = $r[0]['uid'];
43         $page_owner_cid   = $r[0]['id'];
44         $page_owner_nick  = $r[0]['nickname'];
45         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
46
47         if((local_user()) && (local_user() == $page_owner_uid))
48                 $can_post = true;
49         else {
50                 if($community_page && remote_user()) {
51                         $contact_id = 0;
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                         if($contact_id) {
61
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                                 if (DBM::is_result($r)) {
67                                         $can_post = true;
68                                         $visitor = $contact_id;
69                                 }
70                         }
71                 }
72         }
73         if(! $can_post) {
74                 if ($r_json) {
75                         echo json_encode(['error'=>L10n::t('Permission denied.')]);
76                         killme();
77                 }
78                 notice(L10n::t('Permission denied.') . EOL );
79                 killme();
80         }
81
82         if(! x($_FILES,'userfile')) {
83                 if ($r_json) {
84                         echo json_encode(['error'=>L10n::t('Invalid request.')]);
85                 }
86                 killme();
87         }
88
89         $src      = $_FILES['userfile']['tmp_name'];
90         $filename = basename($_FILES['userfile']['name']);
91         $filesize = intval($_FILES['userfile']['size']);
92
93         $maxfilesize = Config::get('system','maxfilesize');
94
95         /* Found html code written in text field of form,
96          * when trying to upload a file with filesize
97          * greater than upload_max_filesize. Cause is unknown.
98          * Then Filesize gets <= 0.
99          */
100
101         if($filesize <=0) {
102                 $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?'));
103                 if ($r_json) {
104                         echo json_encode(['error'=>$msg]);
105                 } else {
106                         notice( $msg. EOL );
107                 }
108                 @unlink($src);
109                 killme();
110         }
111
112         if(($maxfilesize) && ($filesize > $maxfilesize)) {
113                 $msg = L10n::t('File exceeds size limit of %s', formatBytes($maxfilesize));
114                 if ($r_json) {
115                         echo json_encode(['error'=>$msg]);
116                 } else {
117                         echo  $msg. EOL ;
118                 }
119                 @unlink($src);
120                 killme();
121         }
122
123         $filedata = @file_get_contents($src);
124         $mimetype = Mimetype::getContentType($filename);
125         $hash = System::createGUID(64);
126         $created = DateTimeFormat::utcNow();
127
128         $fields = ['uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
129                 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
130                 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => ''];
131
132         $r = dba::insert('attach', $fields);
133
134         @unlink($src);
135
136         if(! $r) {
137                 $msg =  L10n::t('File upload failed.');
138                 if ($r_json) {
139                         echo json_encode(['error'=>$msg]);
140                 } else {
141                         echo  $msg. EOL ;
142                 }
143                 killme();
144         }
145
146         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
147                 intval($page_owner_uid),
148                 dbesc($created),
149                 dbesc($hash)
150         );
151
152         if (! DBM::is_result($r)) {
153                 $msg = L10n::t('File upload failed.');
154                 if ($r_json) {
155                         echo json_encode(['error'=>$msg]);
156                 } else {
157                         echo  $msg. EOL ;
158                 }
159                 killme();
160         }
161
162         if ($r_json) {
163                 echo json_encode(['ok'=>true]);
164                 killme();
165         }
166
167         $lf = "\n";
168
169         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
170
171         killme();
172         // NOTREACHED
173 }