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

include/classes/tools.class.php (1 issue)

parameters are used.

Unused Code 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
 * Helper class for our cronjobs
6
 * Implements some common cron tasks outside
7
 * the scope of our web application
8
 **/
9
class Tools extends Base {
10
  public function getOnlineVersions() {
11
    // Fetch version online, cache for a bit
12
    $key = $this->config['memcache']['keyprefix'] . 'ONLINE_VERSIONS';
13
    if (! $mpos_versions = $this->memcache->get($key)) {
14
      $url = $this->config['version_url'];
15
      $curl = curl_init();
16
      curl_setopt($curl, CURLOPT_URL, $url);
17
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
18
      curl_setopt($curl, CURLOPT_HEADER, false);
19
      $data = curl_exec($curl);
20
      preg_match('/define\(\'MPOS_VERSION\', \'(.*)\'\);/', $data, $match);
21
      $mpos_versions['MPOS_VERSION'] = @$match[1];
22
      preg_match('/define\(\'DB_VERSION\', \'(.*)\'\);/', $data, $match);
23
      $mpos_versions['DB_VERSION'] = @$match[1];
24
      preg_match('/define\(\'CONFIG_VERSION\', \'(.*)\'\);/', $data, $match);
25
      $mpos_versions['CONFIG_VERSION'] = @$match[1];
26
      curl_close($curl);
27
      return $this->memcache->setCache($key, $mpos_versions, 30);
28
    } else {
29
      return $mpos_versions;
30
    }
31
  }
32
  /**
33
   * Fetch JSON data from an API
34
   * @param url string API URL
35
   * @param target string API method
36
   * @param auth array Optional authentication data to be sent with
37
   * @return dec array JSON decoded PHP array
38
   **/
39
  public function getApi($url, $target, $auth=NULL) {
0 ignored issues
show
The parameter $auth is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    static $ch = null;
41
    static $ch = null;
42
    if (is_null($ch)) {
43
      $ch = curl_init();
44
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
45
      curl_setopt($ch, CURLOPT_TIMEOUT, 30);
46
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
47
      curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
48
    }
49
    curl_setopt($ch, CURLOPT_URL, $url . $target);
50
    // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
51
52
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
53
54
    // run the query
55
    $res = curl_exec($ch);
56
    if ($res === false) {
57
      $this->setErrorMessage('Could not get reply: '.curl_error($ch));
58
      return false;
59
    }
60
    $dec = json_decode($res, true);
61
    if (!$dec) {
62
      $this->setErrorMessage('Invalid data received, please make sure connection is working and requested API exists');
63
      return false;
64
    }
65
    return $dec;
66
  }
67
68
  /**
69
   * Detect the API to properly extract information
70
   * @param url string API URL
71
   * @return data string API type
72
   **/
73
  private function getApiType($url) {
74
    if (preg_match('/coinchoose.com/', $url)) {
75
      return 'coinchose';
76
    } else if (preg_match('/btc-e.com/', $url)) {
77
      return 'btce';
78
    } else if (preg_match('/cryptsy.com/', $url)) {
79
      return 'cryptsy';
80
    } else if (preg_match('/cryptopia.co.nz/', $url)) {
81
     return 'cryptopia';
82
    } else if (preg_match('/cryptorush.in/', $url)) {
83
      return 'cryptorush';
84
    } else if (preg_match('/mintpal.com/', $url)) {
85
      return 'mintpal';
86
    } else if (preg_match('/c-cex.com/', $url)) {
87
      return 'c-cex';
88
    } else if (preg_match('/bittrex.com/', $url)) {
89
      return 'bittrex';
90
    }
91
    $this->setErrorMessage("API URL unknown");
92
    return false;
93
  }
94
95
  /**
96
   * Extract price information from API data
97
   **/
98
  public function getPrice() {
99
    $aData = $this->getApi($this->config['price']['url'], $this->config['price']['target']);
100
    $strCurrency = $this->config['currency'];
101
    // Check the API type for configured URL
102
    if (!$strApiType = $this->getApiType($this->config['price']['url']))
103
      return false;
104
    // if api data is valid, extract price depending on API type
105
    if (is_array($aData)) {
106
      switch ($strApiType) {
107
      	case 'coinchose':
108
      	  foreach ($aData as $aItem) {
109
      	    if($strCurrency == $aItem[0])
110
      	      return $aItem['price'];
111
      	  }
112
      	  break;
113
      	case 'btce':
114
      	  return $aData['ticker']['last'];
115
      	  break;
116
      	case 'cryptsy':
117
      	  return @$aData['return']['markets'][$strCurrency]['lasttradeprice'];
118
      	  break;
119
        case 'cryptopia':
120
      	  return @$aData['Data']['LastPrice'];
121
      	  break;
122
      	case 'cryptorush':
123
      	  return @$aData["$strCurrency/" . $this->config['price']['currency']]['last_trade'];
124
      	  break;
125
      	case 'mintpal':
126
      	  return @$aData['0']['last_price'];
127
      	  break;
128
        case 'c-cex':
129
          return @$aData['ticker']['lastprice'];
130
          break;
131
      	case 'bittrex':
132
      	  return @$aData['result']['Last'];
133
      	  break;
134
      }
135
    } else {
136
      $this->setErrorMessage("Got an invalid response from ticker API");
137
      return false;
138
    }
139
    // Catchall, we have no data extractor for this API url
140
    $this->setErrorMessage("Undefined API to getPrice() on URL " . $this->config['price']['url']);
141
    return false;
142
  }
143
}
144
145
$tools = new Tools();
146
$tools->setDebug($debug);
147
$tools->setConfig($config);
148
$tools->setMemcache($memcache);
149