|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use setasign\SetaPDF\Signer\Module\CSC\Client; |
| 6 | +use setasign\SetaPDF\Signer\Module\CSC\Module; |
| 7 | + |
| 8 | +ini_set('display_errors', '1'); |
| 9 | +ini_set('display_startup_errors', '1'); |
| 10 | +error_reporting(E_ALL); |
| 11 | + |
| 12 | +require_once(__DIR__ . '/../vendor/autoload.php'); |
| 13 | + |
| 14 | +session_start(); |
| 15 | + |
| 16 | +if (!file_exists('settings.php')) { |
| 17 | + echo 'The settings.php file is missing. See settings.php.dist for an example.'; |
| 18 | + die(); |
| 19 | +} |
| 20 | + |
| 21 | +$settings = require 'settings.php'; |
| 22 | +$apiUri = $settings['apiUri']; |
| 23 | + |
| 24 | +$fileToSign = __DIR__ . '/assets/Laboratory-Report.pdf'; |
| 25 | +$resultPath = 'signed.pdf'; |
| 26 | + |
| 27 | +// to create or update your access token you have to call generate-token.php first |
| 28 | +if (!isset($_SESSION['accessToken']['access_token'])) { |
| 29 | + echo 'Missing access token! <a href="generate-token.php">Login here</a>'; |
| 30 | + die(); |
| 31 | +} |
| 32 | +// check if the access token is still valid |
| 33 | +if (!isset($_SESSION['accessToken']['expires']) || $_SESSION['accessToken']['expires'] < time()) { |
| 34 | + echo 'Access token is expired! <a href="generate-token.php">Renew here</a>'; |
| 35 | + die(); |
| 36 | +} |
| 37 | +$accessToken = $_SESSION['accessToken']['access_token']; |
| 38 | + |
| 39 | +$httpClient = new GuzzleHttp\Client(); |
| 40 | +$httpClient = new Mjelamanov\GuzzlePsr18\Client($httpClient); |
| 41 | +$requestFactory = new Http\Factory\Guzzle\RequestFactory(); |
| 42 | +$streamFactory = new Http\Factory\Guzzle\StreamFactory(); |
| 43 | +$client = new Client($apiUri, $httpClient, $requestFactory, $streamFactory); |
| 44 | + |
| 45 | +echo '<pre>'; |
| 46 | +$credentialIds = ($client->credentialsList($accessToken))['credentialIDs']; |
| 47 | +var_dump($credentialIds); |
| 48 | +// we just use the first credential on the list |
| 49 | +$credentialId = $credentialIds[0]; |
| 50 | +// fetch all information regarding your credential id like the certificates |
| 51 | +$credentialInfo = $client->credentialsInfo($accessToken, $credentialId, 'chain', true, true); |
| 52 | +var_dump($credentialInfo); |
| 53 | +echo '</pre>'; |
| 54 | +if ($credentialInfo['authMode'] === 'oauth2code') { |
| 55 | + echo 'The selected credentialId does only support oauth2code authentification.' |
| 56 | + . ' A synchronous sign request is not possible - take a look at the <a href="async-demo.php">async-demo</a> instead.'; |
| 57 | + die(); |
| 58 | +} |
| 59 | + |
| 60 | +$certificates = $credentialInfo['cert']['certificates']; |
| 61 | + |
| 62 | +// INFO: YOU SHOULD CACHE THE DATA IN $credentialInfo FOR LESS API REQUESTS |
| 63 | + |
| 64 | +// the first certificate is always the signing certificate |
| 65 | +$certificate = array_shift($certificates); |
| 66 | +$algorithm = $credentialInfo['key']['algo'][0]; |
| 67 | + |
| 68 | +$module = new Module($accessToken, $client); |
| 69 | +$module->setCredentialId($credentialId); |
| 70 | +$module->setSignatureAlgorithmOid($algorithm); |
| 71 | +$module->setCertificate($certificate); |
| 72 | +$module->setExtraCertificates($certificates); |
| 73 | + |
| 74 | +if ($credentialInfo['authMode'] === 'explicit' && !isset($_GET['otp']) && !isset($_GET['pin'])) { |
| 75 | + // you should check the OTP and/or PIN entry in $credentialInfo for how to setup authentication exactly |
| 76 | + echo 'Please enter OTP or PIN:'; |
| 77 | + echo '<form><input type="text" name="otp" placeholder="OTP"> or <input type="text" name="pin" placeholder="PIN">'; |
| 78 | + echo '<input type="submit"/></form>'; |
| 79 | + die(); |
| 80 | +} |
| 81 | + |
| 82 | +if (isset($_GET['otp'])) { |
| 83 | + $module->setOtp($_GET['otp']); |
| 84 | +} |
| 85 | +if (isset($_GET['pin'])) { |
| 86 | + $module->setPin($_GET['pin']); |
| 87 | +} |
| 88 | + |
| 89 | +// create a writer instance |
| 90 | +$writer = new SetaPDF_Core_Writer_File($resultPath); |
| 91 | +// create the document instance |
| 92 | +$document = SetaPDF_Core_Document::loadByFilename($fileToSign, $writer); |
| 93 | + |
| 94 | +// create the signer instance |
| 95 | +$signer = new SetaPDF_Signer($document); |
| 96 | + |
| 97 | +$field = $signer->addSignatureField( |
| 98 | + 'Signature', |
| 99 | + 1, |
| 100 | + SetaPDF_Signer_SignatureField::POSITION_RIGHT_TOP, |
| 101 | + ['x' => -160, 'y' => -100], |
| 102 | + 180, |
| 103 | + 60 |
| 104 | +); |
| 105 | + |
| 106 | +$signer->setSignatureFieldName($field->getQualifiedName()); |
| 107 | + |
| 108 | +$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module); |
| 109 | +$signer->setAppearance($appearance); |
| 110 | + |
| 111 | +$signer->sign($module); |
| 112 | + |
| 113 | +echo '<a href="data:application/pdf;base64,' . base64_encode(file_get_contents($resultPath)) . '" ' . |
| 114 | + 'download="' . basename($resultPath) . '">download</a> | <a href="?">restart</a><br />'; |
0 commit comments