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