]> git.mxchange.org Git - friendica.git/blob - mod/salmon.php
Ops, one more left ...
[friendica.git] / mod / salmon.php
1 <?php
2 /**
3  * @file mod/salmon.php
4  */
5 use Friendica\App;
6 use Friendica\Core\PConfig;
7 use Friendica\Core\Protocol;
8 use Friendica\Core\System;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Contact;
11 use Friendica\Protocol\OStatus;
12 use Friendica\Protocol\Salmon;
13 use Friendica\Util\Crypto;
14
15 require_once 'include/items.php';
16
17 function salmon_post(App $a, $xml = '') {
18
19         if (empty($xml)) {
20                 $xml = file_get_contents('php://input');
21         }
22
23         logger('new salmon ' . $xml, LOGGER_DATA);
24
25         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
26         $mentions   = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
27
28         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `account_expired` = 0 AND `account_removed` = 0 LIMIT 1",
29                 DBA::escape($nick)
30         );
31         if (! DBA::isResult($r)) {
32                 System::httpExit(500);
33         }
34
35         $importer = $r[0];
36
37         // parse the xml
38
39         $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
40
41         $base = null;
42
43         // figure out where in the DOM tree our data is hiding
44         if($dom->provenance->data)
45                 $base = $dom->provenance;
46         elseif($dom->env->data)
47                 $base = $dom->env;
48         elseif($dom->data)
49                 $base = $dom;
50
51         if(! $base) {
52                 logger('unable to locate salmon data in xml ');
53                 System::httpExit(400);
54         }
55
56         // Stash the signature away for now. We have to find their key or it won't be good for anything.
57
58
59         $signature = base64url_decode($base->sig);
60
61         // unpack the  data
62
63         // strip whitespace so our data element will return to one big base64 blob
64         $data = str_replace([" ","\t","\r","\n"],["","","",""],$base->data);
65
66         // stash away some other stuff for later
67
68         $type = $base->data[0]->attributes()->type[0];
69         $keyhash = $base->sig[0]->attributes()->keyhash[0];
70         $encoding = $base->encoding;
71         $alg = $base->alg;
72
73         // Salmon magic signatures have evolved and there is no way of knowing ahead of time which
74         // flavour we have. We'll try and verify it regardless.
75
76         $stnet_signed_data = $data;
77
78         $signed_data = $data  . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
79
80         $compliant_format = str_replace('=', '', $signed_data);
81
82
83         // decode the data
84         $data = base64url_decode($data);
85
86         $author = OStatus::salmonAuthor($data, $importer);
87         $author_link = $author["author-link"];
88
89         if(! $author_link) {
90                 logger('Could not retrieve author URI.');
91                 System::httpExit(400);
92         }
93
94         // Once we have the author URI, go to the web and try to find their public key
95
96         logger('Fetching key for ' . $author_link);
97
98         $key = Salmon::getKey($author_link, $keyhash);
99
100         if(! $key) {
101                 logger('Could not retrieve author key.');
102                 System::httpExit(400);
103         }
104
105         $key_info = explode('.',$key);
106
107         $m = base64url_decode($key_info[1]);
108         $e = base64url_decode($key_info[2]);
109
110         logger('key details: ' . print_r($key_info,true), LOGGER_DEBUG);
111
112         $pubkey = Crypto::meToPem($m, $e);
113
114         // We should have everything we need now. Let's see if it verifies.
115
116         // Try GNU Social format
117         $verify = Crypto::rsaVerify($signed_data, $signature, $pubkey);
118         $mode = 1;
119
120         if (! $verify) {
121                 logger('message did not verify using protocol. Trying compliant format.');
122                 $verify = Crypto::rsaVerify($compliant_format, $signature, $pubkey);
123                 $mode = 2;
124         }
125
126         if (! $verify) {
127                 logger('message did not verify using padding. Trying old statusnet format.');
128                 $verify = Crypto::rsaVerify($stnet_signed_data, $signature, $pubkey);
129                 $mode = 3;
130         }
131
132         if (! $verify) {
133                 logger('Message did not verify. Discarding.');
134                 System::httpExit(400);
135         }
136
137         logger('Message verified with mode '.$mode);
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                 DBA::escape(Protocol::OSTATUS),
150                 DBA::escape(Protocol::DFRN),
151                 DBA::escape(normalise_link($author_link)),
152                 DBA::escape($author_link),
153                 DBA::escape(normalise_link($author_link)),
154                 intval($importer['uid'])
155         );
156         if (! DBA::isResult($r)) {
157                 logger('Author ' . $author_link . ' unknown to user ' . $importer['uid'] . '.');
158                 if(PConfig::get($importer['uid'],'system','ostatus_autofriend')) {
159                         $result = Contact::createFromProbe($importer['uid'], $author_link);
160                         if($result['success']) {
161                                 $r = q("SELECT * FROM `contact` WHERE `network` = '%s' AND ( `url` = '%s' OR `alias` = '%s')
162                                         AND `uid` = %d LIMIT 1",
163                                         DBA::escape(Protocol::OSTATUS),
164                                         DBA::escape($author_link),
165                                         DBA::escape($author_link),
166                                         intval($importer['uid'])
167                                 );
168                         }
169                 }
170         }
171
172         // Have we ignored the person?
173         // If so we can not accept this post.
174
175         //if((DBA::isResult($r)) && (($r[0]['readonly']) || ($r[0]['rel'] == Contact::FOLLOWER) || ($r[0]['blocked']))) {
176         if (DBA::isResult($r) && $r[0]['blocked']) {
177                 logger('Ignoring this author.');
178                 System::httpExit(202);
179                 // NOTREACHED
180         }
181
182         // Placeholder for hub discovery.
183         $hub = '';
184
185         $contact_rec = ((DBA::isResult($r)) ? $r[0] : null);
186
187         OStatus::import($data, $importer, $contact_rec, $hub);
188
189         System::httpExit(200);
190 }