|
| 1 | +<?php |
| 2 | +$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
| 3 | + |
| 4 | +class CoinAddress extends Base { |
| 5 | + protected $table = 'coin_addresses'; |
| 6 | + private $cache = array(); |
| 7 | + |
| 8 | + /** |
| 9 | + * Fetch users coin address for a currency |
| 10 | + * @param userID int UserID |
| 11 | + * @return data string Coin Address |
| 12 | + **/ |
| 13 | + public function getCoinAddress($userID, $currency=NULL) { |
| 14 | + if ($currency === NULL) $currency = $this->config['currency']; |
| 15 | + $this->debug->append("STA " . __METHOD__, 4); |
| 16 | + $stmt = $this->mysqli->prepare(" |
| 17 | + SELECT coin_address |
| 18 | + FROM " . $this->getTableName() . " |
| 19 | + WHERE account_id = ? AND currency = ? |
| 20 | + "); |
| 21 | + if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) { |
| 22 | + if ($result->num_rows == 1) { |
| 23 | + return $result->fetch_object()->coin_address; |
| 24 | + } |
| 25 | + } |
| 26 | + $this->debug->append("Unable to fetch users coin address for " . $currency); |
| 27 | + return $this->sqlError(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Check if a coin address is already set |
| 32 | + * @param address string Coin Address to check for |
| 33 | + * @return bool true or false |
| 34 | + **/ |
| 35 | + public function existsCoinAddress($address) { |
| 36 | + $this->debug->append("STA " . __METHOD__, 4); |
| 37 | + return $this->getSingle($address, 'coin_address', 'coin_address', 's') === $address; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Add a new coin address record for a user |
| 42 | + * @param userID int Account ID |
| 43 | + * @param address string Coin Address |
| 44 | + * @param currency string Currency short handle, defaults to config option |
| 45 | + * @return bool true or false |
| 46 | + **/ |
| 47 | + public function add($userID, $address, $currency=NULL) { |
| 48 | + if ($currency === NULL) $currency = $this->config['currency']; |
| 49 | + if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) { |
| 50 | + $this->setErrorMessage('Unable to update coin address, address already exists'); |
| 51 | + return false; |
| 52 | + } |
| 53 | + $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (account_id, currency, coin_address) VALUES (?, ?, ?)"); |
| 54 | + if ( $this->checkStmt($stmt) && $stmt->bind_param('iss', $userID, $currency, $address) && $stmt->execute()) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + return $this->sqlError(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Remove a coin address record for a user |
| 62 | + * @param userID int Account ID |
| 63 | + * @param currency string Currency short handle, defaults to config option |
| 64 | + * @return bool true or false |
| 65 | + **/ |
| 66 | + public function remove ($userID, $currency=NULL) { |
| 67 | + if ($currency === NULL) $currency = $this->config['currency']; |
| 68 | + $stmt = $this->mysqli->prepare("DELETE FROM " . $this->getTableName() . " WHERE account_id = ? AND currency = ?"); |
| 69 | + if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute()) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + return $this->sqlError(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Update a coin address record for a user and a currency |
| 77 | + * @param userID int Account ID |
| 78 | + * @param address string Coin Address |
| 79 | + * @param currency string Currency short handle, defaults to config option |
| 80 | + * @return bool true or false |
| 81 | + **/ |
| 82 | + public function update($userID, $address, $currency=NULL) { |
| 83 | + if ($currency === NULL) $currency = $this->config['currency']; |
| 84 | + if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) { |
| 85 | + $this->setErrorMessage('Unable to update coin address, address already exists'); |
| 86 | + return false; |
| 87 | + } |
| 88 | + if ($this->getCoinAddress($userID) != NULL) { |
| 89 | + $stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ? WHERE account_id = ? AND currency = ?"); |
| 90 | + if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } else { |
| 94 | + $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, account_id, currency) VALUES (?, ?, ?)"); |
| 95 | + if ( $this->checkStmt($stmt) && $stmt->bind_param('sis', $address, $userID, $currency) && $stmt->execute()) { |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + return $this->sqlError(); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +$coin_address = new CoinAddress(); |
| 104 | +$coin_address->setDebug($debug); |
| 105 | +$coin_address->setConfig($config); |
| 106 | +$coin_address->setMysql($mysqli); |
| 107 | +$coin_address->setErrorCodes($aErrorCodes); |
0 commit comments