]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Class file relocations
[friendica.git] / mod / wall_attach.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
6
7 require_once('include/attach.php');
8 require_once('include/datetime.php');
9
10 function wall_attach_post(App $a) {
11
12         $r_json = (x($_GET,'response') && $_GET['response']=='json');
13
14         if($a->argc > 1) {
15                 $nick = $a->argv[1];
16                 $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                         dbesc($nick)
18                 );
19                 if (! DBM::is_result($r)) {
20                         if ($r_json) {
21                                 echo json_encode(array('error'=>t('Invalid request.')));
22                                 killme();
23                         }
24                         return;
25         }
26
27         } else {
28                 if ($r_json) {
29                         echo json_encode(array('error'=>t('Invalid request.')));
30                         killme();
31                 }
32                 return;
33         }
34
35         $can_post  = false;
36         $visitor   = 0;
37
38         $page_owner_uid   = $r[0]['uid'];
39         $page_owner_cid   = $r[0]['id'];
40         $page_owner_nick  = $r[0]['nickname'];
41         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
42
43         if((local_user()) && (local_user() == $page_owner_uid))
44                 $can_post = true;
45         else {
46                 if($community_page && remote_user()) {
47                         $contact_id = 0;
48                         if(is_array($_SESSION['remote'])) {
49                                 foreach($_SESSION['remote'] as $v) {
50                                         if($v['uid'] == $page_owner_uid) {
51                                                 $contact_id = $v['cid'];
52                                                 break;
53                                         }
54                                 }
55                         }
56                         if($contact_id) {
57
58                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
59                                         intval($contact_id),
60                                         intval($page_owner_uid)
61                                 );
62                                 if (DBM::is_result($r)) {
63                                         $can_post = true;
64                                         $visitor = $contact_id;
65                                 }
66                         }
67                 }
68         }
69         if(! $can_post) {
70                 if ($r_json) {
71                         echo json_encode(array('error'=>t('Permission denied.')));
72                         killme();
73                 }
74                 notice( t('Permission denied.') . EOL );
75                 killme();
76         }
77
78         if(! x($_FILES,'userfile')) {
79                 if ($r_json) {
80                         echo json_encode(array('error'=>t('Invalid request.')));
81                 }
82                 killme();
83         }
84
85         $src      = $_FILES['userfile']['tmp_name'];
86         $filename = basename($_FILES['userfile']['name']);
87         $filesize = intval($_FILES['userfile']['size']);
88
89         $maxfilesize = Config::get('system','maxfilesize');
90
91         /* Found html code written in text field of form,
92          * when trying to upload a file with filesize
93          * greater than upload_max_filesize. Cause is unknown.
94          * Then Filesize gets <= 0.
95          */
96
97         if($filesize <=0) {
98                 $msg = t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?'));
99                 if ($r_json) {
100                         echo json_encode(array('error'=>$msg));
101                 } else {
102                         notice( $msg. EOL );
103                 }
104                 @unlink($src);
105                 killme();
106         }
107
108         if(($maxfilesize) && ($filesize > $maxfilesize)) {
109                 $msg = sprintf(t('File exceeds size limit of %s'), formatBytes($maxfilesize));
110                 if ($r_json) {
111                         echo json_encode(array('error'=>$msg));
112                 } else {
113                         echo  $msg. EOL ;
114                 }
115                 @unlink($src);
116                 killme();
117         }
118
119         $filedata = @file_get_contents($src);
120         $mimetype = z_mime_content_type($filename);
121         $hash = get_guid(64);
122         $created = datetime_convert();
123
124         $fields = array('uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
125                 'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
126                 'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => '');
127
128         $r = dba::insert('attach', $fields);
129
130         @unlink($src);
131
132         if(! $r) {
133                 $msg =  t('File upload failed.');
134                 if ($r_json) {
135                         echo json_encode(array('error'=>$msg));
136                 } else {
137                         echo  $msg. EOL ;
138                 }
139                 killme();
140         }
141
142         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
143                 intval($page_owner_uid),
144                 dbesc($created),
145                 dbesc($hash)
146         );
147
148         if (! DBM::is_result($r)) {
149                 $msg = t('File upload failed.');
150                 if ($r_json) {
151                         echo json_encode(array('error'=>$msg));
152                 } else {
153                         echo  $msg. EOL ;
154                 }
155                 killme();
156         }
157
158         if ($r_json) {
159                 echo json_encode(array('ok'=>true));
160                 killme();
161         }
162
163         $lf = "\n";
164
165         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
166
167         killme();
168         // NOTREACHED
169 }