forked from blockchain-it-hr/h5ai-nginx
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathformatAddress.php
55 lines (46 loc) · 1.41 KB
/
formatAddress.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
<?php
include_once("./Kekkak.php");
use kornrunner\Keccak;
function formatAddress($addr)
{
$address = strtolower($addr);
$addressWithout0x = substr($address, 2);
$chars = str_split($addressWithout0x, 1);
$hashed = str_split(Keccak::hash($addressWithout0x, 256), 2);
for ($i = 0; $i < 40; $i += 2) {
if (hexdec($hashed[$i >> 1]) >> 4 >= 8) {
$chars[$i] = strtoupper($chars[$i]);
}
if ((hexdec($hashed[$i >> 1]) & 0x0f) >= 8) {
$chars[$i + 1] = strtoupper($chars[$i + 1]);
}
}
return "0x" . join($chars);
}
$pathParts = explode("/", $_SERVER['REQUEST_URI']);
$finalPath = '';
// loop on each part of url path
foreach ($pathParts as $pathPart) {
$part = $pathPart;
$matches = null;
// if the part is an address
preg_match('/0x([A-Fa-f0-9]{40})/i', $part, $matches);
if (count($matches) > 0) {
// format the address
$part = formatAddress($part);
}
$finalPath .= $part . "/";
}
// remove last "/"
$finalPath = rtrim($finalPath, "/");
// compare the two path, the one received and the one after formatting the address
if (
count(array_diff($pathParts, explode("/", $finalPath))) === 0
) {
// if they have no difference continue calling h5ai
include('./index.php');
} else {
// otherwise redirect to the right address
header("Location: http://$_SERVER[HTTP_HOST]$finalPath");
exit();
}