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