3 require_once('include/attach.php');
4 require_once('include/datetime.php');
6 function wall_attach_post(&$a) {
8 $r_json = (x($_GET,'response') && $_GET['response']=='json');
12 $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",
16 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
21 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
28 $page_owner_uid = $r[0]['uid'];
29 $page_owner_cid = $r[0]['id'];
30 $page_owner_nick = $r[0]['nickname'];
31 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
33 if((local_user()) && (local_user() == $page_owner_uid))
36 if($community_page && remote_user()) {
38 if(is_array($_SESSION['remote'])) {
39 foreach($_SESSION['remote'] as $v) {
40 if($v['uid'] == $page_owner_uid) {
48 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
50 intval($page_owner_uid)
60 if ($r_json) { echo json_encode(['error'=>t('Permission denied.')]); killme(); }
61 notice( t('Permission denied.') . EOL );
65 if(! x($_FILES,'userfile')) {
66 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
70 $src = $_FILES['userfile']['tmp_name'];
71 $filename = basename($_FILES['userfile']['name']);
72 $filesize = intval($_FILES['userfile']['size']);
74 $maxfilesize = get_config('system','maxfilesize');
76 /* Found html code written in text field of form,
77 * when trying to upload a file with filesize
78 * greater than upload_max_filesize. Cause is unknown.
79 * Then Filesize gets <= 0.
83 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
85 echo json_encode(['error'=>$msg]);
93 if(($maxfilesize) && ($filesize > $maxfilesize)) {
94 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
96 echo json_encode(['error'=>$msg]);
104 $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
105 intval($page_owner_uid)
108 $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
110 if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
111 $msg = upgrade_message(true);
113 echo json_encode(['error'=>$msg]);
122 $filedata = @file_get_contents($src);
123 $mimetype = z_mime_content_type($filename);
124 $hash = get_guid(64);
125 $created = datetime_convert();
126 $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
127 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
128 intval($page_owner_uid),
136 dbesc('<' . $page_owner_cid . '>'),
145 $msg = t('File upload failed.');
147 echo json_encode(['error'=>$msg]);
154 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
155 intval($page_owner_uid),
161 $msg = t('File upload failed.');
163 echo json_encode(['error'=>$msg]);
170 if ($r_json) { echo json_encode(['ok'=>true]); killme(); }
174 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;