simple fetch command
[pharext/replicator.pharext.org] / bin / fetch
1 #!/usr/bin/env php
2 <?php
3
4 if ($argc < 2) {
5 fprintf(STDERR, "Usage: %s <pkg_name> [.ext.phar command line options]\n", $argv[0]);
6 exit(1);
7 }
8
9 function bail() {
10 $error = error_get_last();
11 if (!($error["type"] & error_reporting())) {
12 fprintf(STDERR, "%s\n", $error["message"]);
13 }
14 exit(2);
15 }
16
17 $cmp_mask = Phar::canCompress(Phar::GZ) | (Phar::canCompress(Phar::BZ2) << 1);
18 $file_ext = ["", ".gz", ".bz2", ".bz2"][$cmp_mask];
19 $base_url = "https://replicator.pharext.org/";
20 $json_ctx = stream_context_create(["http"=>["header"=>"Accept:application/json"]]) or bail();
21 $pkg_name = $argv[1];
22 $pkg_json = file_get_contents($base_url . "?" . $pkg_name, false, $json_ctx) or bail();
23 $pkg_data = json_decode($pkg_json, true) or bail();
24 $rel_data = end($pkg_data);
25 $pkg_path = $rel_data[$file_ext]["phar"];
26 $sig_path = $rel_data[$file_ext]["sigs"]["rsa"];
27 $pkg_file = basename($pkg_path);
28 $pkg_data = file_get_contents($base_url . $pkg_path) or bail();
29 $sig_data = file_get_contents($base_url . $sig_path) or bail();
30 $key_data = file_get_contents($base_url . "replicator.pub") or bail();
31
32 openssl_verify($pkg_data, $sig_data, $key_data, "sha256") or
33 (function() {
34 while (($error = openssl_error_string())) echo $error,"\n";
35 exit(3);
36 })();
37
38 $argv = array_slice($argv, 1, --$argc);
39 file_put_contents($pkg_file, $pkg_data) or bail();
40 (include $pkg_file) or bail();