4 use Friendica\Core\Config;
6 require_once('include/attach.php');
7 require_once('include/datetime.php');
9 function wall_attach_post(App $a) {
11 $r_json = (x($_GET,'response') && $_GET['response']=='json');
15 $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",
18 if (! dbm::is_result($r)) {
20 echo json_encode(array('error'=>t('Invalid request.')));
28 echo json_encode(array('error'=>t('Invalid request.')));
37 $page_owner_uid = $r[0]['uid'];
38 $page_owner_cid = $r[0]['id'];
39 $page_owner_nick = $r[0]['nickname'];
40 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
42 if((local_user()) && (local_user() == $page_owner_uid))
45 if($community_page && remote_user()) {
47 if(is_array($_SESSION['remote'])) {
48 foreach($_SESSION['remote'] as $v) {
49 if($v['uid'] == $page_owner_uid) {
50 $contact_id = $v['cid'];
57 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
59 intval($page_owner_uid)
61 if (dbm::is_result($r)) {
63 $visitor = $contact_id;
70 echo json_encode(array('error'=>t('Permission denied.')));
73 notice( t('Permission denied.') . EOL );
77 if(! x($_FILES,'userfile')) {
79 echo json_encode(array('error'=>t('Invalid request.')));
84 $src = $_FILES['userfile']['tmp_name'];
85 $filename = basename($_FILES['userfile']['name']);
86 $filesize = intval($_FILES['userfile']['size']);
88 $maxfilesize = Config::get('system','maxfilesize');
90 /* Found html code written in text field of form,
91 * when trying to upload a file with filesize
92 * greater than upload_max_filesize. Cause is unknown.
93 * Then Filesize gets <= 0.
97 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
99 echo json_encode(array('error'=>$msg));
107 if(($maxfilesize) && ($filesize > $maxfilesize)) {
108 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
110 echo json_encode(array('error'=>$msg));
118 $filedata = @file_get_contents($src);
119 $mimetype = z_mime_content_type($filename);
120 $hash = get_guid(64);
121 $created = datetime_convert();
123 $fields = array('uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
124 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
125 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => '');
127 $r = dba::insert('attach', $fields);
132 $msg = t('File upload failed.');
134 echo json_encode(array('error'=>$msg));
141 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
142 intval($page_owner_uid),
147 if (! dbm::is_result($r)) {
148 $msg = t('File upload failed.');
150 echo json_encode(array('error'=>$msg));
158 echo json_encode(array('ok'=>true));
164 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;