This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
contacts.php
258 lines (256 loc) · 11 KB
/
contacts.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<html>
<head>
<title>Bittorium webwallet</title>
<link rel="shortcut icon" href="images/logo.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://rawgit.com/schmich/instascan-builds/master/instascan.min.js"></script>
</head>
<body>
<div class="header">
<div class="logo"><img src="/images/logo.png"></div>
<div class="pagetitle">Bittorium Web Wallet</div>
</div>
<div class="page">
<?php
require("config.php");
require("lib/daemon.php");
require("lib/database.php");
require("lib/validate.php");
require("lib/users.php");
try {
open_database();
} catch (Exception $e) {
echo '<span class="error">Caught exception while opening database: ', $e->getMessage(), "</span></div></body></html>";
exit();
}
try {
check_database();
} catch (Exception $e) {
echo '<span class="error">Caught exception while reading database: ', $e->getMessage(), "</span></div></body></html>";
exit();
}
// Check if user has logged in or not?
require("lib/login.php");
//
$address = "";
if (logged_in()) {
$spendKey = $_COOKIE['spendKey'];
$feeAddress = "";
if (!validate_spendkey($spendKey)) {
echo "<span class='error'>Invalid spend key!</span></div></body></html>";
exit();
}
$address = get_address($spendKey);
$params = Array();
$params['address'] = $address;
$getBalance = walletrpc_post("getBalance", $params);
$availableBalance = $getBalance->availableBalance;
$lockedBalance = $getBalance->lockedAmount;
require("lib/menu.php");
$action = 'viewall';
if (isset($_POST['action'])) {
$action = $_POST['action'];
}
if (isset($_GET['view'])) {
$action = "view";
}
if ($action == 'viewall') {
echo "<div id='wallet'>Address: ", $address, "</div><br>";
echo "<div id='qr'><img src='qr.php'></div>";
//
$contacts = get_contacts($spendKey);
echo "<div id='content'>";
if ($contacts === false) {
echo "<span class='error'>No contacts</span>";
} else {
// echo "Contacts: ";
// var_dump($contacts);
if (count($contacts) > 0) {
echo "<div class='hscroll'>";
echo "<table class='contacts'>";
echo "<thead>";
echo "<th>Name</th><th>Address</th><th>Payment ID</th>";
echo "</thead>";
echo "<tbody>";
foreach ($contacts as $contact) {
echo "<tr><td><a href='contacts.php?view=".htmlspecialchars($contact["name"])."'>".htmlspecialchars($contact["name"])."</a></td><td>".$contact["address"]."</td><td>".$contact["paymentID"]."</td></tr>";
}
echo "</tbody>";
echo "</table>";
echo "</div>";
}
}
echo "<h3>Add contact</h3>";
echo "<form action='contacts.php' method='post'>";
echo "<input type='hidden' name='action' value='add'>";
echo "<table class='contact'>";
echo "<tr><th>Name:</th><td><input type='string' name='name' minlength='1' required size='64' value=''></td>";
echo "<td rowspan='3'><span id='scan'><a class='button' onclick='scanQR('address');' href='javascript:return false;'>Scan QR</a><br>Cameras: <span id='cameras'>None</span></span></td>";
echo "<td rowspan='4'><video id='preview' style='display: none'></video></td></tr>";
echo "<tr><th>Address:</th><td><input type='string' name='address' maxlength='97' required pattern='bT[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}' size='97' value=''></td></tr>";
echo "<tr><th>Payment ID:</th><td><input type='string' name='paymentID' maxlength='64' pattern='.{0}|[0-9a-fA-F]{64}' size='64' value=''></td></tr>";
echo "<tr><td colspan='4' class='submit'><input type='submit' class='btn' name='submit' value='Add'></td></tr></table></form>";
echo "</div>";
echo "<script src='qrscanner.js'></script>";
} else if ($action == 'add') {
$cname = $_POST['name'];
$caddress = $_POST['address'];
$cpaymentID = $_POST['paymentID'];
if (!validate_contact_name($cname)) {
echo "<span class='error'>Contact name is invalid!</span></div></body></html>";
exit();
}
if (!validate_address($caddress)) {
echo "<span class='error'>Contact address is invalid!</span></div></body></html>";
exit();
}
if (strlen($cpaymentID) > 0 && !validate_paymentid($cpaymentID)) {
echo "<span class='error'>Contact's payment ID is invalid!</span></div></body></html>";
exit();
}
if (has_contact($spendKey, $cname)) {
echo "<span class='error'>Contact with name '".htmlspecialchars($cname)."' already exists!</span><br><br>";
echo "<a href='contacts.php?view=".htmlspecialchars($cname)."' class='btn'>View</a>";
echo "</div></div></body></html>";
exit();
}
$result = create_contact($spendKey, $cname, $caddress, $cpaymentID);
if ($result) {
echo "Contact created.<br>";
} else {
echo "<span class='error'>Creating contact failed.</span><br>";
}
echo "<br><a href='contacts.php' class='btn'>Return</a>";
echo "</div>";
// var_dump($result);
} else if ($action == 'view') {
$cname = $_GET['view'];
if (!validate_contact_name($cname)) {
echo "<span class='error'>Contact name is invalid!</span></div></body></html>";
exit();
}
$contact=get_contact($spendKey, $cname);
// var_dump($contact);
echo "<h3>Contact "", htmlspecialchars($cname), ""</h3>";
echo "<table class='contact'>";
echo "<tr>";
echo "<form action='contacts.php' method='post'>";
echo "<input type='hidden' name='action' value='update'>";
echo "<input type='hidden' name='name' value='".htmlspecialchars($cname)."'>";
echo "<tr><th>Address:</th><td><input type='string' maxlength='97' required pattern='bT[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{95}' name='address' size='97' value='".$contact["address"]."'></td></tr>";
echo "<tr><th>Payment ID:</th><td><input type='string' maxlength='64' pattern='.{0}|[0-9a-fA-F]{64}' name='paymentID' size='64' value='".$contact["paymentID"]."'></td></tr>";
echo "<tr><td colspan='2' class='submit'><input type='submit' class='btn' name='submit' value='Update'></td></tr>";
echo "</form>";
echo "</table>";
echo "<h3>Rename contact</h3>";
echo "<table class='contact'>";
echo "<form action='contacts.php' method='post'>";
echo "<input type='hidden' name='action' value='rename'>";
echo "<input type='hidden' name='oldName' value='".htmlspecialchars($cname)."'>";
echo "<tr><th>New name:</th><td><input type='string' minlength='1' name='newName' size='64' value=''></td></tr>";
echo "<tr><td colspan='2' class='submit'><input type='submit' class='btn' name='submit' value='Rename'></td></tr>";
echo "</form>";
echo "</table>";
$maxAmount = $availableBalance - 1;
$getFeeAddress = daemonrpc_get("/feeaddress");
if (array_key_exists('fee_address', $getFeeAddress)) {
$feeAddress = $getFeeAddress->fee_address;
if (validate_address($feeAddress)) {
$feeAmount = min(1, max(floatval($maxAmount) / 40001, 100));
$maxAmount -= $feeAmount;
}
}
if ($maxAmount > 0) {
echo "<h3>Send BTOR to contact</h3>";
echo "<form action='send.php' method='post'>";
echo "<input type='hidden' name='recipient' value='".$contact['address']."'>";
echo "<input type='hidden' name='paymentID' value='".$contact['paymentID']."'>";
echo "<table class='send'>";
echo "<tr><th>Amount:</th><td><input type='number' min='0.01' max='" . number_format($maxAmount / 100, 2) . "' step='0.01' name='amount' value='0.01'></td>";
echo "<td rowspan='2'><input type='submit' class='btn' name='submit' value='Send'></td>";
echo "</tr>";
echo "<tr><th>Anonymity level:</th><td><input type='number' min='0' max='9' step='1' name='anonymity' value='0'></td></tr>";
echo "</table>";
echo "</form>";
}
echo "<h3>Delete contact</h3>";
echo "<form action='contacts.php' method='post'>";
echo "<input type='hidden' name='action' value='delete'>";
echo "<input type='hidden' name='name' value='".htmlspecialchars($cname)."'>";
echo "<input type='submit' name='submit' class='btn' value='Delete'>";
echo "</form>";
echo "</div>";
} else if ($action == 'rename') {
$oldName=$_POST['oldName'];
$newName=$_POST['newName'];
if (!validate_contact_name($oldName) || !has_contact($spendKey, $oldName)) {
echo "<span class='error'>Old contact name is not valid!</span></div></body></html>";
exit();
}
if (!validate_contact_name($newName) || has_contact($spendKey, $newName)) {
echo "<span class='error'>New contact name is not valid!</span></div></body></html>";
exit();
}
if ($oldName == $newName) {
echo "<span class='error'>Old and new names are the same!</span></div></body></html>";
exit();
}
$result = rename_contact($spendKey, $oldName, $newName);
if ($result) {
echo "Contact renamed...<br><br>";
echo "<a href='contacts.php?view=".htmlspecialchars($newName)."' class='btn'>View</a>";
} else {
echo "<span class='error'>Renaming failed.</span><br><br>";
echo "<a href='contacts.php?view=".htmlspecialchars($oldName)."' class='btn'>View</a>";
}
echo "</div>";
} else if ($action == 'update') {
$cname=$_POST['name'];
$caddress=$_POST['address'];
$cpaymentID=$_POST['paymentID'];
if (!validate_contact_name($cname) || !has_contact($spendKey, $cname)) {
echo "<span class='error'>Contact name is invalid!</span></div></body></html>";
exit();
}
if (!validate_address($caddress)) {
echo "<span class='error'>Contact address is invalid!</span></div></body></html>";
exit();
}
if (strlen($cpaymentID) > 0 && !validate_paymentid($cpaymentID)) {
echo "<span class='error'>Contact's payment ID is invalid!</span></div></body></html>";
exit();
}
$result = update_contact($spendKey, $cname, $caddress, $cpaymentID);
if ($result) {
echo "Contact updated...<br>";
} else {
echo "<span class='error'>Updating of contact failed...</span><br>";
}
echo "<br><a href='contacts.php?view=".htmlspecialchars($cname)."' class='btn'>View</a>";
echo "</div>";
} else if ($action == 'delete') {
$cname=$_POST['name'];
if (!validate_contact_name($cname)) {
echo "<span class='error'>Contact name is invalid!</span></div></body></html>";
exit();
}
if (!has_contact($spendKey, $cname)) {
echo "<span class='error'>Contact does not exist!</span></div></body></html>";
exit();
}
$result=delete_contact($spendKey, $cname);
if ($result) {
echo "Contact deleted...<br><br>";
echo "<a href='contacts.php' class='btn'>Back</a>";
} else {
echo "<span class='error'>Deleting of contact failed...</span><br><br>";
echo "<a href='contacts.php?view=".htmlspecialchars($cname)."' class='btn'>View</a>";
}
echo "</div>";
}
}
?>
</body>
</html>