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