3 * @file mod/wall_attach.php
7 use Friendica\Core\Session;
8 use Friendica\Database\DBA;
10 use Friendica\Model\Attach;
11 use Friendica\Model\User;
12 use Friendica\Util\Strings;
14 function wall_attach_post(App $a) {
16 $r_json = (!empty($_GET['response']) && $_GET['response']=='json');
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",
24 if (! DBA::isResult($r)) {
26 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
33 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
42 $page_owner_uid = $r[0]['uid'];
43 $page_owner_cid = $r[0]['id'];
44 $community_page = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
46 if (local_user() && (local_user() == $page_owner_uid)) {
48 } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
49 $contact_id = Session::getRemoteContactID($page_owner_uid);
50 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
52 intval($page_owner_uid)
55 if (DBA::isResult($r)) {
62 echo json_encode(['error' => DI::l10n()->t('Permission denied.')]);
65 notice(DI::l10n()->t('Permission denied.') . EOL );
69 if (empty($_FILES['userfile'])) {
71 echo json_encode(['error' => DI::l10n()->t('Invalid request.')]);
76 $src = $_FILES['userfile']['tmp_name'];
77 $filename = basename($_FILES['userfile']['name']);
78 $filesize = intval($_FILES['userfile']['size']);
80 $maxfilesize = DI::config()->get('system','maxfilesize');
82 /* Found html code written in text field of form,
83 * when trying to upload a file with filesize
84 * greater than upload_max_filesize. Cause is unknown.
85 * Then Filesize gets <= 0.
89 $msg = DI::l10n()->t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(DI::l10n()->t('Or - did you try to upload an empty file?'));
91 echo json_encode(['error' => $msg]);
99 if ($maxfilesize && $filesize > $maxfilesize) {
100 $msg = DI::l10n()->t('File exceeds size limit of %s', Strings::formatBytes($maxfilesize));
102 echo json_encode(['error' => $msg]);
110 $newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
114 if ($newid === false) {
115 $msg = DI::l10n()->t('File upload failed.');
117 echo json_encode(['error' => $msg]);
125 echo json_encode(['ok' => true, 'id' => $newid]);
131 echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;