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",
15 if(! dbm::is_result($r)){
17 echo json_encode(array('error'=>t('Invalid request.')));
25 echo json_encode(array('error'=>t('Invalid request.')));
34 $page_owner_uid = $r[0]['uid'];
35 $page_owner_cid = $r[0]['id'];
36 $page_owner_nick = $r[0]['nickname'];
37 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
39 if((local_user()) && (local_user() == $page_owner_uid))
42 if($community_page && remote_user()) {
44 if(is_array($_SESSION['remote'])) {
45 foreach($_SESSION['remote'] as $v) {
46 if($v['uid'] == $page_owner_uid) {
47 $contact_id = $v['cid'];
54 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
56 intval($page_owner_uid)
58 if(dbm::is_result($r)) {
60 $visitor = $contact_id;
67 echo json_encode(array('error'=>t('Permission denied.')));
70 notice( t('Permission denied.') . EOL );
74 if(! x($_FILES,'userfile')) {
76 echo json_encode(array('error'=>t('Invalid request.')));
81 $src = $_FILES['userfile']['tmp_name'];
82 $filename = basename($_FILES['userfile']['name']);
83 $filesize = intval($_FILES['userfile']['size']);
85 $maxfilesize = get_config('system','maxfilesize');
87 /* Found html code written in text field of form,
88 * when trying to upload a file with filesize
89 * greater than upload_max_filesize. Cause is unknown.
90 * Then Filesize gets <= 0.
94 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
96 echo json_encode(array('error'=>$msg));
104 if(($maxfilesize) && ($filesize > $maxfilesize)) {
105 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
107 echo json_encode(array('error'=>$msg));
115 $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
116 intval($page_owner_uid)
119 $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
121 if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
122 $msg = upgrade_message(true);
124 echo json_encode(array('error'=>$msg));
133 $filedata = @file_get_contents($src);
134 $mimetype = z_mime_content_type($filename);
135 $hash = get_guid(64);
136 $created = datetime_convert();
137 $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
138 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
139 intval($page_owner_uid),
147 dbesc('<' . $page_owner_cid . '>'),
156 $msg = t('File upload failed.');
158 echo json_encode(array('error'=>$msg));
165 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
166 intval($page_owner_uid),
171 if(! dbm::is_result($r)) {
172 $msg = t('File upload failed.');
174 echo json_encode(array('error'=>$msg));
182 echo json_encode(array('ok'=>true));
188 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;