1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require '../vendor/autoload.php'; |
4
|
|
|
|
5
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
6
|
|
|
use Zend\Diactoros\Response; |
7
|
|
|
use Zend\Diactoros\Response\SapiEmitter; |
8
|
|
|
use Mediapart\LaPresseLibre\Security\Identity; |
9
|
|
|
use Mediapart\LaPresseLibre\Security\Encryption; |
10
|
|
|
use Mediapart\LaPresseLibre\Subscription\Type as SubscriptionType; |
11
|
|
|
use Mediapart\LaPresseLibre\Transaction; |
12
|
|
|
use Mediapart\LaPresseLibre\Endpoint; |
13
|
|
|
|
14
|
|
|
/* |
15
|
|
|
Configuration : |
16
|
|
|
|
17
|
|
|
download the file : `https://partenaire.lapresselibre.fr/gestion/credentials` |
18
|
|
|
into `credentials.txt` |
19
|
|
|
*/ |
20
|
|
|
$config = json_decode(file_get_contents('credentials.txt')); |
21
|
|
|
|
22
|
|
|
$public_key = $config->CodePartenaire; |
23
|
|
|
$identity = new Identity($config->secret); |
24
|
|
|
$encryption = new Encryption($config->Aes, $config->Iv); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Handle an api endpoint of La Presse Libre. |
28
|
|
|
* |
29
|
|
|
* @param string $operation |
30
|
|
|
* @param callable $callback |
31
|
|
|
*/ |
32
|
|
|
$handle = function ($operation, $callback) use ($identity, $encryption, $public_key) { |
33
|
|
|
try { |
34
|
|
|
$request = ServerRequestFactory::fromGlobals(); |
35
|
|
|
$transaction = new Transaction($identity, $encryption, $request); |
36
|
|
|
$endpoint = Endpoint::answer($operation, $callback); |
37
|
|
|
$result = $transaction->process($endpoint); |
38
|
|
|
$status = 200; |
39
|
|
|
} catch (\InvalidArgumentException $e) { |
40
|
|
|
$result = $e->getMessage(); |
41
|
|
|
$status = 400; |
42
|
|
|
} catch (\UnexpectedValueException $e) { |
43
|
|
|
$result = $e->getMessage(); |
44
|
|
|
$status = 401; |
45
|
|
|
} catch (\Exception $e) { |
46
|
|
|
$result = 'Internal Error'; |
47
|
|
|
$status = 500; |
48
|
|
|
} finally { |
49
|
|
|
$response = (new Response()) |
50
|
|
|
->withStatus($status) |
|
|
|
|
51
|
|
|
->withHeader('X-PART', (string) $public_key) |
52
|
|
|
->withHeader('X-LPL', $identity->sign($public_key)) |
53
|
|
|
->withHeader('X-TS', (string) $identity->getDatetime()->getTimestamp()) |
54
|
|
|
; |
55
|
|
|
$response->getBody()->write(200 != $status ? json_encode(['error' => $result]) : $result); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$emitter = new SapiEmitter(); |
59
|
|
|
$emitter->emit($response); |
60
|
|
|
}; |
61
|
|
|
|