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