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