aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Daemon/File_importer.php
blob: 883759a39c3262c6df32e07ec84bcd51f35c9f4d (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\Daemon;

use Zotlabs\Web\HTTPSig;

require_once('include/cli_startup.php');
require_once('include/attach.php');
require_once('include/import.php');

class File_importer {

	static public function run($argc,$argv) {

		cli_startup();

		$attach_id = $argv[1];
		$channel_address = $argv[2];
		$hz_server = urldecode($argv[3]);

		$m = parse_url($hz_server);

		$channel = channelx_by_nick($channel_address);
		if(! $channel) {
			logger('filehelper: channel not found');
			return;
		}

		$headers = [
			'X-API-Token'      => random_string(),
			'X-API-Request'    => $hz_server . '/api/z/1.0/file/export?f=&file_id=' . $attach_id,
			'Host'             => $m['host'],
			'(request-target)' => 'get /api/z/1.0/file/export?f=&file_id=' . $attach_id,
		];

		$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'],channel_url($channel),true,'sha512');
		$x = z_fetch_url($hz_server . '/api/z/1.0/file/export?f=&file_id=' . $attach_id,false,$redirects,[ 'headers' => $headers ]);

		if(! $x['success']) {
			logger('no API response',LOGGER_DEBUG);
			return;
		}

		$j = json_decode($x['body'],true);

		$r = sync_files($channel,[$j]);

		return;
	}
}