Ejemplo n.º 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'];
     }
 }
Ejemplo n.º 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;
 }