aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/Zotfinger.php
blob: 537e440d429c48d7f74c4aa185ce2270c76cdbc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

namespace Zotlabs\Lib;

use Zotlabs\Web\HTTPSig;

class Zotfinger {

	static function exec($resource,$channel = null) {

		if(! $resource) {
			return false;
		}

		if($channel) {
			$headers = [ 
				'Accept'      => 'application/x-zot+json', 
				'X-Zot-Token' => random_string(),
			];
			$h = HTTPSig::create_sig($headers,$channel['channel_prvkey'],channel_url($channel),false);
		}
		else {
			$h = [ 'Accept: application/x-zot+json' ]; 
		}
				
		$result = [];


		$redirects = 0;
		$x = z_fetch_url($resource,false,$redirects, [ 'headers' => $h  ] );

		if($x['success']) {
			
			$result['signature'] = HTTPSig::verify($x);
    
			$result['data'] = json_decode($x['body'],true);

			if($result['data'] && is_array($result['data']) && array_key_exists('encrypted',$result['data']) && $result['data']['encrypted']) {
				$result['data'] = json_decode(crypto_unencapsulate($result['data'],get_config('system','prvkey')),true);
			}

			return $result;
		}

		return false;
	}



}