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