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