-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
208 lines (160 loc) · 6.62 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
202
203
204
205
206
207
208
<?php
//Php MySqlDB-Data
require_once('lib/conf.php');
//Include header
include("header.php");
?>
<p>
Input block height
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get">
<input type="number" name="height" min="0">
<br>
<input type="submit" value="Get">
</form>
</p>
<p>
Input block hash
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="get">
<input type="text" name="hash" size="90">
<br>
<input type="submit" value="Get">
</form>
</p>
<br>
<br>
<?php
//Get the user input and display prime chain
getPrimeChain();
//Include footer
include("footer.php");
?>
<?php
function getPrimeChain(){
echo("<p>");
//Connect to DB
$con = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PW, MYSQL_DB);
//If connection fails
if(!$con){
die("Connection to Database not established");
}
$sql="
SELECT `value` FROM `numinos_prime_data`
WHERE `info`='blocks';
";
//Retrieve block number of most current block
$primeBlockCount = mysqli_fetch_array(mysqli_query($con, $sql));
$height = NULL;
$hash = NULL;
//Check for correctness and safeness of user input
if(isset($_GET["height"])){
$height = $_GET["height"];
if($height > $primeBlockCount["value"] || $height < 0 || is_nan(floatval($height)) || $height == NULL){
echo "404 - Block not found";
return;
}
}
if(isset($_GET["hash"])){
$hash = $_GET["hash"];
if(strlen($hash) != 64 || !ctype_xdigit($hash)){
echo "404 - Block not found";
return;
}else{
$sql="
SELECT `index` FROM `numinos_prime_blockhashes`
WHERE `hash`='" . $hash . "';
";
//Retrieve block number of current hash block
$primeHashBlock = mysqli_fetch_array(mysqli_query($con, $sql));
//Check if hash exists in the database
if($primeHashBlock["index"] == NULL){
echo "404 - Block not found";
return;
}else{
//If it does, set height to hashes height
$height = $primeHashBlock["index"];
}
}
}
////If none of the two variables are set - return nothing
if(isset($_GET["height"]) == FALSE && isset($_GET["hash"]) == FALSE){
//Set height to latest block to show
$height = $primeBlockCount["value"] - 1;
}
//If database is reachable and height is not null
if($con && isset($height)){
//If primechainerPath points to correct exe
if(file_exists(PRIMECHAINERPATH)){
//Get primeorigin, primechain and length for given height
$sql = "
SELECT `hash`, `primeorigin`, `primechain`, `length` FROM `numinos_prime_blockhashes`
WHERE `index`='" . $height . "';
";
//Get prime origin and prime chain type to parse to external primechain calculator primechainer.exe
$primeDataArray = mysqli_fetch_array(mysqli_query($con, $sql));
$primeChainType = explode(".", $primeDataArray["primechain"]);
//array_reverse because primechains are in reverse order
$primeChainArray = array_reverse(explode(",", shell_exec(PRIMECHAINERPATH . " " . $primeDataArray["primeorigin"] . " " . $primeChainType[0] . " " . $primeDataArray["length"])));
//Table to wrap navigation buttons and height/hash table
echo("<table border=\"0\"><tr>");
//Previous Block button
echo("
<td><form action=\"" . htmlentities($_SERVER['PHP_SELF']) . "\" method=\"get\">
<input type=\"hidden\" name=\"height\" value=\"" . ($height - 1) . "\">
<br>
<input type=\"submit\" value=\"Previous\">
</select>
</form></td>
");
//Chain height/hash table
echo("<td><table border=\"1\">
<tr>
<th>Height</th>
<th>Hash</th>
</tr>
<tr>
<td style=\"text-align:center;\">" . $height . "</td>
<td>" . $primeDataArray["hash"] . "</td>
</tr>
</table></td>
</p>");
//Next Block button
echo("
<td><form action=\"" . htmlentities($_SERVER['PHP_SELF']) . "\" method=\"get\">
<input type=\"hidden\" name=\"height\" value=\"" . ($height + 1) . "\">
<br>
<input type=\"submit\" value=\" Next \">
</select>
</form></td>
");
//END Table to wrap navigation buttons and height/hash table
echo("</tr></table>");
//Chain info table
echo("<p>
<table border=\"1\">
<tr>
<th>Origin</th>
<th>Type</th>
<th>Length</th>
</tr>
<tr>
<td>" . $primeDataArray["primeorigin"] . "</td>
<td style=\"text-align:center;\">" . substr($primeChainType[0], 0 , 3) . "</td>
<td style=\"text-align:center;\">" . $primeDataArray["length"] . "</td>
</tr>
</table>
</p>");
//Chain data table
echo("<p>
<table border=\"1\">
<tr>
<th>Chain</th>
</tr>");
foreach(array_filter($primeChainArray) as $primeChainPiece){
echo("<tr><td>" . $primeChainPiece . "</td></tr>");
}
echo("</table>");
echo("</p>");
}
}
}
?>