-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
124 lines (106 loc) · 3.56 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
<?php
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/helpers/functions.php';
require __DIR__.'/config.php';
session_start();
$sigaClient = SigaClient\SigaClient::create([
'url' => SIGA_ENDPOINT,
'name' => SIGA_CLIENT_NAME,
'service' => SIGA_SERVICE_NAME,
'uuid' => SIGA_UUID,
'secret' => SIGA_SIGN_SECRET,
]);
if ($_SESSION['containerId']) {
$sigaClient->setContainerId($_SESSION['containerId']);
}
/**
* Load action template
*
* @param string $action Action name
* @param array $params Parameters passed to template
*
* @return void
*/
function loadActionTemplate(string $action, array $params = [])
{
loadHeader();
loadContentTemplate($action, $params);
loadFooter();
}
function loadHeader()
{
require(__DIR__.'/templates/header.php');
}
function loadContentTemplate(string $action, array $params = [])
{
extract($params);
$allowedActions = ['default','create_new_doc', 'show_doc_info'];
if (in_array($action, $allowedActions)) {
include(__DIR__.'/templates/'.strtolower($action).'.php');
}
}
function loadFooter()
{
require(__DIR__.'/templates/footer.php');
}
/* Parse acts */
if ($_GET['action'] == 'download_container') {
try {
$pathToFile = getUploadDirectory(). DIRECTORY_SEPARATOR . $_SESSION['containerId'].'.asice';
if (!file_exists($pathToFile)) {
throw new Exception("Signed file not found!");
}
$containerName = getContainerName($_SESSION['containerId'], $_SESSION['containerFiles']);
header("Content-Disposition: attachment; filename=\"" . $containerName . "\"");
header("Content-Transfer-Encoding: Binary");
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($pathToFile));
header('Connection: close');
readfile($pathToFile);
die();
} catch (Exception $e) {
loadHeader();
echo showError($e);
loadFooter();
exit;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'];
if ($action === 'create_new_doc') {
loadHeader();
try {
$files = uploadFile();
loadContentTemplate($action, ['files'=> $files]);
$_SESSION['containerId'] = $sigaClient->createContainer($_POST['containerType'], $files);
$_SESSION['containerFiles'] = array_column($files, 'path', 'name');
} catch (Throwable $e) {
echo showError($e);
}
loadFooter();
} elseif ($action === 'prepare_signing') {
try {
header('Content-Type: application/json');
echo json_encode($sigaClient->prepareSigning($_POST['certificateHex']));
} catch (Throwable $e) {
deleteUploadedFiles($_SESSION['containerFiles']);
echo showError($e);
}
} elseif ($action === 'finalize_signing') {
try {
$sigaClient->finalizeSigning($_POST['signatureId'], $_POST['signatureHex']);
} catch (Throwable $e) {
echo showError($e);
}
deleteUploadedFiles($_SESSION['containerFiles']);
} elseif ($action === 'mid_sign') {
require_once('actions/mid_sign.php');
} elseif ($action === 'mid_status') {
require_once('actions/mid_status.php');
} elseif ($action === 'mid_finalize_sign') {
require_once('actions/mid_finalize_sign.php');
}
} else {
unset($_SESSION);
loadActionTemplate('default');
}