]> git.mxchange.org Git - friendica.git/blob - mod/item.php
modify tag handling and replies to stnet
[friendica.git] / mod / item.php
1 <?php
2
3 /**
4  *
5  * This is the POST destination for most all locally posted
6  * text stuff. This function handles status, wall-to-wall status, 
7  * local comments, and remote coments - that are posted on this site 
8  * (as opposed to being delivered in a feed).
9  * All of these become an "item" which is our basic unit of 
10  * information.
11  * Posts that originate externally or do not fall into the above 
12  * posting categories go through item_store() instead of this function. 
13  *
14  */  
15
16 function item_post(&$a) {
17
18         if((! local_user()) && (! remote_user()))
19                 return;
20
21         require_once('include/security.php');
22
23         $uid = local_user();
24
25         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
26
27         $parent_item = null;
28         $parent_contact = null;
29
30         if($parent) {
31                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
32                         intval($parent)
33                 );
34                 if(! count($r)) {
35                         notice( t('Unable to locate original post.') . EOL);
36                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
37                 }
38                 $parent_item = $r[0];
39                 if($parent_item['contact-id'] && $uid) {
40                         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
41                                 intval($parent_item['contact-id']),
42                                 intval($uid)
43                         );
44                         if(count($r))
45                                 $parent_contact = $r[0];
46                 }
47         }
48
49         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
50
51
52         if(! can_write_wall($a,$profile_uid)) {
53                 notice( t('Permission denied.') . EOL) ;
54                 return;
55         }
56
57         $user = null;
58
59         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
60                 intval($profile_uid)
61         );
62         if(count($r))
63                 $user = $r[0];
64         
65
66         $str_group_allow   = perms2str($_POST['group_allow']);
67         $str_contact_allow = perms2str($_POST['contact_allow']);
68         $str_group_deny    = perms2str($_POST['group_deny']);
69         $str_contact_deny  = perms2str($_POST['contact_deny']);
70
71         $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
72
73         if(($parent_item) && 
74                 (($parent_item['private']) 
75                         || strlen($parent_item['allow_cid']) 
76                         || strlen($parent_item['allow_gid']) 
77                         || strlen($parent_item['deny_cid']) 
78                         || strlen($parent_item['deny_gid'])
79                 )
80         ) {
81                 $private = 1;
82         }
83
84         $title             = notags(trim($_POST['title']));
85         $body              = escape_tags(trim($_POST['body']));
86         $location          = notags(trim($_POST['location']));
87         $coord             = notags(trim($_POST['coord']));
88         $verb              = notags(trim($_POST['verb']));
89         $emailcc           = notags(trim($_POST['emailcc']));
90
91         if(! strlen($body)) {
92                 notice( t('Empty post discarded.') . EOL );
93                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
94
95         }
96
97         // get contact info for poster
98
99         $author = null;
100         $self   = false;
101
102         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
103                 $self = true;
104                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
105                         intval($_SESSION['uid'])
106                 );
107         }
108         else {
109                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
110                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
111                                 intval($_SESSION['visitor_id'])
112                         );
113                 }
114         }
115
116         if(count($r)) {
117                 $author = $r[0];
118                 $contact_id = $author['id'];
119         }
120
121         // get contact info for owner
122         
123         if($profile_uid == $_SESSION['uid']) {
124                 $contact_record = $author;
125         }
126         else {
127                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
128                         intval($profile_uid)
129                 );
130                 if(count($r))
131                         $contact_record = $r[0];
132         }
133
134         $post_type = notags(trim($_POST['type']));
135
136         if($post_type === 'net-comment') {
137                 if($parent_item !== null) {
138                         if($parent_item['type'] === 'remote') {
139                                 $post_type = 'remote-comment';
140                         } 
141                         else {          
142                                 $post_type = 'wall-comment';
143                         }
144                 }
145         }
146
147
148         /**
149          *
150          * When a photo was uploaded into the message using the (profile wall) ajax 
151          * uploader, The permissions are initially set to disallow anybody but the
152          * owner from seeing it. This is because the permissions may not yet have been
153          * set for the post. If it's private, the photo permissions should be set
154          * appropriately. But we didn't know the final permissions on the post until
155          * now. So now we'll look for links of uploaded messages that are in the
156          * post and set them to the same permissions as the post itself.
157          *
158          */
159
160         $match = null;
161
162         if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
163                 $images = $match[1];
164                 if(count($images)) {
165                         foreach($images as $image) {
166                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
167                                         continue;
168                                 $image_uri = substr($image,strrpos($image,'/') + 1);
169                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
170                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
171                                         WHERE `resource-id` = '%s' AND `album` = '%s' ",
172                                         dbesc($str_contact_allow),
173                                         dbesc($str_group_allow),
174                                         dbesc($str_contact_deny),
175                                         dbesc($str_group_deny),
176                                         dbesc($image_uri),
177                                         dbesc( t('Wall Photos'))
178                                 );
179  
180                         }
181                 }
182         }
183
184         /**
185          * Fold multi-line [code] sequences
186          */
187
188         $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body); 
189
190         /**
191          * Look for any tags and linkify them
192          */
193
194         $str_tags = '';
195         $inform   = '';
196
197
198         $tags = get_tags($body);
199
200         if(($parent_contact) && ($parent_contact['network'] === 'stat') && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
201                 $body = '@' . $parent_contact['nick'] . ' ' . $body;
202                 $tags[] = '@' . $parent_contact['nick'];
203         }               
204
205         if(count($tags)) {
206                 foreach($tags as $tag) {
207                         if(strpos($tag,'#') === 0) {
208                                 $basetag = str_replace('_',' ',substr($tag,1));
209                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
210                                 if(strlen($str_tags))
211                                         $str_tags .= ',';
212                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
213                                 continue;
214                         }
215                         if(strpos($tag,'@') === 0) {
216                                 $stat = false;
217                                 $name = substr($tag,1);
218                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
219                                         $newname = $name;
220                                         $links = @lrdd($name);
221                                         if(count($links)) {
222                                                 foreach($links as $link) {
223                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
224                                         $profile = $link['@attributes']['href'];
225                                                         if($link['@attributes']['rel'] === 'salmon') {
226                                                                 if(strlen($inform))
227                                                                         $inform .= ',';
228                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
229                                                         }
230                                                 }
231                                         }
232                                 }
233                                 else {
234                                         $newname = $name;
235                                         if(strstr($name,'_')) {
236                                                 $newname = str_replace('_',' ',$name);
237                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
238                                                         dbesc($newname),
239                                                         intval($profile_uid)
240                                                 );
241                                         }
242                                         else {
243                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
244                                                         dbesc($name),
245                                                         intval($profile_uid)
246                                                 );
247                                         }
248                                         if(count($r)) {
249                                                 if($r[0]['network'] === 'stat')
250                                                         $stat = true;
251                                                 $profile = $r[0]['url'];
252                                                 $newname = $r[0]['name'];
253                                                 if(strlen($inform))
254                                                         $inform .= ',';
255                                                 $inform .= 'cid:' . $r[0]['id'];
256                                         }
257                                 }
258                                 if($profile) {
259                                         if(! $stat)
260                                                 $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);
261                                         $profile = str_replace(',','%2c',$profile);
262                                         if(strlen($str_tags))
263                                                 $str_tags .= ',';
264                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
265                                 }
266                         }
267                 }
268         }
269
270
271
272         $wall = 0;
273         if($post_type === 'wall' || $post_type === 'wall-comment')
274                 $wall = 1;
275
276         if(! strlen($verb))
277                 $verb = ACTIVITY_POST ;
278
279         $gravity = (($parent) ? 6 : 0 );
280  
281         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
282
283         $uri = item_new_uri($a->get_hostname(),$profile_uid);
284
285         $datarray = array();
286         $datarray['uid']           = $profile_uid;
287         $datarray['type']          = $post_type;
288         $datarray['wall']          = $wall;
289         $datarray['gravity']       = $gravity;
290         $datarray['contact-id']    = $contact_id;
291         $datarray['owner-name']    = $contact_record['name'];
292         $datarray['owner-link']    = $contact_record['url'];
293         $datarray['owner-avatar']  = $contact_record['thumb'];
294         $datarray['author-name']   = $author['name'];
295         $datarray['author-link']   = $author['url'];
296         $datarray['author-avatar'] = $author['thumb'];
297         $datarray['created']       = datetime_convert();
298         $datarray['edited']        = datetime_convert();
299         $datarray['changed']       = datetime_convert();
300         $datarray['uri']           = $uri;
301         $datarray['title']         = $title;
302         $datarray['body']          = $body;
303         $datarray['location']      = $location;
304         $datarray['coord']         = $coord;
305         $datarray['tag']           = $str_tags;
306         $datarray['inform']        = $inform;
307         $datarray['verb']          = $verb;
308         $datarray['allow_cid']     = $str_contact_allow;
309         $datarray['allow_gid']     = $str_group_allow;
310         $datarray['deny_cid']      = $str_contact_deny;
311         $datarray['deny_gid']      = $str_group_deny;
312         $datarray['private']       = $private;
313
314         /**
315          * These fields are for the convenience of plugins...
316          * 'self' if true indicates the owner is posting on their own wall
317          * If parent is 0 it is a top-level post.
318          */
319
320         $datarray['parent']        = $parent;
321         $datarray['self']          = $self;
322
323
324         call_hooks('post_local',$datarray);
325
326         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
327                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
328                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
329                 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 )",
330                 intval($datarray['uid']),
331                 dbesc($datarray['type']),
332                 intval($datarray['wall']),
333                 intval($datarray['gravity']),
334                 intval($datarray['contact-id']),
335                 dbesc($datarray['owner-name']),
336                 dbesc($datarray['owner-link']),
337                 dbesc($datarray['owner-avatar']),
338                 dbesc($datarray['author-name']),
339                 dbesc($datarray['author-link']),
340                 dbesc($datarray['author-avatar']),
341                 dbesc($datarray['created']),
342                 dbesc($datarray['edited']),
343                 dbesc($datarray['changed']),
344                 dbesc($datarray['uri']),
345                 dbesc($datarray['title']),
346                 dbesc($datarray['body']),
347                 dbesc($datarray['location']),
348                 dbesc($datarray['coord']),
349                 dbesc($datarray['tag']),
350                 dbesc($datarray['inform']),
351                 dbesc($datarray['verb']),
352                 dbesc($datarray['allow_cid']),
353                 dbesc($datarray['allow_gid']),
354                 dbesc($datarray['deny_cid']),
355                 dbesc($datarray['deny_gid']),
356                 intval($datarray['private'])
357         );
358
359         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
360                 dbesc($datarray['uri']));
361         if(count($r)) {
362                 $post_id = $r[0]['id'];
363                 logger('mod_item: saved item ' . $post_id);
364
365                 if($parent) {
366
367                         // This item is the last leaf and gets the comment box, clear any ancestors
368                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
369                                 dbesc(datetime_convert()),
370                                 intval($parent)
371                         );
372
373                         // Inherit ACL's from the parent item.
374
375                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
376                                 WHERE `id` = %d LIMIT 1",
377                                 dbesc($parent_item['allow_cid']),
378                                 dbesc($parent_item['allow_gid']),
379                                 dbesc($parent_item['deny_cid']),
380                                 dbesc($parent_item['deny_gid']),
381                                 intval($parent_item['private']),
382                                 intval($post_id)
383                         );
384
385                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
386                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
387                                 require_once('bbcode.php');
388                                 $from = $author['name'];
389                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
390                                 $email_tpl = replace_macros($tpl, array(
391                                         '$sitename' => $a->config['sitename'],
392                                         '$siteurl' =>  $a->get_baseurl(),
393                                         '$username' => $user['username'],
394                                         '$email' => $user['email'],
395                                         '$from' => $from,
396                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
397                                         '$body' => strip_tags(bbcode($datarray['body']))
398                                 ));
399
400                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
401                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
402                         }
403                 }
404                 else {
405                         $parent = $post_id;
406
407                         // let me know if somebody did a wall-to-wall post on my profile
408
409                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
410                                 require_once('bbcode.php');
411                                 $from = $author['name'];
412                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
413                                 $email_tpl = replace_macros($tpl, array(
414                                         '$sitename' => $a->config['sitename'],
415                                         '$siteurl' =>  $a->get_baseurl(),
416                                         '$username' => $user['username'],
417                                         '$email' => $user['email'],
418                                         '$from' => $from,
419                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
420                                         '$body' => strip_tags(bbcode($datarray['body']))
421                                 ));
422
423                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
424                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
425                         }
426                 }
427
428                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
429                         WHERE `id` = %d LIMIT 1",
430                         intval($parent),
431                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
432                         dbesc(datetime_convert()),
433                         intval($post_id)
434                 );
435
436                 // photo comments turn the corresponding item visible to the profile wall
437                 // This way we don't see every picture in your new photo album posted to your wall at once.
438                 // They will show up as people comment on them.
439
440                 if(! $parent_item['visible']) {
441                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
442                                 intval($parent_item['id'])
443                         );
444                 }
445         }
446         else {
447                 logger('mod_item: unable to retrieve post that was just stored.');
448                 notify( t('System error. Post not saved.'));
449                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
450                 // NOTREACHED
451         }
452
453         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
454
455         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
456
457         proc_run($php_path, "include/notifier.php", $notify_type, "$post_id");
458
459         $datarray['id'] = $post_id;
460
461         call_hooks('post_local_end', $datarray);
462
463         if(strlen($emailcc) && $profile_uid == local_user()) {
464                 $erecips = explode(',', $emailcc);
465                 if(count($erecips)) {
466                         foreach($erecips as $recip) {
467                                 $addr = trim($recip);
468                                 if(! strlen($addr))
469                                         continue;
470                                 $disclaimer = '<hr />' . t('This message was sent to you by ') . $a->user['username'] 
471                                         . t(', a member of the Friendika social network.') . '<br />';
472                                 $disclaimer .= t('You may visit them online at') . ' ' 
473                                         . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '<br />';
474                                 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . '<br />'; 
475
476                                 $subject  = '[Friendika]' . ' ' . $a->user['username'] . ' ' . t('posted an update.');
477                                 $headers  = 'From: ' . $a->user['username'] . ' <' . $a->user['email'] . '>' . "\n";
478                                 $headers .= 'MIME-Version: 1.0' . "\n";
479                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
480                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
481                                 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
482                                 $html    = prepare_body($datarray);
483                                 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
484                                 @mail($addr, $subject, $message, $headers);
485                         }
486                 }
487         }
488
489         goaway($a->get_baseurl() . "/" . $_POST['return'] );
490         // NOTREACHED
491 }
492
493
494
495
496
497 function item_content(&$a) {
498
499         if((! local_user()) && (! remote_user()))
500                 return;
501
502         require_once('include/security.php');
503
504         $uid = $_SESSION['uid'];
505
506         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
507
508                 // locate item to be deleted
509
510                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
511                         intval($a->argv[2])
512                 );
513
514                 if(! count($r)) {
515                         notice( t('Item not found.') . EOL);
516                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
517                 }
518                 $item = $r[0];
519
520                 // check if logged in user is either the author or owner of this item
521
522                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
523
524                         // delete the item
525
526                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
527                                 dbesc(datetime_convert()),
528                                 dbesc(datetime_convert()),
529                                 intval($item['id'])
530                         );
531
532                         // If item is a link to a photo resource, nuke all the associated photos 
533                         // (visitors will not have photo resources)
534                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
535                         // generate a resource-id and therefore aren't intimately linked to the item. 
536
537                         if(strlen($item['resource-id'])) {
538                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
539                                         dbesc($item['resource-id']),
540                                         intval($item['uid'])
541                                 );
542                                 // ignore the result
543                         }
544
545                         // If it's the parent of a comment thread, kill all the kids
546
547                         if($item['uri'] == $item['parent-uri']) {
548                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
549                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
550                                         dbesc(datetime_convert()),
551                                         dbesc(datetime_convert()),
552                                         dbesc($item['parent-uri']),
553                                         intval($item['uid'])
554                                 );
555                                 // ignore the result
556                         }
557                         else {
558                                 // ensure that last-child is set in case the comment that had it just got wiped.
559                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
560                                         dbesc(datetime_convert()),
561                                         dbesc($item['parent-uri']),
562                                         intval($item['uid'])
563                                 );
564                                 // who is the last child now? 
565                                 $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",
566                                         dbesc($item['parent-uri']),
567                                         intval($item['uid'])
568                                 );
569                                 if(count($r)) {
570                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
571                                                 intval($r[0]['id'])
572                                         );
573                                 }       
574                         }
575                         $drop_id = intval($item['id']);
576                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
577                         
578                         // send the notification upstream/downstream as the case may be
579
580                         //proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &", array(), $foo));
581                         proc_run($php_path,"include/notifier.php","drop","$drop_id");
582
583                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
584                         //NOTREACHED
585                 }
586                 else {
587                         notice( t('Permission denied.') . EOL);
588                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
589                         //NOTREACHED
590                 }
591         }
592 }