3 * @file mod/wall_attach.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Util\DateTimeFormat;
13 use Friendica\Util\Mimetype;
15 function wall_attach_post(App $a) {
17 $r_json = (x($_GET,'response') && $_GET['response']=='json');
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",
25 if (! DBA::isResult($r)) {
27 echo json_encode(['error' => L10n::t('Invalid request.')]);
34 echo json_encode(['error' => L10n::t('Invalid request.')]);
44 $page_owner_uid = $r[0]['uid'];
45 $page_owner_cid = $r[0]['id'];
46 $page_owner_nick = $r[0]['nickname'];
47 $community_page = (($r[0]['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
49 if ((local_user()) && (local_user() == $page_owner_uid)) {
52 if ($community_page && remote_user()) {
55 if (is_array($_SESSION['remote'])) {
56 foreach ($_SESSION['remote'] as $v) {
57 if ($v['uid'] == $page_owner_uid) {
58 $contact_id = $v['cid'];
64 if ($contact_id > 0) {
65 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
67 intval($page_owner_uid)
70 if (DBA::isResult($r)) {
72 $visitor = $contact_id;
80 echo json_encode(['error' => L10n::t('Permission denied.')]);
83 notice(L10n::t('Permission denied.') . EOL );
87 if (! x($_FILES,'userfile')) {
89 echo json_encode(['error' => L10n::t('Invalid request.')]);
94 $src = $_FILES['userfile']['tmp_name'];
95 $filename = basename($_FILES['userfile']['name']);
96 $filesize = intval($_FILES['userfile']['size']);
98 $maxfilesize = Config::get('system','maxfilesize');
100 /* Found html code written in text field of form,
101 * when trying to upload a file with filesize
102 * greater than upload_max_filesize. Cause is unknown.
103 * Then Filesize gets <= 0.
106 if ($filesize <= 0) {
107 $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?'));
109 echo json_encode(['error' => $msg]);
117 if ($maxfilesize && $filesize > $maxfilesize) {
118 $msg = L10n::t('File exceeds size limit of %s', formatBytes($maxfilesize));
120 echo json_encode(['error' => $msg]);
128 $filedata = @file_get_contents($src);
129 $mimetype = Mimetype::getContentType($filename);
130 $hash = System::createGUID(64);
131 $created = DateTimeFormat::utcNow();
133 $fields = ['uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
134 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
135 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => ''];
137 $r = DBA::insert('attach', $fields);
142 $msg = L10n::t('File upload failed.');
144 echo json_encode(['error' => $msg]);
151 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
152 intval($page_owner_uid),
153 DBA::escape($created),
157 if (! DBA::isResult($r)) {
158 $msg = L10n::t('File upload failed.');
160 echo json_encode(['error' => $msg]);
168 echo json_encode(['ok' => true]);
174 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;