1
|
|
|
<?php namespace Comodojo\RpcClient\Components; |
2
|
|
|
|
3
|
|
|
use \Comodojo\RpcClient\Interfaces\Transport as TransportInterface; |
4
|
|
|
use \Comodojo\Httprequest\Httprequest; |
5
|
|
|
use \phpseclib\Crypt\AES; |
6
|
|
|
use \Psr\Log\LoggerInterface; |
7
|
|
|
use \Comodojo\Exception\HttpException; |
8
|
|
|
use \Comodojo\Exception\RpcException; |
9
|
|
|
use \Exception; |
10
|
|
|
|
11
|
|
|
class HttpTransport extends Httprequest implements TransportInterface { |
12
|
|
|
|
13
|
|
|
private $aes = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Send pre-econded request to server |
17
|
|
|
* |
18
|
|
|
* @param string $data |
19
|
|
|
* @param string $content_type |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
* |
23
|
|
|
* @throws \Comodojo\Exception\RpcException |
24
|
|
|
* @throws \Comodojo\Exception\HttpException |
25
|
|
|
*/ |
26
|
51 |
|
public function performCall(LoggerInterface $logger, $data, $content_type, $encrypt=false) { |
27
|
|
|
|
28
|
51 |
|
$this->setHttpMethod("POST"); |
29
|
|
|
|
30
|
|
|
try { |
31
|
|
|
|
32
|
51 |
|
$logger->notice("Sending RPC data"); |
33
|
|
|
|
34
|
51 |
|
$logger->debug("Original request data dump: ".$data); |
35
|
|
|
|
36
|
51 |
|
$data = $this->can($data, $encrypt); |
37
|
|
|
|
38
|
51 |
|
$logger->debug("Real request data dump: ".$data); |
39
|
|
|
|
40
|
51 |
|
$response = $this->setContentType($content_type)->send($data); |
41
|
|
|
|
42
|
51 |
|
$logger->debug("Real response data dump: ".$response); |
43
|
|
|
|
44
|
51 |
|
$return = $this->uncan($response, $encrypt); |
45
|
|
|
|
46
|
51 |
|
$logger->debug("Decoded response data dump: ".$return); |
47
|
|
|
|
48
|
51 |
|
} catch (HttpException $he) { |
49
|
|
|
|
50
|
|
|
$logger->error("HTTP Transport error: ".$he->getMessage()); |
51
|
|
|
|
52
|
|
|
throw $he; |
53
|
|
|
|
54
|
|
|
} catch (RpcException $re) { |
55
|
|
|
|
56
|
|
|
$logger->error("RPC Client error: ".$re->getMessage()); |
57
|
|
|
|
58
|
|
|
throw $re; |
59
|
|
|
|
60
|
|
|
} catch (Exception $e) { |
61
|
|
|
|
62
|
|
|
$logger->critical("Generic Client error: ".$e->getMessage()); |
63
|
|
|
|
64
|
|
|
throw $e; |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
51 |
|
return $return; |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
51 |
|
private function can($data, $key) { |
73
|
|
|
|
74
|
51 |
|
if ( !empty($key) && is_string($key) ) { |
75
|
|
|
|
76
|
|
|
$this->aes = new AES(); |
77
|
|
|
|
78
|
|
|
$this->aes->setKey($key); |
79
|
|
|
|
80
|
|
|
$return = 'comodojo_encrypted_request-'.base64_encode( $this->aes->encrypt($data) ); |
81
|
|
|
|
82
|
|
|
} else { |
83
|
|
|
|
84
|
51 |
|
$return = $data; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
51 |
|
return $return; |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
51 |
|
private function uncan($data, $key) { |
93
|
|
|
|
94
|
51 |
|
if ( !empty($key) && is_string($key) ) { |
95
|
|
|
|
96
|
|
|
if ( self::checkEncryptedResponseConsistency($data) === false ) throw new RpcException("Inconsistent encrypted response received"); |
97
|
|
|
|
98
|
|
|
$return = $this->aes->decrypt(base64_decode(substr($data, 28))); |
99
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
|
102
|
51 |
|
$return = $data; |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
51 |
|
return $return; |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if an encrypted envelope is consisent or not |
112
|
|
|
* |
113
|
|
|
* @param string $data |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
private static function checkEncryptedResponseConsistency($data) { |
118
|
|
|
|
119
|
|
|
return substr($data, 0, 27) == 'comodojo_encrypted_response' ? true : false; |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|