mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
php-decrypt data crypted using js
This commit is contained in:
parent
1bf595334e
commit
a7af7e7874
@ -26,6 +26,10 @@ use PhpOffice\PhpWord\TemplateProcessor;
|
|||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
||||||
|
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
|
||||||
|
use Jose\Component\Core\JWK;
|
||||||
|
use Base64Url\Base64Url;
|
||||||
|
|
||||||
// TODO à mettre dans services
|
// TODO à mettre dans services
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
@ -48,11 +52,39 @@ class DocGeneratorTemplateController extends AbstractController
|
|||||||
): Response {
|
): Response {
|
||||||
$getUrlGen = $tempUrlGenerator->generate(
|
$getUrlGen = $tempUrlGenerator->generate(
|
||||||
'GET',
|
'GET',
|
||||||
$template->getFile()
|
$template->getFile()->getFilename());
|
||||||
);
|
|
||||||
|
|
||||||
$tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE');
|
$data = file_get_contents($getUrlGen->url);
|
||||||
file_put_contents($tmpfname, file_get_contents($getUrlGen->url));
|
|
||||||
|
$iv = $template->getFile()->getIv(); // iv as an Array
|
||||||
|
$ivGoodFormat = pack('C*', ...$iv); // iv as a String (ok for openssl_decrypt)
|
||||||
|
|
||||||
|
$method = 'AES-256-CBC';
|
||||||
|
|
||||||
|
$key = $template->getFile()->getKeyInfos()['k'];
|
||||||
|
$keyGoodFormat = Base64Url::decode($key);
|
||||||
|
|
||||||
|
$dataDecrypted = openssl_decrypt($data, $method, $keyGoodFormat, 1, $ivGoodFormat);
|
||||||
|
|
||||||
|
if ($dataDecrypted === FALSE) {
|
||||||
|
throw new \Exception("Error during Decrypt ", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tmpfnameDeCrypted = tempnam(sys_get_temp_dir(), 'DECRYPT_DOC_TEMPLATE');
|
||||||
|
|
||||||
|
if (!$handle = fopen($tmpfnameDeCrypted, 'a')) {
|
||||||
|
echo "Cannot open file ($tmpfnameDeCrypted)";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fwrite($handle, $dataDecrypted) === FALSE) {
|
||||||
|
echo "Cannot write to file ($tmpfnameDeCrypted)";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
dump("Success, wrote ($dataDecrypted) to file ($tmpfnameDeCrypted)");
|
||||||
|
|
||||||
|
fclose($handle);
|
||||||
|
|
||||||
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
|
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
|
||||||
|
|
||||||
@ -63,7 +95,7 @@ class DocGeneratorTemplateController extends AbstractController
|
|||||||
throw new Exception('Not implemented', 1);
|
throw new Exception('Not implemented', 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$templateProcessor = new TemplateProcessor($tmpfname);
|
$templateProcessor = new TemplateProcessor($tmpfnameDeCrypted);
|
||||||
|
|
||||||
foreach ($datas['setValues'] as $setValuesConf) {
|
foreach ($datas['setValues'] as $setValuesConf) {
|
||||||
$templateProcessor->setValues($setValuesConf);
|
$templateProcessor->setValues($setValuesConf);
|
||||||
@ -73,12 +105,10 @@ class DocGeneratorTemplateController extends AbstractController
|
|||||||
$templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]);
|
$templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmpfname2 = tempnam(sys_get_temp_dir(), 'DOC_GENERATED');
|
$tmpfnameGenerated = tempnam(sys_get_temp_dir(), 'DOC_GENERATED');
|
||||||
$templateProcessor->saveAs($tmpfname2);
|
$templateProcessor->saveAs($tmpfnameGenerated);
|
||||||
|
|
||||||
unlink($tmpfname);
|
$fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content
|
||||||
|
|
||||||
$fileContent = fopen($tmpfname2, 'rb'); // the generated file content
|
|
||||||
|
|
||||||
$genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx';
|
$genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx';
|
||||||
|
|
||||||
@ -87,7 +117,8 @@ class DocGeneratorTemplateController extends AbstractController
|
|||||||
$genDocName
|
$genDocName
|
||||||
);
|
);
|
||||||
|
|
||||||
unlink($tmpfname2);
|
unlink($tmpfnameDeCrypted);
|
||||||
|
unlink($tmpfnameGenerated);
|
||||||
|
|
||||||
$client = new Client();
|
$client = new Client();
|
||||||
|
|
||||||
@ -128,7 +159,7 @@ class DocGeneratorTemplateController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new Exception('Unable to generate document.');
|
throw new Exception('Unable to generate document.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route(
|
* @Route(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user