BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

donationsbitcoin.class.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. class DonationsBitcoin
  3. {
  4. /**
  5. * Ask bitcoind for a list of all addresses that have received bitcoins
  6. *
  7. * @return array (BitcoinAddress => Amount, ...)
  8. */
  9. public static function get_received()
  10. {
  11. if (defined('BITCOIN_RPC_URL')) {
  12. $Donations = BitcoinRpc::listreceivedbyaddress();
  13. }
  14. if (empty($Donations)) {
  15. return [];
  16. }
  17. $BTCUsers = [];
  18. foreach ($Donations as $Account) {
  19. $BTCUsers[$Account->address] = $Account->amount;
  20. }
  21. return $BTCUsers;
  22. }
  23. /**
  24. * Ask bitcoind for the current account balance
  25. *
  26. * @return float balance
  27. */
  28. public static function get_balance()
  29. {
  30. if (defined('BITCOIN_RPC_URL')) {
  31. return BitcoinRpc::getbalance();
  32. }
  33. }
  34. /**
  35. * Get a user's existing bitcoin address or generate a new one
  36. *
  37. * @param int $UserID
  38. * @param bool $GenAddress whether to create a new address if it doesn't exist
  39. * @return false if no address exists and $GenAddress is false
  40. * string bitcoin address otherwise
  41. */
  42. public static function get_address($UserID, $GenAddress = false)
  43. {
  44. $UserID = (int)$UserID;
  45. $QueryID = G::$DB->get_query_id();
  46. G::$DB->query("
  47. SELECT BitcoinAddress
  48. FROM users_info
  49. WHERE UserID = '$UserID'");
  50. list($Addr) = G::$DB->next_record();
  51. G::$DB->set_query_id($QueryID);
  52. if (!empty($Addr)) {
  53. return $Addr;
  54. } elseif ($GenAddress) {
  55. if (defined('BITCOIN_RPC_URL')) {
  56. $NewAddr = BitcoinRpc::getnewaddress();
  57. }
  58. if (empty($NewAddr)) {
  59. error(0);
  60. }
  61. $QueryID = G::$DB->get_query_id();
  62. G::$DB->query("
  63. UPDATE users_info
  64. SET BitcoinAddress = '".db_string($NewAddr)."'
  65. WHERE UserID = '$UserID'
  66. AND BitcoinAddress IS NULL");
  67. G::$DB->set_query_id($QueryID);
  68. return $NewAddr;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * Ask bitcoind for the total amount of bitcoins received
  75. *
  76. * @return float amount
  77. */
  78. public static function get_total_received()
  79. {
  80. if (defined('BITCOIN_RPC_URL')) {
  81. $Accounts = BitcoinRpc::listreceivedbyaccount();
  82. }
  83. if (empty($Accounts)) {
  84. return 0.0;
  85. }
  86. foreach ($Accounts as $Account) {
  87. if ($Account->account === '') {
  88. return $Account->amount;
  89. }
  90. }
  91. return 0.0;
  92. }
  93. /**
  94. * Translate bitcoin addresses to user IDs
  95. *
  96. * @param array $Addresses list of bitcoin addresses
  97. * @return array (BitcoinAddress => UserID, ...)
  98. */
  99. public static function get_userids($Addresses)
  100. {
  101. if (!is_array($Addresses) || empty($Addresses)) {
  102. return false;
  103. }
  104. $QueryID = G::$DB->get_query_id();
  105. G::$DB->query("
  106. SELECT BitcoinAddress, UserID
  107. FROM users_info
  108. WHERE BitcoinAddress IN ('" . implode("', '", $Addresses) . "')");
  109. if (G::$DB->has_results()) {
  110. $UserIDs = G::$DB->to_pair(0, 1);
  111. } else {
  112. $UserIDs = [];
  113. }
  114. G::$DB->set_query_id($QueryID);
  115. return $UserIDs;
  116. }
  117. /**
  118. * Find and process new donations since the last time this function was called.
  119. */
  120. public static function find_new_donations()
  121. {
  122. global $Debug;
  123. if (($OldAmount = G::$Cache->get_value('btc_total_received')) === false) {
  124. $QueryID = G::$DB->get_query_id();
  125. G::$DB->query("
  126. SELECT IFNULL(SUM(Amount), 0)
  127. FROM donations_bitcoin");
  128. list($OldAmount) = G::$DB->next_record(MYSQLI_NUM, false);
  129. G::$DB->set_query_id($QueryID);
  130. }
  131. $NewAmount = self::get_total_received();
  132. if ($NewAmount < $OldAmount) {
  133. // This shouldn't happen. Perhaps bitcoind was restarted recently
  134. // or the block index was removed. Either way, try again later
  135. send_irc(DEBUG_CHAN, "Bad bitcoin donation data (is $NewAmount, was $OldAmount). If this persists, something is probably wrong.");
  136. return false;
  137. }
  138. if ($NewAmount > $OldAmount) {
  139. // I really wish we didn't have to do it like this
  140. $QueryID = G::$DB->get_query_id();
  141. G::$DB->query("
  142. SELECT BitcoinAddress, SUM(Amount)
  143. FROM donations_bitcoin
  144. GROUP BY BitcoinAddress");
  145. $OldDonations = G::$DB->to_pair(0, 1, false);
  146. G::$DB->set_query_id($QueryID);
  147. $NewDonations = self::get_received();
  148. foreach ($NewDonations as $Address => &$Amount) {
  149. if (isset($OldDonations[$Address])) {
  150. if ($Amount == $OldDonations[$Address]) { // Direct comparison should be fine as everything comes from bitcoind
  151. unset($NewDonations[$Address]);
  152. continue;
  153. }
  154. $Debug->log_var(array('old' => $OldDonations[$Address], 'new' => $Amount), "New donations from $Address");
  155. // PHP doesn't do fixed-point math, and json_decode has already botched the precision
  156. // so let's just round this off to satoshis and pray that we're on a 64 bit system
  157. $Amount = round($Amount - $OldDonations[$Address], 8);
  158. }
  159. $NewDonations[$Address] = $Amount;
  160. }
  161. $Debug->log_var($NewDonations, '$NewDonations');
  162. foreach (self::get_userids(array_keys($NewDonations)) as $Address => $UserID) {
  163. Donations::regular_donate($UserID, $NewDonations[$Address], 'Bitcoin Parser', '', 'BTC');
  164. self::store_donation($Address, $NewDonations[$Address]);
  165. }
  166. G::$Cache->cache_value('btc_total_received', $NewAmount, 0);
  167. }
  168. }
  169. /**
  170. * Record a donation in the database
  171. *
  172. * @param string $Address bitcoin address
  173. * @param double $Amount amount of bitcoins transferred
  174. */
  175. public static function store_donation($Address, $Amount)
  176. {
  177. if (!is_numeric($Amount) || $Amount <= 0) {
  178. // Panic!
  179. return false;
  180. }
  181. G::$DB->query("
  182. INSERT INTO donations_bitcoin
  183. (BitcoinAddress, Amount)
  184. VALUES
  185. ('$Address', $Amount)");
  186. }
  187. }