]> git.mxchange.org Git - friendica.git/blob - mod/salmon.php
Reworked salmon import
[friendica.git] / mod / salmon.php
1 <?php
2
3
4 // There is a lot of debug stuff in here because this is quite a
5 // complicated process to try and sort out.
6
7 require_once('include/salmon.php');
8 require_once('include/ostatus.php');
9 require_once('include/crypto.php');
10
11 function salmon_return($val) {
12
13         if($val >= 400)
14                 $err = 'Error';
15         if($val >= 200 && $val < 300)
16                 $err = 'OK';
17
18         logger('mod-salmon returns ' . $val);
19         header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
20         killme();
21
22 }
23
24 function salmon_post(&$a) {
25
26         $xml = file_get_contents('php://input');
27
28         logger('mod-salmon: new salmon ' . $xml, LOGGER_DATA);
29
30         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
31         $mentions   = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
32
33         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
34                 dbesc($nick)
35         );
36         if(! count($r))
37                 http_status_exit(500);
38
39         $importer = $r[0];
40
41         // parse the xml
42
43         $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
44
45         // figure out where in the DOM tree our data is hiding
46
47         if($dom->provenance->data)
48                 $base = $dom->provenance;
49         elseif($dom->env->data)
50                 $base = $dom->env;
51         elseif($dom->data)
52                 $base = $dom;
53
54         if(! $base) {
55                 logger('mod-salmon: unable to locate salmon data in xml ');
56                 http_status_exit(400);
57         }
58
59         // Stash the signature away for now. We have to find their key or it won't be good for anything.
60
61
62         $signature = base64url_decode($base->sig);
63
64         // unpack the  data
65
66         // strip whitespace so our data element will return to one big base64 blob
67         $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
68
69         // stash away some other stuff for later
70
71         $type = $base->data[0]->attributes()->type[0];
72         $keyhash = $base->sig[0]->attributes()->keyhash[0];
73         $encoding = $base->encoding;
74         $alg = $base->alg;
75
76         // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
77         // flavour we have. We'll try and verify it regardless.
78
79         $stnet_signed_data = $data;
80
81         $signed_data = $data  . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
82
83         $compliant_format = str_replace('=','',$signed_data);
84
85
86         // decode the data
87         $data = base64url_decode($data);
88
89         $author = ostatus_salmon_author($data,$importer);
90         $author_link = $author["author-link"];
91
92         if(! $author_link) {
93                 logger('mod-salmon: Could not retrieve author URI.');
94                 http_status_exit(400);
95         }
96
97         // Once we have the author URI, go to the web and try to find their public key
98
99         logger('mod-salmon: Fetching key for ' . $author_link );
100
101
102         $key = get_salmon_key($author_link,$keyhash);
103
104         if(! $key) {
105                 logger('mod-salmon: Could not retrieve author key.');
106                 http_status_exit(400);
107         }
108
109         $key_info = explode('.',$key);
110
111         $m = base64url_decode($key_info[1]);
112         $e = base64url_decode($key_info[2]);
113
114         logger('mod-salmon: key details: ' . print_r($key_info,true), LOGGER_DEBUG);
115
116         $pubkey = metopem($m,$e);
117
118         // We should have everything we need now. Let's see if it verifies.
119
120         $verify = rsa_verify($compliant_format,$signature,$pubkey);
121
122         if(! $verify) {
123                 logger('mod-salmon: message did not verify using protocol. Trying padding hack.');
124             $verify = rsa_verify($signed_data,$signature,$pubkey);
125         }
126
127         if(! $verify) {
128                 logger('mod-salmon: message did not verify using padding. Trying old statusnet hack.');
129             $verify = rsa_verify($stnet_signed_data,$signature,$pubkey);
130         }
131
132         if(! $verify) {
133                 logger('mod-salmon: Message did not verify. Discarding.');
134                 http_status_exit(400);
135         }
136
137         logger('mod-salmon: Message verified.');
138
139
140         /*
141         *
142         * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
143         *
144         */
145
146         $r = q("SELECT * FROM `contact` WHERE `network` IN ('%s', '%s')
147                                                 AND (`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s')
148                                                 AND `uid` = %d LIMIT 1",
149                 dbesc(NETWORK_OSTATUS),
150                 dbesc(NETWORK_DFRN),
151                 dbesc(normalise_link($author_link)),
152                 dbesc($author_link),
153                 dbesc(normalise_link($author_link)),
154                 intval($importer['uid'])
155         );
156         if(! count($r)) {
157                 logger('mod-salmon: Author unknown to us.');
158                 if(get_pconfig($importer['uid'],'system','ostatus_autofriend')) {
159                         require_once('include/follow.php');
160                         $result = new_contact($importer['uid'],$author_link);
161                         if($result['success']) {
162                                 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s' ) 
163                                         AND `uid` = %d LIMIT 1",
164                                         dbesc(NETWORK_OSTATUS),
165                                         dbesc($author_link),
166                                         dbesc($author_link),
167                                         intval($importer['uid'])
168                                 );
169                         }
170                 }
171         }
172
173         // is this a follower? Or have we ignored the person?
174         // If so we can not accept this post.
175
176         if((count($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == CONTACT_IS_FOLLOWER) || ($r[0]['blocked']))) {
177                 logger('mod-salmon: Ignoring this author.');
178                 http_status_exit(202);
179                 // NOTREACHED
180         }
181
182         require_once('include/items.php');
183
184         // Placeholder for hub discovery. We shouldn't find any hubs
185         // since we supplied the fake feed header - and it doesn't have any.
186
187         $hub = '';
188
189         /**
190          *
191          * anti-spam measure: consume_feed will accept a follow activity from 
192          * this person (and nothing else) if there is no existing contact record.
193          *
194          */
195
196         $contact_rec = ((count($r)) ? $r[0] : null);
197
198         //consume_feed($feedxml,$importer,$contact_rec,$hub);
199         ostatus_import($data,$importer,$contact_rec, $hub);
200
201         http_status_exit(200);
202 }