Completed
Push — development ( d1d4a0...e79b55 )
by Sebastian
02:47
created

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

Check for suspicious calls to parent methods

Bug Comprehensibility Minor

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
/**
5
 * We use a wrapper class around BitcoinClient to add
6
 * some basic caching functionality and some debugging
7
 **/
8
class BitcoinWrapper extends BitcoinClient {
9
  public function __construct($type, $username, $password, $host, $debug_level, $debug_object, $memcache) {
10
    $this->type = $type;
11
    $this->username = $username;
12
    $this->password = $password;
13
    $this->host = $host;
14
    // $this->debug is already used
15
    $this->oDebug = $debug_object;
16
    $this->memcache = $memcache;
17
    $debug_level > 0 ? $debug_level = true : $debug_level = false;
18
    return parent::__construct($this->type, $this->username, $this->password, $this->host, '', $debug_level);
19
  }
20
  /**
21
   * Wrap variouns methods to add caching
22
   **/
23
  // Caching this, used for each can_connect call
24
  public function getinfo() {
25
    $this->oDebug->append("STA " . __METHOD__, 4);
26
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
27
    if (!(parent::getwalletinfo()['walletname']))
28
      return $this->memcache->setCache(__FUNCTION__, parent::getinfo(), 30);
29
    else
30
      return $this->memcache->setCache(__FUNCTION__, parent::getnetworkinfo()+parent::getmininginfo()+parent::getwalletinfo(), 30);
31
  }
32
33
  public function is_testnet() {
34
    $this->oDebug->append("STA " . __METHOD__, 4);
35
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
36
    if (!(parent::getblockchaininfo()))
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getblockchaininfo() instead of is_testnet()). Are you sure this is correct? If so, you might want to change this to $this->getblockchaininfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
37
      return $this->memcache->setCache(__FUNCTION__, parent::is_testnet(), 30);
38
    else
39
      return $this->memcache->setCache(__FUNCTION__, parent::getblockchaininfo()['chain'] == 'test', 30);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getblockchaininfo() instead of is_testnet()). Are you sure this is correct? If so, you might want to change this to $this->getblockchaininfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
40
  }
41
  
42
  public function getmininginfo() {
43
    $this->oDebug->append("STA " . __METHOD__, 4);
44
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
45
    return $this->memcache->setCache(__FUNCTION__, parent::getmininginfo(), 30);
46
  }
47
48
  public function getblockcount() {
49
    $this->oDebug->append("STA " . __METHOD__, 4);
50
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
51
    return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30);
52
  }
53
  // Wrapper method to get the real main account balance
54
  public function getrealbalance() {
55
    $this->oDebug->append("STA " . __METHOD__, 4);
56
    $aAccounts = parent::listaccounts();
57
    $dBalance = parent::getbalance('');
58
    // Account checks
59
    if (count($aAccounts) == 1) {
60
      // We only have a single account so getbalance will be fine
61
      return $dBalance;
62
    } else {
63
      $dMainBalance = $aAccounts[''];
64
      return $dMainBalance;
65
    }
66
  }
67
  public function getdifficulty() {
68
    $this->oDebug->append("STA " . __METHOD__, 4);
69
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
70
    $data = parent::getdifficulty();
71
    // Check for PoS/PoW coins
72
    if (is_array($data) && array_key_exists('proof-of-work', $data))
73
      $data = $data['proof-of-work'];
74
    return $this->memcache->setCache(__FUNCTION__, $data, 30);
75
  }
76
  public function getestimatedtime($iCurrentPoolHashrate) {
77
    $this->oDebug->append("STA " . __METHOD__, 4);
78
    if ($iCurrentPoolHashrate == 0) return 0;
79
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
80
    $dDifficulty = $this->getdifficulty();
81
    return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30);
82
  }
83
  public function getblockchaindownload() {
84
    $aPeerInfo = $this->getpeerinfo();
85
    $aInfo = $this->getinfo();
86
    $iStartingHeight = 0;
87
    foreach ($aPeerInfo as $aPeerData) {
88
      if ($iStartingHeight < $aPeerData['startingheight']) $iStartingHeight = $aPeerData['startingheight'];
89
    }
90
    if ($iStartingHeight > $aInfo['blocks']) {
91
      return number_format(round($aInfo['blocks'] / $iStartingHeight * 100, 2), 2);
92
    } else {
93
      return false;
94
    }
95
  }
96
  public function getnetworkhashps() {
97
    $this->oDebug->append("STA " . __METHOD__, 4);
98
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
99
    try {
100
      $dNetworkHashrate = $this->getmininginfo();
101
      if (is_array($dNetworkHashrate)) {
102
        if (array_key_exists('networkhashps', $dNetworkHashrate)) {
103
          $dNetworkHashrate = $dNetworkHashrate['networkhashps'];
104
        } else if (array_key_exists('networkmhps', $dNetworkHashrate)) {
105
          $dNetworkHashrate = $dNetworkHashrate['networkmhps'] * 1000 * 1000;
106
        } else if (array_key_exists('networkghps', $dNetworkHashrate)) {
107
          $dNetworkHashrate = $dNetworkHashrate['networkghps'] * 1000 * 1000 * 1000;
108
        } else if (array_key_exists('hashespersec', $dNetworkHashrate)) {
109
          $dNetworkHashrate = $dNetworkHashrate['hashespersec'];
110
        } else if (array_key_exists('netmhashps', $dNetworkHashrate)) {
111
          $dNetworkHashrate = $dNetworkHashrate['netmhashps'] * 1000 * 1000;
112
        } else {
113
          // Unsupported implementation
114
          $dNetworkHashrate = 0;
115
        }
116
      }
117
    } catch (Exception $e) {
118
      // getmininginfo does not exist, cache for an hour
119
      return $this->memcache->setCache(__FUNCTION__, 0, 3600);
120
    }
121
    return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30);
122
  }
123
}
124
125
// Load this wrapper
126
$bitcoin = new BitcoinWrapper($config['wallet']['type'], $config['wallet']['username'], $config['wallet']['password'], $config['wallet']['host'], $config['DEBUG'], $debug, $memcache);
127