/
tools.class.php
148 lines (142 loc) · 4.9 KB
/
tools.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
/**
* Helper class for our cronjobs
* Implements some common cron tasks outside
* the scope of our web application
**/
class Tools extends Base {
public function getOnlineVersions() {
// Fetch version online, cache for a bit
$key = $this->config['memcache']['keyprefix'] . 'ONLINE_VERSIONS';
if (! $mpos_versions = $this->memcache->get($key)) {
$url = $this->config['version_url'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
preg_match('/define\(\'MPOS_VERSION\', \'(.*)\'\);/', $data, $match);
$mpos_versions['MPOS_VERSION'] = @$match[1];
preg_match('/define\(\'DB_VERSION\', \'(.*)\'\);/', $data, $match);
$mpos_versions['DB_VERSION'] = @$match[1];
preg_match('/define\(\'CONFIG_VERSION\', \'(.*)\'\);/', $data, $match);
$mpos_versions['CONFIG_VERSION'] = @$match[1];
curl_close($curl);
return $this->memcache->setCache($key, $mpos_versions, 30);
} else {
return $mpos_versions;
}
}
/**
* Fetch JSON data from an API
* @param url string API URL
* @param target string API method
* @param auth array Optional authentication data to be sent with
* @return dec array JSON decoded PHP array
**/
public function getApi($url, $target, $auth=NULL) {
static $ch = null;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, $url . $target);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// run the query
$res = curl_exec($ch);
if ($res === false) {
$this->setErrorMessage('Could not get reply: '.curl_error($ch));
return false;
}
$dec = json_decode($res, true);
if (!$dec) {
$this->setErrorMessage('Invalid data received, please make sure connection is working and requested API exists');
return false;
}
return $dec;
}
/**
* Detect the API to properly extract information
* @param url string API URL
* @return data string API type
**/
private function getApiType($url) {
if (preg_match('/coinchoose.com/', $url)) {
return 'coinchose';
} else if (preg_match('/btc-e.com/', $url)) {
return 'btce';
} else if (preg_match('/cryptsy.com/', $url)) {
return 'cryptsy';
} else if (preg_match('/cryptopia.co.nz/', $url)) {
return 'cryptopia';
} else if (preg_match('/cryptorush.in/', $url)) {
return 'cryptorush';
} else if (preg_match('/mintpal.com/', $url)) {
return 'mintpal';
} else if (preg_match('/c-cex.com/', $url)) {
return 'c-cex';
} else if (preg_match('/bittrex.com/', $url)) {
return 'bittrex';
}
$this->setErrorMessage("API URL unknown");
return false;
}
/**
* Extract price information from API data
**/
public function getPrice() {
$aData = $this->getApi($this->config['price']['url'], $this->config['price']['target']);
$strCurrency = $this->config['currency'];
// Check the API type for configured URL
if (!$strApiType = $this->getApiType($this->config['price']['url']))
return false;
// if api data is valid, extract price depending on API type
if (is_array($aData)) {
switch ($strApiType) {
case 'coinchose':
foreach ($aData as $aItem) {
if($strCurrency == $aItem[0])
return $aItem['price'];
}
break;
case 'btce':
return $aData['ticker']['last'];
break;
case 'cryptsy':
return @$aData['return']['markets'][$strCurrency]['lasttradeprice'];
break;
case 'cryptopia':
return @$aData['Data']['LastPrice'];
break;
case 'cryptorush':
return @$aData["$strCurrency/" . $this->config['price']['currency']]['last_trade'];
break;
case 'mintpal':
return @$aData['0']['last_price'];
break;
case 'c-cex':
return @$aData['ticker']['lastprice'];
break;
case 'bittrex':
return @$aData['result']['Last'];
break;
}
} else {
$this->setErrorMessage("Got an invalid response from ticker API");
return false;
}
// Catchall, we have no data extractor for this API url
$this->setErrorMessage("Undefined API to getPrice() on URL " . $this->config['price']['url']);
return false;
}
}
$tools = new Tools();
$tools->setDebug($debug);
$tools->setConfig($config);
$tools->setMemcache($memcache);