@@ -45,10 +45,6 @@ class MastodonBridge extends BridgeAbstract {
45
45
)
46
46
));
47
47
48
- const AP_HEADER = array (
49
- 'Accept: application/activity+json '
50
- );
51
-
52
48
public function getName () {
53
49
if ($ this ->getInput ('canusername ' )) {
54
50
return $ this ->getInput ('canusername ' );
@@ -89,7 +85,7 @@ public function getURI(){
89
85
90
86
public function collectData () {
91
87
$ url = $ this ->getURI () . '/outbox?page=true ' ;
92
- $ content = json_decode ( getContents ( $ url, self :: AP_HEADER ), true );
88
+ $ content = $ this -> fetchAP ( $ url );
93
89
if ($ content ['id ' ] === $ url ) {
94
90
foreach ($ content ['orderedItems ' ] as $ status ) {
95
91
$ this ->items [] = $ this ->parseItem ($ status );
@@ -112,7 +108,7 @@ protected function parseItem($content) {
112
108
$ rtUser = $ this ->loadCacheValue ($ rtContent ['attributedTo ' ], 86400 );
113
109
if (!isset ($ rtUser )) {
114
110
// We fetch the author, since we cannot always assume the format of the URL.
115
- $ user = json_decode ( getContents ( $ rtContent ['attributedTo ' ], self :: AP_HEADER ), true );
111
+ $ user = $ this -> fetchAP ( $ rtContent ['attributedTo ' ]);
116
112
preg_match ('/https?:\/\/([a-z0-9-\.]{0,})\// ' , $ rtContent ['attributedTo ' ], $ matches );
117
113
// We assume that the server name as indicated by the path is the actual server name,
118
114
// since using webfinger to delegate domains is not officially supported, and it only
@@ -168,4 +164,31 @@ protected function parseObject($object, $item) {
168
164
}
169
165
return $ item ;
170
166
}
167
+
168
+ protected function fetchAP ($ url ) {
169
+ $ d = new DateTime ();
170
+ $ d ->setTimezone (new DateTimeZone ('GMT ' ));
171
+ $ date = $ d ->format ('D, d M Y H:i:s e ' );
172
+ preg_match ('/https?:\/\/([a-z0-9-\.]{0,})(\/[^?#]+)/ ' , $ url , $ matches );
173
+ $ headers = array (
174
+ 'Accept: application/activity+json ' ,
175
+ 'Host: ' . $ matches [1 ],
176
+ 'Date: ' . $ date
177
+ );
178
+ $ privateKey = $ this ->getOption ('private_key ' );
179
+ $ keyId = $ this ->getOption ('key_id ' );
180
+ if ($ privateKey && $ keyId ) {
181
+ $ pkey = openssl_pkey_get_private ('file:// ' . $ privateKey );
182
+ $ toSign = '(request-target): get ' . $ matches [2 ] . "\nhost: " . $ matches [1 ] . "\ndate: " . $ date ;
183
+ $ result = openssl_sign ($ toSign , $ signature , $ pkey , 'RSA-SHA256 ' );
184
+ if ($ result ) {
185
+ Debug::log ($ toSign );
186
+ $ sig = 'Signature: keyId=" ' . $ keyId . '",headers="(request-target) host date",signature=" ' .
187
+ base64_encode ($ signature ) . '" ' ;
188
+ Debug::log ($ sig );
189
+ array_push ($ headers , $ sig );
190
+ }
191
+ }
192
+ return json_decode (getContents ($ url , $ headers ), true );
193
+ }
171
194
}
0 commit comments