]> git.mxchange.org Git - friendica.git/blob - mod/item.php
cb89745790c702f6db3bf350ac8d8eab0490c119
[friendica.git] / mod / item.php
1 <?php
2
3 function sanitise_acl(&$item) {
4         $item = '<' . intval(notags(trim($item))) . '>';
5 }
6
7 function item_post(&$a) {
8
9         if((! local_user()) && (! remote_user()))
10                 return;
11
12         require_once('include/security.php');
13
14         $uid = $_SESSION['uid'];
15
16         if(($a->argc == 3) && ($a->argv[1] == 'drop') && intval($a->argv[2])) {
17                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
18                         intval($argv[2])
19                 );
20                 if(! count($r)) {
21                         notice("Permission denied." . EOL);
22                         goway($a->get_baseurl() . $_SESSION['return_url']);
23                 }
24                 $item = $r[0];
25                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
26                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' WHERE `id` = %d LIMIT 1",
27                         dbesc(datetime_convert()),
28                         intval($item['id'])
29                 );
30                 if($item['uri'] == $item['parent-uri']) {
31                         $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' WHERE `parent-uri` = '%s',
32                                 dbesc(datetime_convert()),
33                                 dbesc($item['parent-uri'])
34                         );
35                 }
36
37                 $url = $a->get_baseurl();
38                 $drop_id = intval($item['id'])l
39
40                 proc_close(proc_open("php include/notifier.php \"$url\" \"$drop" \"$drop_id\" > notify.log &",
41                         array(),$foo));
42
43                 goway($a->get_baseurl() . $_SESSION['return_url']);
44
45         }
46
47         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
48
49         $parent_item = null;
50
51         if($parent) {
52                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
53                         intval($parent)
54                 );
55                 if(! count($r)) {
56                         notice("Unable to locate original post." . EOL);
57                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
58                 }
59                 $parent_item = $r[0];
60         }
61
62         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
63
64         if(! can_write_wall($a,$profile_uid)) {
65                 notice("Permission denied." . EOL) ;
66                 return;
67         }
68         
69         $str_group_allow = '';
70         $group_allow = $_POST['group_allow'];
71         if(is_array($group_allow)) {
72                 array_walk($group_allow,'sanitise_acl');
73                 $str_group_allow = implode('',$group_allow);
74         }
75
76         $str_contact_allow = '';
77         $contact_allow = $_POST['contact_allow'];
78         if(is_array($contact_allow)) {
79                 array_walk($contact_allow,'sanitise_acl');
80                 $str_contact_allow = implode('',$contact_allow);
81         }
82
83         $str_group_deny = '';
84         $group_deny = $_POST['group_deny'];
85         if(is_array($group_deny)) {
86                 array_walk($group_deny,'sanitise_acl');
87                 $str_group_deny = implode('',$group_deny);
88         }
89
90         $str_contact_deny = '';
91         $contact_deny = $_POST['contact_deny'];
92         if(is_array($contact_deny)) {
93                 array_walk($contact_deny,'sanitise_acl');
94                 $str_contact_deny = implode('',$contact_deny);
95         }
96
97
98         $body = escape_tags(trim($_POST['body']));
99
100         if(! strlen($body)) {
101                 notice("Empty post discarded." . EOL );
102                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
103
104         }
105
106         // get contact info for poster
107
108         if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
109                 $contact_id = $_SESSION['visitor_id'];
110         }
111         else {
112                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
113                         intval($_SESSION['uid']));
114                 if(count($r))
115                         $contact_id = $r[0]['id'];
116         }
117
118         // get contact info for owner
119         
120         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
121                 intval($profile_uid)
122         );
123         if(count($r))
124                 $contact_record = $r[0];
125
126         $post_type == notags(trim($_POST['type']));
127
128         if($post_type == 'net-comment') {
129                 if($parent_item !== null && $parent_item['type'] != 'remote')           
130                         $post_type = 'wall-comment';
131         }
132
133         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
134
135         if(($_POST['type'] == 'wall') || ($_POST['type'] == 'wall-comment') || ($_POST['type'] == 'net-comment')) {
136
137                 do {
138                         $dups = false;
139                         $hash = random_string();
140
141                         $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $hash;
142
143                         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
144                         dbesc($uri));
145                         if(count($r))
146                                 $dups = true;
147                 } while($dups == true);
148
149
150                 $r = q("INSERT INTO `item` (`uid`,`type`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
151                         `edited`, `uri`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
152                         VALUES( %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
153                         intval($profile_uid),
154                         dbesc($_POST['type']),
155                         intval($contact_id),
156                         dbesc($contact_record['name']),
157                         dbesc($contact_record['url']),
158                         dbesc($contact_record['thumb']),
159                         datetime_convert(),
160                         datetime_convert(),
161                         dbesc($uri),
162                         dbesc(escape_tags(trim($_POST['body']))),
163                         dbesc($str_contact_allow),
164                         dbesc($str_group_allow),
165                         dbesc($str_contact_deny),
166                         dbesc($str_group_deny)
167
168                 );
169                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
170                         dbesc($uri));
171                 if(count($r)) {
172                         $post_id = $r[0]['id'];
173
174                         if($parent) {
175
176                                 // This item is the last leaf and gets the comment box, clear any ancestors
177                                 $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent` = %d ",
178                                         intval($parent)
179                                 );
180
181                                 // Inherit ACL's from the parent item.
182                                 // TODO merge with subsequent UPDATE operation and save a db write 
183
184                                 $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
185                                         WHERE `id` = %d LIMIT 1",
186                                         dbesc($parent_item['allow_cid']),
187                                         dbesc($parent_item['allow_gid']),
188                                         dbesc($parent_item['deny_cid']),
189                                         dbesc($parent_item['deny_gid']),
190                                         intval($post_id)
191                                 );
192                         }
193                         else {
194                                 $parent = $post_id;
195                         }
196
197                         $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `last-child` = 1, `visible` = 1
198                                 WHERE `id` = %d LIMIT 1",
199                                 intval($parent),
200                                 dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
201                                 intval($post_id)
202                         );
203                 }
204
205                 $url = $a->get_baseurl();
206
207                 proc_close(proc_open("php include/notifier.php \"$url\" \"$notify_type\" \"$post_id\" > notify.log &",
208                         array(),$foo));
209
210         }
211         goaway($a->get_baseurl() . "/" . $_POST['return'] );
212         return; // NOTREACHED
213 }