5 require_once('include/attach.php');
6 require_once('include/datetime.php');
8 function wall_attach_post(App $a) {
10 $r_json = (x($_GET,'response') && $_GET['response']=='json');
14 $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",
17 if (! dbm::is_result($r)) {
19 echo json_encode(array('error'=>t('Invalid request.')));
27 echo json_encode(array('error'=>t('Invalid request.')));
36 $page_owner_uid = $r[0]['uid'];
37 $page_owner_cid = $r[0]['id'];
38 $page_owner_nick = $r[0]['nickname'];
39 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
41 if((local_user()) && (local_user() == $page_owner_uid))
44 if($community_page && remote_user()) {
46 if(is_array($_SESSION['remote'])) {
47 foreach($_SESSION['remote'] as $v) {
48 if($v['uid'] == $page_owner_uid) {
49 $contact_id = $v['cid'];
56 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
58 intval($page_owner_uid)
60 if (dbm::is_result($r)) {
62 $visitor = $contact_id;
69 echo json_encode(array('error'=>t('Permission denied.')));
72 notice( t('Permission denied.') . EOL );
76 if(! x($_FILES,'userfile')) {
78 echo json_encode(array('error'=>t('Invalid request.')));
83 $src = $_FILES['userfile']['tmp_name'];
84 $filename = basename($_FILES['userfile']['name']);
85 $filesize = intval($_FILES['userfile']['size']);
87 $maxfilesize = get_config('system','maxfilesize');
89 /* Found html code written in text field of form,
90 * when trying to upload a file with filesize
91 * greater than upload_max_filesize. Cause is unknown.
92 * Then Filesize gets <= 0.
96 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
98 echo json_encode(array('error'=>$msg));
106 if(($maxfilesize) && ($filesize > $maxfilesize)) {
107 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
109 echo json_encode(array('error'=>$msg));
117 $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
120 $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
121 intval($page_owner_uid)
123 $size = $r[0]['total'];
125 if (($size + strlen($imagedata)) > $limit) {
126 $msg = upgrade_message(true);
128 echo json_encode(array('error'=>$msg));
137 $filedata = @file_get_contents($src);
138 $mimetype = z_mime_content_type($filename);
139 $hash = get_guid(64);
140 $created = datetime_convert();
141 $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
142 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
143 intval($page_owner_uid),
151 dbesc('<' . $page_owner_cid . '>'),
160 $msg = t('File upload failed.');
162 echo json_encode(array('error'=>$msg));
169 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
170 intval($page_owner_uid),
175 if (! dbm::is_result($r)) {
176 $msg = t('File upload failed.');
178 echo json_encode(array('error'=>$msg));
186 echo json_encode(array('ok'=>true));
192 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;