1 <?php
2
3 namespace Satabank\IPC;
4
5 6 7
8 class Response{
9
10 11 12
13 private $cnf;
14 private $raw_data, $format, $data, $signature;
15
16 17 18 19 20 21
22 public function __construct(Config $cnf, $raw_data, $format){
23 $this->cnf = $cnf;
24 $this->_setData($raw_data, $format);
25 }
26
27 28 29 30 31 32
33 public static function getInstance(Config $cnf, $raw_data, $format){
34 return new self($cnf, $raw_data, $format);
35 }
36
37 38 39 40
41 public function isSignatureCorrect(){
42 try{
43 $this->_verifySignature();
44 }catch(Exception $ex){
45 return false;
46 }
47 return true;
48 }
49
50 51 52 53
54 function getSignature(){
55 return $this->signature;
56 }
57
58 59 60 61
62 function getStatus(){
63 return Helper::getArrayVal($this->getData(CASE_LOWER), 'status');
64 }
65
66 67 68 69
70 function getStatusMsg(){
71 return Helper::getArrayVal($this->getData(CASE_LOWER), 'statusmsg');
72 }
73
74 75 76 77 78 79
80 function getData($case = null){
81 if($case !== null){
82 if(!in_array($case, array(CASE_LOWER, CASE_UPPER))){
83 throw new IPC_Exception('Invalid Key Case!');
84 }
85 return array_change_key_case($this->data, $case);
86 }
87
88 return $this->data;
89 }
90
91 92 93 94
95 function getDataRaw(){
96 return $this->raw_data;
97 }
98
99
100
101 private function _setData($raw_data, $format){
102 if(empty($raw_data)){
103 throw new IPC_Exception('Invalid Reponce data');
104 }
105
106 $this->format = $format;
107 $this->raw_data = $raw_data;
108
109 switch($this->format){
110 case Defines::COMMUNICATION_FORMAT_JSON:
111 $this->data = json_decode($this->raw_data, 1);
112 break;
113 case Defines::COMMUNICATION_FORMAT_XML:
114 $this->data = (array) new \SimpleXMLElement($this->raw_data);
115 break;
116 case Defines::COMMUNICATION_FORMAT_POST:
117 $this->data = $this->raw_data;
118 break;
119 default:
120 throw new IPC_Exception('Invalid response format!');
121 break;
122 }
123
124 if(empty($this->data)){
125 throw new IPC_Exception('No IPC Response!');
126 }
127
128 $this->_exctractSignature() && $this->_verifySignature();
129 return $this;
130 }
131
132 private function _exctractSignature(){
133 if(is_array($this->data) && !empty($this->data)){
134 foreach($this->data as $k => $v){
135 if(strtolower($k) == 'signature'){
136 $this->signature = $v;
137 unset($this->data[$k]);
138 }
139 }
140 }
141 return true;
142 }
143
144 private function _verifySignature(){
145 if(empty($this->signature)){
146 throw new IPC_Exception('Missing request signature!');
147 }
148
149 if(!$this->cnf){
150 throw new IPC_Exception('Missing config object!');
151 }
152
153 $pubKeyId = openssl_get_publickey($this->cnf->getAPIPublicKey());
154 if(!openssl_verify($this->_getSignData(), base64_decode($this->signature), $pubKeyId, Defines::SIGNATURE_ALGO)){
155 throw new IPC_Exception('Signature check failed!');
156 }
157 }
158
159 private function _getSignData(){
160 return base64_encode(implode('-', Helper::getValuesFromMultiDemensionalArray($this->data)));
161 }
162
163 }
164