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