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