Пример #1
0
 /**
  * Get a block from a random mirror
  * 
  * @param string $pkg_name
  * @param string $publickey
  * @param string $secretkey
  */
 private function getBlockForPackage($pkg_name, $publickey = null, $secretkey = null)
 {
     if (empty($this->http)) {
         $this->setup();
     }
     $request_uri = $this->basepath . self::$api['package']['block'] . \urlencode($pkg_name);
     // Make the first request. Free packages will return here.
     if (empty($publickey)) {
         $postresp = $this->http->post($request_uri);
         $initial = \json_decode($postresp, true);
     } else {
         $initial = \json_decode($this->http->post($request_uri, ['publickey' => $publickey]), true);
     }
     if (!empty($initial['block'])) {
         return $initial['block'];
     }
     if (empty($publickey) || empty($secretkey)) {
         return false;
     }
     // Challenge-response below
     $solution = Base\Utilities::challengeResponse($initial['public_key'], $initial['nonce'], $initial['challenge'], $secretkey);
     // Second HTTP request
     $solved = \json_decode($this->http->post($request_uri, ['publickey' => $publickey, 'response' => \base64_encode($solution)], true));
     if (!empty($initial['block'])) {
         return $solved['block'];
     }
 }
Пример #2
0
 /**
  * Download a package
  * 
  * @param string $package Package name
  * @param string $publickey Public key (base64 encoded)
  * @param string $secretkey Secret key (base64 encoded)
  * @param boolean $echo Display verbose information?
  * 
  * @return boolean|string
  */
 public function downloadFile($package, $publickey = null, $secretkey = null, $echo = false)
 {
     $request_uri = $this->basepath . self::$api['package']['download'] . \urlencode($package);
     if (empty($publickey)) {
         $initial = $this->http->post($request_uri);
     } else {
         $initial = $this->http->post($request_uri, ['publickey' => $publickey]);
     }
     // Let's decode the challenge
     $chal = \json_decode($initial, true);
     // Is this a free package? If so, we download it immediately!
     if (!empty($chal['url'])) {
         $file = \file_get_contents($chal['url']);
         $filename = $this->saveTemp($file);
         if ($echo) {
             echo 'Saving temporary file to ', $filename, "\n";
         }
         return $filename;
     }
     // Challenge response authentication:
     if (empty($publickey) || empty($secretkey)) {
         if ($echo) {
             echo 'No license key.', "\n\n";
         }
         return false;
     }
     if (!empty($chal['error'])) {
         if ($echo) {
             echo 'Server Response:', "\n", $chal['error'], "\n\n";
         }
         return false;
     }
     if (empty($chal['challenge']) || empty($chal['nonce']) || empty($chal['publickey'])) {
         if ($echo) {
             echo $initial;
         }
         return false;
     }
     // We use base64 for JSON transport
     $solution = Base\Utilities::challengeResponse($chal['public_key'], $chal['nonce'], $chal['challenge'], $secretkey);
     // Second HTTP request
     $solved = $this->http->post($request_uri, ['publickey' => $publickey, 'response' => \base64_encode($solution)]);
     // Was it JSON?
     if ($this->http->lastHeader('Content-Type') !== 'application/json') {
         $filename = $this->saveTemp($solved);
         if ($echo) {
             echo 'Saving temporary file to ', $filename, "\n";
         }
         return $filename;
     }
     // If we are still here, we had an error.
     $srv = \json_decode($solved, true);
     if (!empty($srv['error'])) {
         if ($echo) {
             echo 'Server Response:', "\n", $srv['error'], "\n\n";
         }
         return false;
     }
     if ($echo) {
         echo $solved, "\n";
     }
     return false;
 }
Пример #3
0
 /**
  * Update rows in a SQLite database
  * 
  * @param string $table
  * @param string $rows
  * @param string $where
  * @return type
  * @throws \Exception
  */
 public function update($table, $rows, $where = [])
 {
     // UPDATE [table]
     $queryString = ' UPDATE ' . Asgard\Utilities::escapeSqlIdentifier($table) . ' ';
     $params = [];
     // SET
     $preface = false;
     foreach ($rows as $i => $v) {
         if ($preface) {
             $queryString .= ', ';
         }
         $queryString .= Asgard\Utilities::escapeSqlIdentifier($i) . ' = ?';
         $params[] = $v;
         $preface = true;
     }
     // WHERE
     $queryString .= ' WHERE ';
     if (empty($where)) {
         $queryString .= ' 1';
         // Sanity check. Use a direct query if you need to update all rows.
         throw new \Exception("Please don't use update() without a where clause.\n" . print_r([$queryString, $params], true));
     }
     $preface = false;
     foreach ($where as $i => $v) {
         if ($preface) {
             $queryString .= ' AND ';
         }
         $queryString .= ' ' . Asgard\Utilities::escapeSqlIdentifier($i) . ' = ? ';
         $params[] = $v;
         $preface = true;
     }
     $statement = $this->pdo->prepare($queryString);
     return $statement->execute($params);
 }
Пример #4
0
 public function __construct()
 {
     $this->config = \json_decode(\file_get_contents(CONFIGDIR . '/notary_server.json'), true);
     $this->db = \ParagonIE\AsgardClient\Utilities::getStorageAdapter('notary', \file_get_contents(CONFIGDIR . '/config.json'), true);
 }
Пример #5
0
 */
if (\is_readable(__DIR__ . "/data/config.json")) {
    // Allow people to edit the JSON config and define their own locations
    $config = \json_decode(\file_get_contents(__DIR__ . "/data/config.json"), true);
} else {
    // Sane defaults
    $config = ['storage' => ['blockchain' => ['driver' => 'sqlite', 'path' => 'blockchain.etilqs'], 'settings' => ['driver' => 'sqlite', 'path' => 'asgard.etilqs']]];
}
if (!\class_exists('Sodium')) {
    // Don't disable this. Our BLAKE2b source is Sodium::crypto_generichash()
    die("Please install libsodium and the libsodium-php extension from PECL\n\n" . "\thttp://doc.libsodium.org/installation/README.html\n" . "\thttp://pecl.php.net/package/libsodium\n");
}
// Let's load our storage adapters
$store = \ParagonIE\AsgardClient\Utilities::getStorageAdapter('settings', $config);
$bchain = \ParagonIE\AsgardClient\Utilities::getStorageAdapter('blockchain', $config);
$notary = \ParagonIE\AsgardClient\Utilities::getStorageAdapter('notary', $config);
/**
 * 3. Process the CLI parameters
 */
$showAll = true;
if ($argc < 2) {
    // Default behavior: Display the help menu
    $argv[1] = 'help';
    $showAll = false;
    $argc = 2;
}
// Create a little cache for the Help command, if applicable. Doesn't contain objects.
$commands = [];
foreach (\glob(__DIR__ . '/Client/Commands/*.php') as $file) {
    // Let's build a queue of all the file names
    // Grab the filename from the Commands directory: