Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

include/classes/coin_address.class.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
7
  /**
8
   * We allow changing the database for shared accounts across pools
9
   * Load the config on construct so we can assign the DB name
10
   * @param config array MPOS configuration
11
   * @return none
12
   **/
13
  public function __construct($config) {
14
    $this->setConfig($config);
15
    $this->table = $this->config['db']['shared']['accounts'] . '.' . $this->table;
16
  }
17
18
  /**
19
   * Fetch users coin address for a currency
20
   * @param userID int UserID
21
   * @return data string Coin Address
22
   **/
23 View Code Duplication
  public function getCoinAddress($userID, $currency=NULL) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    if ($currency === NULL) $currency = $this->config['currency'];
25
    $this->debug->append("STA " . __METHOD__, 4);
26
    $stmt = $this->mysqli->prepare("
27
      SELECT coin_address
28
      FROM " . $this->getTableName() . "
29
      WHERE account_id = ? AND currency = ?
30
      ");
31
    if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) {
32
      if ($result->num_rows == 1) {
33
        return $result->fetch_object()->coin_address;
34
      }
35
    }
36
    $this->debug->append("Unable to fetch users coin address for " . $currency);
37
    return $this->sqlError();
38
  }
39
40
  /**
41
   * Fetch users Auto Payout Threshold for a currency
42
   * @param UserID int UserID
43
   * @return mixed Float value for threshold, false on error
44
   **/
45 View Code Duplication
  public function getAPThreshold($userID, $currency=NULL) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    if ($currency === NULL) $currency = $this->config['currency'];
47
    $this->debug->append("STA " . __METHOD__, 4);
48
    $stmt = $this->mysqli->prepare("
49
      SELECT ap_threshold
50
      FROM " . $this->getTableName() . "
51
      WHERE account_id = ? AND currency = ?
52
      ");
53
    if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute() && $result = $stmt->get_result()) {
54
      if ($result->num_rows == 1) {
55
        return $result->fetch_object()->ap_threshold;
56
      }
57
    }
58
    $this->debug->append("Unable to fetch users auto payout threshold for " . $currency);
59
    return $this->sqlError();
60
  }
61
62
63
  /**
64
   * Check if a coin address is already set
65
   * @param address string Coin Address to check for
66
   * @return bool true or false
67
   **/
68
  public function existsCoinAddress($address) {
69
    $this->debug->append("STA " . __METHOD__, 4);
70
    return $this->getSingle($address, 'coin_address', 'coin_address', 's') === $address;
71
  }
72
73
  /**
74
   * Add a new coin address record for a user
75
   * @param userID int Account ID
76
   * @param address string Coin Address
77
   * @param currency string Currency short handle, defaults to config option
78
   * @return bool true or false
79
   **/
80
  public function add($userID, $address, $currency=NULL) {
81
    if ($currency === NULL) $currency = $this->config['currency'];
82 View Code Duplication
    if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) {
83
      $this->setErrorMessage('Unable to update coin address, address already exists');
84
      return false;
85
    }
86
    $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (account_id, currency, coin_address) VALUES (?, ?, ?)");
87 View Code Duplication
    if ( $this->checkStmt($stmt) && $stmt->bind_param('iss', $userID, $currency, $address) && $stmt->execute()) {
88
      return true;
89
    }
90
    return $this->sqlError();
91
  }
92
93
  /**
94
   * Remove a coin address record for a user
95
   * @param userID int Account ID
96
   * @param currency string Currency short handle, defaults to config option
97
   * @return bool true or false
98
   **/
99
  public function remove ($userID, $currency=NULL) {
100
    if ($currency === NULL) $currency = $this->config['currency'];
101
    $stmt = $this->mysqli->prepare("DELETE FROM " . $this->getTableName() . " WHERE account_id = ? AND currency = ?");
102 View Code Duplication
    if ( $this->checkStmt($stmt) && $stmt->bind_param('is', $userID, $currency) && $stmt->execute()) {
103
      return true;
104
    }
105
    return $this->sqlError();
106
  }
107
108
  /**
109
   * Update a coin address record for a user and a currency
110
   * @param userID int Account ID
111
   * @param address string Coin Address
112
   * @param ap_threshold float Threshold for auto payouts for this currency
113
   * @param currency string Currency short handle, defaults to config option
114
   * @return bool true or false
115
   **/
116
  public function update($userID, $address, $ap_threshold, $currency=NULL) {
117
    if ($currency === NULL) $currency = $this->config['currency'];
118 View Code Duplication
    if ($address != $this->getCoinAddress($userID) && $this->existsCoinAddress($address)) {
119
      $this->setErrorMessage('Unable to update coin address, address already exists');
120
      return false;
121
    }
122
    if ($this->getCoinAddress($userID) != NULL) {
123
      $stmt = $this->mysqli->prepare("UPDATE " . $this->getTableName() . " SET coin_address = ?, ap_threshold = ? WHERE account_id = ? AND currency = ?");
124 View Code Duplication
      if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) {
125
        return true;
126
      }
127
    } else {
128
      $stmt = $this->mysqli->prepare("INSERT INTO " . $this->getTableName() . " (coin_address, ap_threshold, account_id, currency) VALUES (?, ?, ?, ?)");
129 View Code Duplication
      if ( $this->checkStmt($stmt) && $stmt->bind_param('sdis', $address, $ap_threshold, $userID, $currency) && $stmt->execute()) {
130
        return true;
131
      }
132
    }
133
    return $this->sqlError();
134
  }
135
}
136
137
$coin_address = new CoinAddress($config);
138
$coin_address->setDebug($debug);
139
$coin_address->setMysql($mysqli);
140
$coin_address->setErrorCodes($aErrorCodes);
141