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