3 * @file mod/wall_attach.php
6 use Friendica\Core\Config;
7 use Friendica\Database\DBM;
8 use Friendica\Util\Mimetype;
10 require_once 'include/datetime.php';
12 function wall_attach_post(App $a) {
14 $r_json = (x($_GET,'response') && $_GET['response']=='json');
18 $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 if (! DBM::is_result($r)) {
23 echo json_encode(['error'=>t('Invalid request.')]);
31 echo json_encode(['error'=>t('Invalid request.')]);
40 $page_owner_uid = $r[0]['uid'];
41 $page_owner_cid = $r[0]['id'];
42 $page_owner_nick = $r[0]['nickname'];
43 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
45 if((local_user()) && (local_user() == $page_owner_uid))
48 if($community_page && remote_user()) {
50 if(is_array($_SESSION['remote'])) {
51 foreach($_SESSION['remote'] as $v) {
52 if($v['uid'] == $page_owner_uid) {
53 $contact_id = $v['cid'];
60 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
62 intval($page_owner_uid)
64 if (DBM::is_result($r)) {
66 $visitor = $contact_id;
73 echo json_encode(['error'=>t('Permission denied.')]);
76 notice( t('Permission denied.') . EOL );
80 if(! x($_FILES,'userfile')) {
82 echo json_encode(['error'=>t('Invalid request.')]);
87 $src = $_FILES['userfile']['tmp_name'];
88 $filename = basename($_FILES['userfile']['name']);
89 $filesize = intval($_FILES['userfile']['size']);
91 $maxfilesize = Config::get('system','maxfilesize');
93 /* Found html code written in text field of form,
94 * when trying to upload a file with filesize
95 * greater than upload_max_filesize. Cause is unknown.
96 * Then Filesize gets <= 0.
100 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
102 echo json_encode(['error'=>$msg]);
110 if(($maxfilesize) && ($filesize > $maxfilesize)) {
111 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
113 echo json_encode(['error'=>$msg]);
121 $filedata = @file_get_contents($src);
122 $mimetype = Mimetype::getContentType($filename);
123 $hash = get_guid(64);
124 $created = datetime_convert();
126 $fields = ['uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
127 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
128 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => ''];
130 $r = dba::insert('attach', $fields);
135 $msg = t('File upload failed.');
137 echo json_encode(['error'=>$msg]);
144 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
145 intval($page_owner_uid),
150 if (! DBM::is_result($r)) {
151 $msg = t('File upload failed.');
153 echo json_encode(['error'=>$msg]);
161 echo json_encode(['ok'=>true]);
167 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;