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