]> git.mxchange.org Git - friendica.git/blob - mod/receive.php
turn diaspora posts into x-www-form-urlencoded
[friendica.git] / mod / receive.php
1 <?php
2
3 /**
4  * Diaspora endpoint
5  */
6
7
8 require_once('include/salmon.php');
9 require_once('include/crypto.php');
10 require_once('include/diaspora.php');
11
12
13         
14 function receive_post(&$a) {
15
16         if($a->argc != 3 || $a->argv[1] !== 'users')
17                 http_status_exit(500);
18
19         $guid = $a->argv[2];
20
21         $r = q("SELECT * FROM `user` WHERE `guid` = '%s' LIMIT 1",
22                 dbesc($guid)
23         );
24         if(! count($r))
25                 http_status_exit(500);
26
27         $importer = $r[0];
28
29         // I really don't know why we need urldecode - PHP should be doing this for us.
30         // It is an application/x-www-form-urlencoded
31
32         $xml = urldecode($_POST['xml']);
33
34         logger('mod-diaspora: new salmon ' . $xml, LOGGER_DATA);
35
36         if(! $xml)
37                 http_status_exit(500);
38
39         $msg = diaspora_decode($importer,$xml);
40
41         logger('mod-diaspora: decoded msg: ' . $msg, LOGGER_DATA);
42
43         if(! $msg)
44                 http_status_exit(500);
45
46
47         $parsed_xml = parse_xml_string($msg,false);
48
49         $xmlbase = $parsed_xml->post;
50
51         // If we reached this point, the message is good. 
52         // Now let's figure out if the author is allowed to send us stuff.
53
54         $r = q("SELECT * FROM `contact` WHERE `network` = 'dspr' AND ( `url` = '%s' OR `alias` = '%s') 
55                 AND `uid` = %d LIMIT 1",
56                 dbesc($author_link),
57                 dbesc($author_link),
58                 intval($importer['uid'])
59         );
60         if(! count($r)) {
61                 logger('mod-diaspora: Author unknown to us.');
62         }       
63
64         // is this a follower? Or have we ignored the person?
65         // If so we can not accept this post.
66         // However we will accept a sharing e.g. friend request
67         // or a retraction of same.
68
69
70         $allow_blocked = (($xmlbase->request || ($xmlbase->retraction && $xmlbase->retraction->type == 'Person')) ? true : false);
71
72         if((count($r)) 
73                 && (($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']) || ($r[0]['readonly'])) 
74                 && (! $allow_blocked)) {
75                         logger('mod-diaspora: Ignoring this author.');
76                         http_status_exit(202);
77                         // NOTREACHED
78         }
79
80         require_once('include/items.php');
81
82         $contact = ((count($r)) ? $r[0] : null);
83
84         if($xmlbase->request) {
85                 diaspora_request($importer,$contact,$xmlbase->request);
86         }
87         elseif($xmlbase->status_message) {
88                 diaspora_post($importer,$contact,$xmlbase->status_message);
89         }
90         elseif($xmlbase->comment) {
91                 diaspora_comment($importer,$contact,$xmlbase->comment);
92         }
93         elseif($xmlbase->like) {
94                 diaspora_like($importer,$contact,$xmlbase->like);
95         }
96         elseif($xmlbase->retraction) {
97                 diaspora_retraction($importer,$contact,$xmlbase->retraction);
98         }
99         else {
100                 logger('mod-diaspora: unknown message type: ' . print_r($xmlbase,true));
101         }
102
103         http_status_exit(200);
104         // NOTREACHED
105 }
106