]> git.mxchange.org Git - friendica.git/blob - mod/salmon.php
put lrdd link into html headers for completeness
[friendica.git] / mod / salmon.php
1 <?php
2
3
4 // TODO: pass keyhash to key discovery
5 // add relevant contacts so they can use this
6
7 // There is a lot of debug stuff in here because this is quite a
8 // complicated process to try and sort out. 
9
10 require_once('include/salmon.php');
11 require_once('simplepie/simplepie.inc');
12
13 function salmon_return($val) {
14
15         if($val >= 500)
16                 $err = 'Error';
17         if($val == 200)
18                 $err = 'OK';
19         
20         header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
21         killme();
22
23 }
24
25 function salmon_post(&$a) {
26
27         $xml = file_get_contents('php://input');
28         
29         $debugging = get_config('system','debugging');
30         if($debugging)
31                 file_put_contents('salmon.out','New Salmon: ' . $xml . "\n",FILE_APPEND);
32
33         $nick       = (($a->argc > 1) ? notags(trim($a->argv[1])) : '');
34         $mentions   = (($a->argc > 2 && $a->argv[2] === 'mention') ? true : false);
35
36         $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
37                 dbesc($nick)
38         );
39         if(! count($r))
40                 salmon_return(500);
41
42         $importer = $r[0];
43
44         // parse the xml
45
46         $dom = simplexml_load_string($xml,'SimpleXMLElement',0,NAMESPACE_SALMON_ME);
47
48
49         if($debugging)
50                 file_put_contents('salmon.out', "\n" . print_r($dom,true) . "\n" , FILE_APPEND);
51
52         // figure out where in the DOM tree our data is hiding
53
54         if($dom->provenance->data)
55                 $base = $dom->provenance;
56         elseif($dom->env->data)
57                 $base = $dom->env;
58         elseif($dom->data)
59                 $base = $dom;
60         
61         if(! $base) {
62                 if($debugging)
63                         file_put_contents('salmon.out', "\n" . 'Unable to find salmon data in XML' . "\n" , FILE_APPEND);
64                 salmon_return(500);
65         }
66
67         // Stash the signature away for now. We have to find their key or it won't be good for anything.
68
69
70         $signature = base64url_decode($base->sig);
71         if($debugging)
72                 file_put_contents('salmon.out', "\n" . 'Encoded Signature: ' . $base->sig . "\n" , FILE_APPEND);
73
74         // unpack our data element.
75
76         // strip whitespace
77         $data = str_replace(array(" ","\t","\r","\n"),array("","","",""),$base->data);
78         $type = $base->data[0]->attributes()->type[0];
79         $encoding = $base->encoding;
80         $alg = $base->alg;
81
82         $signed_data = $data;
83         // . '.' . base64url_encode($type) . '.' . base64url_encode($encoding) . '.' . base64url_encode($alg);
84         // decode it
85         $data = base64url_decode($data);
86
87         if($debugging)
88                 file_put_contents('salmon.out', "\n" . 'Signed data:>>>' . $signed_data . "<<<\n" , FILE_APPEND);
89
90         // Remove the xml declaration
91         $data = preg_replace('/\<\?xml[^\?].*\?\>/','',$data);
92
93         // Create a fake feed wrapper so simplepie doesn't choke
94
95         $tpl = load_view_file('view/fake_feed.tpl');
96         
97         $base = substr($data,strpos($data,'<entry'));
98
99         $feedxml = $tpl . $base . '</feed>';
100
101         if($debugging) {
102                 file_put_contents('salmon.out', 'Processed feed: ' . $feedxml . "\n", FILE_APPEND);
103         }
104
105         // Now parse it like a normal atom feed to scrape out the author URI
106         
107     $feed = new SimplePie();
108     $feed->set_raw_data($feedxml);
109     $feed->enable_order_by_date(false);
110     $feed->init();
111
112         if($debugging) {
113                 file_put_contents('salmon.out', "\n" . 'Feed parsed.' . "\n", FILE_APPEND);
114         }
115
116
117         if($feed->get_item_quantity()) {
118                 foreach($feed->get_items() as $item) {
119                         $author = $item->get_author();
120                         $author_link = unxmlify($author->get_link());
121                         break;
122                 }
123         }
124
125         if(! $author_link) {
126                 if($debugging)
127                         file_put_contents('salmon.out',"\n" . 'Could not retrieve author URI.' . "\n", FILE_APPEND);
128                 salmon_return(500);
129         }
130
131         // Once we have the author URI, go to the web and find their public key
132
133         if($debugging) {
134                 file_put_contents('salmon.out', "\n" . 'Fetching key for ' . $author_link . "\n", FILE_APPEND);
135         }
136
137         $key = get_salmon_key($author_link,$keyhash);
138
139         if(! $key) {
140                 if($debugging)
141                         file_put_contents('salmon.out',"\n" . 'Could not retrieve author key.' . "\n", FILE_APPEND);
142                 salmon_return(500);
143         }
144
145         // Setup RSA stuff to verify the signature
146
147         set_include_path(get_include_path() . PATH_SEPARATOR . 'phpsec');
148
149         require_once('phpsec/Crypt/RSA.php');
150
151         $key_info = explode('.',$key);
152
153         $m = base64url_decode($key_info[1]);
154         $e = base64url_decode($key_info[2]);
155         if($debugging)
156                 file_put_contents('salmon.out',"\n" . print_r($key_info,true) . "\n", FILE_APPEND);
157
158     $rsa = new CRYPT_RSA();
159     $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
160     $rsa->setHash('sha256');
161
162     $rsa->modulus = new Math_BigInteger($m, 256);
163     $rsa->k = strlen($rsa->modulus->toBytes());
164     $rsa->exponent = new Math_BigInteger($e, 256);
165
166         // We should have everything we need now. Let's see if it verifies.
167
168     $verify = $rsa->verify($signed_data,$signature);
169
170         if(! $verify) {
171                 if($debugging)
172                         file_put_contents('salmon.out',"\n" . 'Message did not verify. Discarding.' . "\n", FILE_APPEND);
173                 salmon_return(500);
174         }
175
176         if($debugging)
177                 file_put_contents('salmon.out',"\n" . 'Message verified.' . "\n", FILE_APPEND);
178
179
180         /*
181         *
182         * If we reached this point, the message is good. Now let's figure out if the author is allowed to send us stuff.
183         *
184         */
185
186         $r = q("SELECT * FROM `contact` WHERE `network` = 'stat' AND `lrdd` = '%s' AND `uid` = %d LIMIT 1",
187                 dbesc($author_link),
188                 intval($importer['uid'])
189         );
190         if(! count($r)) {
191                 if($debugging)
192                         file_put_contents('salmon.out',"\n" . 'Author unknown to us.' . "\n", FILE_APPEND);
193                 salmon_return(500);
194         }       
195
196
197         require_once('include/items.php');
198
199         $hub = '';
200
201         consume_feed($feedxml,$importer,$r[0],$hub);
202
203         salmon_return(200);
204 }
205
206
207
208