-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.php
59 lines (51 loc) · 1.41 KB
/
backend.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
<?php
require_once "common.php";
if (!session_start()) {
error($ERROR_SESSION_INIT);
}
// Security
if (empty($_GET['action'])) {
error($ERROR_MISSING_ARGS);
}
if (!empty($_SESSION['disqualified'])) {
error($ERROR_PROOF_INCORRECT);
}
if (!empty($_SESSION['purchased'])) {
error($ERROR_PROOF_ALREADY_CONFIRMED);
}
// Action Router
switch ($_GET["action"]) {
case "ping":
ok('pong');
break;
case "order":
// Secret Initialization
if (!empty($_SESSION['secret'])) {
error($ERROR_ACTIVE_SESSION);
}
$salt = hash("sha1", openssl_random_pseudo_bytes(1024));
$secret = rand(1, $HASHCASH_RAND_MAX); // workSize
$uuaa = "$salt$secret";
for ($i = 0; $i < $HASHCASH_ROUNDS; $i++) {
$uuaa = hash("sha256", $uuaa);
}
$_SESSION["secret"] = $secret;
ok("$salt;$uuaa;$HASHCASH_ROUNDS");
break;
case "purchase":
// Proof Delivery and Validation
if (empty($_SESSION['secret'])) {
error($ERROR_MISSING_SECRET);
}
if (empty($_GET['secret'])) {
error($ERROR_MISSING_ARGS);
}
if ($_SESSION["secret"] == $_GET['secret']) {
$_SESSION["purchased"] = true;
ok("purchase ok");
} else {
$_SESSION["disqualified"] = true;
error($ERROR_PROOF_INCORRECT);
}
break;
}