/
LibresignController.php
230 lines (211 loc) · 6.2 KB
/
LibresignController.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace OCA\Libresign\Controller;
use OC\Files\Filesystem;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\FileUserMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\JLibresignHandler;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\AccountService;
use OCA\Libresign\Service\LibresignService;
use OCA\Libresign\Service\WebhookService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use setasign\Fpdi\Fpdi;
class LibresignController extends Controller {
use HandleErrorsTrait;
use HandleParamsTrait;
/** @var LibresignService */
private $service;
/** @var FileUserMapper */
private $fileUserMapper;
/** @var FileMapper */
private $fileMapper;
/** @var IRootFolder */
private $root;
/** @var IL10N */
private $l10n;
/** @var AccountService */
private $account;
/** @var JLibresignHandler */
private $libresignHandler;
/** @var WebhookService */
private $webhook;
/** @var LoggerInterface */
private $logger;
/** @var string */
private $userId;
public function __construct(
IRequest $request,
LibresignService $service,
FileUserMapper $fileUserMapper,
FileMapper $fileMapper,
IRootFolder $root,
IL10N $l10n,
AccountService $account,
JLibresignHandler $libresignHandler,
WebhookService $webhook,
LoggerInterface $logger,
$userId
) {
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
$this->fileUserMapper = $fileUserMapper;
$this->fileMapper = $fileMapper;
$this->root = $root;
$this->l10n = $l10n;
$this->account = $account;
$this->libresignHandler = $libresignHandler;
$this->webhook = $webhook;
$this->logger = $logger;
$this->userId = $userId;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @todo remove NoCSRFRequired
*/
public function sign(
string $inputFilePath = null,
string $outputFolderPath = null,
string $certificatePath = null,
string $password = null
): JSONResponse {
try {
$this->checkParams([
'inputFilePath' => $inputFilePath,
'outputFolderPath' => $outputFolderPath,
'certificatePath' => $certificatePath,
'password' => $password,
]);
$fileSigned = $this->service->sign($inputFilePath, $outputFolderPath, $certificatePath, $password);
return new JSONResponse(
['fileSigned' => $fileSigned->getInternalPath()],
HTTP::STATUS_OK
);
} catch (\Exception $exception) {
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [$this->l10n->t($exception->getMessage())]
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function signUsingUuid(string $uuid, string $password): JSONResponse {
try {
try {
$fileUser = $this->fileUserMapper->getByUuidAndUserId($uuid, $this->userId);
} catch (\Throwable $th) {
throw new LibresignException($this->l10n->t('Invalid data to sign file'), 1);
}
if ($fileUser->getSigned()) {
throw new LibresignException($this->l10n->t('File already signed by you'), 1);
}
$fileData = $this->fileMapper->getById($fileUser->getFileId());
Filesystem::initMountPoints($fileData->getuserId());
$originalFile = $this->root->getById($fileData->getNodeId());
if (count($originalFile) < 1) {
throw new LibresignException($this->l10n->t('File not found'));
}
$originalFile = $originalFile[0];
$signedFilePath = preg_replace(
'/' . $originalFile->getExtension() . '$/',
$this->l10n->t('signed.%s', $originalFile->getExtension()),
$originalFile->getPath()
);
if ($this->root->nodeExists($signedFilePath)) {
/** @var \OCP\Files\File */
$fileToSign = $this->root->get($signedFilePath);
} else {
/** @var \OCP\Files\File */
$fileToSign = $this->root->newFile($signedFilePath);
$buffer = $this->writeFooter($originalFile);
$fileToSign->putContent($buffer);
}
$certificatePath = $this->account->getPfx($fileUser->getUserId());
list(, $signedContent) = $this->libresignHandler->signExistingFile($fileToSign, $certificatePath, $password);
$fileToSign->putContent($signedContent);
$fileUser->setSigned(time());
$this->fileUserMapper->update($fileUser);
$signers = $this->fileUserMapper->getByFileId($fileUser->getFileId());
$total = array_reduce($signers, function ($carry, $signer) {
$carry += $signer->getSigned() ? 1 : 0;
return $carry;
});
if (count($signers) == $total) {
$callbackUrl = $fileData->getCallback();
if ($callbackUrl) {
$this->webhook->notifyCallback(
$callbackUrl,
$fileData->getUuid(),
$fileToSign
);
}
}
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'message' => $this->l10n->t('File signed')
],
Http::STATUS_OK
);
} catch (LibresignException $e) {
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [$e->getMessage()]
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
} catch (\Throwable $th) {
$message = $th->getMessage();
$this->logger->error($message);
switch ($message) {
case 'Host violates local access rules.':
case 'Certificate Password Invalid.':
case 'Certificate Password is Empty.':
$message = $this->l10n->t($message);
break;
default:
$message = $this->l10n->t('Internal error. Contact admins.');
}
return new JSONResponse(
[
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [$message]
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}
private function writeFooter($file) {
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile($file->fopen('r'));
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$templateId = $pdf->importPage($pageNo);
$pdf->AddPage();
$pdf->useTemplate($templateId, ['adjustPageSize' => true]);
$pdf->SetFont('Helvetica');
$pdf->SetFontSize(8);
$pdf->SetAutoPageBreak(false);
$pdf->SetXY(5, -10);
$pdf->Write(8, $this->l10n->t(
'Digital signed by LibreSign. Validate in %s', 'http://validador.lt.coop.br'
));
}
return $pdf->Output('S');
}
}