Advertisement
Guest User

Untitled

a guest
Sep 29th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. <?php
  2.  
  3. $faucethub_lib_version = "v1.0.1";
  4. class FaucetBOX extends FaucetHub {
  5. public function __construct($api_key, $currency = "BTC", $disable_curl = false, $verify_peer = true) {
  6. parent::__construct($api_key, $currency, $disable_curl, $verify_peer);
  7. }
  8. }
  9.  
  10. class FaucetHub
  11. {
  12. protected $api_key;
  13. protected $currency;
  14. protected $timeout;
  15. public $last_status = null;
  16. protected $api_base = "https://faucethub.io/api/v1/";
  17.  
  18. public function __construct($api_key) {
  19. $currency = "BTC";
  20. $disable_curl = false;
  21. $verify_peer = true;
  22. $timeout = null;
  23. $this->currency = $currency;
  24. $this->disable_curl = $disable_curl;
  25. $this->verify_peer = $verify_peer;
  26. $this->curl_warning = false;
  27. $this->setTimeout($timeout);
  28. $this->api_key =$api_key;
  29. }
  30. public function setAPIkey($api_key){
  31. $this->api_key =$api_key;
  32. }
  33.  
  34. public function setTimeout($timeout) {
  35. if($timeout === null) {
  36. $socket_timeout = ini_get('default_socket_timeout');
  37. $script_timeout = ini_get('max_execution_time');
  38. $timeout = min($script_timeout / 2, $socket_timeout);
  39. }
  40. $this->timeout = $timeout;
  41. }
  42.  
  43. public function __execPHP($method, $params = array()) {
  44. $params = array_merge($params, array("api_key" => $this->api_key, "currency" => $this->currency));
  45. $opts = array(
  46. "http" => array(
  47. "method" => "POST",
  48. "header" => "Content-type: application/x-www-form-urlencoded\r\n",
  49. "content" => http_build_query($params),
  50. "timeout" => $this->timeout,
  51. ),
  52. "ssl" => array(
  53. "verify_peer" => $this->verify_peer
  54. )
  55. );
  56. $ctx = stream_context_create($opts);
  57. $fp = fopen($this->api_base . $method, 'rb', null, $ctx);
  58.  
  59. if (!$fp) {
  60. return json_encode(array(
  61. 'status' => 503,
  62. 'message' => 'Connection to FaucetHub failed, please try again later',
  63. ), TRUE);
  64. }
  65.  
  66. $response = stream_get_contents($fp);
  67. if($response && !$this->disable_curl) {
  68. $this->curl_warning = true;
  69. }
  70. fclose($fp);
  71. return $response;
  72. }
  73.  
  74. public function __exec($method, $params = array()) {
  75. $this->last_status = null;
  76. if($this->disable_curl) {
  77. $response = $this->__execPHP($method, $params);
  78. } else {
  79. $response = $this->__execCURL($method, $params);
  80. }
  81. $response = json_decode($response, true);
  82. if($response) {
  83. $this->last_status = $response['status'];
  84. } else {
  85. $this->last_status = null;
  86. $response = array(
  87. 'status' => 502,
  88. 'message' => 'Invalid response',
  89. );
  90. }
  91. return $response;
  92. }
  93.  
  94. public function __execCURL($method, $params = array()) {
  95. $params = array_merge($params, array("api_key" => $this->api_key, "currency" => $this->currency));
  96.  
  97. $ch = curl_init($this->api_base . $method);
  98. curl_setopt($ch, CURLOPT_POST, true);
  99. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  100. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_peer);
  101. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  102. curl_setopt($ch, CURLOPT_TIMEOUT, (int)$this->timeout);
  103.  
  104. $response = curl_exec($ch);
  105. if(!$response) {
  106. //$response = $this->__execPHP($method, $params); // disabled the exec fallback when using curl
  107. return json_encode(array(
  108. 'status' => 504,
  109. 'message' => 'Connection error',
  110. ), TRUE);
  111. }
  112. curl_close($ch);
  113.  
  114. return $response;
  115. }
  116.  
  117. public function send($to, $amount, $referral = false, $ip_address = "") {
  118. $referral = ($referral === true) ? 'true' : 'false';
  119.  
  120. $r = $this->__exec("send", array("to" => $to, "amount" => $amount, "referral" => $referral, "ip_address" => $ip_address));
  121. if (array_key_exists("status", $r) && $r["status"] == 200) {
  122. return array(
  123. 'success' => true,
  124. 'message' => 'Payment sent to your address using FaucetHub.io',
  125. 'html' => '<div class="alert alert-success">' . htmlspecialchars($amount) . ' satoshi was sent to <a target="_blank" href="https://faucethub.io/balance/' . rawurlencode($to) . '">your account at FaucetHub.io</a>.</div>',
  126. 'html_coin' => '<div class="alert alert-success">' . htmlspecialchars(rtrim(rtrim(sprintf("%.8f", $amount/100000000), '0'), '.')) . ' '.$this->currency.' was sent to <a target="_blank" href="https://faucethub.io/balance/' . rawurlencode($to) . '">your account at FaucetHub.io</a>.</div>',
  127. 'balance' => $r["balance"],
  128. 'balance_bitcoin' => $r["balance_bitcoin"],
  129. 'response' => json_encode($r)
  130. );
  131. }
  132.  
  133. // Let the user know they need an account to claim
  134. if (array_key_exists("status", $r) && $r["status"] == 456) {
  135. return array(
  136. 'success' => false,
  137. 'message' => $r['message'],
  138. 'html' => '<div class="alert alert-danger">Before you can receive payments at FaucetHub.io with this address you must link it to an account. <a href="http://faucethub.io/signup" target="_blank">Create an account at FaucetHub.io</a> and link your address, then come back and claim again.</div>',
  139. 'response' => json_encode($r)
  140. );
  141. }
  142.  
  143. if (array_key_exists("message", $r)) {
  144. return array(
  145. 'success' => false,
  146. 'message' => $r["message"],
  147. 'html' => '<div class="alert alert-danger">' . htmlspecialchars($r["message"]) . '</div>',
  148. 'response' => json_encode($r)
  149. );
  150. }
  151.  
  152. return array(
  153. 'success' => false,
  154. 'message' => 'Unknown error.',
  155. 'html' => '<div class="alert alert-danger">Unknown error.</div>',
  156. 'response' => json_encode($r)
  157. );
  158. }
  159.  
  160. public function sendReferralEarnings($to, $amount, $ip_address = "") {
  161. return $this->send($to, $amount, true, $ip_address);
  162. }
  163.  
  164. public function getPayouts($count) {
  165. $r = $this->__exec("payouts", array("count" => $count) );
  166. return $r;
  167. }
  168.  
  169. public function getCurrencies() {
  170. $r = $this->__exec("currencies");
  171. return $r['currencies'];
  172. }
  173.  
  174. public function getBalance() {
  175. $r = $this->__exec("balance");
  176. return $r;
  177. }
  178.  
  179. public function checkAddress($address, $currency = "BTC") {
  180. $r = $this->__exec("checkaddress", array('address' => $address, 'currency' => $currency));
  181. return $r;
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement