|
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
|
|
|
/** |
|
12
|
|
|
* @package Comodojo Spare Parts |
|
13
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
14
|
|
|
* @license MIT |
|
15
|
|
|
* |
|
16
|
|
|
* LICENSE: |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
class HttpTransport extends Httprequest implements TransportInterface { |
|
28
|
|
|
|
|
29
|
|
|
private $aes; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Send pre-econded request to server |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $data |
|
35
|
|
|
* @param string $content_type |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
* |
|
39
|
|
|
* @throws RpcException |
|
40
|
|
|
* @throws HttpException |
|
41
|
|
|
* @throws Exception |
|
42
|
|
|
*/ |
|
43
|
63 |
|
public function performCall( |
|
44
|
|
|
LoggerInterface $logger, |
|
45
|
|
|
$data, |
|
46
|
|
|
$content_type, |
|
47
|
|
|
$encrypt=false |
|
48
|
|
|
) { |
|
49
|
|
|
|
|
50
|
63 |
|
$this->setHttpMethod("POST"); |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
|
|
54
|
63 |
|
$logger->debug("Sending RPC data"); |
|
55
|
|
|
|
|
56
|
63 |
|
$logger->debug("Original request data dump: ".$data); |
|
57
|
|
|
|
|
58
|
63 |
|
$data = $this->can($data, $encrypt); |
|
59
|
|
|
|
|
60
|
63 |
|
$logger->debug("Real request data dump: ".$data); |
|
61
|
|
|
|
|
62
|
63 |
|
$response = $this->setContentType($content_type)->send($data); |
|
63
|
|
|
|
|
64
|
63 |
|
$logger->debug("Real response data dump: ".$response); |
|
65
|
|
|
|
|
66
|
63 |
|
$return = $this->uncan($response, $encrypt); |
|
67
|
|
|
|
|
68
|
63 |
|
$logger->debug("Decoded response data dump: ".$return); |
|
69
|
|
|
|
|
70
|
21 |
|
} catch (HttpException $he) { |
|
71
|
|
|
|
|
72
|
|
|
$logger->error("HTTP Transport error: ".$he->getMessage()); |
|
73
|
|
|
|
|
74
|
|
|
throw $he; |
|
75
|
|
|
|
|
76
|
|
|
} catch (RpcException $re) { |
|
77
|
|
|
|
|
78
|
|
|
$logger->error("RPC Client error: ".$re->getMessage()); |
|
79
|
|
|
|
|
80
|
|
|
throw $re; |
|
81
|
|
|
|
|
82
|
|
|
} catch (Exception $e) { |
|
83
|
|
|
|
|
84
|
|
|
$logger->critical("Generic Client error: ".$e->getMessage()); |
|
85
|
|
|
|
|
86
|
|
|
throw $e; |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
63 |
|
return $return; |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
63 |
|
private function can($data, $key) { |
|
95
|
|
|
|
|
96
|
63 |
|
if ( !empty($key) && is_string($key) ) { |
|
97
|
|
|
|
|
98
|
|
|
$this->aes = new AES(); |
|
99
|
|
|
|
|
100
|
|
|
$this->aes->setKey($key); |
|
101
|
|
|
|
|
102
|
|
|
$return = 'comodojo_encrypted_request-'.base64_encode( $this->aes->encrypt($data) ); |
|
103
|
|
|
|
|
104
|
|
|
} else { |
|
105
|
|
|
|
|
106
|
63 |
|
$return = $data; |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
63 |
|
return $return; |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
63 |
|
private function uncan($data, $key) { |
|
115
|
|
|
|
|
116
|
63 |
|
if ( !empty($key) && is_string($key) ) { |
|
117
|
|
|
|
|
118
|
|
|
if ( self::checkEncryptedResponseConsistency($data) === false ) throw new RpcException("Inconsistent encrypted response received"); |
|
119
|
|
|
|
|
120
|
|
|
$return = $this->aes->decrypt(base64_decode(substr($data, 28))); |
|
121
|
|
|
|
|
122
|
|
|
} else { |
|
123
|
|
|
|
|
124
|
63 |
|
$return = $data; |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
63 |
|
return $return; |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Check if an encrypted envelope is consisent or not |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $data |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
private static function checkEncryptedResponseConsistency($data) { |
|
140
|
|
|
|
|
141
|
|
|
return substr($data, 0, 27) == 'comodojo_encrypted_response' ? true : false; |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|