]> git.mxchange.org Git - friendica.git/blob - mod/item.php
display the fact that a conversation is private without disclosing the details of...
[friendica.git] / mod / item.php
1 <?php
2
3 // This is the POST destination for most all locally posted
4 // text stuff. This function handles status, wall-to-wall status, 
5 // local comments, and remote coments - that are posted on this site 
6 // (as opposed to being delivered in a feed).
7 // All of these become an "item" which is our basic unit of 
8 // information. 
9
10 function item_post(&$a) {
11
12         if((! local_user()) && (! remote_user()))
13                 return;
14
15         require_once('include/security.php');
16
17         $uid = local_user();
18
19         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
20
21         $parent_item = null;
22
23         if($parent) {
24                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
25                         intval($parent)
26                 );
27                 if(! count($r)) {
28                         notice( t('Unable to locate original post.') . EOL);
29                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
30                 }
31                 $parent_item = $r[0];
32         }
33
34         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
35
36
37         if(! can_write_wall($a,$profile_uid)) {
38                 notice( t('Permission denied.') . EOL) ;
39                 return;
40         }
41
42         $user = null;
43
44         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
45                 intval($profile_uid)
46         );
47         if(count($r))
48                 $user = $r[0];
49         
50
51         $str_group_allow   = perms2str($_POST['group_allow']);
52         $str_contact_allow = perms2str($_POST['contact_allow']);
53         $str_group_deny    = perms2str($_POST['group_deny']);
54         $str_contact_deny  = perms2str($_POST['contact_deny']);
55
56         $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
57
58         $title             = notags(trim($_POST['title']));
59         $body              = escape_tags(trim($_POST['body']));
60         $location          = notags(trim($_POST['location']));
61         $coord             = notags(trim($_POST['coord']));
62         $verb              = notags(trim($_POST['verb']));
63
64         if(! strlen($body)) {
65                 notice( t('Empty post discarded.') . EOL );
66                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
67
68         }
69
70         // get contact info for poster
71
72         $author = null;
73
74         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
75                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
76                         intval($_SESSION['uid'])
77                 );
78         }
79         else {
80                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
81                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
82                                 intval($_SESSION['visitor_id'])
83                         );
84                 }
85         }
86
87         if(count($r)) {
88                 $author = $r[0];
89                 $contact_id = $author['id'];
90         }
91
92         // get contact info for owner
93         
94         if($profile_uid == $_SESSION['uid']) {
95                 $contact_record = $author;
96         }
97         else {
98                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
99                         intval($profile_uid)
100                 );
101                 if(count($r))
102                         $contact_record = $r[0];
103         }
104
105         $post_type = notags(trim($_POST['type']));
106
107         if($post_type === 'net-comment') {
108                 if($parent_item !== null) {
109                         if($parent_item['type'] === 'remote') {
110                                 $post_type = 'remote-comment';
111                         } 
112                         else {          
113                                 $post_type = 'wall-comment';
114                         }
115                 }
116         }
117
118         $str_tags = '';
119         $inform   = '';
120
121         $tags = get_tags($body);
122
123
124         if(count($tags)) {
125                 foreach($tags as $tag) {
126                         if(strpos($tag,'#') === 0) {
127                                 $basetag = str_replace('_',' ',substr($tag,1));
128                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
129                                 if(strlen($str_tags))
130                                         $str_tags .= ',';
131                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
132                                 continue;
133                         }
134                         if(strpos($tag,'@') === 0) {
135                                 $name = substr($tag,1);
136                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
137                                         $newname = $name;
138                                         $links = @lrdd($name);
139                                         if(count($links)) {
140                                                 foreach($links as $link) {
141                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
142                                         $profile = $link['@attributes']['href'];
143                                                         if($link['@attributes']['rel'] === 'salmon') {
144                                                                 if(strlen($inform))
145                                                                         $inform .= ',';
146                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
147                                                         }
148                                                 }
149                                         }
150                                 }
151                                 else {
152                                         $newname = $name;
153                                         if(strstr($name,'_')) {
154                                                 $newname = str_replace('_',' ',$name);
155                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
156                                                         dbesc($newname),
157                                                         intval($profile_uid)
158                                                 );
159                                         }
160                                         else {
161                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
162                                                         dbesc($name),
163                                                         intval($profile_uid)
164                                                 );
165                                         }
166                                         if(count($r)) {
167                                                 $profile = $r[0]['url'];
168                                                 $newname = $r[0]['name'];
169                                                 if(strlen($inform))
170                                                         $inform .= ',';
171                                                 $inform .= 'cid:' . $r[0]['id'];
172                                         }
173                                 }
174                                 if($profile) {
175                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
176                                         $profile = str_replace(',','%2c',$profile);
177                                         if(strlen($str_tags))
178                                                 $str_tags .= ',';
179                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
180                                 }
181                         }
182                 }
183         }
184
185         $wall = 0;
186         if($post_type === 'wall' || $post_type === 'wall-comment')
187                 $wall = 1;
188
189         if(! strlen($verb))
190                 $verb = ACTIVITY_POST ;
191
192         $gravity = (($parent) ? 6 : 0 );
193  
194         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
195
196         $uri = item_new_uri($a->get_hostname(),$profile_uid);
197
198         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
199                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
200                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
201                 VALUES( %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d )",
202                 intval($profile_uid),
203                 dbesc($post_type),
204                 intval($wall),
205                 intval($gravity),
206                 intval($contact_id),
207                 dbesc($contact_record['name']),
208                 dbesc($contact_record['url']),
209                 dbesc($contact_record['thumb']),
210                 dbesc($author['name']),
211                 dbesc($author['url']),
212                 dbesc($author['thumb']),
213                 dbesc(datetime_convert()),
214                 dbesc(datetime_convert()),
215                 dbesc(datetime_convert()),
216                 dbesc($uri),
217                 dbesc($title),
218                 dbesc($body),
219                 dbesc($location),
220                 dbesc($coord),
221                 dbesc($str_tags),
222                 dbesc($inform),
223                 dbesc($verb),
224                 dbesc($str_contact_allow),
225                 dbesc($str_group_allow),
226                 dbesc($str_contact_deny),
227                 dbesc($str_group_deny),
228                 intval($private)
229         );
230         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
231                 dbesc($uri));
232         if(count($r)) {
233                 $post_id = $r[0]['id'];
234                 logger('mod_item: saved item ' . $post_id);
235
236                 if($parent) {
237
238                         // This item is the last leaf and gets the comment box, clear any ancestors
239                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
240                                 dbesc(datetime_convert()),
241                                 intval($parent)
242                         );
243
244                         // Inherit ACL's from the parent item.
245                         // TODO merge with subsequent UPDATE operation and save a db write 
246
247                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
248                                 WHERE `id` = %d LIMIT 1",
249                                 dbesc($parent_item['allow_cid']),
250                                 dbesc($parent_item['allow_gid']),
251                                 dbesc($parent_item['deny_cid']),
252                                 dbesc($parent_item['deny_gid']),
253                                 intval($parent_item['private']),
254                                 intval($post_id)
255                         );
256
257                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
258                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
259                                 require_once('bbcode.php');
260                                 $from = $author['name'];
261                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
262                                 $email_tpl = replace_macros($tpl, array(
263                                         '$sitename' => $a->config['sitename'],
264                                         '$siteurl' =>  $a->get_baseurl(),
265                                         '$username' => $user['username'],
266                                         '$email' => $user['email'],
267                                         '$from' => $from,
268                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
269                                         '$body' => strip_tags(bbcode($body))
270                                 ));
271
272                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
273                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
274                         }
275                 }
276                 else {
277                         $parent = $post_id;
278
279                         // let me know if somebody did a wall-to-wall post on my profile
280
281                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
282                                 require_once('bbcode.php');
283                                 $from = $author['name'];
284                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
285                                 $email_tpl = replace_macros($tpl, array(
286                                         '$sitename' => $a->config['sitename'],
287                                         '$siteurl' =>  $a->get_baseurl(),
288                                         '$username' => $user['username'],
289                                         '$email' => $user['email'],
290                                         '$from' => $from,
291                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
292                                         '$body' => strip_tags(bbcode($body))
293                                 ));
294
295                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
296                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
297                         }
298                 }
299
300                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
301                         WHERE `id` = %d LIMIT 1",
302                         intval($parent),
303                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
304                         dbesc(datetime_convert()),
305                         intval($post_id)
306                 );
307
308                 // photo comments turn the corresponding item visible to the profile wall
309                 // This way we don't see every picture in your new photo album posted to your wall at once.
310                 // They will show up as people comment on them.
311
312                 if(! $parent_item['visible']) {
313                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
314                                 intval($parent_item['id'])
315                         );
316                 }
317         }
318
319         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
320
321         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
322
323         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
324                 array(),$foo));
325
326         goaway($a->get_baseurl() . "/" . $_POST['return'] );
327         return; // NOTREACHED
328 }
329
330 function item_content(&$a) {
331
332         if((! local_user()) && (! remote_user()))
333                 return;
334
335         require_once('include/security.php');
336
337         $uid = $_SESSION['uid'];
338
339         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
340
341                 // locate item to be deleted
342
343                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
344                         intval($a->argv[2])
345                 );
346
347                 if(! count($r)) {
348                         notice( t('Item not found.') . EOL);
349                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
350                 }
351                 $item = $r[0];
352
353                 // check if logged in user is either the author or owner of this item
354
355                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
356
357                         // delete the item
358
359                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
360                                 dbesc(datetime_convert()),
361                                 dbesc(datetime_convert()),
362                                 intval($item['id'])
363                         );
364
365                         // If item is a link to a photo resource, nuke all the associated photos 
366                         // (visitors will not have photo resources)
367                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
368                         // generate a resource-id and therefore aren't intimately linked to the item. 
369
370                         if(strlen($item['resource-id'])) {
371                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
372                                         dbesc($item['resource-id']),
373                                         intval($item['uid'])
374                                 );
375                                 // ignore the result
376                         }
377
378                         // If it's the parent of a comment thread, kill all the kids
379
380                         if($item['uri'] == $item['parent-uri']) {
381                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
382                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
383                                         dbesc(datetime_convert()),
384                                         dbesc(datetime_convert()),
385                                         dbesc($item['parent-uri']),
386                                         intval($item['uid'])
387                                 );
388                                 // ignore the result
389                         }
390                         else {
391                                 // ensure that last-child is set in case the comment that had it just got wiped.
392                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
393                                         dbesc(datetime_convert()),
394                                         dbesc($item['parent-uri']),
395                                         intval($item['uid'])
396                                 );
397                                 // who is the last child now? 
398                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
399                                         dbesc($item['parent-uri']),
400                                         intval($item['uid'])
401                                 );
402                                 if(count($r)) {
403                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
404                                                 intval($r[0]['id'])
405                                         );
406                                 }       
407                         }
408                         $drop_id = intval($item['id']);
409                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
410                         
411                         // send the notification upstream/downstream as the case may be
412
413                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
414                                 array(), $foo));
415
416                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
417                         return; //NOTREACHED
418                 }
419                 else {
420                         notice( t('Permission denied.') . EOL);
421                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
422                         //NOTREACHED
423                 }
424         }
425 }