]> git.mxchange.org Git - friendica.git/blob - wall_attach.php
Merge pull request #956 from annando/master
[friendica.git] / 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                         $cid = 0;
33                         if(is_array($_SESSION['remote'])) {
34                                 foreach($_SESSION['remote'] as $v) {
35                                         if($v['uid'] == $page_owner_uid) {
36                                                 $cid = $v['cid'];
37                                                 break;
38                                         }
39                                 }
40                         }
41                         if($cid) {
42
43                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
44                                         intval($cid),
45                                         intval($page_owner_uid)
46                                 );
47                                 if(count($r)) {
48                                         $can_post = true;
49                                         $visitor = $cid;
50                                 }
51                         }
52                 }
53         }
54         if(! $can_post) {
55                 notice( t('Permission denied.') . EOL );
56                 killme();
57         }
58
59         if(! x($_FILES,'userfile'))
60                 killme();
61
62         $src      = $_FILES['userfile']['tmp_name'];
63         $filename = basename($_FILES['userfile']['name']);
64         $filesize = intval($_FILES['userfile']['size']);
65
66         $maxfilesize = get_config('system','maxfilesize');
67
68         /* Found html code written in text field of form,
69          * when trying to upload a file with filesize
70          * greater than upload_max_filesize. Cause is unknown.
71          * Then Filesize gets <= 0.
72          */
73
74         if($filesize <=0) {
75                 notice(t('Sorry, maybe your upload is bigger than the PHP configuration allows') . EOL .(t('Or - did you try to upload an empty file?')) . EOL);
76                 @unlink($src);
77                 killme();
78         }
79
80         if(($maxfilesize) && ($filesize > $maxfilesize)) {
81                 notice( sprintf(t('File exceeds size limit of %d'), $maxfilesize) . EOL);
82                 @unlink($src);
83                 return;
84         }
85
86         $r = q("select sum(octet_length(data)) as total from attach where uid = %d ",
87                 intval($page_owner_uid)
88         );
89
90         $limit = service_class_fetch($page_owner_uid,'attach_upload_limit');
91
92         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
93                 echo upgrade_message(true) . EOL ;
94                 @unlink($src);
95                 killme();
96         }
97
98
99         $filedata = @file_get_contents($src);
100         $mimetype = z_mime_content_type($filename);
101         $hash = random_string();
102         $created = datetime_convert();
103         $r = q("INSERT INTO `attach` ( `uid`, `hash`, `filename`, `filetype`, `filesize`, `data`, `created`, `edited`, `allow_cid`, `allow_gid`,`deny_cid`, `deny_gid` )
104                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
105                 intval($page_owner_uid),
106                 dbesc($hash),
107                 dbesc($filename),
108                 dbesc($mimetype),
109                 intval($filesize),
110                 dbesc($filedata),
111                 dbesc($created),
112                 dbesc($created),
113                 dbesc('<' . $page_owner_cid . '>'),
114                 dbesc(''),
115                 dbesc(''),
116                 dbesc('')
117         );              
118
119         @unlink($src);
120
121         if(! $r) {
122                 echo ( t('File upload failed.') . EOL);
123                 killme();
124         }
125
126         $r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
127                 intval($page_owner_uid),
128                 dbesc($created),
129                 dbesc($hash)
130         );
131
132         if(! count($r)) {
133                 echo ( t('File upload failed.') . EOL);
134                 killme();
135         }
136
137         $lf = "\n";
138
139         echo  $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
140         
141         killme();
142         // NOTREACHED
143 }