-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.php
201 lines (171 loc) · 7.16 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php require_once('init.php');
if(User::is_logged_in())
{
$user = new User($_SESSION['user_id']);
$asset = Asset::init(XMR,$config['asset'][XMR]['properties']);
$asset_config = $config['asset'][XMR];
$asset_id = $asset->get_id();
$user_id = $user->id();
$payment_id = null;
$balance = null;
$balance = $asset->get_balance($user);
$payment_id = $asset->get_payment_id($user);
// If payment id was not found, create one
if( ! $payment_id OR isset($_POST['new_payment_id']))
{
$asset->create_payment_id($user);
refresh();
}
if(isset($_POST['withdraw_xmr']))
{
$amount = trim($_POST['xmr_amount']);
// Prepare POST data
$post = array(
'address' => trim($_POST['xmr_address']),
'payment_id' => trim($_POST['xmr_payment_id']),
'amount' => $amount,
'mixin' => filter_var($_POST['xmr_mixin'], FILTER_VALIDATE_INT, array(
'options' => array(
'default' => $asset_config['default_mixin'],
'min_range' => $asset_config['min_mixin'],
'max_range' => $asset_config['max_mixin']
),
)),
'receivable_amount' => bc::op($amount,'-',$asset_config['withdraw_fee']),
'asset_id' => $asset->id
);
if( ! csrf_check($_POST['csrf_token']))
{
$error->set('xmr_address','Invalid CSRF, session expired. Please refresh.');
}
if( ! $asset->valid_address($post['address']))
{
$error->set('xmr_address','Please enter a valid XMR Address');
}
if( ! $asset->valid_payment_id($post['payment_id']))
{
$error->set('xmr_payment_id','Please enter a valid Payment ID (64 characters, alpha-numeric string) or leave the field empty to send without payment id');
}
if( ! $asset->valid_amount($post['amount']))
{
$error->set('xmr_amount','Enter a valid amount');
}
if( ! $asset->valid_withdraw($post['amount'],$asset_config['withdraw_fee']))
{
$error->set('xmr_amount','Enter a valid amount');
}
if( ! $asset->available_balance($user,$post['amount']))
{
$error->set('xmr_amount','You do not have sufficient balance to withdraw this amount');
}
if( ! $error->is_errors())
{
$db->query('START TRANSACTION');
// Detuct balance
if($db->query('UPDATE users_assets SET balance = balance - '.$post['amount'].' WHERE user_id = '.$user_id.' AND asset_id = '.$asset_id.' AND balance >= '.$post['amount']))
{
// Add withdraw
$sql = insert_query('withdraws_pending',array(
'user_id' => $user_id,
'address' => $post['address'],
'payment_id' => $post['payment_id'],
'amount' => $post['receivable_amount'],
'fee' => $asset_config['withdraw_fee'],
'date_requested' => array('UTC_TIMESTAMP()'),
'asset_id' => $post['asset_id'],
'mixin' => $post['mixin'],
'status' => 1,
'error' => '-'
));
if($db->query($sql))
{
$db->query('COMMIT');
$_SESSION['flash_message'] = '<p class="flashmsg bg-success">Your withdraw has been successfully submitted!</p>';
refresh();
}
else
{
$db->query('ROLLBACK');
}
}
else
{
$db->query('ROLLBACK');
// Could not detuct funds, try again etc
}
}
}
$deposits = $db->query("
SELECT uct.id,
uct.amount,
uct.block_height,
uct.tx_hash,
uct.datetime,
uct.status
FROM users_cn_payment_ids AS ucpi
LEFT JOIN users_cn_transactions AS uct
ON ucpi.pid = uct.pid
WHERE ucpi.user_id = ".$user->id()."
AND ucpi.asset_id = ".$asset->get_id()."
AND uct.id IS NOT NULL
");
$withdraws_pending = $db->query('
SELECT *
FROM withdraws_pending
WHERE user_id = '.$user->id().'
ORDER BY id
DESC'
);
$withdraws_complete = $db->query('
SELECT *
FROM withdraws_complete
WHERE user_id = '.$user->id().'
ORDER BY id
DESC'
);
}
else // if not logged in
{
if($_POST)
{
if(isset($_POST['login']))
{
if(User::login($_POST['username'],$_POST['password']))
{
refresh();
}
else
{
$error->set('username','Invalid username or password.');
$error->set('password','Password');
}
}
if(isset($_POST['register']))
{
$username = trim($_POST['register_username']);
$password = $_POST['register_password'];
if( ! User::valid_username($username))
{
$error->set('register_username','Invalid username, please enter an alphanumeric '.$config['user']['username']['min_length'].'-'.$config['user']['username']['max_length'].' in length');
}
// Do not allow multiple users with the same username
if(User::username_exists($username))
{
$error->set('register_username','Username has been taken, please try something else...');
}
if ( ! User::valid_password($password))
{
$error->set('register_password','Enter a password of minimum '.$config['user']['password']['min_length'].' characters');
}
if( ! $error->is_errors())
{
if(User::register($username,$password))
{
User::login($username,$password);
refresh();
}
}
}
}
}
include 'views/dashboard.php';