damn mixed case packages
[pharext/replicator.pharext.org] / bin / fetch
index c55e785d1f0530d8e5d287f3caa5c7028801445d..e2b8af7195cbfb5b48ec38e96673795f1d20c95a 100755 (executable)
--- a/bin/fetch
+++ b/bin/fetch
@@ -1,48 +1,40 @@
-#!/usr/bin/php
+#!/usr/bin/env php
 <?php
 
-ini_set("display_errors", 0);
-ini_set("log_errors", 1);
-
-use http\Url;
-use http\Client;
-use http\Client\Request;
-
-$procs = [];
-$client = new Client;
-$client->setOptions([
-       "timetout" => 60,
-       "redirect" => 1,
-       "connecttimeout" => 6
-]);
-
-while (!feof(STDIN)) {
-   if (strlen($line = rtrim(fgets(STDIN)))) {
-        if (list($pkg, $ver, $url) = explode("\t", $line)) {
-                       $url = new Url($url, null, 0);
-                       $skp = sprintf("%s/../build/%s-%s.skip", __DIR__, $pkg, $ver);
-                       $tgz = sprintf("%s/../build/%s-%s.tgz", __DIR__, $pkg, $ver);
-                       $ext = sprintf("%s/../public/phars/%s/%s-%s.ext.phar", __DIR__, $pkg, $pkg, $ver);
-
-                       if (is_file($skp)) {
-                               // skip
-                       } elseif (is_file($tgz) || is_file($ext)) {
-                               printf("%s\t%s\t%s\n", $pkg, $ver, $tgz);
-                       } else {
-                               $client->enqueue(new Request("GET", $url), function($res) use($tgz, $pkg, $ver) {
-                                       $res->getBody()->toStream(fopen($tgz, "w"));
-                                       printf("%s\t%s\t%s\n", $pkg, $ver, $tgz);
-                                       return true;
-                               });
-                       }
-               }
-       }
+if ($argc < 2) {
+       fprintf(STDERR, "Usage: %s <pkg_name> [.ext.phar command line options]\n", $argv[0]);
+       exit(1);
 }
 
-while (count($client)) {
-       try {
-               $client->send();
-       } catch (Exception $e) {
-               error_log($e->getMessage());
+function bail() {
+       $error = error_get_last();
+       if (!($error["type"] & error_reporting())) {
+               fprintf(STDERR, "%s\n", $error["message"]);
        }
+       exit(2);
 }
+
+$cmp_mask = Phar::canCompress(Phar::GZ) | (Phar::canCompress(Phar::BZ2) << 1);
+$file_ext = ["", ".gz", ".bz2", ".bz2"][$cmp_mask];
+$base_url = "https://replicator.pharext.org/";
+$json_ctx = stream_context_create(["http"=>["header"=>"Accept:application/json"]]) or bail();
+$pkg_name = $argv[1];
+$pkg_json = file_get_contents($base_url . "?" . $pkg_name, false, $json_ctx) or bail();
+$pkg_data = json_decode($pkg_json, true) or bail();
+$rel_data = end($pkg_data);
+$pkg_path = $rel_data[$file_ext]["phar"];
+$sig_path = $rel_data[$file_ext]["sigs"]["rsa"];
+$pkg_file = basename($pkg_path);
+$pkg_data = file_get_contents($base_url . $pkg_path) or bail();
+$sig_data = file_get_contents($base_url . $sig_path) or bail();
+$key_data = file_get_contents($base_url . "replicator.pub") or bail();
+
+openssl_verify($pkg_data, $sig_data, $key_data, "sha256") or
+       (function() {
+               while (($error = openssl_error_string())) echo $error,"\n";
+               exit(3);
+       })();
+
+$argv = array_slice($argv, 1, --$argc);
+file_put_contents($pkg_file, $pkg_data) or bail();
+(include $pkg_file) or bail();