1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace MyAllocator\phpsdkota\src\Api\Inbound;
19 use MyAllocator\phpsdkota\src\Api\Inbound\MaInboundInterface;
20 use MyAllocator\phpsdkota\src\Object\MaResponse;
21
22 class MaRouter
23 {
24 25 26
27 protected $apis = null;
28
29 30 31
32 protected $ota_intf = null;
33
34 35 36 37 38 39 40
41 public function __construct(MaInboundInterface $interface)
42 {
43
44 $this->config = require(dirname(__FILE__) . '/../../Config/MaConfig.php');
45
46
47 $this->apis = require(dirname(__FILE__) . '/../../Config/MaApisInbound.php');
48
49
50 $this->ota_intf = $interface;
51 }
52
53 54 55 56 57 58 59 60 61
62 public function processRequest($post_body = null)
63 {
64 $rsp = new \MyAllocator\phpsdkota\src\Object\MaResponse($this->ota_intf);
65
66
67 if (!$post_body) {
68 return $rsp->error(MA_OTA_ERR_ARGS_INVALID)->toArray();
69 }
70
71
72 try {
73 $this->ota_intf->log('Request',$post_body);
74 $request = $this->decode($post_body);
75 } catch (\Exception $e) {
76 return $rsp->error(MA_OTA_ERR_JSON_INVALID, $e->getMessage())->toArray();
77 }
78
79
80 if (!is_array($request) || empty($request)) {
81 return $rsp->error(MA_OTA_ERR_JSON_INVALID)->toArray();
82 }
83
84
85 if (!isset($request['shared_secret'])) {
86 return $rsp->error(MA_OTA_ERR_AUTH_INVALID)->toArray();
87 } else {
88 if ($request['shared_secret'] !== $this->config['shared_secret']) {
89 return $rsp->error(MA_OTA_ERR_AUTH_INVALID)->toArray();
90 } else {
91
92 unset($request['shared_secret']);
93 }
94 }
95
96
97 if (!isset($request['verb'])) {
98 return $rsp->error(MA_OTA_ERR_VERB_INVALID)->toArray();
99 } else if (!isset($this->apis[$request['verb']])) {
100 return $rsp->error(MA_OTA_ERR_VERB_INVALID, $request['verb'])->toArray();
101 } else {
102
103 foreach ($this->apis[$request['verb']]['args']['req'] as $key) {
104 if (!isset($request[$key])) {
105 return $rsp->error(MA_OTA_ERR_ARGS_INVALID, $key)->toArray();
106 }
107 }
108
109 foreach ($request as $key => $val) {
110 if (!in_array($key, $this->apis[$request['verb']]['args']['req']) &&
111 !in_array($key, $this->apis[$request['verb']]['args']['opt'])
112 ) {
113 unset($request[$key]);
114 }
115 }
116 }
117
118
119 $rsp = $this->ota_intf->authenticateProperty($request);
120 if (!$rsp->success) {
121 return $rsp->response();
122 }
123
124
125 switch ($request['verb']) {
126 case 'HealthCheck':
127 $rsp->success();
128 break;
129 case 'SetupProperty':
130 $rsp = $this->ota_intf->setupProperty($request);
131 break;
132 case 'GetRoomTypes':
133 $rsp = $this->ota_intf->getRoomTypes($request);
134 break;
135 case 'GetBookingId':
136 $rsp = $this->ota_intf->getBookingId($request);
137 break;
138 case 'GetBookingList':
139 $rsp = $this->ota_intf->getBookingList($request);
140 break;
141 case 'ARIUpdate':
142 $rsp = $this->ota_intf->ARIUpdate($request);
143 break;
144 default:
145 $rsp->error(MA_OTA_ERR_VERB_INVALID);
146 break;
147 }
148
149
150 if (get_class($rsp) != 'MyAllocator\phpsdkota\src\Object\MaResponse') {
151 $rsp = new \MyAllocator\phpsdkota\src\Object\MaResponse();
152 $rsp->error(MA_OTA_ERR_RSP_INVALID);
153 } else {
154 $rsp->setLogger($this->ota_intf);
155 }
156
157 return $rsp->response();
158 }
159
160 private function decode($json)
161 {
162 $result = json_decode($json, true);
163 switch(json_last_error())
164 {
165 case JSON_ERROR_DEPTH:
166 $error = 'Maximum stack depth exceeded';
167 break;
168 case JSON_ERROR_CTRL_CHAR:
169 $error = 'Unexpected control character found';
170 break;
171 case JSON_ERROR_SYNTAX:
172 $error = 'Syntax error, malformed JSON';
173 break;
174 case JSON_ERROR_NONE:
175 default:
176 $error = '';
177 }
178
179 if (!empty($error)) {
180 throw new \Exception('JSON Error: '.$error);
181 }
182
183 return $result;
184 }
185 }
186