forked from s4l1h/openex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
executable file
·159 lines (145 loc) · 5.13 KB
/
api.php
File metadata and controls
executable file
·159 lines (145 loc) · 5.13 KB
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
149
150
151
152
153
154
155
156
157
158
159
<?php
header('Content-type: text/json');
require_once("models/config.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
if(isset($_GET["Pub"]) && isset($_GET["Priv"]))
{
$pub = mysql_real_escape_string($_GET["Pub"]);
$priv = mysql_real_escape_string($_GET["Priv"]);
}
else
{
$pub = null;
$priv = null;
}
$method = $_GET["Method"];
$user_id = 0; //Store user id. If a user does not give their api information they can still access public api functions.
$status = false;
$errors = array();
$results = array();
if($method == NULL)
{
$errors[count($errors)] = "Method not defined";
}
if($pub != NULL && $priv != NULL)
{
$sql = @mysql_query("SELECT * FROM Api_Keys WHERE `Public_Key`='$pub' AND `Authentication_Key` ='$priv' ");
$user_id = @mysql_result($sql,0,"User_ID") or 0;
if($user_id == 0)
{
$errors[count($errors)] = "The provided API key was invalid";
}
}
if($user_id != 0)//Allow only verified users to use the following api functions.
{
if($method = "CreateNewTrade")
{
$price = mysql_real_escape_string($_GET["Price"]);
$amount = mysql_real_escape_string($_GET["Amount"]);
$mid = mysql_real_escape_string($_GET["MarketId"]);
if($price == NULL)
$errors[count($errors)] = "Price not defined";
if($amount == NULL)
$errors[count($errors)] = "Amount not defined";
if($mid == NULL)
$errors[count($errors)] = "MarketId not defined";
}
}
//Public methods that do not require API key go below!
if($method == "GetTradeHistory")
{
$mid = @mysql_real_escape_string($_GET["MarketId"]) or null;
$limit = @mysql_real_escape_string($_GET["Limit"]);
if($mid == NULL)
{
$errors[count($errors)] = "MarketId not defined";
}
if($mid != NULL)
{
$trade_array = array();
$sql = @mysql_query("SELECT * FROM Trade_History WHERE `Market_Id`='$mid' ORDER BY Timestamp DESC LIMIT " . intval($limit));
$num = @mysql_num_rows($sql);
for($i = 0;$i<$num;$i++)
{
$amount = @mysql_result($sql,$i,"Quantity");
$price = @mysql_result($sql,$i,"Price");
$total = $amount * $price;
$timestamp = @mysql_result($sql,$i,"Timestamp");
$results[count($results)] = array("Quantity" => sprintf("%.8f",$amount),"PricePer" => sprintf("%.8f",$price),"Total" => sprintf("%.8f",$total),"Timestamp" => $timestamp);
}
//$results[count($results)] = $trade_array;
$status = true;
}
else
{
$status = false;
}
}
if($method == "GetMarketData")
{
$mid = mysql_real_escape_string($_GET["MarketId"]);
if($mid == NULL)
{
$errors[count($errors)] = "MarketId not defined";
}
else
{
$sql = @mysql_query("SELECT * FROM Wallets WHERE `Id`='$mid'");
$name = @mysql_result($sql,0,"Name");
$acronymn = @mysql_result($sql,0,"Acronymn");
$sql = @mysql_query("SELECT * FROM Trade_History WHERE `Market_Id`='$mid' ORDER BY Timestamp DESC LIMIT 1");
$last_trade = @mysql_result($sql,0,"Price");
$sql = @mysql_query("SELECT * FROM trades WHERE `From`='$acronymn' AND `Type`='$acronymn' ORDER BY `Value` ASC limit 10");//Grab Sell Orders
$num = @mysql_num_rows($sql);
$sell_array = array();
for($i = 0;$i<$num;$i++)
{
$amount = mysql_result($sql,$i,"Amount");
$price = mysql_result($sql,$i,"Value");
$total = $amount * $price;
$sell_array[$i] = array("Quantity" => sprintf("%.8f",$amount),"PricePer" => sprintf("%.8f",$price),"Total" => sprintf("%.8f",$total));
}
$sql = @mysql_query("SELECT * FROM trades WHERE `To`='$acronymn' AND `Type`='$acronymn' ORDER BY `Value` ASC limit 10");//Grab Sell Orders
$num = @mysql_num_rows($sql);
$buy_array = array();
for($i = 0;$i<$num;$i++)
{
$amount = @mysql_result($sql,$i,"Amount");
$price = @mysql_result($sql,$i,"Value");
$total = $amount * $price;
$buy_array[$i] = array("Quantity" => sprintf("%.8f",$amount),"PricePer" => sprintf("%.8f",$price),"Total" => sprintf("%.8f",$total));
}
$status = true;
$results[count($results)] = array("MarketId" => $mid, "Name" => $name, "Acronymn" => $acronymn, "LastTradePrice" => $last_trade,"SellOrders" => $sell_array,"BuyOrders" => $buy_array);
}
}
if($method == "GetConversion")
{
$mid = mysql_real_escape_string($_GET["MarketId"]);
$amt = mysql_real_escape_string($_GET["Amount"]);
$sql = @mysql_query("SELECT * FROM Wallets WHERE `Id`='$mid'");
$acronymn = @mysql_result($sql,0,"Acronymn");
if($mid == NULL)
$errors[count($errors)] = "MarketId not defined";
if($amt == NULL)
$errors[count($errors)] = "Amount not defined";
if($mid != NULL && $amt != NULL)
{
$sql = @mysql_query("SELECT * FROM Trade_History WHERE `Market_Id`='$mid' ORDER BY Timestamp DESC LIMIT 1");
$last_trade = @mysql_result($sql,0,"Price");
$total = sprintf("%.8f",$last_trade * $amt);
$status = true;
$results[count($results)] = array('Total' => $total,'Acronymn' => "BTC");
}
}
//This makes everything where it prints out in a single json string in order for there to be no confusion on software that is interpreting the output.
if($status == false)
{
echo json_encode(array('Status' => 'Failed','Errors' => $errors));
}
else
{
echo json_encode(array('Status' => 'Success','Results' => $results));
}
?>