4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
7 require_once('include/attach.php');
8 require_once('include/datetime.php');
10 function wall_attach_post(App $a) {
12 $r_json = (x($_GET,'response') && $_GET['response']=='json');
16 $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",
19 if (! DBM::is_result($r)) {
21 echo json_encode(array('error'=>t('Invalid request.')));
29 echo json_encode(array('error'=>t('Invalid request.')));
38 $page_owner_uid = $r[0]['uid'];
39 $page_owner_cid = $r[0]['id'];
40 $page_owner_nick = $r[0]['nickname'];
41 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
43 if((local_user()) && (local_user() == $page_owner_uid))
46 if($community_page && remote_user()) {
48 if(is_array($_SESSION['remote'])) {
49 foreach($_SESSION['remote'] as $v) {
50 if($v['uid'] == $page_owner_uid) {
51 $contact_id = $v['cid'];
58 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
60 intval($page_owner_uid)
62 if (DBM::is_result($r)) {
64 $visitor = $contact_id;
71 echo json_encode(array('error'=>t('Permission denied.')));
74 notice( t('Permission denied.') . EOL );
78 if(! x($_FILES,'userfile')) {
80 echo json_encode(array('error'=>t('Invalid request.')));
85 $src = $_FILES['userfile']['tmp_name'];
86 $filename = basename($_FILES['userfile']['name']);
87 $filesize = intval($_FILES['userfile']['size']);
89 $maxfilesize = Config::get('system','maxfilesize');
91 /* Found html code written in text field of form,
92 * when trying to upload a file with filesize
93 * greater than upload_max_filesize. Cause is unknown.
94 * Then Filesize gets <= 0.
98 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
100 echo json_encode(array('error'=>$msg));
108 if(($maxfilesize) && ($filesize > $maxfilesize)) {
109 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
111 echo json_encode(array('error'=>$msg));
119 $filedata = @file_get_contents($src);
120 $mimetype = z_mime_content_type($filename);
121 $hash = get_guid(64);
122 $created = datetime_convert();
124 $fields = array('uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
125 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
126 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => '');
128 $r = dba::insert('attach', $fields);
133 $msg = t('File upload failed.');
135 echo json_encode(array('error'=>$msg));
142 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
143 intval($page_owner_uid),
148 if (! DBM::is_result($r)) {
149 $msg = t('File upload failed.');
151 echo json_encode(array('error'=>$msg));
159 echo json_encode(array('ok'=>true));
165 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;