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