]> git.mxchange.org Git - friendica.git/blob - mod/item.php
cc1886c17b64f2dfd193bfec3a6d5a2308b5e816
[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         $emailcc           = notags(trim($_POST['emailcc']));
75
76         if(! strlen($body)) {
77                 notice( t('Empty post discarded.') . EOL );
78                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
79
80         }
81
82         // get contact info for poster
83
84         $author = null;
85         $self   = false;
86
87         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
88                 $self = true;
89                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
90                         intval($_SESSION['uid'])
91                 );
92         }
93         else {
94                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
95                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
96                                 intval($_SESSION['visitor_id'])
97                         );
98                 }
99         }
100
101         if(count($r)) {
102                 $author = $r[0];
103                 $contact_id = $author['id'];
104         }
105
106         // get contact info for owner
107         
108         if($profile_uid == $_SESSION['uid']) {
109                 $contact_record = $author;
110         }
111         else {
112                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
113                         intval($profile_uid)
114                 );
115                 if(count($r))
116                         $contact_record = $r[0];
117         }
118
119         $post_type = notags(trim($_POST['type']));
120
121         if($post_type === 'net-comment') {
122                 if($parent_item !== null) {
123                         if($parent_item['type'] === 'remote') {
124                                 $post_type = 'remote-comment';
125                         } 
126                         else {          
127                                 $post_type = 'wall-comment';
128                         }
129                 }
130         }
131
132
133         /**
134          *
135          * When a photo was uploaded into the message using the (profile wall) ajax 
136          * uploader, The permissions are initially set to disallow anybody but the
137          * owner from seeing it. This is because the permissions may not yet have been
138          * set for the post. If it's private, the photo permissions should be set
139          * appropriately. But we didn't know the final permissions on the post until
140          * now. So now we'll look for links of uploaded messages that are in the
141          * post and set them to the same permissions as the post itself.
142          *
143          */
144
145         $match = null;
146
147         if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
148                 $images = $match[1];
149                 if(count($images)) {
150                         foreach($images as $image) {
151                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
152                                         continue;
153                                 $image_uri = substr($image,strrpos($image,'/') + 1);
154                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
155                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
156                                         WHERE `resource-id` = '%s' AND `album` = '%s' ",
157                                         dbesc($str_contact_allow),
158                                         dbesc($str_group_allow),
159                                         dbesc($str_contact_deny),
160                                         dbesc($str_group_deny),
161                                         dbesc($image_uri),
162                                         dbesc( t('Wall Photos'))
163                                 );
164  
165                         }
166                 }
167         }
168
169         /**
170          * Fold multi-line [code] sequences
171          */
172
173         $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body); 
174
175         /**
176          * Look for any tags and linkify them
177          */
178
179         $str_tags = '';
180         $inform   = '';
181
182         $tags = get_tags($body);
183
184
185         if(count($tags)) {
186                 foreach($tags as $tag) {
187                         if(strpos($tag,'#') === 0) {
188                                 $basetag = str_replace('_',' ',substr($tag,1));
189                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
190                                 if(strlen($str_tags))
191                                         $str_tags .= ',';
192                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
193                                 continue;
194                         }
195                         if(strpos($tag,'@') === 0) {
196                                 $name = substr($tag,1);
197                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
198                                         $newname = $name;
199                                         $links = @lrdd($name);
200                                         if(count($links)) {
201                                                 foreach($links as $link) {
202                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
203                                         $profile = $link['@attributes']['href'];
204                                                         if($link['@attributes']['rel'] === 'salmon') {
205                                                                 if(strlen($inform))
206                                                                         $inform .= ',';
207                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
208                                                         }
209                                                 }
210                                         }
211                                 }
212                                 else {
213                                         $newname = $name;
214                                         if(strstr($name,'_')) {
215                                                 $newname = str_replace('_',' ',$name);
216                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
217                                                         dbesc($newname),
218                                                         intval($profile_uid)
219                                                 );
220                                         }
221                                         else {
222                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
223                                                         dbesc($name),
224                                                         intval($profile_uid)
225                                                 );
226                                         }
227                                         if(count($r)) {
228                                                 $profile = $r[0]['url'];
229                                                 $newname = $r[0]['name'];
230                                                 if(strlen($inform))
231                                                         $inform .= ',';
232                                                 $inform .= 'cid:' . $r[0]['id'];
233                                         }
234                                 }
235                                 if($profile) {
236                                         $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);
237                                         $profile = str_replace(',','%2c',$profile);
238                                         if(strlen($str_tags))
239                                                 $str_tags .= ',';
240                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
241                                 }
242                         }
243                 }
244         }
245
246         $wall = 0;
247         if($post_type === 'wall' || $post_type === 'wall-comment')
248                 $wall = 1;
249
250         if(! strlen($verb))
251                 $verb = ACTIVITY_POST ;
252
253         $gravity = (($parent) ? 6 : 0 );
254  
255         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
256
257         $uri = item_new_uri($a->get_hostname(),$profile_uid);
258
259         $datarray = array();
260         $datarray['uid']           = $profile_uid;
261         $datarray['type']          = $post_type;
262         $datarray['wall']          = $wall;
263         $datarray['gravity']       = $gravity;
264         $datarray['contact-id']    = $contact_id;
265         $datarray['owner-name']    = $contact_record['name'];
266         $datarray['owner-link']    = $contact_record['url'];
267         $datarray['owner-avatar']  = $contact_record['thumb'];
268         $datarray['author-name']   = $author['name'];
269         $datarray['author-link']   = $author['url'];
270         $datarray['author-avatar'] = $author['thumb'];
271         $datarray['created']       = datetime_convert();
272         $datarray['edited']        = datetime_convert();
273         $datarray['changed']       = datetime_convert();
274         $datarray['uri']           = $uri;
275         $datarray['title']         = $title;
276         $datarray['body']          = $body;
277         $datarray['location']      = $location;
278         $datarray['coord']         = $coord;
279         $datarray['tag']           = $str_tags;
280         $datarray['inform']        = $inform;
281         $datarray['verb']          = $verb;
282         $datarray['allow_cid']     = $str_contact_allow;
283         $datarray['allow_gid']     = $str_group_allow;
284         $datarray['deny_cid']      = $str_contact_deny;
285         $datarray['deny_gid']      = $str_group_deny;
286         $datarray['private']       = $private;
287
288         /**
289          * These fields are for the convenience of plugins...
290          * 'self' if true indicates the owner is posting on their own wall
291          * If parent is 0 it is a top-level post.
292          */
293
294         $datarray['parent']        = $parent;
295         $datarray['self']          = $self;
296
297
298         call_hooks('post_local',$datarray);
299
300         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
301                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
302                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
303                 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 )",
304                 intval($datarray['uid']),
305                 dbesc($datarray['type']),
306                 intval($datarray['wall']),
307                 intval($datarray['gravity']),
308                 intval($datarray['contact-id']),
309                 dbesc($datarray['owner-name']),
310                 dbesc($datarray['owner-link']),
311                 dbesc($datarray['owner-avatar']),
312                 dbesc($datarray['author-name']),
313                 dbesc($datarray['author-link']),
314                 dbesc($datarray['author-avatar']),
315                 dbesc($datarray['created']),
316                 dbesc($datarray['edited']),
317                 dbesc($datarray['changed']),
318                 dbesc($datarray['uri']),
319                 dbesc($datarray['title']),
320                 dbesc($datarray['body']),
321                 dbesc($datarray['location']),
322                 dbesc($datarray['coord']),
323                 dbesc($datarray['tag']),
324                 dbesc($datarray['inform']),
325                 dbesc($datarray['verb']),
326                 dbesc($datarray['allow_cid']),
327                 dbesc($datarray['allow_gid']),
328                 dbesc($datarray['deny_cid']),
329                 dbesc($datarray['deny_gid']),
330                 intval($datarray['private'])
331         );
332
333         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
334                 dbesc($datarray['uri']));
335         if(count($r)) {
336                 $post_id = $r[0]['id'];
337                 logger('mod_item: saved item ' . $post_id);
338
339                 if($parent) {
340
341                         // This item is the last leaf and gets the comment box, clear any ancestors
342                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
343                                 dbesc(datetime_convert()),
344                                 intval($parent)
345                         );
346
347                         // Inherit ACL's from the parent item.
348
349                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
350                                 WHERE `id` = %d LIMIT 1",
351                                 dbesc($parent_item['allow_cid']),
352                                 dbesc($parent_item['allow_gid']),
353                                 dbesc($parent_item['deny_cid']),
354                                 dbesc($parent_item['deny_gid']),
355                                 intval($parent_item['private']),
356                                 intval($post_id)
357                         );
358
359                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
360                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
361                                 require_once('bbcode.php');
362                                 $from = $author['name'];
363                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
364                                 $email_tpl = replace_macros($tpl, array(
365                                         '$sitename' => $a->config['sitename'],
366                                         '$siteurl' =>  $a->get_baseurl(),
367                                         '$username' => $user['username'],
368                                         '$email' => $user['email'],
369                                         '$from' => $from,
370                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
371                                         '$body' => strip_tags(bbcode($datarray['body']))
372                                 ));
373
374                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
375                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
376                         }
377                 }
378                 else {
379                         $parent = $post_id;
380
381                         // let me know if somebody did a wall-to-wall post on my profile
382
383                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
384                                 require_once('bbcode.php');
385                                 $from = $author['name'];
386                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
387                                 $email_tpl = replace_macros($tpl, array(
388                                         '$sitename' => $a->config['sitename'],
389                                         '$siteurl' =>  $a->get_baseurl(),
390                                         '$username' => $user['username'],
391                                         '$email' => $user['email'],
392                                         '$from' => $from,
393                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
394                                         '$body' => strip_tags(bbcode($datarray['body']))
395                                 ));
396
397                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
398                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
399                         }
400                 }
401
402                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
403                         WHERE `id` = %d LIMIT 1",
404                         intval($parent),
405                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
406                         dbesc(datetime_convert()),
407                         intval($post_id)
408                 );
409
410                 // photo comments turn the corresponding item visible to the profile wall
411                 // This way we don't see every picture in your new photo album posted to your wall at once.
412                 // They will show up as people comment on them.
413
414                 if(! $parent_item['visible']) {
415                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
416                                 intval($parent_item['id'])
417                         );
418                 }
419         }
420
421         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
422
423         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
424
425   proc_run($php_path, "include/notifier.php", $notify_type, "$post_id");
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\" &", array(), $foo));
524                         proc_run($php_path,"include/notifier.php","drop","$drop_id");
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 }