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\Attach;
12 use Friendica\Model\Contact;
13 use Friendica\Util\DateTimeFormat;
14 use Friendica\Util\Mimetype;
15 use Friendica\Util\Strings;
17 function wall_attach_post(App $a) {
19 $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
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",
27 if (! DBA::isResult($r)) {
29 echo json_encode(['error' => L10n::t('Invalid request.')]);
36 echo json_encode(['error' => L10n::t('Invalid request.')]);
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);
51 if ((local_user()) && (local_user() == $page_owner_uid)) {
54 if ($community_page && remote_user()) {
57 if (is_array($_SESSION['remote'])) {
58 foreach ($_SESSION['remote'] as $v) {
59 if ($v['uid'] == $page_owner_uid) {
60 $contact_id = $v['cid'];
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",
69 intval($page_owner_uid)
72 if (DBA::isResult($r)) {
74 $visitor = $contact_id;
82 echo json_encode(['error' => L10n::t('Permission denied.')]);
85 notice(L10n::t('Permission denied.') . EOL );
89 if (empty($_FILES['userfile'])) {
91 echo json_encode(['error' => L10n::t('Invalid request.')]);
96 $src = $_FILES['userfile']['tmp_name'];
97 $filename = basename($_FILES['userfile']['name']);
98 $filesize = intval($_FILES['userfile']['size']);
100 $maxfilesize = Config::get('system','maxfilesize');
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.
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?'));
111 echo json_encode(['error' => $msg]);
119 if ($maxfilesize && $filesize > $maxfilesize) {
120 $msg = L10n::t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
122 echo json_encode(['error' => $msg]);
130 $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
134 if ($newid === false) {
135 $msg = L10n::t('File upload failed.');
137 echo json_encode(['error' => $msg]);
145 echo json_encode(['ok' => true, 'id' => $newid]);
151 echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;