]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Statusnet is now GNU Social
[friendica.git] / mod / wall_attach.php
1 <?php
2
3 require_once('include/attach.php');
4 require_once('include/datetime.php');
5
6 function wall_attach_post(&$a) {
7
8         $r_json = (x($_GET,'response') && $_GET['response']=='json');
9
10         if($a->argc > 1) {
11                 $nick = $a->argv[1];
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",
13                         dbesc($nick)
14                 );
15                 if(! count($r)){
16                         if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
17                         return;
18         }
19
20         } else {
21                 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
22                 return;
23     }
24
25         $can_post  = false;
26         $visitor   = 0;
27
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);
32
33         if((local_user()) && (local_user() == $page_owner_uid))
34                 $can_post = true;
35         else {
36                 if($community_page && remote_user()) {
37                         $cid = 0;
38                         if(is_array($_SESSION['remote'])) {
39                                 foreach($_SESSION['remote'] as $v) {
40                                         if($v['uid'] == $page_owner_uid) {
41                                                 $cid = $v['cid'];
42                                                 break;
43                                         }
44                                 }
45                         }
46                         if($cid) {
47
48                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
49                                         intval($cid),
50                                         intval($page_owner_uid)
51                                 );
52                                 if(count($r)) {
53                                         $can_post = true;
54                                         $visitor = $cid;
55                                 }
56                         }
57                 }
58         }
59         if(! $can_post) {
60                 if ($r_json) { echo json_encode(['error'=>t('Permission denied.')]); killme(); }
61                 notice( t('Permission denied.') . EOL );
62                 killme();
63         }
64
65         if(! x($_FILES,'userfile')) {
66                 if ($r_json) { echo json_encode(['error'=>t('Invalid request.')]); killme(); }
67                 killme();
68         }
69
70         $src      = $_FILES['userfile']['tmp_name'];
71         $filename = basename($_FILES['userfile']['name']);
72         $filesize = intval($_FILES['userfile']['size']);
73
74         $maxfilesize = get_config('system','maxfilesize');
75
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.
80          */
81
82         if($filesize <=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?'));
84                 if ($r_json) {
85                         echo json_encode(['error'=>$msg]);
86                 } else {
87                         notice( $msg. EOL );
88                 }
89                 @unlink($src);
90                 killme();
91         }
92
93         if(($maxfilesize) && ($filesize > $maxfilesize)) {
94                 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
95                 if ($r_json) {
96                         echo json_encode(['error'=>$msg]);
97                 } else {
98                         echo  $msg. EOL ;
99                 }
100                 @unlink($src);
101                 killme();
102         }
103
104         $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
105                 intval($page_owner_uid)
106         );
107
108         $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
109
110         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
111                 $msg = upgrade_message(true);
112                 if ($r_json) {
113                         echo json_encode(['error'=>$msg]);
114                 } else {
115                         echo  $msg. EOL ;
116                 }
117                 @unlink($src);
118                 killme();
119         }
120
121
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),
129                 dbesc($hash),
130                 dbesc($filename),
131                 dbesc($mimetype),
132                 intval($filesize),
133                 dbesc($filedata),
134                 dbesc($created),
135                 dbesc($created),
136                 dbesc('<' . $page_owner_cid . '>'),
137                 dbesc(''),
138                 dbesc(''),
139                 dbesc('')
140         );
141
142         @unlink($src);
143
144         if(! $r) {
145                 $msg =  t('File upload failed.');
146                 if ($r_json) {
147                         echo json_encode(['error'=>$msg]);
148                 } else {
149                         echo  $msg. EOL ;
150                 }
151                 killme();
152         }
153
154         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
155                 intval($page_owner_uid),
156                 dbesc($created),
157                 dbesc($hash)
158         );
159
160         if(! count($r)) {
161                 $msg = t('File upload failed.');
162                 if ($r_json) {
163                         echo json_encode(['error'=>$msg]);
164                 } else {
165                         echo  $msg. EOL ;
166                 }
167                 killme();
168         }
169
170         if ($r_json) { echo json_encode(['ok'=>true]); killme(); }
171
172         $lf = "\n";
173
174         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
175
176         killme();
177         // NOTREACHED
178 }