|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Http\Request as Request; |
|
|
|
|
|
|
4
|
|
|
use TQ\Shamir\Secret; |
|
5
|
|
|
|
|
6
|
|
|
$app->get('/', function () use ($app) { |
|
7
|
|
|
return view( |
|
8
|
|
|
'index', |
|
9
|
|
|
[ |
|
10
|
|
|
'version' => $app->version(), |
|
11
|
|
|
'status' => null, |
|
12
|
|
|
] |
|
13
|
|
|
); |
|
14
|
|
|
}); |
|
15
|
|
|
|
|
16
|
|
|
$app->post('/', function (Request $request) use ($app) { |
|
17
|
|
|
$response = 'An error ocurred'; |
|
18
|
|
|
|
|
19
|
|
|
// Set up post values |
|
20
|
|
|
|
|
21
|
|
|
$action = $request->input('action'); |
|
22
|
|
|
|
|
23
|
|
|
// Check if action was sent |
|
24
|
|
|
|
|
25
|
|
|
if (!isset($action) || trim($action) == '') { |
|
26
|
|
|
throw new \Exception('No action specified.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Share |
|
30
|
|
|
|
|
31
|
|
|
if ($action === 'share') { |
|
32
|
|
|
$sharesThreshold = (string) $request->input('shares_threshold'); |
|
33
|
|
|
$sharesAmount = (string) $request->input('shares_amount'); |
|
34
|
|
|
$secret = (string) $request->input('secret'); |
|
35
|
|
|
try { |
|
36
|
|
|
//Check if fields are not empty |
|
37
|
|
|
$fields = [ |
|
38
|
|
|
'secret', |
|
39
|
|
|
'shares_amount', |
|
40
|
|
|
'shares_threshold', |
|
41
|
|
|
]; |
|
42
|
|
|
foreach ($fields as $field) { |
|
43
|
|
|
$fieldValue = $request->input($field); |
|
44
|
|
|
if (!isset($fieldValue) || trim($fieldValue) == '') { |
|
45
|
|
|
throw new \Exception('All of the fields are required.'); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!filter_var($sharesThreshold, FILTER_VALIDATE_INT) |
|
50
|
|
|
|| !filter_var($sharesAmount, FILTER_VALIDATE_INT)) { |
|
51
|
|
|
throw new \Exception('The threshold and amount of shares must be integers.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($sharesThreshold > $sharesAmount) { |
|
55
|
|
|
throw new \Exception('The threshold must be lower than or equal to the amount of shares.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$status = 'success'; |
|
59
|
|
|
$response = Secret::share( |
|
60
|
|
|
$secret, |
|
61
|
|
|
$sharesAmount, |
|
62
|
|
|
$sharesThreshold |
|
63
|
|
|
); |
|
64
|
|
|
} catch (Exception $exception) { |
|
65
|
|
|
$status = 'error'; |
|
66
|
|
|
$response = $exception->getMessage(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Recover |
|
71
|
|
|
|
|
72
|
|
|
if ($action === 'recover') { |
|
73
|
|
|
$shares = $request->input('shares'); |
|
74
|
|
|
try { |
|
75
|
|
|
$status = 'success'; |
|
76
|
|
|
$response = Secret::recover(array_filter($shares)); |
|
77
|
|
|
} catch (Exception $exception) { |
|
78
|
|
|
$status = 'error'; |
|
79
|
|
|
$response = $exception->getMessage(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Return response |
|
84
|
|
|
return view( |
|
85
|
|
|
'index', |
|
86
|
|
|
[ |
|
87
|
|
|
'version' => $app->version(), |
|
88
|
|
|
'action' => $action, |
|
89
|
|
|
'status' => (empty($status)) ? null : $status, |
|
90
|
|
|
'response' => json_encode($response), |
|
91
|
|
|
'shareAmount' => (empty($sharesAmount)) ? null : $sharesAmount, |
|
92
|
|
|
'shareThreshold' => (empty($sharesThreshold)) ? null : $sharesThreshold, |
|
93
|
|
|
] |
|
94
|
|
|
); |
|
95
|
|
|
}); |
|
96
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: