1
|
|
|
<?php namespace Comodojo\RpcClient\Transport; |
2
|
|
|
|
3
|
|
|
use \Psr\Log\LoggerInterface; |
4
|
|
|
use \Comodojo\Httprequest\Httprequest; |
5
|
|
|
use \Crypt_AES; |
6
|
|
|
use \Comodojo\Exception\HttpException; |
7
|
|
|
use \Comodojo\Exception\RpcException; |
8
|
|
|
use \Exception; |
9
|
|
|
|
10
|
|
|
class Sender { |
11
|
|
|
|
12
|
|
|
private $transport = null; |
13
|
|
|
|
14
|
|
|
private $logger = null; |
15
|
|
|
|
16
|
|
|
private $aes = null; |
17
|
|
|
|
18
|
|
|
public function __construct($server, LoggerInterface $logger) { |
19
|
|
|
|
20
|
|
|
$this->logger = $logger; |
21
|
|
|
|
22
|
|
|
try { |
23
|
|
|
|
24
|
|
|
$this->transport = new Httprequest($server); |
25
|
|
|
|
26
|
|
|
$this->transport->setHttpMethod("POST"); |
27
|
|
|
|
28
|
|
|
} catch (HttpException $he) { |
29
|
|
|
|
30
|
|
|
throw $he; |
31
|
|
|
|
32
|
|
|
} catch (Exception $e) { |
33
|
|
|
|
34
|
|
|
throw $e; |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function transport() { |
41
|
|
|
|
42
|
|
|
return $this->transport; |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function logger() { |
47
|
|
|
|
48
|
|
|
return $this->logger(); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Send pre-econded request to server |
54
|
|
|
* |
55
|
|
|
* @param string $data |
56
|
|
|
* @param string $content_type |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
* |
60
|
|
|
* @throws \Comodojo\Exception\RpcException |
61
|
|
|
* @throws \Comodojo\Exception\HttpException |
62
|
|
|
*/ |
63
|
|
|
public function performCall($data, $content_type, $encrypt=false) { |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
|
67
|
|
|
$this->logger->notice("Sending data to ".$this->transport); |
68
|
|
|
|
69
|
|
|
$this->logger->debug("Original request data dump ", $data); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$data = $this->can($data, $encrypt); |
72
|
|
|
|
73
|
|
|
$this->logger->debug("Real request data dump ", $data); |
74
|
|
|
|
75
|
|
|
$response = $this->transport->setContentType($content_type)->send($data); |
76
|
|
|
|
77
|
|
|
$this->logger->debug("Real response data dump ", $response); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$return = $this->uncan($response, $encrypt); |
80
|
|
|
|
81
|
|
|
$this->logger->debug("Decoded response data dump ", $return); |
82
|
|
|
|
83
|
|
|
} catch (HttpException $he) { |
84
|
|
|
|
85
|
|
|
$this->logger->error("HTTP Transport error: ".$he->getMessage()); |
86
|
|
|
|
87
|
|
|
throw $he; |
88
|
|
|
|
89
|
|
|
} catch (RpcException $re) { |
90
|
|
|
|
91
|
|
|
$this->logger->error("RPC Client error: ".$re->getMessage()); |
92
|
|
|
|
93
|
|
|
throw $re; |
94
|
|
|
|
95
|
|
|
} catch (Exception $e) { |
96
|
|
|
|
97
|
|
|
$this->logger->critical("Generic Client error: ".$e->getMessage()); |
98
|
|
|
|
99
|
|
|
throw $e; |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $return; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function can($data, $key) { |
108
|
|
|
|
109
|
|
|
if ( !empty($key) && is_string($key) ) { |
110
|
|
|
|
111
|
|
|
$this->aes = new Crypt_AES(); |
112
|
|
|
|
113
|
|
|
$this->aes->setKey($key); |
114
|
|
|
|
115
|
|
|
$return = 'comodojo_encrypted_request-'.base64_encode( $this->aes->encrypt($data) ); |
116
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
|
119
|
|
|
$return = $data; |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $return; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function uncan($data, $key) { |
128
|
|
|
|
129
|
|
|
if ( !empty($key) && is_string($key) ) { |
130
|
|
|
|
131
|
|
|
if ( self::checkEncryptedResponseConsistency($data) === false ) throw new RpcException("Inconsistent encrypted response received"); |
132
|
|
|
|
133
|
|
|
$return = $this->aes->decrypt(base64_decode(substr($response, 28))); |
|
|
|
|
134
|
|
|
|
135
|
|
|
} else { |
136
|
|
|
|
137
|
|
|
$return = $data; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $return; |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if an encrypted envelop is consisent or not |
147
|
|
|
* |
148
|
|
|
* @param string $data |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
private static function checkEncryptedResponseConsistency($data) { |
153
|
|
|
|
154
|
|
|
return substr($data, 0, 27) == 'comodojo_encrypted_response' ? true : false; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: