1
|
|
|
<?php namespace Comodojo\RpcClient\Components; |
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 Transport extends Httprequest { |
11
|
|
|
|
12
|
|
|
private $aes = null; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Send pre-econded request to server |
16
|
|
|
* |
17
|
|
|
* @param string $data |
18
|
|
|
* @param string $content_type |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
* |
22
|
|
|
* @throws \Comodojo\Exception\RpcException |
23
|
|
|
* @throws \Comodojo\Exception\HttpException |
24
|
|
|
*/ |
25
|
|
|
public function performCall($logger, $data, $content_type, $encrypt=false) { |
26
|
|
|
|
27
|
|
|
$this->setHttpMethod("POST"); |
28
|
|
|
|
29
|
|
|
try { |
30
|
|
|
|
31
|
|
|
$logger->notice("Sending RPC data"); |
32
|
|
|
|
33
|
|
|
$logger->debug("Original request data dump: ".$data); |
34
|
|
|
|
35
|
|
|
$data = $this->can($data, $encrypt); |
36
|
|
|
|
37
|
|
|
$logger->debug("Real request data dump: ".$data); |
38
|
|
|
|
39
|
|
|
$response = $this->setContentType($content_type)->send($data); |
40
|
|
|
|
41
|
|
|
$logger->debug("Real response data dump: ".$response); |
42
|
|
|
|
43
|
|
|
$return = $this->uncan($response, $encrypt); |
44
|
|
|
|
45
|
|
|
$logger->debug("Decoded response data dump: ".$return); |
46
|
|
|
|
47
|
|
|
} catch (HttpException $he) { |
48
|
|
|
|
49
|
|
|
$logger->error("HTTP Transport error: ".$he->getMessage()); |
50
|
|
|
|
51
|
|
|
throw $he; |
52
|
|
|
|
53
|
|
|
} catch (RpcException $re) { |
54
|
|
|
|
55
|
|
|
$logger->error("RPC Client error: ".$re->getMessage()); |
56
|
|
|
|
57
|
|
|
throw $re; |
58
|
|
|
|
59
|
|
|
} catch (Exception $e) { |
60
|
|
|
|
61
|
|
|
$logger->critical("Generic Client error: ".$e->getMessage()); |
62
|
|
|
|
63
|
|
|
throw $e; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $return; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function can($data, $key) { |
72
|
|
|
|
73
|
|
|
if ( !empty($key) && is_string($key) ) { |
74
|
|
|
|
75
|
|
|
$this->aes = new Crypt_AES(); |
76
|
|
|
|
77
|
|
|
$this->aes->setKey($key); |
78
|
|
|
|
79
|
|
|
$return = 'comodojo_encrypted_request-'.base64_encode( $this->aes->encrypt($data) ); |
80
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
|
83
|
|
|
$return = $data; |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $return; |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function uncan($data, $key) { |
92
|
|
|
|
93
|
|
|
if ( !empty($key) && is_string($key) ) { |
94
|
|
|
|
95
|
|
|
if ( self::checkEncryptedResponseConsistency($data) === false ) throw new RpcException("Inconsistent encrypted response received"); |
96
|
|
|
|
97
|
|
|
$return = $this->aes->decrypt(base64_decode(substr($response, 28))); |
|
|
|
|
98
|
|
|
|
99
|
|
|
} else { |
100
|
|
|
|
101
|
|
|
$return = $data; |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $return; |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if an encrypted envelop is consisent or not |
111
|
|
|
* |
112
|
|
|
* @param string $data |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
private static function checkEncryptedResponseConsistency($data) { |
117
|
|
|
|
118
|
|
|
return substr($data, 0, 27) == 'comodojo_encrypted_response' ? true : false; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.