3 require_once('include/attach.php');
4 require_once('include/datetime.php');
6 if(! function_exists('wall_attach_post')) {
7 function wall_attach_post(&$a) {
9 $r_json = (x($_GET,'response') && $_GET['response']=='json');
13 $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 echo json_encode(array('error'=>t('Invalid request.')));
26 echo json_encode(array('error'=>t('Invalid request.')));
35 $page_owner_uid = $r[0]['uid'];
36 $page_owner_cid = $r[0]['id'];
37 $page_owner_nick = $r[0]['nickname'];
38 $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
40 if((local_user()) && (local_user() == $page_owner_uid))
43 if($community_page && remote_user()) {
45 if(is_array($_SESSION['remote'])) {
46 foreach($_SESSION['remote'] as $v) {
47 if($v['uid'] == $page_owner_uid) {
55 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
57 intval($page_owner_uid)
68 echo json_encode(array('error'=>t('Permission denied.')));
71 notice( t('Permission denied.') . EOL );
75 if(! x($_FILES,'userfile')) {
77 echo json_encode(array('error'=>t('Invalid request.')));
82 $src = $_FILES['userfile']['tmp_name'];
83 $filename = basename($_FILES['userfile']['name']);
84 $filesize = intval($_FILES['userfile']['size']);
86 $maxfilesize = get_config('system','maxfilesize');
88 /* Found html code written in text field of form,
89 * when trying to upload a file with filesize
90 * greater than upload_max_filesize. Cause is unknown.
91 * Then Filesize gets <= 0.
95 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
97 echo json_encode(array('error'=>$msg));
105 if(($maxfilesize) && ($filesize > $maxfilesize)) {
106 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
108 echo json_encode(array('error'=>$msg));
116 $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
117 intval($page_owner_uid)
120 $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
122 if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
123 $msg = upgrade_message(true);
125 echo json_encode(array('error'=>$msg));
134 $filedata = @file_get_contents($src);
135 $mimetype = z_mime_content_type($filename);
136 $hash = get_guid(64);
137 $created = datetime_convert();
138 $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
139 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
140 intval($page_owner_uid),
148 dbesc('<' . $page_owner_cid . '>'),
157 $msg = t('File upload failed.');
159 echo json_encode(array('error'=>$msg));
166 $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
167 intval($page_owner_uid),
173 $msg = t('File upload failed.');
175 echo json_encode(array('error'=>$msg));
183 echo json_encode(array('ok'=>true));
189 echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;