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