-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveWallet.php
executable file
·132 lines (112 loc) · 4 KB
/
saveWallet.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
<?php
ob_start();
include 'checkUser.php';
if (!canEdit()){
header('Location: ./consult.php');
exit();
}
$curr=$_POST['cur'];
$pid=$_POST['id'];
$origin=$_POST['o'];
$validated=0;
if (isset($_POST['validated'])){
$validated=1;
}
// Check format
$addr = strtolower(preg_replace("/[^a-zA-Z0-9]+/", "", $_POST['wallet']));
if (strlen($addr) != 42) {
header('Location: ./addWallet.php?cur='.$curr.'&id='.$pid.'&wallet='.$_POST['wallet'].'&error=1&o='.$origin);
exit();
}
include 'connectionFactory.php';
$mysqli= ConnectionFactory::GetConnection();
// Check unique
$query = 'SELECT count(Id)
FROM Reg_Wallet
WHERE address=? AND (PersonId IS NOT NULL OR CodeId IS NOT NULL)
';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s",$addr);
$stmt->bind_result($number);
$stmt->execute();
$stmt->fetch();
$stmt->close();
if ($number>0){
header('Location: ./addWallet.php?cur='.$curr.'&id='.$pid.'&wallet='.$_POST['wallet'].'&error=2&o='.$origin);
exit();
} else {
$query = 'DELETE FROM Reg_Wallet
WHERE address=? AND PersonId IS NULL AND CodeId IS NULL';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s",$addr);
$stmt->execute();
$stmt->close();
}
// get code
$code='';
$codeId='';
$url = "https://node-001.cchosting.org/specific/MLGetCode.php";
$data = array('server' => 'Monnaie-Leman', 'addresses'=>$addr);
if ($curr=='EUR') {
$data = array('server' => 'Leman-EU', 'addresses'=>$addr);
}
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$res = json_decode($result);
if ($result !== FALSE && $result!='KO' && isset($res->$addr)) {
$code = $res->$addr;
} else {
//// insert without code
$query = 'INSERT INTO Reg_Wallet (address, PersonId, Validated, Currency) VALUES (?,?,?,?)';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("siis",$addr,$pid,$validated,$curr);
$stmt->execute();
$stmt->close();
header('Location: ./consultPerson.php?id='.$pid);
//header('Location: ./addWallet.php?cur='$curr.'&id='.$pid.'&wallet='.$_POST['wallet'].'&error=3');
exit();
}
// CHECK code belong to the right person
if($code!=''){
$query = 'SELECT Id, PersonId
FROM Reg_Code
WHERE Code=?
';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s",$code);
$stmt->bind_result($codeId, $conflictingid);
$stmt->execute();
$stmt->fetch();
$stmt->close();
if ($conflictingid>0 ){
if ($conflictingid !=$pid) {
header('Location: ./addWallet.php?cur='.$curr.'&id='.$pid.'&wallet='.$_POST['wallet'].'&error=4&cid='.$codeId.'&o='.$origin);
exit();
} else {
// code ok
}
} else {
// insert code
$query = 'INSERT INTO Reg_Code (PersonId,Code, Currency) VALUES (?,?,?)';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("iss",$pid,$code,$curr);
$stmt->execute();
$codeId = $stmt->insert_id;
$stmt->close();
}
}
// insert address
$query = 'INSERT INTO Reg_Wallet (address, PersonId, CodeId, Validated, Currency) VALUES (?,?,?,?,?)';
$stmt = $mysqli->prepare($query);
$stmt->bind_param("siiis",$addr,$pid,$codeId,$validated,$curr);
$stmt->execute();
$stmt->close();
header('Location: ./consultPerson.php?id='.$pid.'&o='.$origin);
exit();
?>