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