]> git.mxchange.org Git - friendica.git/blob - mod/item.php
OpenID delegation in profile page
[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         $str_tags = '';
132         $inform   = '';
133
134         $tags = get_tags($body);
135
136
137         if(count($tags)) {
138                 foreach($tags as $tag) {
139                         if(strpos($tag,'#') === 0) {
140                                 $basetag = str_replace('_',' ',substr($tag,1));
141                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
142                                 if(strlen($str_tags))
143                                         $str_tags .= ',';
144                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
145                                 continue;
146                         }
147                         if(strpos($tag,'@') === 0) {
148                                 $name = substr($tag,1);
149                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
150                                         $newname = $name;
151                                         $links = @lrdd($name);
152                                         if(count($links)) {
153                                                 foreach($links as $link) {
154                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
155                                         $profile = $link['@attributes']['href'];
156                                                         if($link['@attributes']['rel'] === 'salmon') {
157                                                                 if(strlen($inform))
158                                                                         $inform .= ',';
159                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
160                                                         }
161                                                 }
162                                         }
163                                 }
164                                 else {
165                                         $newname = $name;
166                                         if(strstr($name,'_')) {
167                                                 $newname = str_replace('_',' ',$name);
168                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
169                                                         dbesc($newname),
170                                                         intval($profile_uid)
171                                                 );
172                                         }
173                                         else {
174                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
175                                                         dbesc($name),
176                                                         intval($profile_uid)
177                                                 );
178                                         }
179                                         if(count($r)) {
180                                                 $profile = $r[0]['url'];
181                                                 $newname = $r[0]['name'];
182                                                 if(strlen($inform))
183                                                         $inform .= ',';
184                                                 $inform .= 'cid:' . $r[0]['id'];
185                                         }
186                                 }
187                                 if($profile) {
188                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
189                                         $profile = str_replace(',','%2c',$profile);
190                                         if(strlen($str_tags))
191                                                 $str_tags .= ',';
192                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
193                                 }
194                         }
195                 }
196         }
197
198         $wall = 0;
199         if($post_type === 'wall' || $post_type === 'wall-comment')
200                 $wall = 1;
201
202         if(! strlen($verb))
203                 $verb = ACTIVITY_POST ;
204
205         $gravity = (($parent) ? 6 : 0 );
206  
207         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
208
209         $uri = item_new_uri($a->get_hostname(),$profile_uid);
210
211         $datarray = array();
212         $datarray['uid']           = $profile_uid;
213         $datarray['type']          = $post_type;
214         $datarray['wall']          = $wall;
215         $datarray['gravity']       = $gravity;
216         $datarray['contact-id']    = $contact_id;
217         $datarray['owner-name']    = $contact_record['name'];
218         $datarray['owner-link']    = $contact_record['url'];
219         $datarray['owner-avatar']  = $contact_record['thumb'];
220         $datarray['author-name']   = $author['name'];
221         $datarray['author-link']   = $author['url'];
222         $datarray['author-avatar'] = $author['thumb'];
223         $datarray['created']       = datetime_convert();
224         $datarray['edited']        = datetime_convert();
225         $datarray['changed']       = datetime_convert();
226         $datarray['uri']           = $uri;
227         $datarray['title']         = $title;
228         $datarray['body']          = $body;
229         $datarray['location']      = $location;
230         $datarray['coord']         = $coord;
231         $datarray['tag']           = $str_tags;
232         $datarray['inform']        = $inform;
233         $datarray['verb']          = $verb;
234         $datarray['allow_cid']     = $str_contact_allow;
235         $datarray['allow_gid']     = $str_group_allow;
236         $datarray['deny_cid']      = $str_contact_deny;
237         $datarray['deny_gid']      = $str_group_deny;
238         $datarray['private']       = $private;
239
240         /**
241          * These fields are for the convenience of plugins...
242          * 'self' if true indicates the owner is posting on their own wall
243          * If parent is 0 it is a top-level post.
244          */
245
246         $datarray['parent']        = $parent;
247         $datarray['self']          = $self;
248
249
250         call_hooks('post_local',$datarray);
251
252         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
253                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
254                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
255                 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 )",
256                 intval($datarray['uid']),
257                 dbesc($datarray['type']),
258                 intval($datarray['wall']),
259                 intval($datarray['gravity']),
260                 intval($datarray['contact-id']),
261                 dbesc($datarray['owner-name']),
262                 dbesc($datarray['owner-link']),
263                 dbesc($datarray['owner-avatar']),
264                 dbesc($datarray['author-name']),
265                 dbesc($datarray['author-link']),
266                 dbesc($datarray['author-avatar']),
267                 dbesc($datarray['created']),
268                 dbesc($datarray['edited']),
269                 dbesc($datarray['changed']),
270                 dbesc($datarray['uri']),
271                 dbesc($datarray['title']),
272                 dbesc($datarray['body']),
273                 dbesc($datarray['location']),
274                 dbesc($datarray['coord']),
275                 dbesc($datarray['tag']),
276                 dbesc($datarray['inform']),
277                 dbesc($datarray['verb']),
278                 dbesc($datarray['allow_cid']),
279                 dbesc($datarray['allow_gid']),
280                 dbesc($datarray['deny_cid']),
281                 dbesc($datarray['deny_gid']),
282                 intval($datarray['private'])
283         );
284
285         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
286                 dbesc($datarray['uri']));
287         if(count($r)) {
288                 $post_id = $r[0]['id'];
289                 logger('mod_item: saved item ' . $post_id);
290
291                 if($parent) {
292
293                         // This item is the last leaf and gets the comment box, clear any ancestors
294                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
295                                 dbesc(datetime_convert()),
296                                 intval($parent)
297                         );
298
299                         // Inherit ACL's from the parent item.
300
301                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
302                                 WHERE `id` = %d LIMIT 1",
303                                 dbesc($parent_item['allow_cid']),
304                                 dbesc($parent_item['allow_gid']),
305                                 dbesc($parent_item['deny_cid']),
306                                 dbesc($parent_item['deny_gid']),
307                                 intval($parent_item['private']),
308                                 intval($post_id)
309                         );
310
311                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
312                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
313                                 require_once('bbcode.php');
314                                 $from = $author['name'];
315                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
316                                 $email_tpl = replace_macros($tpl, array(
317                                         '$sitename' => $a->config['sitename'],
318                                         '$siteurl' =>  $a->get_baseurl(),
319                                         '$username' => $user['username'],
320                                         '$email' => $user['email'],
321                                         '$from' => $from,
322                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
323                                         '$body' => strip_tags(bbcode($datarray['body']))
324                                 ));
325
326                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
327                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
328                         }
329                 }
330                 else {
331                         $parent = $post_id;
332
333                         // let me know if somebody did a wall-to-wall post on my profile
334
335                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
336                                 require_once('bbcode.php');
337                                 $from = $author['name'];
338                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
339                                 $email_tpl = replace_macros($tpl, array(
340                                         '$sitename' => $a->config['sitename'],
341                                         '$siteurl' =>  $a->get_baseurl(),
342                                         '$username' => $user['username'],
343                                         '$email' => $user['email'],
344                                         '$from' => $from,
345                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
346                                         '$body' => strip_tags(bbcode($datarray['body']))
347                                 ));
348
349                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
350                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
351                         }
352                 }
353
354                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
355                         WHERE `id` = %d LIMIT 1",
356                         intval($parent),
357                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
358                         dbesc(datetime_convert()),
359                         intval($post_id)
360                 );
361
362                 // photo comments turn the corresponding item visible to the profile wall
363                 // This way we don't see every picture in your new photo album posted to your wall at once.
364                 // They will show up as people comment on them.
365
366                 if(! $parent_item['visible']) {
367                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
368                                 intval($parent_item['id'])
369                         );
370                 }
371         }
372
373         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
374
375         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
376
377         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
378                 array(),$foo));
379
380         /**
381          * Post to Facebook stream
382          */
383
384         if((local_user()) && (local_user() == $profile_uid) && (! $private)) {
385                 $appid  = get_config('system', 'facebook_appid'  );
386                 $secret = get_config('system', 'facebook_secret' );
387                 if($appid && $secret) {
388                         $fb_post = get_pconfig($local_user(),'facebook','post');
389                         if($fb_post) {
390                                 require_once('library/facebook.php');
391                                 require_once('include/bbcode.php');     
392
393                                 $facebook = new Facebook(array(
394                                         'appId'  => $appid,
395                                         'secret' => $secret,
396                                         'cookie' => true
397                                 ));                     
398                                 try {
399                                         $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> bbcode($datarray['body']), 'cb' => ''));
400                                 } 
401                                 catch (FacebookApiException $e) {
402                                         notice( t('Facebook status update failed.') . EOL);
403                                 }
404                         }
405                 }
406         }
407
408         goaway($a->get_baseurl() . "/" . $_POST['return'] );
409         return; // NOTREACHED
410 }
411
412
413
414
415
416 function item_content(&$a) {
417
418         if((! local_user()) && (! remote_user()))
419                 return;
420
421         require_once('include/security.php');
422
423         $uid = $_SESSION['uid'];
424
425         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
426
427                 // locate item to be deleted
428
429                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
430                         intval($a->argv[2])
431                 );
432
433                 if(! count($r)) {
434                         notice( t('Item not found.') . EOL);
435                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
436                 }
437                 $item = $r[0];
438
439                 // check if logged in user is either the author or owner of this item
440
441                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
442
443                         // delete the item
444
445                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
446                                 dbesc(datetime_convert()),
447                                 dbesc(datetime_convert()),
448                                 intval($item['id'])
449                         );
450
451                         // If item is a link to a photo resource, nuke all the associated photos 
452                         // (visitors will not have photo resources)
453                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
454                         // generate a resource-id and therefore aren't intimately linked to the item. 
455
456                         if(strlen($item['resource-id'])) {
457                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
458                                         dbesc($item['resource-id']),
459                                         intval($item['uid'])
460                                 );
461                                 // ignore the result
462                         }
463
464                         // If it's the parent of a comment thread, kill all the kids
465
466                         if($item['uri'] == $item['parent-uri']) {
467                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
468                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
469                                         dbesc(datetime_convert()),
470                                         dbesc(datetime_convert()),
471                                         dbesc($item['parent-uri']),
472                                         intval($item['uid'])
473                                 );
474                                 // ignore the result
475                         }
476                         else {
477                                 // ensure that last-child is set in case the comment that had it just got wiped.
478                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
479                                         dbesc(datetime_convert()),
480                                         dbesc($item['parent-uri']),
481                                         intval($item['uid'])
482                                 );
483                                 // who is the last child now? 
484                                 $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",
485                                         dbesc($item['parent-uri']),
486                                         intval($item['uid'])
487                                 );
488                                 if(count($r)) {
489                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
490                                                 intval($r[0]['id'])
491                                         );
492                                 }       
493                         }
494                         $drop_id = intval($item['id']);
495                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
496                         
497                         // send the notification upstream/downstream as the case may be
498
499                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
500                                 array(), $foo));
501
502                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
503                         return; //NOTREACHED
504                 }
505                 else {
506                         notice( t('Permission denied.') . EOL);
507                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
508                         //NOTREACHED
509                 }
510         }
511 }