1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: if (!defined('_PS_VERSION_')) {
34: exit;
35: }
36:
37: 38: 39: 40: 41: 42: 43:
44: class eabi_dpd_parcelstore_dpd_api {
45:
46: protected $_store;
47: protected $_code;
48:
49: protected static $_logRequestsLimit = 60;
50:
51: 52: 53: 54:
55: protected static $log;
56:
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68:
69: protected static $_loggedRequests = array();
70:
71: 72: 73: 74:
75: protected static $_isRequestLogged = false;
76:
77: public function __construct() {
78: ;
79: }
80:
81: public function setStore($store) {
82: $this->_store = $store;
83: return $this;
84: }
85:
86: public function setCode($code) {
87: $this->_code = $code;
88: return $this;
89: }
90:
91: public function getCode() {
92: return $this->_code;
93: }
94:
95: public function getStore() {
96: return $this->_store;
97: }
98:
99: 100: 101: 102: 103: 104:
105: public function getConfigData($field) {
106: if (!$this->getCode()) {
107: return false;
108: }
109: $path = $this->getCode().$field;
110: return Configuration::get($path, null, null, $this->getStore());
111: }
112:
113: 114: 115: 116: 117: 118:
119: public function getOfficeList() {
120: $body = @$this->_getRequest();
121: return $body;
122: }
123:
124:
125: 126: 127: 128: 129:
130: public function getCourierCollectionTimes(array $requestData = array()) {
131: $details = array(
132: 'op' => 'date',
133: 'Po_postal' => $this->getConfigData('RETURN_POSTCODE'),
134: 'Po_country' => strtolower($this->getConfigData('RETURN_COUNTRY')),
135: 'Po_type' => isset($requestData['Po_type'])?strtolower($requestData['Po_type']):'po',
136: );
137: if ($details['Po_country'] == 'ee') {
138:
139:
140:
141: $details['Po_country'] = 'eesti';
142: }
143: foreach ($details as $key => $detail) {
144: $requestData[$key] = $detail;
145: }
146: $requestResult = $this->_getRequest($requestData);
147: return $requestResult;
148:
149: }
150:
151: 152: 153: 154: 155:
156: public function autoSendData(array $requestData) {
157: $returnDetails = array(
158: 'op' => 'order',
159: 'Po_name' => $this->getConfigData('RETURN_NAME'),
160: 'Po_company' => $this->getConfigData('RETURN_COMPANY'),
161: 'Po_street' => $this->getConfigData('RETURN_STREET'),
162: 'Po_postal' => $this->getConfigData('RETURN_POSTCODE'),
163: 'Po_country' => strtolower($this->getConfigData('RETURN_COUNTRY')),
164: 'Po_city' => $this->getConfigData('RETURN_CITYCOUNTY'),
165: 'Po_contact' => $this->getConfigData('RETURN_NAME'),
166: 'Po_phone' => $this->getConfigData('RETURN_PHONE'),
167:
168: 'Po_email' => $this->getConfigData('RETURN_EMAIL'),
169: 'Po_show_on_label' => $this->getConfigData('PO_SHOW_ON_LABEL') == 'yes'?'true':'false',
170: 'Po_save_address' => $this->getConfigData('PO_SAVE_ADDRESS')?'true':'false',
171:
172: 'LabelsPosition' => $this->getConfigData('LABEL_POSITION'),
173:
174: );
175:
176: foreach ($returnDetails as $key => $returnDetail) {
177: $requestData[$key] = $returnDetail;
178: }
179: if (!isset($requestData['Po_type'])) {
180: $requestData['Po_type'] = 'LO';
181: }
182:
183: $requestResult = $this->_getRequest($requestData);
184: return $requestResult;
185: }
186:
187:
188: 189: 190: 191: 192: 193: 194: 195: 196: 197:
198: public function isCourierComing() {
199: $pickupTime = $this->getConfigData('COURIER_PICKUP_TIME');
200: $time = time();
201: $timezoneDiff = 0;
202: $time2 = new DateTime("now");
203: $timeZone = new DateTimeZone('Etc/GMT+0');
204:
205: $timezoneDiff = date('Z');
206: $time += $timezoneDiff;
207:
208:
209: if ($pickupTime) {
210: $pickupTime = explode(',', $pickupTime);
211: }
212: if ($pickupTime[0] >= $time) {
213: return $pickupTime;
214: }
215: return false;
216: }
217:
218:
219: 220: 221: 222: 223: 224: 225: 226:
227: protected function _getRequest($params = array('op' => 'pudo'), $url = null) {
228: if (!$url) {
229: $url = $this->getConfigData('API_URL');
230: }
231: $auth = '';
232: if (strpos($url, 'dpd.surflink.ee') > 0) {
233: $auth = "Authorization: Basic " . base64_encode("demo:demo") . "\r\n";
234: }
235:
236: if (isset($params['op']) && $params['op'] != 'pudo') {
237: $params['User_login'] = $this->getConfigData('SENDPACKAGE_USERNAME');
238: $params['User_password'] = $this->getConfigData('SENDPACKAGE_PASSWORD');
239: }
240: $logRequest = array(
241: 'url' => $url,
242: 'request' => $this->_removeSensitiveData($params),
243: 'response' => '',
244: );
245:
246:
247:
248:
249:
250: $options = array(
251: 'http' => array(
252: 'method' => 'POST',
253: 'header' => $auth . "Content-type: application/x-www-form-urlencoded\r\n",
254: 'content' => http_build_query($params),
255: 'timeout' => $this->getConfigData('HTTP_REQUEST_TIMEOUT') > 10 ? $this->getConfigData('HTTP_REQUEST_TIMEOUT') : 10,
256: 'ignore_errors' => true,
257: ));
258:
259:
260: $context = stream_context_create($options);
261: $postRequestResult = file_get_contents($url, false, $context);
262: $body = @json_decode($postRequestResult, true);
263: if (!is_array($body) || !isset($body['Error_code']) || $body['Error_code'] !== 0) {
264: $finalResult = $body ? $body : $postRequestResult;
265: $translatedText = sprintf($this->l('DPD request failed with response: %s'), print_r($finalResult, true));
266: $logRequest['response'] = $finalResult;
267: $this->_addLogRequest($logRequest);
268: if ($this->_isLogEnabled()) {
269: self::log($logRequest, AbstractLogger::ERROR);
270: }
271:
272: throw new Exception($translatedText);
273: }
274: $logRequest['response'] = $body;
275: $this->_addLogRequest($logRequest);
276: if ($this->_isLogEnabled()) {
277: self::log($logRequest, AbstractLogger::DEBUG);
278: }
279: return $logRequest['response'];
280: }
281:
282: 283: 284: 285: 286:
287: protected function _removeSensitiveData($params) {
288: $sensitiveDataToRemove = array(
289: 'User_login',
290: 'User_password'
291: );
292: foreach ($sensitiveDataToRemove as $toRemoval) {
293: if (isset($params[$toRemoval])) {
294: $params[$toRemoval] = '*** removed ***';
295: }
296: }
297: return $params;
298: }
299:
300: 301: 302: 303: 304:
305: protected function _addLogRequest($logRequest) {
306: if (count(self::$_loggedRequests) > self::$_logRequestsLimit) {
307: return;
308: }
309: if (count(self::$_loggedRequests) == self::$_logRequestsLimit) {
310: self::$_loggedRequests[] = array(
311: 'url' => '',
312: 'request' => 'Logging limit reached',
313: 'response' => '',
314: );
315: return;
316: }
317: self::$_loggedRequests[] = $logRequest;
318:
319: }
320:
321: 322: 323: 324: 325: 326: 327: 328: 329: 330:
331: public function getLoggedRequests() {
332: return self::$_loggedRequests;
333: }
334:
335: 336: 337: 338: 339:
340: protected function l($i) {
341: return Module::getInstanceByName(eabi_dpd_parcelstore::NAME)->l($i);
342: }
343:
344: 345: 346: 347: 348:
349: private static function log($data, $level = AbstractLogger::DEBUG) {
350:
351: if (is_null(self::$log)) {
352: self::$log = new FileLogger(AbstractLogger::DEBUG);
353: self::$log->setFilename(_PS_ROOT_DIR_ . '/log/' . __CLASS__ . '.log');
354: }
355:
356: if (!self::$_isRequestLogged) {
357: self::$log->log('POST=' . print_r($_POST, true), $level);
358: self::$log->log('GET=' . print_r($_GET, true), $level);
359: self::$_isRequestLogged = true;
360: }
361:
362:
363: if (is_object($data) || is_array($data)) {
364: $data = print_r($data, true);
365: }
366: self::$log->log($data, $level);
367: }
368:
369: 370: 371: 372:
373: protected function _isLogEnabled() {
374: return $this->getConfigData('ENABLE_LOG') == 'yes';
375: }
376:
377:
378:
379:
380: }
381: