3 require_once('include/attach.php');
4 require_once('include/datetime.php');
6 function wall_attach_post(App $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 $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
118 $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
119 intval($page_owner_uid)
121 $size = $r[0]['total'];
123 if (($size + strlen($imagedata)) > $limit) {
124 $msg = upgrade_message(true);
126 echo json_encode(array('error'=>$msg));
135 $filedata = @file_get_contents($src);
136 $mimetype = z_mime_content_type($filename);
137 $hash = get_guid(64);
138 $created = datetime_convert();
139 $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
140 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
141 intval($page_owner_uid),
149 dbesc('<' . $page_owner_cid . '>'),
158 $msg = t('File upload failed.');
160 echo json_encode(array('error'=>$msg));
167 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
168 intval($page_owner_uid),
173 if (! dbm::is_result($r)) {
174 $msg = t('File upload failed.');
176 echo json_encode(array('error'=>$msg));
184 echo json_encode(array('ok'=>true));
190 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;