]> git.mxchange.org Git - friendica.git/blob - mod/wall_attach.php
Merge pull request #415 from 23n/patch-2
[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         if($a->argc > 1) {
9                 $nick = $a->argv[1];
10                 $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",
11                         dbesc($nick)
12                 );
13                 if(! count($r))
14                         return;
15
16         }
17         else
18                 return;
19
20         $can_post  = false;
21         $visitor   = 0;
22
23         $page_owner_uid   = $r[0]['uid'];
24         $page_owner_cid   = $r[0]['id'];
25         $page_owner_nick  = $r[0]['nickname'];
26         $community_page   = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
27
28         if((local_user()) && (local_user() == $page_owner_uid))
29                 $can_post = true;
30         else {
31                 if($community_page && remote_user()) {
32                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
33                                 intval(remote_user()),
34                                 intval($page_owner_uid)
35                         );
36                         if(count($r)) {
37                                 $can_post = true;
38                                 $visitor = remote_user();
39                         }
40                 }
41         }
42
43         if(! $can_post) {
44                 notice( t('Permission denied.') . EOL );
45                 killme();
46         }
47
48         if(! x($_FILES,'userfile'))
49                 killme();
50
51         $src      = $_FILES['userfile']['tmp_name'];
52         $filename = basename($_FILES['userfile']['name']);
53         $filesize = intval($_FILES['userfile']['size']);
54
55         $maxfilesize = get_config('system','maxfilesize');
56
57         if(($maxfilesize) && ($filesize > $maxfilesize)) {
58                 notice( sprintf(t('File exceeds size limit of %d'), $maxfilesize) . EOL);
59                 @unlink($src);
60                 return;
61         }
62
63         $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
64                 intval($page_owner_uid)
65         );
66
67         $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
68
69         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
70                 echo upgrade_message(true) . EOL ;
71                 @unlink($src);
72                 killme();
73         }
74
75
76         $filedata = @file_get_contents($src);
77         $mimetype = z_mime_content_type($filename);
78         $hash = random_string();
79         $created = datetime_convert();
80         $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
81                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
82                 intval($page_owner_uid),
83                 dbesc($hash),
84                 dbesc($filename),
85                 dbesc($mimetype),
86                 intval($filesize),
87                 dbesc($filedata),
88                 dbesc($created),
89                 dbesc($created),
90                 dbesc('<' . $page_owner_cid . '>'),
91                 dbesc(''),
92                 dbesc(''),
93                 dbesc('')
94         );              
95
96         @unlink($src);
97
98         if(! $r) {
99                 echo ( t('File upload failed.') . EOL);
100                 killme();
101         }
102
103         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
104                 intval($page_owner_uid),
105                 dbesc($created),
106                 dbesc($hash)
107         );
108
109         if(! count($r)) {
110                 echo ( t('File upload failed.') . EOL);
111                 killme();
112         }
113
114         $lf = '<br />';
115
116         if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
117                 $lf = "\n";
118
119         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
120         
121         killme();
122         // NOTREACHED
123 }